restructured

This commit is contained in:
2022-01-14 11:26:42 +01:00
parent 38c95b587c
commit 1b67de0db9
24 changed files with 69 additions and 104 deletions

39
boot.py
View File

@@ -1,36 +1,5 @@
import busio
import digitalio
import usb_hid
from hardware import io_extenders_pinout, pinout
from adafruit_mcp230xx.mcp23017 import MCP23017
import storage
import usb_cdc
from nmlkpy.boot import enable_nkro_device, disable_dev_mode_unless_first_key_is_held
from keyboards.macropad_rev1.default.pinouts import io_extenders_pinout, pinout
io_extender_index, pin_number = pinout[0]
address, scl, sda = io_extenders_pinout[io_extender_index]
io_extender = MCP23017(busio.I2C(scl, sda), address)
pin = io_extender.get_pin(pin_number)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
if pin.value:
storage.disable_usb_drive()
usb_cdc.disable()
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'),
report_ids=(4,),
in_report_lengths=(16,),
out_report_lengths=(1,),
usage_page=0x1,
usage=0x6,
)
print(bitmap_keyboard)
devices = [
bitmap_keyboard,
usb_hid.Device.CONSUMER_CONTROL,
usb_hid.Device.MOUSE,
]
usb_hid.enable(devices)
print("enabled HID with custom keyboard device")
disable_dev_mode_unless_first_key_is_held(pinout, io_extenders_pinout)
enable_nkro_device()

10
code.py
View File

@@ -1,8 +1,8 @@
from keyboard import Keyboard
from keymap import keymap, layer_colors
from hardware import debug_repl, io_extenders_pinout, pinout, rgb_pins
from nmlkpy.keyboard import Keyboard
from keyboards.macropad_rev1.default.keymap import keymap, layer_colors
from keyboards.macropad_rev1.default.pinouts import debug_repl, io_extenders_pinout, pinout, rgb_pins
keyboard_state_manager = Keyboard(
kb = Keyboard(
io_extenders_pinout, pinout, keymap, rgb_pins, layer_colors, debug_repl)
keyboard_state_manager.start()
kb.start()

View File

@@ -1,49 +0,0 @@
# pimodori tiny 2040 8MB
import board
from keyboard import Hold, Toggle
from keycodes import SE
from keytypes import Keycode, Modifier
io_extenders_pinout = [(0x20, board.GP1, board.GP0)]
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 = [[
Hold(1), Toggle(2), Toggle(3), Toggle(4),
Toggle(0), Keycode(SE.B), Keycode(SE.C), Keycode(SE.D),
Keycode(SE.E), Keycode(SE.F), Keycode(SE.G), Keycode(SE.H),
Keycode(SE.I), Keycode(SE.J), Keycode(SE.K), Keycode(SE.L)
], [
Hold(1), Toggle(2), Toggle(3), Toggle(4),
Keycode(SE.ONE), Hold(1), Keycode(SE.C), Keycode(SE.D),
Keycode(SE.E), Keycode(SE.F), Keycode(SE.G), Keycode(SE.H),
Keycode(SE.BACKSPACE), Keycode(SE.J), Keycode(SE.K), Keycode(SE.L)
], [
Hold(1), Toggle(2), Toggle(3), Toggle(4),
Keycode(SE.A), Hold(1), Hold(2), Keycode(SE.D),
Keycode(SE.E), Keycode(SE.F), Keycode(SE.G), Keycode(SE.H),
Keycode(SE.I), Keycode(SE.J), Keycode(SE.K), Keycode(SE.L)
], [
Hold(1), Toggle(2), Toggle(3), Toggle(4),
Keycode(SE.A), Keycode(SE.B), Hold(2), Hold(3),
Keycode(SE.E), Keycode(SE.F), Keycode(SE.G), Keycode(SE.H),
Keycode(SE.I), Keycode(SE.J), Keycode(SE.K), Keycode(SE.L)
], [
Hold(1), Toggle(2), Toggle(3), Toggle(4),
Keycode(SE.A), Keycode(SE.B), Keycode(SE.C), Hold(3),
Keycode(SE.E), Keycode(SE.F), Keycode(SE.G), Keycode(SE.H),
Keycode(SE.I), Keycode(SE.J), Keycode(SE.K), Modifier(SE.LEFT_ALT)
]]
layer_colors = [(255, 255, 255), (0, 255, 0), (0, 0, 255),
(255, 0, 255), (255, 255, 0)]
rgb_pins: tuple[int, int, int] = (board.LED_R, board.LED_G, board.LED_B)
debug_repl = True

View File

@@ -1,6 +1,6 @@
from keycodes import SE
from keytypes import Hold, Keycode, Modifier, Toggle
from layer_manager import LayerManager
from nmlkpy.keycodes import SE
from nmlkpy.keytypes import Keycode, Modifier
from nmlkpy.layer_manager import LayerManager
lm = LayerManager(5)

View File

@@ -1,8 +1,9 @@
import board
io_extenders_pinout = [(0x20, board.GP1, board.GP0)]
io_extenders_pinout: list[tuple[int, int, int]] = [
(0x20, board.GP1, board.GP0)]
pinout: tuple[int, int] = [
pinout: list[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
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/mcp23s08.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

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

0
nmlkpy/__init__.py Executable file
View File

39
nmlkpy/boot.py Executable file
View File

@@ -0,0 +1,39 @@
import usb_hid
import busio
import digitalio
import storage
import usb_cdc
from adafruit_mcp230xx.mcp23017 import MCP23017
def enable_nkro_device():
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'),
report_ids=(4,),
in_report_lengths=(16,),
out_report_lengths=(1,),
usage_page=0x1,
usage=0x6,
)
print(bitmap_keyboard)
devices = [
bitmap_keyboard,
usb_hid.Device.CONSUMER_CONTROL,
usb_hid.Device.MOUSE,
]
usb_hid.enable(devices)
print("enabled HID with custom keyboard device")
def disable_dev_mode_unless_first_key_is_held(pinout: list[tuple[int, int]], io_extenders_pinout: list[tuple[int, int, int]]) -> None:
io_extender_index, pin_number = pinout[0]
address, scl, sda = io_extenders_pinout[io_extender_index]
io_extender = MCP23017(busio.I2C(scl, sda), address)
pin = io_extender.get_pin(pin_number)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
if pin.value:
storage.disable_usb_drive()
usb_cdc.disable()

View File

@@ -2,12 +2,13 @@ from adafruit_mcp230xx.digital_inout import DigitalInOut
from adafruit_mcp230xx.mcp23017 import MCP23017
import digitalio
import busio
from tests import test_keymap
from .tests import test_keymap
import usb_hid
import pwmio
import time
from micropython import const
from keytypes import KeyBase, LayerKeyBase, Pin, Toggle, Hold
from .keytypes import KeyBase
from .pin import Pin
__RED = (255, 0, 0)

15
keytypes.py → nmlkpy/keytypes.py Executable file → Normal file
View File

@@ -1,5 +1,5 @@
from micropython import const
from pin import Pin, PinEvent
from .pin import Pin, PinEvent
_VALID_KEYCODES = range(0, 255)
_VALID_MODIFIERS = [0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000]
@@ -125,12 +125,16 @@ class KeycodeBase(KeyBase):
if current_key_event == KeyEvent.SOFT_RELEASED:
if previous_key_event == KeyEvent.RELEASED:
print(1)
return self._set_event(KeyEvent.RELEASED)
if next_pin_event == PinEvent.RELEASED:
print(2)
return self._set_event(KeyEvent.RELEASED)
if previous_key_event == KeyEvent.PRESSED_ or next_pin_event == PinEvent.PRESSED:
print(3)
return self._set_event(KeyEvent.SOFT_RELEASED)
return self._set_event(KeyEvent.NO_EVENT)
print(4)
return self._set_event(KeyEvent.SOFT_RELEASED)
if current_key_event == KeyEvent.NO_EVENT:
if next_pin_event == PinEvent.NO_EVENT:
@@ -158,14 +162,17 @@ class KeycodeBase(KeyBase):
def _handle_event(self, keyboard) -> None:
current = self.events.current
if current == KeyEvent.NO_EVENT:
return
if current == KeyEvent.PRESSED_:
keyboard.add_keycode_to_report(self.keycode)
return
if current == KeyEvent.RELEASED:
keyboard.remove_keycode_from_report(self.keycode)
return
if current == KeyEvent.SOFT_RELEASED:
keyboard.remove_keycode_from_report(self.keycode)
return
raise NotImplementedError()
def handle(self, keyboard, pin):
if not self._consume_next_event(pin):

2
layer_manager.py → nmlkpy/layer_manager.py Normal file → Executable file
View File

@@ -1,4 +1,4 @@
from keytypes import Hold, Toggle
from .keytypes import Hold, Toggle
class LayerManager:

0
pin.py → nmlkpy/pin.py Normal file → Executable file
View File

View File

@@ -1,6 +1,3 @@
from keytypes import Keycode, Modifier, Toggle
def test_keymap(keymap: list[list], output: bool = False) -> None:
if output:
print("Testing keymap for errors")