-
Notifications
You must be signed in to change notification settings - Fork 7
/
badge.py
134 lines (115 loc) · 3.15 KB
/
badge.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
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
## Here's all the stuff to make your badge's hardware operate normally.
import pyb
import dcfurs
import settings
from pyb import Pin
from pyb import Timer
from pyb import ExtInt
from pyb import I2C
from pyb import UART
from lis2de12 import lis2de12
from bluetooth import bluetooth
# The I2C bus used for all the off-chip stuff.
bus = I2C(3, I2C.MASTER, baudrate=400000)
##-----------------------------------------------
## LED Matrix Drivers
##-----------------------------------------------
import micropython
micropython.alloc_emergency_exception_buf(100)
## Bring up the LED matrix
pwmclk = Timer(8, freq=500000)
dcfurs.init(pwmclk)
dcfurs.clear()
##-----------------------------------------------
## Bluetooth Module
##-----------------------------------------------
ble = None
try:
if (bus.mem_read(8, 0x42, 0)):
ble = bluetooth(bus)
ble.write(ble.REG_COOLDOWN, settings.blecooldown)
except Exception as e:
ble = None
##-----------------------------------------------
## Pushbutton Class
##-----------------------------------------------
class switch(Pin):
def __init__(self, pin):
self.pin = pin
self.prev = self.pin.value()
## Returns 1 on a rising edge, or 0 on no change.
def event(self):
if not self.prev:
self.prev = self.pin.value()
return self.prev
else:
self.prev = self.pin.value()
return 0
right = switch(Pin('SW1', Pin.IN))
left = switch(Pin('SW2', Pin.IN))
##-----------------------------------------------
## Capacative Touch Controller
##-----------------------------------------------
boop = dcfurs.boop(settings.boopselect)
tmr = Timer(2, freq=100)
tmr.callback(lambda t: boop.start())
##-----------------------------------------------
## Motion Detection / Accelerometer
##-----------------------------------------------
class dummy:
def x(self):
return 0
def y(self):
return 0
def z(self):
return 0
def read(self, addr):
return 0
def filtered_xyz(self):
return (0, 0, 0)
# Wrap in a try/except in case we are running on
# a prototype with a bad accelerometer.
try:
imu = lis2de12(bus)
except:
imu = dummy()
##-----------------------------------------------
## Low Power Sleep
##-----------------------------------------------
## Check for low power states, or do nothing.
def trysuspend():
return False
##-----------------------------------------------
## Hue/Value to RGB888 conversion
##-----------------------------------------------
def hue2rgb(hue, val=255):
sextant = hue // 60
remainder = hue % 60
p = 0 # full saturation always results in zero.
q = (val * (60 - remainder)) // 60
t = (val * remainder) // 60
if (sextant == 0):
r = val
g = t
b = p
elif (sextant == 1):
r = q
g = val
b = p
elif (sextant == 2):
r = p
g = val
b = t
elif (sextant == 3):
r = p
g = q
b = val
elif (sextant == 4):
r = t
g = p
b = val
else:
r = val
g = p
b = q
return (r << 16) | (g << 8) | b