-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRainbowBLE.py
More file actions
130 lines (117 loc) · 3.96 KB
/
RainbowBLE.py
File metadata and controls
130 lines (117 loc) · 3.96 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import sys
import time
import board
import neopixel
import evdev
import threading
from datetime import datetime
pixel_pin = board.D18
num_pixels = 144
# This is the host machine MAC - it will need customising
btMac = "b8:27:eb:60:5c:77"
global go
try:
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.1, auto_write=False, pixel_order=ORDER
)
def trackButton():
doOutput("BLE button tracker thread running")
global go
while True:
try:
doOutput("Monitoring for button activity")
for event in device.read_loop():
if event.type == evdev.ecodes.EV_KEY:
data = evdev.categorize(event)
if event.code == 330 and data.keystate == 0 and go == 0:
doOutput("Short Push - LED panel Rainbow ON")
go = 1
elif event.code == 330 and data.keystate == 0 and go == 1:
doOutput("Short Push - LED panel Rainbow OFF")
go = 0
elif event.code == 114 and data.keystate == 0:
doOutput("Long push - Rebooting!")
os.system('/usr/sbin/reboot')
except:
doOutput("Bluetooth is having an issue. Glitch or timeout? Who knows. Restarting program.")
sys.stdout.flush()
os.execv(sys.executable, ['python'] + [sys.argv[0]])
def panel(pos):
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow():
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = panel(pixel_index & 255)
if go == 0:
blankDisplay()
break
pixels.show()
def doOutput(msg):
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S") + " " + msg)
def blankDisplay():
pixels.fill((0,0,0))
pixels.show()
#######
doOutput("Modified rainbow LED demo with BLE button control v0.0001")
blankDisplay()
buttonPresent = 0
butMsg = 0
blueLed = 0
while buttonPresent == 0:
try:
while buttonPresent == 0:
# This is a disgrace. Device needs enumerating properly.
device = evdev.InputDevice('/dev/input/event2')
if str(device.phys) == btMac:
doOutput("BLE button detected. Let's go!")
buttonPresent = 1
pixels[0] = [ 0, 255, 0 ]
pixels.show()
time.sleep(0.1)
blueLed = 1
except:
if butMsg == 0:
doOutput("Waiting for button device to appear (click it!)")
butMsg = 1
# Blink top left pixel while waiting for BLE device to appear
if blueLed == 0:
pixels[0] = [0, 0, 255]
pixels.show()
blueLed = 1
elif blueLed == 1:
pixels[0] = [0, 0, 0]
pixels.show()
blueLed = 0
time.sleep(0.2)
blankDisplay()
buttonMonitor = threading.Thread(target=trackButton)
buttonMonitor.start()
go = 0
while True:
while go == 1:
rainbow()
time.sleep(0.01)
except KeyboardInterrupt:
blankDisplay()
doOutput("Killed by death!")
quit()