-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeat.py
34 lines (28 loc) · 809 Bytes
/
beat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Scratch client test program
# sends 10 "beat" broadcasts to Scratch
from array import array
import socket
import time
HOST = '127.0.0.1'
PORT = 42001
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
scratchSock.send(a.tostring() + cmd)
print a.tostring()
print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("connected")
for i in xrange(10):
sendScratchCommand('sensor-update note ' + str(60 + (2 * i)) + ' beats 0.4')
sendScratchCommand('broadcast "beat"')
print("beat!")
time.sleep(0.5)
print("closing socket...")
scratchSock.close()
print("done")