-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlumenXIAO.py
More file actions
101 lines (92 loc) · 3.37 KB
/
Copy pathlumenXIAO.py
File metadata and controls
101 lines (92 loc) · 3.37 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
from machine import Timer
from neopixel import NeoPixel
from config import config
from gc import collect
'''
Lumen (LED Indicator) for the Seeedstudio XIAO RP2040
Drives the onboard NeoPixel with moods
The additional RGB 'user' led is cycled RGB to indocate send events
'''
class lumen:
def __init__(self, bright, standby, flash):
'''
start led/neopixel etc
properties:
bright = float(0..1), intensity
standby = float(0..1), intensity when off
flash = int(), flash duration in ms
'''
self.bright = bright
self.standby = standby
self.flash = flash
self._timer = None # otherwise timer gets garbage collected..
self._moods = {'off':(255,128,0),
'idle':(0,255,0),
'busy':(255,255,255),
'job':(255,0,255),
'paused':(0,0,255),
'err':(255,0,0)}
# Neopixel; on the xiao the pixel VCC comes from a Pin; allowing it to
# be fully powered off in low power modes.
self._pixelVcc = config.pixel_power # Pixel VCC pin
self._pixelVcc.value(1) # turn on immediately
self._pixel = NeoPixel(config.pixel_pin,1) # data pin
self._pixel[0]=(0,0,0)
self._pixel.write()
def blink(self, mood, dim=False, auto=True):
'''
flash the mood after an update finishes
if auto=True it uses an interrupt timer
to turn off after 'self.flash' time
'''
def unblink(t):
# called by timer
self._pixel[0] = (0,0,0)
self._pixel.write()
if mood is None:
return
bright = self.standby if dim else self.bright
neo = self._moods[mood]
self._pixel[0] = (int(neo[0]*bright),
int(neo[1]*bright),
int(neo[2]*bright))
self._pixel.write()
# neopixel driver seems to bleed memory. sigh.
collect()
if auto:
self._timer = Timer(period=self.flash, mode=Timer.ONE_SHOT, callback=unblink)
def emote(self,model,net=None):
'''
Use the model to find our mood by mapping the
status to colors, crudely.
'''
if model is None:
return('err')
if net is not None:
interface = model['network']['interfaces'][net]
online = True if interface['state'] is 'active' else False
else:
# If no netwok; set online=True so that we do not signal 'error' when off or idle
online = True
if model['state']['machineMode'] == '':
return('err')
status = model['state']['status']
if status in ['disconnected','halted']:
return('err')
if status in ['off','idle']:
return status if online else 'err'
if status in ['starting','updating','busy','changingTool']:
return('busy')
if status in ['pausing','paused','resuming','cancelling']:
return('paused')
if status in ['processing','simulating']:
return('job')
return None
def off(self):
# Instant off
self._pixel[0] = (0,0,0)
self._pixel.write()
try:
self._timer.deinit()
except:
pass