Skip to content

Commit 7d0bb04

Browse files
committed
Keybow2040: Basic-ish USB examples.
1 parent 392890e commit 7d0bb04

File tree

3 files changed

+419
-0
lines changed

3 files changed

+419
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Borrows heavily from : https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731/blob/main/adafruit_is31fl3731/keybow2040.py
2+
# and : https://github.com/adafruit/micropython-adafruit-is31fl3731/blob/master/is31fl3731.py
3+
import math
4+
import utime
5+
6+
7+
_MODE_REGISTER = const(0x00) # noqa: F821
8+
_FRAME_REGISTER = const(0x01) # noqa: F821
9+
_AUTOPLAY1_REGISTER = const(0x02) # noqa: F821
10+
_AUTOPLAY2_REGISTER = const(0x03) # noqa: F821
11+
_BLINK_REGISTER = const(0x05) # noqa: F821
12+
_AUDIOSYNC_REGISTER = const(0x06) # noqa: F821
13+
_BREATH1_REGISTER = const(0x08) # noqa: F821
14+
_BREATH2_REGISTER = const(0x09) # noqa: F821
15+
_SHUTDOWN_REGISTER = const(0x0a) # noqa: F821
16+
_GAIN_REGISTER = const(0x0b) # noqa: F821
17+
_ADC_REGISTER = const(0x0c) # noqa: F821
18+
19+
_CONFIG_BANK = const(0x0b) # noqa: F821
20+
_BANK_ADDRESS = const(0xfd) # noqa: F821
21+
22+
_PICTURE_MODE = const(0x00) # noqa: F821
23+
_AUTOPLAY_MODE = const(0x08) # noqa: F821
24+
_AUDIOPLAY_MODE = const(0x18) # noqa: F821
25+
26+
_ENABLE_OFFSET = const(0x00) # noqa: F821
27+
_BLINK_OFFSET = const(0x12) # noqa: F821
28+
_COLOR_OFFSET = const(0x24) # noqa: F821
29+
30+
31+
class Matrix:
32+
width = 16
33+
height = 9
34+
35+
def __init__(self, i2c, address=0x74):
36+
self.i2c = i2c
37+
self.address = address
38+
self.reset()
39+
self.init()
40+
41+
def _bank(self, bank=None):
42+
if bank is None:
43+
return self.i2c.readfrom_mem(self.address, _BANK_ADDRESS, 1)[0]
44+
self.i2c.writeto_mem(self.address, _BANK_ADDRESS, bytearray([bank]))
45+
46+
def _register(self, bank, register, value=None):
47+
self._bank(bank)
48+
if value is None:
49+
return self.i2c.readfrom_mem(self.address, register, 1)[0]
50+
self.i2c.writeto_mem(self.address, register, bytearray([value]))
51+
52+
def _mode(self, mode=None):
53+
return self._register(_CONFIG_BANK, _MODE_REGISTER, mode)
54+
55+
def init(self):
56+
self._mode(_PICTURE_MODE)
57+
self.frame(0)
58+
for frame in range(8):
59+
self.fill(0, False, frame=frame)
60+
for col in range(18):
61+
self._register(frame, _ENABLE_OFFSET + col, 0xff)
62+
self.audio_sync(False)
63+
64+
def reset(self):
65+
self.sleep(True)
66+
utime.sleep_us(10)
67+
self.sleep(False)
68+
69+
def sleep(self, value):
70+
return self._register(_CONFIG_BANK, _SHUTDOWN_REGISTER, not value)
71+
72+
def autoplay(self, delay=0, loops=0, frames=0):
73+
if delay == 0:
74+
self._mode(_PICTURE_MODE)
75+
return
76+
delay //= 11
77+
if not 0 <= loops <= 7:
78+
raise ValueError("Loops out of range")
79+
if not 0 <= frames <= 7:
80+
raise ValueError("Frames out of range")
81+
if not 1 <= delay <= 64:
82+
raise ValueError("Delay out of range")
83+
self._register(_CONFIG_BANK, _AUTOPLAY1_REGISTER, loops << 4 | frames)
84+
self._register(_CONFIG_BANK, _AUTOPLAY2_REGISTER, delay % 64)
85+
self._mode(_AUTOPLAY_MODE | self._frame)
86+
87+
def fade(self, fade_in=None, fade_out=None, pause=0):
88+
if fade_in is None and fade_out is None:
89+
self._register(_CONFIG_BANK, _BREATH2_REGISTER, 0)
90+
elif fade_in is None:
91+
fade_in = fade_out
92+
elif fade_out is None:
93+
fade_out = fade_in
94+
fade_in = int(math.log(fade_in / 26, 2))
95+
fade_out = int(math.log(fade_out / 26, 2))
96+
pause = int(math.log(pause / 26, 2))
97+
if not 0 <= fade_in <= 7:
98+
raise ValueError("Fade in out of range")
99+
if not 0 <= fade_out <= 7:
100+
raise ValueError("Fade out out of range")
101+
if not 0 <= pause <= 7:
102+
raise ValueError("Pause out of range")
103+
self._register(_CONFIG_BANK, _BREATH1_REGISTER, fade_out << 4 | fade_in)
104+
self._register(_CONFIG_BANK, _BREATH2_REGISTER, 1 << 4 | pause)
105+
106+
def frame(self, frame=None, show=True):
107+
if frame is None:
108+
return self._frame
109+
if not 0 <= frame <= 8:
110+
raise ValueError("Frame out of range")
111+
self._frame = frame
112+
if show:
113+
self._register(_CONFIG_BANK, _FRAME_REGISTER, frame)
114+
115+
def audio_sync(self, value=None):
116+
return self._register(_CONFIG_BANK, _AUDIOSYNC_REGISTER, value)
117+
118+
def audio_play(self, sample_rate, audio_gain=0,
119+
agc_enable=False, agc_fast=False):
120+
if sample_rate == 0:
121+
self._mode(_PICTURE_MODE)
122+
return
123+
sample_rate //= 46
124+
if not 1 <= sample_rate <= 256:
125+
raise ValueError("Sample rate out of range")
126+
self._register(_CONFIG_BANK, _ADC_REGISTER, sample_rate % 256)
127+
audio_gain //= 3
128+
if not 0 <= audio_gain <= 7:
129+
raise ValueError("Audio gain out of range")
130+
self._register(_CONFIG_BANK, _GAIN_REGISTER,
131+
bool(agc_enable) << 3 | bool(agc_fast) << 4 | audio_gain)
132+
self._mode(_AUDIOPLAY_MODE)
133+
134+
def blink(self, rate=None):
135+
if rate is None:
136+
return (self._register(_CONFIG_BANK, _BLINK_REGISTER) & 0x07) * 270
137+
elif rate == 0:
138+
self._register(_CONFIG_BANK, _BLINK_REGISTER, 0x00)
139+
return
140+
rate //= 270
141+
self._register(_CONFIG_BANK, _BLINK_REGISTER, rate & 0x07 | 0x08)
142+
143+
def fill(self, color=None, blink=None, frame=None):
144+
if frame is None:
145+
frame = self._frame
146+
self._bank(frame)
147+
if color is not None:
148+
if not 0 <= color <= 255:
149+
raise ValueError("Color out of range")
150+
data = bytearray([color] * 24)
151+
for row in range(6):
152+
self.i2c.writeto_mem(self.address,
153+
_COLOR_OFFSET + row * 24, data)
154+
if blink is not None:
155+
data = bool(blink) * 0xff
156+
for col in range(18):
157+
self._register(frame, _BLINK_OFFSET + col, data)
158+
159+
def write_frame(self, data, frame=None):
160+
if len(data) > 144:
161+
raise ValueError("Bytearray too large for frame")
162+
if frame is None:
163+
frame = self._frame
164+
self._bank(frame)
165+
self.i2c.writeto_mem(self.address, _COLOR_OFFSET, data)
166+
167+
def _pixel_addr(self, x, y):
168+
return x + y * 16
169+
170+
def pixel(self, x, y, color=None, blink=None, frame=None):
171+
if not 0 <= x <= self.width:
172+
return
173+
if not 0 <= y <= self.height:
174+
return
175+
pixel = self._pixel_addr(x, y)
176+
if color is None and blink is None:
177+
return self._register(self._frame, pixel)
178+
if frame is None:
179+
frame = self._frame
180+
if color is not None:
181+
if not 0 <= color <= 255:
182+
raise ValueError("Color out of range")
183+
self._register(frame, _COLOR_OFFSET + pixel, color)
184+
if blink is not None:
185+
addr, bit = divmod(pixel, 8)
186+
bits = self._register(frame, _BLINK_OFFSET + addr)
187+
if blink:
188+
bits |= 1 << bit
189+
else:
190+
bits &= ~(1 << bit)
191+
self._register(frame, _BLINK_OFFSET + addr, bits)
192+
193+
194+
class Matrix_Keybow2040(Matrix):
195+
width = 16
196+
height = 3
197+
198+
def pixelrgb(self, x, y, r, g, b, blink=None, frame=None):
199+
"""
200+
Blink or brightness for x, y-pixel
201+
202+
:param x: horizontal pixel position
203+
:param y: vertical pixel position
204+
:param r: red brightness value 0->255
205+
:param g: green brightness value 0->255
206+
:param b: blue brightness value 0->255
207+
:param blink: True to blink
208+
:param frame: the frame to set the pixel
209+
"""
210+
x = (4 * (3 - x)) + y
211+
212+
super().pixel(x, 0, r, blink, frame)
213+
super().pixel(x, 1, g, blink, frame)
214+
super().pixel(x, 2, b, blink, frame)
215+
216+
def _pixel_addr(self, x, y):
217+
lookup = [
218+
(120, 88, 104), # 0, 0
219+
(136, 40, 72), # 1, 0
220+
(112, 80, 96), # 2, 0
221+
(128, 32, 64), # 3, 0
222+
(121, 89, 105), # 0, 1
223+
(137, 41, 73), # 1, 1
224+
(113, 81, 97), # 2, 1
225+
(129, 33, 65), # 3, 1
226+
(122, 90, 106), # 0, 2
227+
(138, 25, 74), # 1, 2
228+
(114, 82, 98), # 2, 2
229+
(130, 17, 66), # 3, 2
230+
(123, 91, 107), # 0, 3
231+
(139, 26, 75), # 1, 3
232+
(115, 83, 99), # 2, 3
233+
(131, 18, 67), # 3, 3
234+
]
235+
236+
return lookup[x][y]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import time
2+
import usb.device
3+
from usb.device.keyboard import KeyboardInterface, KeyCode
4+
from pimoroni import Button
5+
6+
# A very basic HID Keyboard example for Keybow 2040
7+
# Inspired by: https://github.com/micropython/micropython-lib/blob/master/micropython/usb/examples/device/keyboard_example.py
8+
9+
# This example requires a Pico USB compatible firmware, eg: pico_usb-1.23.0-pimoroni-micropython.uf2
10+
11+
# The pin order for Keybow 2040 is weird,
12+
# But the below is laid out to match the pad with USB up
13+
# from top left to bottom right.
14+
KEYS = {
15+
18: KeyCode.A, 14: KeyCode.B, 10: KeyCode.C, 6: KeyCode.D,
16+
19: KeyCode.E, 15: KeyCode.F, 11: KeyCode.G, 7: KeyCode.H,
17+
20: KeyCode.I, 16: KeyCode.J, 12: KeyCode.K, 8: KeyCode.L,
18+
21: KeyCode.M, 17: KeyCode.N, 13: KeyCode.O, 9: KeyCode.P,
19+
}
20+
21+
# We'll fill this with Button instances
22+
BUTTONS = {}
23+
24+
25+
class Keybow2040(KeyboardInterface):
26+
def on_led_update(self, led_mask):
27+
pass
28+
29+
30+
def main():
31+
for pin, keycode in KEYS.items():
32+
BUTTONS[pin] = Button(pin)
33+
34+
k = Keybow2040()
35+
usb.device.get().init(k, builtin_driver=True)
36+
37+
keys = [0, 0, 0, 0, 0, 0]
38+
39+
while True:
40+
changed = False
41+
if k.is_open():
42+
for pin, code in KEYS.items():
43+
pressed = BUTTONS[pin].read()
44+
45+
# If the key is pressed then try to insert
46+
# it at the first zero. Otherwise (try to) replace
47+
# its keycode with 0 to clear that press.
48+
c = code if pressed else 0
49+
i = 0 if pressed else code
50+
51+
try:
52+
keys[keys.index(i)] = c
53+
changed = True
54+
except ValueError:
55+
# Either our 6-key list is full
56+
# or we're releasing a key that's not in it!
57+
# Or handling a key that isn't pressed.
58+
pass
59+
60+
if changed:
61+
k.send_keys(keys)
62+
time.sleep_ms(1)
63+
64+
65+
if __name__ == "__main__":
66+
print("Starting Keybow 2040...")
67+
main()

0 commit comments

Comments
 (0)