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": [
"",
"/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
print("setting up keyboard")
bitmap_keyboard = usb_hid.Device(
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'),

19
config.py Normal file → Executable file
View File

@@ -1,24 +1,23 @@
from adafruit_hid.keycode import Keycode
# pimodori tiny 2040 8MB
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)]
pinout = [
pinout: tuple[int,int] = [
(0,7), (0,11), (0,15), (0,0),
(0,8), (0,4), (0,14), (0,1),
(0,6), (0,10), (0,13), (0,2),
(0,9), (0,5), (0,12), (0,3)
]
keymap = [
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,
keymap: list[int] = [
121, SE.TWO, SE.THREE, SE.FOUR,
SE.A, SE.B, SE.C, SE.D,
SE.E, SE.F, SE.G, SE.H,
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.mcp23017 import MCP23017
import digitalio
import busio
import usb_hid
import pwmio
import time
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)
__DUTY_CYCLE_OFF = __INVERT_8_BIT_INTEGER_BITMASK
class RGBLED:
@@ -26,11 +32,39 @@ class RGBLED:
self.leds[i].duty_cycle = value
def off(self):
self.set((0, 0, 0))
self.set(__OFF)
def __to_inverse_8_bit_value(self, value: int) -> int:
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
__UNPRESSED = True
class Keyboard:
@@ -60,8 +94,7 @@ class Keyboard:
self.keymap = keymap
self.led = RGBLED(rgb_pins)
self.led.off()
self.led.set((100,100,100))
self.led.indicate_boot()
def initialize_hid(self) -> tuple[usb_hid.Device, usb_hid.Device]:
"""Initializes keyboard and media device if availabe"""
@@ -101,6 +134,7 @@ class Keyboard:
return pins
def start(self):
try:
self.pressed_keys = set()
self.pressed_keys_last_cycle = set()
self.pin_states_last_cycle = []
@@ -128,6 +162,12 @@ class Keyboard:
if self.pressed_keys != self.pressed_keys_last_cycle:
self.send_nkro_report()
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):
"""Sends the USB HID NKRO keyboard report."""

229
keycodes.py Normal file → Executable file
View File

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