keycodes, animation and error handling

This commit is contained in:
2022-01-04 22:20:47 +01:00
parent 281a4cad8c
commit 7e94798685
25 changed files with 225 additions and 152 deletions

1
.gitignore vendored Executable file
View File

@@ -0,0 +1 @@
boot_out.txt

View File

@@ -7,6 +7,7 @@
"python.analysis.extraPaths": [ "python.analysis.extraPaths": [
"", "",
"/home/wholteza/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.15/stubs", "/home/wholteza/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.15/stubs",
"/home/wholteza/.config/Code/User/globalStorage/joedevivo.vscode-circuitpython/bundle/20211231/adafruit-circuitpython-bundle-py-20211231/lib" "/home/wholteza/.config/Code/User/globalStorage/joedevivo.vscode-circuitpython/bundle/20220104/adafruit-circuitpython-bundle-py-20220104/lib"
] ],
"circuitpython.board.version": "7.2.0-alpha.1"
} }

1
boot.py Normal file → Executable file
View File

@@ -1,6 +1,5 @@
import usb_hid import usb_hid
print("setting up keyboard")
bitmap_keyboard = usb_hid.Device( bitmap_keyboard = usb_hid.Device(
report_descriptor = ( report_descriptor = (
b'\x05\x01\t\x06\xa1\x01\x85\x04u\x01\x95\x08\x05\x07\x19\xe0)\xe7\x15\x00%\x01\x81\x02\x95\x05u\x01\x05\x08\x19\x01)\x05\x91\x02\x95\x01u\x03\x91\x03\x95xu\x01\x15\x00%\x01\x05\x07\x19\x00)w\x81\x02\xc0'), b'\x05\x01\t\x06\xa1\x01\x85\x04u\x01\x95\x08\x05\x07\x19\xe0)\xe7\x15\x00%\x01\x81\x02\x95\x05u\x01\x05\x08\x19\x01)\x05\x91\x02\x95\x01u\x03\x91\x03\x95xu\x01\x15\x00%\x01\x05\x07\x19\x00)w\x81\x02\xc0'),

19
config.py Normal file → Executable file
View File

@@ -1,24 +1,23 @@
from adafruit_hid.keycode import Keycode
# pimodori tiny 2040 8MB # pimodori tiny 2040 8MB
import board import board
from keycodes import SE_1, SE_2, SE_3, SE_4, SE_A, SE_B, SE_C, SE_D, SE_E, SE_F, SE_G, SE_H, SE_I, SE_J, SE_K, SE_L from keycodes import SE
io_extenders_pinout = [(0x20, board.GP1, board.GP0)] io_extenders_pinout = [(0x20, board.GP1, board.GP0)]
pinout = [ pinout: tuple[int,int] = [
(0,7), (0,11), (0,15), (0,0), (0,7), (0,11), (0,15), (0,0),
(0,8), (0,4), (0,14), (0,1), (0,8), (0,4), (0,14), (0,1),
(0,6), (0,10), (0,13), (0,2), (0,6), (0,10), (0,13), (0,2),
(0,9), (0,5), (0,12), (0,3) (0,9), (0,5), (0,12), (0,3)
] ]
keymap = [ keymap: list[int] = [
SE_1, SE_2, SE_3, SE_4, 121, SE.TWO, SE.THREE, SE.FOUR,
SE_A, SE_B, SE_C, SE_D, SE.A, SE.B, SE.C, SE.D,
SE_E, SE_F, SE_G, SE_H, SE.E, SE.F, SE.G, SE.H,
SE_I, SE_J, SE_K, SE_L, SE.I, SE.J, SE.K, SE.L
] ]
rgb_pins = (board.LED_R, board.LED_G, board.LED_B)
rgb_pins: tuple[int,int,int] = (board.LED_R, board.LED_G, board.LED_B)

48
keyboard.py Normal file → Executable file
View File

@@ -1,12 +1,18 @@
from adafruit_hid.keycode import Keycode
from adafruit_mcp230xx.digital_inout import DigitalInOut from adafruit_mcp230xx.digital_inout import DigitalInOut
from adafruit_mcp230xx.mcp23017 import MCP23017 from adafruit_mcp230xx.mcp23017 import MCP23017
import digitalio import digitalio
import busio import busio
import usb_hid import usb_hid
import pwmio import pwmio
import time
from micropython import const from micropython import const
__RED = (255,0,0)
__BLUE = (0,0,255)
__GREEN = (0,255,0)
__WHITE = (255,255,255)
__OFF = (0,0,0)
__BLINK = (0.05,0,0)
__INVERT_8_BIT_INTEGER_BITMASK = const(0xffff) __INVERT_8_BIT_INTEGER_BITMASK = const(0xffff)
__DUTY_CYCLE_OFF = __INVERT_8_BIT_INTEGER_BITMASK __DUTY_CYCLE_OFF = __INVERT_8_BIT_INTEGER_BITMASK
class RGBLED: class RGBLED:
@@ -26,11 +32,39 @@ class RGBLED:
self.leds[i].duty_cycle = value self.leds[i].duty_cycle = value
def off(self): def off(self):
self.set((0, 0, 0)) self.set(__OFF)
def __to_inverse_8_bit_value(self, value: int) -> int: def __to_inverse_8_bit_value(self, value: int) -> int:
return ~(value * 257) & __INVERT_8_BIT_INTEGER_BITMASK return ~(value * 257) & __INVERT_8_BIT_INTEGER_BITMASK
def indicate_exception(self) -> None:
self.animate(
__RED,
__BLINK,
__OFF,
__BLINK,
__RED,
__BLINK,
__OFF,
__BLINK,
__RED,
__BLINK,
__OFF)
def indicate_boot(self) -> None:
self.animate(__WHITE, __BLINK, __OFF)
def animate(self,*color_sleep_cycles: tuple[int, int, int]) -> None:
"""Takes arguments of tuple with three int values.
Argument tuples are in the structure of 'color to display' and 'time to wait' after each other
example: animate((255,255,255),(0.05,0,0),(0,0,0)) - will blink the led white for 0.05 seconds
"""
for i in range(len(color_sleep_cycles)):
if not i % 2:
self.set(color_sleep_cycles[i])
else:
time.sleep(color_sleep_cycles[i][0])
__PRESSED = False __PRESSED = False
__UNPRESSED = True __UNPRESSED = True
class Keyboard: class Keyboard:
@@ -60,8 +94,7 @@ class Keyboard:
self.keymap = keymap self.keymap = keymap
self.led = RGBLED(rgb_pins) self.led = RGBLED(rgb_pins)
self.led.off() self.led.indicate_boot()
self.led.set((100,100,100))
def initialize_hid(self) -> tuple[usb_hid.Device, usb_hid.Device]: def initialize_hid(self) -> tuple[usb_hid.Device, usb_hid.Device]:
"""Initializes keyboard and media device if availabe""" """Initializes keyboard and media device if availabe"""
@@ -101,6 +134,7 @@ class Keyboard:
return pins return pins
def start(self): def start(self):
try:
self.pressed_keys = set() self.pressed_keys = set()
self.pressed_keys_last_cycle = set() self.pressed_keys_last_cycle = set()
self.pin_states_last_cycle = [] self.pin_states_last_cycle = []
@@ -128,6 +162,12 @@ class Keyboard:
if self.pressed_keys != self.pressed_keys_last_cycle: if self.pressed_keys != self.pressed_keys_last_cycle:
self.send_nkro_report() self.send_nkro_report()
self.pressed_keys_last_cycle = set(self.pressed_keys) self.pressed_keys_last_cycle = set(self.pressed_keys)
except Exception as e:
print(f"Exception thrown: {e}, restarting..")
self.led.indicate_exception()
time.sleep(1)
self.led.indicate_boot()
self.start()
def send_nkro_report(self): def send_nkro_report(self):
"""Sends the USB HID NKRO keyboard report.""" """Sends the USB HID NKRO keyboard report."""

229
keycodes.py Normal file → Executable file
View File

@@ -1,102 +1,135 @@
from micropython import const from micropython import const
class SE:
SE_A = const(4) # Letters
SE_B = const(5) A = const(4)
SE_C = const(6) B = const(5)
SE_D = const(7) C = const(6)
SE_E = const(8) D = const(7)
SE_F = const(9) E = const(8)
SE_G = const(10) F = const(9)
SE_H = const(11) G = const(10)
SE_I = const(12) H = const(11)
SE_J = const(13) I = const(12)
SE_K = const(14) J = const(13)
SE_L = const(15) K = const(14)
SE_M = const(16) L = const(15)
SE_N = const(17) M = const(16)
SE_O = const(18) N = const(17)
SE_P = const(19) O = const(18)
SE_Q = const(20) P = const(19)
SE_R = const(21) Q = const(20)
SE_S = const(22) R = const(21)
SE_T = const(23) S = const(22)
SE_U = const(24) T = const(23)
SE_V = const(25) U = const(24)
SE_W = const(26) V = const(25)
SE_X = const(27) W = const(26)
SE_Y = const(28) X = const(27)
SE_Z = const(29) Y = const(28)
SE_Å = const(47) Z = const(29)
SE_Ä = const(52) Å = const(47)
SE_Ö = const(51) Ä = const(52)
Ö = const(51)
SE_1 = const(30) # Numbers
SE_2 = const(31) ZERO = const(39)
SE_3 = const(32) ONE = const(30)
SE_4 = const(33) TWO = const(31)
SE_5 = const(34) THREE = const(32)
SE_6 = const(35) FOUR = const(33)
SE_7 = const(36) FIVE = const(34)
SE_8 = const(37) SIX = const(35)
SE_9 = const(38) SEVEN = const(36)
SE_0 = const(39) EIGHT = const(37)
NINE = const(38)
#checked # Signs
HYPHEN = const(56) # - and _
DOT = const(55) # . and :
COMMA = const(54) # , and ;
SE_HYPHEN = const(56) # - PARAGRAPH = const(53) # § and ½
SE_DOT = const(55) # . QUOTE = const(49) # ' and *
SE_COMMA = const(54) # , UMLAUT = const(48) # ¨ and ~
SE_PARAGRAPH = const(53) # §/½ TICK = const(46) # ´ and `
SE_QUOTE = const(49) # '/* PLUS = const(45) # + and ?
SE_arst = const(48) # ¨ EQUAL = const(103) # =
SE_TICK = const(46) # ´/` ANGLE_BRACKET = const(100) # < and >
SE_PLUS = const(45) # +/? # Function keys
F1 = const(58)
SE_F1 = const(58) F2 = const(59)
SE_F2 = const(59) F3 = const(60)
SE_F3 = const(60) F4 = const(61)
SE_F4 = const(61) F5 = const(62)
SE_F5 = const(62) F6 = const(63)
SE_F6 = const(63) F7 = const(64)
SE_F7 = const(64) F8 = const(65)
SE_F8 = const(65) F9 = const(66)
SE_F9 = const(66) F10 = const(67)
SE_F10 = const(67) F11 = const(68)
SE_F11 = const(68) F12 = const(69)
SE_F12 = const(69) PRINTSCREEN = const(70)
CAPSLOCK = const(57)
SE_TAB = const(43) # Tab ESCAPE = const(41)
SE_SPACE = const(44) # Space SCROLLLOCK = const(71)
SE_ENTER = const(40) # Enter PAUSEBREAK = const(72)
INSERT = const(73)
SE_PRINTSCREEN = const(70) # Print screen MENU = const(101)
SE_CAPSLOCK = const(57) # Caps Lock # Function keys tested in gnome
SE_ESCAPE = const(41) # Esc TOUCHPAD_TOGGLE = const(112)
SE_BACKSPACE = const(42) # Backspace TOUCHPAD_ON = const(113)
SE_SCROLLLOCK = const(71) # Scroll lock TOUCHPAD_OFF = const(114)
SE_PAUSEBREAK = const(72) # Pause/Break MIC_MUTE = const(111)
SE_INSERT = const(73) # Insert POWER = const(102)
SE_HOME = const(74) # Home SETTINGS = const(104)
SE_PAGEUP = const(75) # Page up HELP = const(117)
SE_DELETE = const(76) # Delete # Spacing
SE_END = const(77) # End TAB = const(43)
SE_PAGEDOWN = const(78) # Page down SPACE = const(44)
ENTER = const(40)
SE_RIGHT = const(79) # Arrow right # Navigation
SE_LEFT = const(80) # Arrow left BACKSPACE = const(42)
SE_UP = const(81) # Arrow down DELETE = const(76)
SE_DOWN = const(82) # Arrow up HOME = const(74)
END = const(77)
SE_LEFT_CTRL = const(0x0100) PAGEUP = const(75)
SE_LEFT_SHIFT = const(0x0200) PAGEDOWN = const(78)
SE_LEFT_ALT = const(0x0400) RIGHT = const(79)
SE_LEFT_SUPER = const(0x0800) LEFT = const(80)
SE_RIGHT_CTRL = const(0x1000) UP = const(81)
SE_RIGHT_SHIFT = const(0x2000) DOWN = const(82)
SE_RIGHT_ALT = const(0x4000) # Modifiers
SE_RIGHT_SUPER = const(0x800) LEFT_CTRL = const(0x0100)
LEFT_SHIFT = const(0x0200)
LEFT_ALT = const(0x0400)
LEFT_SUPER = const(0x0800)
RIGHT_CTRL = const(0x1000)
RIGHT_SHIFT = const(0x2000)
RIGHT_ALT = const(0x4000)
# Others
XF86_LAUNCH_5 = const(105)
XF86_LAUNCH_6 = const(106)
XF86_LAUNCH_7 = const(107)
XF86_LAUNCH_8 = const(108)
XF86_LAUNCH_9 = const(109)
XF86_OPEN = const(116)
SUN_FRONT = const(119)
SUN_PROPS = const(118)
# Numpad
NUM_DOT = const(99)
NUM_0 = const(98)
NUM_1 = const(89)
NUM_2 = const(90)
NUM_3 = const(91)
NUM_4 = const(92)
NUM_5 = const(93)
NUM_6 = const(94)
NUM_7 = const(95)
NUM_8 = const(96)
NUM_9 = const(97)
NUM_ENTER = const(88)
NUM_PLUS = const(87) # +
NUM_MINUS = const(86) # -
NUM_ASTERISK = const(85) # *
NUM_SLASH = const(84) # /
NUMLOCK = const(83)
# Need fix # Need fix
#SE_MUTE = const(-226) # Mute #SE_MUTE = const(-226) # Mute

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

0
lib/adafruit_mcp230xx/__init__.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/digital_inout.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23008.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23016.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23017.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp230xx.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23s17.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23sxx.mpy Executable file → Normal file
View File

0
lib/adafruit_mcp230xx/mcp23xxx.mpy Executable file → Normal file
View File

Binary file not shown.

0
lib/simpleio.mpy Executable file → Normal file
View File