-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
288 lines (226 loc) · 8.89 KB
/
code.py
File metadata and controls
288 lines (226 loc) · 8.89 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Circuit Playground Bluefruit Interactive Globe
# Use with the Adafruit BlueFruit LE Connect app
# Works with CircuitPython 5.0.0-beta.0 and later
# running on an nRF52840 CPB board.
import time
import board
import digitalio
import neopixel
import pwmio
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.color_packet import ColorPacket
from adafruit_bluefruit_connect.button_packet import ButtonPacket
# Prep the status LED on the CPB
red_led = digitalio.DigitalInOut(board.D13)
red_led.direction = digitalio.Direction.OUTPUT
# Setup bluetooth
ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)
# Setup neopixels
neopixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)
PURPLE = (120, 0, 160)
YELLOW = (100, 100, 0)
AQUA = (0, 100, 100)
BLACK = (0, 0, 0)
color = PURPLE # current NeoPixel color
neopixels.fill(color)
# Setup PWM output
pwm = pwmio.PWMOut(board.D1, duty_cycle=0)
pwm_max = 65535
pwm_max_half = pwm_max // 2
pwm_step = 1024
pwm_step_n_max = 64
pwm_step_n = 0
# Setup state machine variables
mode = ''
state = ''
i = 0
LAST_TIME = -1
TIME_INTERVAL = 0.5
print("BLE PWM control")
print("Use Adafruit Bluefruit app to connect")
while True:
# set a pixel black when bluetooth is disconnected
neopixels[0] = BLACK
neopixels.show()
ble.start_advertising(advertisement)
while not ble.connected:
# Wait for a connection.
pass
# set a pixel blue when connected
neopixels[0] = BLUE
neopixels.show()
# main program runs with bluetooth connection
while ble.connected:
# Bluetooth message center
if uart_service.in_waiting:
# Packet is arriving.
red_led.value = False # turn off red LED
packet = Packet.from_stream(uart_service)
if isinstance(packet, ColorPacket):
# Change the color.
color = packet.color
neopixels.fill(color)
# control PWM and state machine with buttons
if isinstance(packet, ButtonPacket) and packet.pressed:
red_led.value = True # blink to show packet has been received
if packet.button == ButtonPacket.UP:
# Step up PWM
if pwm_step_n < pwm_step_n_max:
neopixels.fill(color)
pwm_step_n += 1
pwm.duty_cycle = pwm_step_n * pwm_step - 1
print("PWM level {}".format(pwm.duty_cycle))
elif packet.button == ButtonPacket.DOWN:
# Step down PWM
if pwm_step_n > 0:
neopixels.fill(color)
pwm_step_n -= 1
if pwm_step_n != 0:
pwm.duty_cycle = pwm_step_n * pwm_step - 1
else:
pwm.duty_cycle = 0
print("PWM level {}".format(pwm.duty_cycle))
elif packet.button == ButtonPacket.RIGHT:
# Turn PWM off
color = YELLOW
neopixels.fill(color)
pwm_step_n = pwm_step_n_max // 2
pwm.duty_cycle = pwm_max_half
print("PWM level {}".format(pwm.duty_cycle))
elif packet.button == ButtonPacket.LEFT:
# Turn PWM fully on
color = YELLOW
neopixels.fill(color)
pwm_step_n = 0
pwm.duty_cycle = 0
print("PWM level {}".format(pwm.duty_cycle))
elif packet.button == ButtonPacket.BUTTON_1:
# Start ramp state machine
mode = 'ramp'
state = 'initial'
elif packet.button == ButtonPacket.BUTTON_2:
color = GREEN
neopixels.fill(color)
mode = 'glow'
state = 'initial'
elif packet.button == ButtonPacket.BUTTON_3:
color = BLUE
neopixels.fill(color)
elif packet.button == ButtonPacket.BUTTON_4:
color = PURPLE
neopixels.fill(color)
# do this when some buttons are released
elif isinstance(packet, ButtonPacket) and not packet.pressed:
if packet.button == ButtonPacket.UP:
neopixels.fill(RED)
if packet.button == ButtonPacket.DOWN:
neopixels.fill(RED)
# State machine
if 'ramp' in mode:
# What is the time?
now = time.monotonic()
if now >= LAST_TIME + TIME_INTERVAL:
# Do something after an set interval
if 'initial' in state:
# # Initial state - kick start PWM
#
# # Full red colour bar for 100% PWM
# neopixels.fill(RED)
#
# # Start PWM at full power
# pwm.duty_cycle = pwm_max
#
# # Move to next state
# state = 'kick'
#
# # Set LAST_TIME so that the next interval
# # is short
# now = now + (TIME_INTERVAL * 0.9)
#
# elif 'kick' in state:
# # End kick and move to next state
# Full AQUA colour bar for 0% PWM
# PWM set in next state
neopixels.fill(AQUA)
# Set 0% PWM
pwm.duty_cycle = 0
# Transition to next state
state = 'ramp_up'
steps = 50
min_frac = 0.5
max_frac = 0.70
i = 0
elif 'ramp_up' in state:
# PWM up and down
if i < steps:
# Up ramp
pwm.duty_cycle = int(pwm_max * max_frac * (1 + (1 - min_frac) * (i/steps - 1)))
print("PWM level {}".format(pwm.duty_cycle))
neopixel_index = i // 5
neopixels[neopixel_index] = RED
i += 1
else:
state = 'ramp_down'
i = 0
elif 'ramp_down' in state:
# PWM up and down
if i < steps:
# Down ramp
pwm.duty_cycle = int(pwm_max * max_frac * (1 - (1 - min_frac) * i / steps))
print("PWM level {}".format(pwm.duty_cycle))
neopixel_index = 9 - i // 5
neopixels[neopixel_index] = AQUA
i += 1
else:
state = 'end'
elif 'end' in state:
# Turn off PWM
pwm.duty_cycle = 0
neopixels.fill(color)
state = ''
mode = ''
# Record time since last
LAST_TIME = now
elif "glow" in mode:
# What is the time?
now = time.monotonic()
if now >= LAST_TIME + TIME_INTERVAL:
if 'initial' in state:
pwm.duty_cycle = int(pwm_max * 0.70)
i = 0
steps = 100
neopixels.fill((255, 255, 255))
neopixels.brightness = 0
TIME_INTERVAL = 0.1
state = 'ramp_down'
elif 'ramp_up' in state:
# LED up and down
if i < steps:
# Up ramp
neopixels.brightness = i / steps
print("LED level {}".format(neopixels.brightness))
i += 1
else:
state = 'ramp_down'
i = 0
elif 'ramp_down' in state:
# PWM up and down
if i < steps:
# Down ramp
neopixels.brightness = 1 - (i / steps)
print("LED level {}".format(neopixels.brightness))
i += 1
else:
neopixels.brightness = 0
i = 0
state = 'ramp_up'
# Record time since last
LAST_TIME = now