Skip to content

Commit

Permalink
Documentation and robustness!
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkembedded-joel committed May 17, 2024
1 parent e9fdc3d commit 28f7a58
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 25 deletions.
103 changes: 96 additions & 7 deletions macropad/code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,76 @@
from collections import OrderedDict
from adafruit_hid.keycode import Keycode


import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from keyboard_layout_win_uk import KeyboardLayout
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

from display_manager import DisplayManager
from keys import pages, rotary_press_function

dm = DisplayManager()
dm.show_splash_screen()

try:
import keys
except TypeError:
dm.show_err("Can't import pages\nMissing comma?")


def swap_lines(kbd, layout):
kbd.send(Keycode.HOME)
kbd.send(Keycode.SHIFT, Keycode.END)
kbd.send(Keycode.CONTROL, Keycode.X)
kbd.send(Keycode.UP_ARROW)
kbd.send(Keycode.HOME)
kbd.send(Keycode.CONTROL, Keycode.V)
kbd.send(Keycode.SHIFT, Keycode.END)
kbd.send(Keycode.CONTROL, Keycode.X)
kbd.send(Keycode.DOWN_ARROW)
kbd.send(Keycode.CONTROL, Keycode.V)

default_pages = (
(
"Function Keys", (
("F13", Keycode.F13),
("F14", Keycode.F14),
("F15", Keycode.F15),
("F16", Keycode.F16),
("F17", Keycode.F17),
("F18", Keycode.F18),
("F19", Keycode.F19),
("F20", Keycode.F20),
),
),
(
"Text Utils", (
("S All", (Keycode.CONTROL, Keycode.A)),
("SelLn", (Keycode.HOME, Keycode.SHIFT, Keycode.END)),
("SwpLn", swap_lines),
("Sign", "\n\nThanks,\nJoel Fergusson"),
("Undo", (Keycode.CONTROL, Keycode.Z)),
("Cut", (Keycode.CONTROL, Keycode.X)),
("Copy", (Keycode.CONTROL, Keycode.C)),
("Paste", (Keycode.CONTROL, Keycode.V)),
)
),
(
"Gaming", (
("Shoot", Keycode.ENTER),
(" ^", Keycode.W),
("Jump", Keycode.SPACE),
("Chng", Keycode.I),
(" <", Keycode.A),
(" v", Keycode.S),
(" >", Keycode.D),
("Rload", Keycode.R),
),
),

)

class Leds():
def __init__(self, show_binary=True):
self.show_binary = show_binary
Expand Down Expand Up @@ -104,6 +160,25 @@ def __init__(self, name, presses, keyboard, layout):
self.presses = presses
self.kbd = keyboard
self.layout = layout

# Verify presses is sensible
if not isinstance(presses, (int, str, list, tuple)) \
and presses is not None \
and not callable(presses):
# Get the display manager. This is a bit dirty, should
# probably use a singleton or something
global dm
dm.show_err("Failed to set up\nkey {}".format(self.name))
time.sleep(3)
self.presses = None

if isinstance(presses, (list, tuple)):
for press in presses:
if not isinstance(press, int):
global dm
dm.show_err("Failed to set up\nkey {} - bad element\nin list".format(self.name))
time.sleep(3)
self.presses = None

def press(self):
if isinstance(self.presses, str):
Expand All @@ -113,6 +188,15 @@ def press(self):
elif isinstance(self.presses, (list, tuple)):
# This assumes any list or tuple is all integers from Keycode
self.kbd.press(*self.presses)
elif callable(self.presses):
try:
self.presses(self.kbd, self.layout)
except Exception:
global dm
dm.show_err("Failed to run function\nfor key {}".format(self.name))
# No need for a sleep here, there's nothing that should
# overwrite the screen immediately.


def release(self):
if isinstance(self.presses, int):
Expand Down Expand Up @@ -183,11 +267,19 @@ def release(self, k):
dm.set_loading_bar(21)
kbd = Keyboard(usb_hid.devices)
dm.set_loading_bar(42)
layout = KeyboardLayoutUS(kbd)
layout = KeyboardLayout(kbd)
dm.set_loading_bar(63)
cctl = ConsumerControl(usb_hid.devices)
dm.set_loading_bar(84)


try:
pages = keys.pages
except Exception as e:
dm.show_err("User defined keys\nimport failed.\nLoading defaults...")
time.sleep(3)
pages = default_pages

pm = PageManager(pages, kbd, layout)
dm.set_loading_bar(105)

Expand Down Expand Up @@ -229,12 +321,9 @@ def release(self, k):
d = encoder.get_delta()
while d:
if d > 0:
print("Volume up")
cctl.send(ConsumerControlCode.VOLUME_INCREMENT)
d -= 1
if d < 0:

print("Volume down")
cctl.send(ConsumerControlCode.VOLUME_DECREMENT)
d += 1

Expand Down
10 changes: 10 additions & 0 deletions macropad/code/display_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ def show_page(self, page):
self.disp.text(key_names[7], 96, 20, 1)
self.disp.show()


def show_err(self, err):
l = err.split("\n")
self.disp.fill(0)
self.disp.text(l[0], 0, 0, 1)
if len(l) > 1:
self.disp.text(l[1], 0, 10, 1)
if len(l) > 2:
self.disp.text(l[2], 0, 20, 1)
self.disp.show()


157 changes: 157 additions & 0 deletions macropad/code/keyboard_layout_win_uk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# SPDX-FileCopyrightText: 2022 Neradoc NeraOnGit@ri1.fr
# SPDX-License-Identifier: MIT
"""
This file was automatically generated using Circuitpython_Keyboard_Layouts
"""
from adafruit_hid.keyboard_layout_base import KeyboardLayoutBase


__version__ = "0.0.1-alpha.0"
__repo__ = "https://github.com/Neradoc/Circuitpython_Keyboard_Layouts.git"


class KeyboardLayout(KeyboardLayoutBase):
ASCII_TO_KEYCODE = (
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x2a' # BACKSPACE
b'\x2b' # '\t'
b'\x28' # '\n'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x29' # ESC
b'\x00'
b'\x00'
b'\x00'
b'\x00'
b'\x2c' # ' '
b'\x9e' # '!'
b'\x9f' # '"'
b'\x31' # '#'
b'\xa1' # '$'
b'\xa2' # '%'
b'\xa4' # '&'
b'\x34' # "'"
b'\xa6' # '('
b'\xa7' # ')'
b'\xa5' # '*'
b'\xae' # '+'
b'\x36' # ','
b'\x2d' # '-'
b'\x37' # '.'
b'\x38' # '/'
b'\x27' # '0'
b'\x1e' # '1'
b'\x1f' # '2'
b'\x20' # '3'
b'\x21' # '4'
b'\x22' # '5'
b'\x23' # '6'
b'\x24' # '7'
b'\x25' # '8'
b'\x26' # '9'
b'\xb3' # ':'
b'\x33' # ';'
b'\xb6' # '<'
b'\x2e' # '='
b'\xb7' # '>'
b'\xb8' # '?'
b'\xb4' # '@'
b'\x84' # 'A'
b'\x85' # 'B'
b'\x86' # 'C'
b'\x87' # 'D'
b'\x88' # 'E'
b'\x89' # 'F'
b'\x8a' # 'G'
b'\x8b' # 'H'
b'\x8c' # 'I'
b'\x8d' # 'J'
b'\x8e' # 'K'
b'\x8f' # 'L'
b'\x90' # 'M'
b'\x91' # 'N'
b'\x92' # 'O'
b'\x93' # 'P'
b'\x94' # 'Q'
b'\x95' # 'R'
b'\x96' # 'S'
b'\x97' # 'T'
b'\x98' # 'U'
b'\x99' # 'V'
b'\x9a' # 'W'
b'\x9b' # 'X'
b'\x9c' # 'Y'
b'\x9d' # 'Z'
b'\x2f' # '['
b'\x31' # '\\'
b'\x30' # ']'
b'\xa3' # '^'
b'\xad' # '_'
b'\x35' # '`'
b'\x04' # 'a'
b'\x05' # 'b'
b'\x06' # 'c'
b'\x07' # 'd'
b'\x08' # 'e'
b'\x09' # 'f'
b'\x0a' # 'g'
b'\x0b' # 'h'
b'\x0c' # 'i'
b'\x0d' # 'j'
b'\x0e' # 'k'
b'\x0f' # 'l'
b'\x10' # 'm'
b'\x11' # 'n'
b'\x12' # 'o'
b'\x13' # 'p'
b'\x14' # 'q'
b'\x15' # 'r'
b'\x16' # 's'
b'\x17' # 't'
b'\x18' # 'u'
b'\x19' # 'v'
b'\x1a' # 'w'
b'\x1b' # 'x'
b'\x1c' # 'y'
b'\x1d' # 'z'
b'\xaf' # '{'
b'\xe4' # '|'
b'\xb0' # '}'
b'\xb1' # '~'
b'\x00'
)
NEED_ALTGR = '\\¦áéíóú€'
HIGHER_ASCII = {
0xa3: 0xa0, # '£'
0x20ac: 0x21, # '€'
0xe9: 0x08, # 'é'
0xfa: 0x18, # 'ú'
0xed: 0x0c, # 'í'
0xf3: 0x12, # 'ó'
0xe1: 0x04, # 'á'
0xac: 0xb5, # '¬'
0xa6: 0x35, # '¦'
}
COMBINED_KEYS = {
}
Loading

0 comments on commit 28f7a58

Please sign in to comment.