-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (56 loc) · 1.95 KB
/
main.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import bluetooth
def to_byte(value):
return value.to_bytes(1, 'big')
def parameter(servoID, servoAngle, runTime, frameDelay):
return message(b'\x22', [to_byte(servoID), to_byte(servoAngle), to_byte(runTime), to_byte(frameDelay)])
def main():
msg4 = parameter(4,0, 0, 0)
multipleServo = [ # 123
to_byte(111), to_byte(0), to_byte(0),
# 456
to_byte(180), to_byte(180), to_byte(180),
# 7891011
to_byte(90), to_byte(60), to_byte(74), to_byte(110), to_byte(90),
# 1213141516
to_byte(0), to_byte(120), to_byte(184), to_byte(70), to_byte(90),
to_byte(157), to_byte(50), to_byte(50),
to_byte(152), to_byte(50), to_byte(50),
to_byte(255), to_byte(255), to_byte(255), to_byte(255), to_byte(0),
to_byte(0), to_byte(0), to_byte(0), to_byte(0), to_byte(0)]
msg5 = message(b'\x23', multipleServo)
bd_addr = discover()
if bd_addr:
port = 6
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
print('Connected')
sock.settimeout(60.0)
sock.send(msg4)
sock.send(msg5)
print('Sent data')
sock.close()
def message(command, parameters):
header = b'\xFB\xBF'
end = b'\xED'
parameter = b''.join(parameters)
# len(header + length + command +parameters + check)
length = bytearray([len(parameters) + 5])
data = [command, length]
data.extend(parameters)
total = 0;
for x in data:
total += ord(x)
total %= 256
check = bytes([total])
return header + length + command + parameter + check + end
def discover():
print("searching ...")
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("found %d devices" % len(nearby_devices))
print(nearby_devices)
for addr, name in nearby_devices:
if name == "Alpha1_C5C4":
print(addr)
return addr
if __name__ == '__main__':
main()