diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..dc31d0e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +boot_out.txt diff --git a/.vscode/settings.json b/.vscode/settings.json index 4956f76..ee34996 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" } \ No newline at end of file diff --git a/boot.py b/boot.py old mode 100644 new mode 100755 index 8da90ff..998283e --- a/boot.py +++ b/boot.py @@ -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'), diff --git a/config.py b/config.py old mode 100644 new mode 100755 index 4a184eb..53cb532 --- a/config.py +++ b/config.py @@ -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) \ No newline at end of file + +rgb_pins: tuple[int,int,int] = (board.LED_R, board.LED_G, board.LED_B) \ No newline at end of file diff --git a/keyboard.py b/keyboard.py old mode 100644 new mode 100755 index 929b14d..7f828f8 --- a/keyboard.py +++ b/keyboard.py @@ -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,33 +134,40 @@ class Keyboard: return pins def start(self): - self.pressed_keys = set() - self.pressed_keys_last_cycle = set() - self.pin_states_last_cycle = [] - for pin in self.pins: self.pin_states_last_cycle.append(__UNPRESSED) - while True: - for pin in self.pins: - pin_index = self.pins.index(pin) - keycode = self.keymap[pin_index] - previously = self.pin_states_last_cycle[pin_index] - value = pin.value - currentlyPressed = value is __PRESSED - previouslyPressed = previously is __PRESSED - if currentlyPressed: - if not previouslyPressed: - self.pressed_keys.add(keycode) - else: - if previouslyPressed: - try: - self.pressed_keys.remove(keycode) - # Catch silenly if same keycode is pressed twice then released - except KeyError: - pass - self.pin_states_last_cycle[pin_index] = value + try: + self.pressed_keys = set() + self.pressed_keys_last_cycle = set() + self.pin_states_last_cycle = [] + for pin in self.pins: self.pin_states_last_cycle.append(__UNPRESSED) + while True: + for pin in self.pins: + pin_index = self.pins.index(pin) + keycode = self.keymap[pin_index] + previously = self.pin_states_last_cycle[pin_index] + value = pin.value + currentlyPressed = value is __PRESSED + previouslyPressed = previously is __PRESSED + if currentlyPressed: + if not previouslyPressed: + self.pressed_keys.add(keycode) + else: + if previouslyPressed: + try: + self.pressed_keys.remove(keycode) + # Catch silenly if same keycode is pressed twice then released + except KeyError: + pass + self.pin_states_last_cycle[pin_index] = value - if self.pressed_keys != self.pressed_keys_last_cycle: - self.send_nkro_report() - self.pressed_keys_last_cycle = set(self.pressed_keys) + 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.""" diff --git a/keycodes.py b/keycodes.py old mode 100644 new mode 100755 index cb58622..1e4e7c8 --- a/keycodes.py +++ b/keycodes.py @@ -1,111 +1,144 @@ from micropython import const +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) -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) - -# Need fix -#SE_MUTE = const(-226) # Mute -#SE_VU = const(-233) # Volume up -#SE_VD = const(-234) # Volume down -# -#SE_RR = const(-179) # Rewind -#SE_FF = const(-180) # Fast forward -#SE_NT = const(-181) # Next track -#SE_PT = const(-182) # Prev track -#SE_ST = const(-183) # Stop track -#SE_PP = const(-205) # Play/pause \ No newline at end of file + # Need fix + #SE_MUTE = const(-226) # Mute + #SE_VU = const(-233) # Volume up + #SE_VD = const(-234) # Volume down + # + #SE_RR = const(-179) # Rewind + #SE_FF = const(-180) # Fast forward + #SE_NT = const(-181) # Next track + #SE_PT = const(-182) # Prev track + #SE_ST = const(-183) # Stop track + #SE_PP = const(-205) # Play/pause \ No newline at end of file diff --git a/lib/adafruit_hid/__init__.mpy b/lib/adafruit_hid/__init__.mpy deleted file mode 100755 index 8b98beb..0000000 Binary files a/lib/adafruit_hid/__init__.mpy and /dev/null differ diff --git a/lib/adafruit_hid/consumer_control.mpy b/lib/adafruit_hid/consumer_control.mpy deleted file mode 100755 index 02cb9dc..0000000 Binary files a/lib/adafruit_hid/consumer_control.mpy and /dev/null differ diff --git a/lib/adafruit_hid/consumer_control_code.mpy b/lib/adafruit_hid/consumer_control_code.mpy deleted file mode 100755 index 618edca..0000000 Binary files a/lib/adafruit_hid/consumer_control_code.mpy and /dev/null differ diff --git a/lib/adafruit_hid/keyboard.mpy b/lib/adafruit_hid/keyboard.mpy deleted file mode 100755 index 12b45b9..0000000 Binary files a/lib/adafruit_hid/keyboard.mpy and /dev/null differ diff --git a/lib/adafruit_hid/keyboard_layout_base.mpy b/lib/adafruit_hid/keyboard_layout_base.mpy deleted file mode 100755 index 2cf77eb..0000000 Binary files a/lib/adafruit_hid/keyboard_layout_base.mpy and /dev/null differ diff --git a/lib/adafruit_hid/keyboard_layout_us.mpy b/lib/adafruit_hid/keyboard_layout_us.mpy deleted file mode 100755 index 7b3398c..0000000 Binary files a/lib/adafruit_hid/keyboard_layout_us.mpy and /dev/null differ diff --git a/lib/adafruit_hid/keycode.mpy b/lib/adafruit_hid/keycode.mpy deleted file mode 100755 index 4a30986..0000000 Binary files a/lib/adafruit_hid/keycode.mpy and /dev/null differ diff --git a/lib/adafruit_hid/mouse.mpy b/lib/adafruit_hid/mouse.mpy deleted file mode 100755 index 9e09cc3..0000000 Binary files a/lib/adafruit_hid/mouse.mpy and /dev/null differ diff --git a/lib/adafruit_mcp230xx/__init__.mpy b/lib/adafruit_mcp230xx/__init__.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/digital_inout.mpy b/lib/adafruit_mcp230xx/digital_inout.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23008.mpy b/lib/adafruit_mcp230xx/mcp23008.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23016.mpy b/lib/adafruit_mcp230xx/mcp23016.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23017.mpy b/lib/adafruit_mcp230xx/mcp23017.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp230xx.mpy b/lib/adafruit_mcp230xx/mcp230xx.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23s17.mpy b/lib/adafruit_mcp230xx/mcp23s17.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23sxx.mpy b/lib/adafruit_mcp230xx/mcp23sxx.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_mcp230xx/mcp23xxx.mpy b/lib/adafruit_mcp230xx/mcp23xxx.mpy old mode 100755 new mode 100644 diff --git a/lib/adafruit_rgbled.mpy b/lib/adafruit_rgbled.mpy deleted file mode 100755 index 160447b..0000000 Binary files a/lib/adafruit_rgbled.mpy and /dev/null differ diff --git a/lib/simpleio.mpy b/lib/simpleio.mpy old mode 100755 new mode 100644