-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLaserExample.py
executable file
·83 lines (63 loc) · 2.92 KB
/
LaserExample.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
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
import sys
import binascii
import struct
import time
from time import sleep
from bluepy.btle import UUID, Peripheral
startPosition = 0
endPosition = 12543
motorSpeed = 255
laserServiceUuid = UUID("SLASER SERVICE ".encode("ascii").hex()) # Laser service
laserStatusUuid = UUID("LASER STATUS ".encode("ascii").hex()) # Laser status
motorServiceUuid = UUID("SMOTOR SERVICE ".encode("ascii").hex()) # Motor service
motorCposUuid = UUID("MOTOR CPOS ".encode("ascii").hex()) # Motor position notification
motorIposUuid = UUID("MOTOR IPOS ".encode("ascii").hex()) # Motor posision
motorMModeUuid = UUID("MOTOR MMODE ".encode("ascii").hex()) # No clue what this does
motorSModeUuid = UUID("MOTOR SMODE ".encode("ascii").hex()) # I guess this could be the microstepping of the motor
motorSpeedUuid = UUID("MOTOR SPEED ".encode("ascii").hex()) # Motor speed
ledServiceUuid = UUID("SLED SERVICE ".encode("ascii").hex()) # Led service
ledTypeUuid = UUID("LED TYPE ".encode("ascii").hex()) # Led type
device = Peripheral("F2:33:8B:40:86:D0", "random")
def decodeUuid(uuid):
try:
return bytes.fromhex(str(uuid).replace("-", "")).decode("ascii")
except:
return ""
def listServices():
print ("Services...")
for svc in device.services:
serviceName = decodeUuid(svc.uuid)
print (str(svc.uuid) + ": '" + serviceName + "'")
for ch in svc.getCharacteristics():
channelName = decodeUuid(ch.uuid)
print (" " + str(ch.uuid) + ": '" + channelName + "'")
# To debug services
# listServices()
try:
LaserService = device.getServiceByUUID(laserServiceUuid)
MotorService = device.getServiceByUUID(motorServiceUuid)
LedService = device.getServiceByUUID(ledServiceUuid)
laserStatusCharacteristics = LaserService.getCharacteristics(laserStatusUuid)[0]
motorIposCharacteristics = MotorService.getCharacteristics(motorIposUuid)[0]
motorMModeCharacteristics = MotorService.getCharacteristics(motorMModeUuid)[0]
motorSModeCharacteristics = MotorService.getCharacteristics(motorSModeUuid)[0]
motorSpeedCharacteristics = MotorService.getCharacteristics(motorSpeedUuid)[0]
print("Waiting until device has calibrated")
sleep(30)
print("Enable laser")
laserStatusCharacteristics.write(b"\x01")
print("Move to endposition")
motorSpeedCharacteristics.write(motorSpeed.to_bytes(1, byteorder="little"))
motorIposCharacteristics.write(endPosition.to_bytes(4, byteorder="little"))
print("Wait for move to finish")
sleep(10)
print("Move back to start position")
motorSpeedCharacteristics.write(motorSpeed.to_bytes(1, byteorder="little"))
motorIposCharacteristics.write(startPosition.to_bytes(4, byteorder="little"))
print("Wait for move to finish")
sleep(10)
print("Disable laser")
laserStatusCharacteristics.write(b"\x00")
finally:
device.disconnect()