diff --git a/characters.py b/characters.py index 862d3f4..a678af9 100644 --- a/characters.py +++ b/characters.py @@ -1,3 +1,5 @@ +from adafruit_hid.keycode import Keycode + characters = [ [ [ @@ -13,7 +15,7 @@ characters = [ ], [ [ - ["\u232b", "\u003a", None, "\u0033"], #00100 [BACKSPACE, :, None, 3] + ["\u232b", "\u003a", None, "\u0033"], #00100 [BACKSPACE, COLON, None, 3] ["\u0061", "\u0041", "\u002b", "\u0036"], #00101 [a, A, PLUS, 6] ], [ @@ -86,13 +88,130 @@ characters = [ ], [ ["\u006e", "\u004e", "\u005b", "\u29c9"], #11110 [n, N, OPEN SQUARE BRACKET, CTRL+C] - ["\u0074", "\u0054", "\u0026", "▢"], #11111 [t, T, AMPERSAND, SUPER] + ["\u0074", "\u0054", "\u0026", "\u2318"], #11111 [t, T, AMPERSAND, SUPER] ], ], ], ], ] +keycodes = { + "\u2423": [Keycode.SPACEBAR], + "\u002e": [Keycode.PERIOD], + "\u003c": [Keycode.SHIFT, Keycode.COMMA], + "\u0031": [Keycode.ONE], + "\u21b9": [Keycode.TAB], + "\u002c": [Keycode.COMMA], + "\u0032": [Keycode.TWO], + "\u0065": [Keycode.E], + "\u0045": [Keycode.SHIFT, Keycode.E], + "\u0045": [Keycode.EQUALS], + "\u0035": [Keycode.FIVE], + "\u232b": [Keycode.BACKSPACE], + "\u003a": [Keycode.SHIFT, Keycode.SEMICOLON], + "\u0033": [Keycode.THREE], + "\u0061": [Keycode.A], + "\u0041": [Keycode.SHIFT, Keycode.A], + "\u002b": [Keycode.SHIFT, Keycode.EQUALS], + "\u0036": [Keycode.SIX], + "\u0072": [Keycode.R], + "\u0052": [Keycode.SHIFT, Keycode.R], + "\u002f": [Keycode.FORWARD_SLASH], + "\u1f50e": [Keycode.LEFT_CONTROL, Keycode.F], + "\u0075": [Keycode.U], + "\u0055": [Keycode.SHIFT, Keycode.U], + "\u0027": [Keycode.QUOTE], + "\u0038": [Keycode.EIGHT], + "\u238b": [Keycode.ESCAPE], + "\u003b": [Keycode.SEMICOLON], + "\u0034": [Keycode.FOUR], + "\u0069": [Keycode.I], + "\u0049": [Keycode.SHIFT, Keycode.I], + "\u002a": [Keycode.SHIFT, Keycode.EIGHT], + "\u0037": [Keycode.SEVEN], + "\u0063": [Keycode.C], + "\u0043": [Keycode.SHIFT, Keycode.C], + "\u005c": [Keycode.BACKSLASH], + "\u261d": [Keycode.LEFT_CONTROL, Keycode.A], + "\u0078": [Keycode.X], + "\u0058": [Keycode.SHIFT, Keycode.X], + "\u0022": [Keycode.SHIFT, Keycode.QUOTE], + "\u0039": [Keycode.NINE], + "\u006c": [Keycode.L], + "\u004c": [Keycode.SHIFT, Keycode.L], + "\u007e": [Keycode.SHIFT, Keycode.GRAVE_ACCENT], + "\u2190\u2318": [Keycode.APPLICATION, Keycode.LEFT_ARROW], + "\u007a": [Keycode.Z], + "\u005a": [Keycode.SHIFT, Keycode.Z], + "\u003e": [Keycode.SHIFT, Keycode.PERIOD], + "\u2192\u2318": [Keycode.APPLICATION, Keycode.RIGHT_ARROW], + "\u0064": [Keycode.D], + "\u0044": [Keycode.SHIFT, Keycode.D], + "\u0060": [Keycode.GRAVE_ACCENT], + "\u29c8": [Keycode.LEFT_CONTROL, Keycode.V], + "\u0068": [Keycode.H], + "\u0048": [Keycode.SHIFT, Keycode.H], + "\u0040": [Keycode.SHIFT, Keycode.TWO], + "\u0030": [Keycode.ZERO], + "\u23ce": [Keycode.RETURN], + "\u21e7\u23ce": [Keycode.SHIFT, Keycode.RETURN], + "\u002d": [Keycode.MINUS], + "\u2399": [Keycode.PRINT_SCREEN], + "\u006f": [Keycode.O], + "\u004f": [Keycode.SHIFT, Keycode.O], + "\u0029": [Keycode.SHIFT, Keycode.ZERO], + "\u2193": [Keycode.DOWN_ARROW], + "\u006d": [Keycode.M], + "\u004d": [Keycode.SHIFT, Keycode.M], + "\u0028": [Keycode.SHIFT, Keycode.NINE], + "\u2192": [Keycode.RIGHT_ARROW], + "\u0070": [Keycode.P], + "\u0050": [Keycode.SHIFT, Keycode.P], + "\u005f": [Keycode.SHIFT, Keycode.MINUS], + "\u0066": [Keycode.F], + "\u0046": [Keycode.SHIFT, Keycode.F], + "\u0023": [Keycode.SHIFT, Keycode.THREE], + "\u2191": [Keycode.UP_ARROW], + "\u0077": [Keycode.W], + "\u0057": [Keycode.SHIFT, Keycode.W], + "\u007c": [Keycode.SHIFT, Keycode.BACKSLASH], + "\u006a": [Keycode.J], + "\u004a": [Keycode.SHIFT, Keycode.J], + "\u0025": [Keycode.SHIFT, Keycode.FIVE], + "\u0071": [Keycode.Q], + "\u0051": [Keycode.SHIFT, Keycode.Q], + "\u003f": [Keycode.SHIFT, Keycode.FORWARD_SLASH], + "\u0079": [Keycode.Y], + "\u0059": [Keycode.SHIFT, Keycode.Y], + "\u0024": [Keycode.SHIFT, Keycode.FOUR], + "\u2318": [Keycode.LEFT_ARROW], + "\u0062": [Keycode.B], + "\u0042": [Keycode.SHIFT, Keycode.B], + "\u007d": [Keycode.SHIFT, Keycode.RIGHT_BRACKET], + "\u006b": [Keycode.K], + "\u004b": [Keycode.SHIFT, Keycode.K], + "\u007b": [Keycode.SHIFT, Keycode.LEFT_BRACKET], + "\u0067": [Keycode.G], + "\u0047": [Keycode.SHIFT, Keycode.G], + "\u0021": [Keycode.SHIFT, Keycode.ONE], + "\u0073": [Keycode.S], + "\u0053": [Keycode.SHIFT, Keycode.S], + "\u005e": [Keycode.SHIFT, Keycode.SIX], + "\u21e4": [Keycode.HOME], + "\u0076": [Keycode.V], + "\u0056": [Keycode.SHIFT, Keycode.V], + "\u005d": [Keycode.RIGHT_BRACKET], + "\u21e5": [Keycode.END], + "\u006e": [Keycode.N], + "\u004e": [Keycode.SHIFT, Keycode.N], + "\u005b": [Keycode.LEFT_BRACKET], + "\u29c9": [Keycode.LEFT_CONTROL, Keycode.C], + "\u0074": [Keycode.T], + "\u0054": [Keycode.SHIFT, Keycode.T], + "\u0026": [Keycode.SHIFT, Keycode.SEVEN], + "\u2318": [Keycode.APPLICATION], +} + def get_character_set(): return sorted(list(set(sum([list(a) for a in sum(sum(sum(sum(sum(characters, []), []), []), []), []) if a is not None], [])))) diff --git a/code.py b/code.py index 5f828ca..47605bc 100644 --- a/code.py +++ b/code.py @@ -10,9 +10,8 @@ from adafruit_display_text import label from adafruit_bitmap_font import bitmap_font import gc9a01 import usb_hid -from characters import characters +from characters import characters, keycodes from adafruit_hid.keyboard import Keyboard -from adafruit_hid.keycode import Keycode kbd = Keyboard(usb_hid.devices) @@ -28,29 +27,29 @@ mode_labels = [ language_labels = ["English", "Francais"] backlight_labels = ["Backlight", "Retroeclairage"] -tft_clk = board.GP2 -tft_mosi= board.GP3 -tft_rst = board.GP4 -tft_dc = board.GP1 -tft_cs = board.GP0 +tft_clk = board.GP14 +tft_mosi= board.GP15 +tft_rst = board.GP27 +tft_dc = board.GP28 +tft_cs = board.GP29 -tft_bl = DigitalInOut(board.GP5) +tft_bl = DigitalInOut(board.GP26) tft_bl.direction = Direction.OUTPUT tft_bl.value = True -key0 = DigitalInOut(board.GP6) +key0 = DigitalInOut(board.GP5) key0.pull = Pull.UP -key1 = DigitalInOut(board.GP7) +key1 = DigitalInOut(board.GP6) key1.pull = Pull.UP -key2 = DigitalInOut(board.GP8) +key2 = DigitalInOut(board.GP4) key2.pull = Pull.UP -key3 = DigitalInOut(board.GP9) +key3 = DigitalInOut(board.GP3) key3.pull = Pull.UP -key4 = DigitalInOut(board.GP10) +key4 = DigitalInOut(board.GP1) key4.pull = Pull.UP -key5 = DigitalInOut(board.GP11) +key5 = DigitalInOut(board.GP2) key5.pull = Pull.UP -key6 = DigitalInOut(board.GP12) +key6 = DigitalInOut(board.GP0) key6.pull = Pull.UP spi = busio.SPI(clock=tft_clk, MOSI=tft_mosi) @@ -222,7 +221,7 @@ def swap_to_character_display(): bottom_text.text = "" def edit_character(pin, character, key_text, key_palette, colour): - if pin is 0: + if not pin: key_palette[0] = (0, 0, 0) if character is None: key_text.text = "" @@ -258,75 +257,52 @@ def pressed(key): return not key.value def poll(): - return [pressed(key0), pressed(key1), pressed(key2), pressed(key3), pressed(key4), pressed(key5), pressed(key6)] + return [pressed(key0), pressed(key1), pressed(key2), pressed(key3), pressed(key4)], [pressed(key5), pressed(key6)] -def main(): - for selection in range(4): - mode_display(selection) - character_display(0, 0, 0, 0, 0, selection) - time.sleep(1) - character_display(0, 0, 0, 0, 1, selection) - time.sleep(1) - character_display(0, 0, 0, 1, 0, selection) - time.sleep(1) - character_display(0, 0, 1, 0, 0, selection) - time.sleep(1) - character_display(0, 1, 0, 0, 0, selection) - time.sleep(1) - character_display(1, 0, 0, 0, 0, selection) - time.sleep(1) - character_display(0, 0, 0, 1, 1, selection) - time.sleep(1) - character_display(0, 0, 1, 0, 1, selection) - time.sleep(1) - character_display(0, 1, 0, 0, 1, selection) - time.sleep(1) - character_display(1, 0, 0, 0, 1, selection) - time.sleep(1) - character_display(0, 0, 1, 1, 0, selection) - time.sleep(1) - character_display(0, 1, 0, 1, 0, selection) - time.sleep(1) - character_display(1, 0, 0, 1, 0, selection) - time.sleep(1) - character_display(0, 1, 1, 0, 0, selection) - time.sleep(1) - character_display(1, 0, 1, 0, 0, selection) - time.sleep(1) - character_display(1, 1, 0, 0, 0, selection) - time.sleep(1) - character_display(0, 0, 1, 1, 1, selection) - time.sleep(1) - character_display(0, 1, 0, 1, 1, selection) - time.sleep(1) - character_display(1, 0, 0, 1, 1, selection) - time.sleep(1) - character_display(0, 1, 1, 1, 0, selection) - time.sleep(1) - character_display(1, 0, 1, 1, 0, selection) - time.sleep(1) - character_display(1, 1, 1, 0, 0, selection) - time.sleep(1) - character_display(0, 1, 1, 1, 1, selection) - time.sleep(1) - character_display(1, 1, 1, 0, 1, selection) - time.sleep(1) - character_display(1, 1, 0, 1, 1, selection) - time.sleep(1) - character_display(1, 0, 1, 1, 1, selection) - time.sleep(1) - character_display(0, 1, 1, 1, 1, selection) - time.sleep(1) - character_display(1, 1, 1, 1, 1, selection) - time.sleep(1) - tft_bl.value = False - while True: - pass -# keypresses = poll() -# if any(keypresses): -# kbd.send(Keycode.A) -# time.sleep(1) +def toggle(_p, p, t): + updates = update(_p, p) + toggles = [not _t if u and __p else _t for _t, u, __p in zip(t, updates, _p)] + return any(updates), toggles + +def update(_p, p): + return [___p != __p for ___p, __p in zip(_p, p)] + +def _get_time(p, t): + return [_t+1 if _p else 0 for _p, _t in zip(p, t)] + +def get_time(p, t, m, tm): + return _get_time(p, t), _get_time(m, tm) + +def get_character(p, s): + return characters[p[0]][p[1]][p[2]][p[3]][p[4]][s] +def main(): + p = [False]*5 + #t = [0]*5 + m = [False]*2 + #tm = [0]*2 + toggles = [False]*2 + selection = 0 + while True: + _p, _m = poll() + #t, tm = get_time(_p, t, _m, tm) + u = any(update(_p, p)) + um, toggles = toggle(_m, m, toggles) + if um: + selection = toggles[0] + 2 * toggles[1] + mode_display(selection) + if u or um: + character_display(*_p, selection) + + if u and not any(_p): + c = get_character(p, selection) + if c is not None: + kbd.send(*keycodes[c]) + + p = _p + m = _m + time.sleep(0.05) + main()