From 97091ff4fd23f0ee21c9e398b2b9060787d6f4ce Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Sat, 22 Sep 2018 22:36:28 -0700 Subject: [PATCH] Make user keymaps fully declarative This removes the need for the user to define... most things, honestly. Notably, `main()` is no longer the end user's responsibility. This also allows us to do fun stuff going forward like validating keymaps for sanity (ex: the key assigned to `KC_MO(x)` should be assigned to `KC_TRNS` on the target layer or the user will never be able to escape that layer). This also disambiguates `BOARD` to always refer to an actual slab of silicon, renaming to `USER_KEYMAP`. Entrypoints are now a bit more wild, and mostly-unsupported boards no longer have working entrypoints. It's probably just time to scrap those boards for now (until we have BLE HID and/or bitbang USB HID, at least). --- BOARD_SETUP.md | 13 ------ Makefile | 44 +++++++++--------- boards/klardotsh/threethree_matrix_pyboard.py | 46 ------------------- entrypoints/feather_nrf52832.py | 12 ----- entrypoints/pyboard.py | 10 ---- entrypoints/teensy31.py | 10 ---- kmk/entrypoints/__init__.py | 0 kmk/entrypoints/global.py | 4 ++ kmk/entrypoints/handwire/Pipfile | 11 +++++ kmk/entrypoints/handwire/__init__.py | 0 kmk/entrypoints/handwire/pyboard.py | 24 ++++++++++ .../entrypoints/handwire}/pyboard_boot.py | 0 setup.cfg | 2 +- .../kdb424/handwire_planck_pyboard.py | 0 .../klardotsh/threethree_matrix_pyboard.py | 29 ++++++++++++ .../klardotsh/twotwo_matrix_feather.py | 0 {boards => user_keymaps}/noop.py | 0 17 files changed, 91 insertions(+), 114 deletions(-) delete mode 100644 BOARD_SETUP.md delete mode 100644 boards/klardotsh/threethree_matrix_pyboard.py delete mode 100644 entrypoints/feather_nrf52832.py delete mode 100644 entrypoints/pyboard.py delete mode 100644 entrypoints/teensy31.py create mode 100644 kmk/entrypoints/__init__.py create mode 100644 kmk/entrypoints/global.py create mode 100644 kmk/entrypoints/handwire/Pipfile create mode 100644 kmk/entrypoints/handwire/__init__.py create mode 100644 kmk/entrypoints/handwire/pyboard.py rename {entrypoints => kmk/entrypoints/handwire}/pyboard_boot.py (100%) rename {boards => user_keymaps}/kdb424/handwire_planck_pyboard.py (100%) create mode 100644 user_keymaps/klardotsh/threethree_matrix_pyboard.py rename {boards => user_keymaps}/klardotsh/twotwo_matrix_feather.py (100%) rename {boards => user_keymaps}/noop.py (100%) diff --git a/BOARD_SETUP.md b/BOARD_SETUP.md deleted file mode 100644 index b4df227..0000000 --- a/BOARD_SETUP.md +++ /dev/null @@ -1,13 +0,0 @@ -- Arch dependencies: adafruit-ampy python-dotenv wget -- Optional Arch dependencies: picocom (serial interface) -- `pip3 install --user adafruit nrfutil` -- `mkdir feather_flashing && cd feather_flashing` -- `git clone https://github.com/adafruit/Adafruit_nRF52_Bootloader.git && cd Adafruit_nRF52_Bootloader` -- `make BOARD=feather_nrf52832 VERSION=2.0.1 SERIAL=/dev/ttyUSB0 dfu-flash` (no - root req) -- `cd .. && git clone git@github.com:adafruit/circuitpython.git && cd circuitpython` -- `git submodule update --init` -- `make -C mpy-cross` -- `cd ports/nrf` -- `./drivers/bluetooth/download_ble_stack.sh` -- `make BOARD=feather_nrf52832 SERIAL=/dev/ttyUSB0 dfu-gen dfu-flash` diff --git a/Makefile b/Makefile index 11bbce9..1ac3daf 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ lint: devdeps @pipenv run flake8 fix-isort: devdeps - @find kmk/ boards/ entrypoints/ -name "*.py" | xargs pipenv run isort + @find kmk/ user_keymaps/ -name "*.py" | xargs pipenv run isort .submodules: .gitmodules @echo "===> Pulling dependencies, this may take several minutes" @@ -86,28 +86,28 @@ circuitpy-build-nrf: circuitpy-flash-nrf: circuitpy-build-nrf @echo "===> Flashing CircuitPython with KMK and your keymap" - @make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=${AMPY_PORT} SD=s132 FROZEN_MPY_DIR=freeze dfu-gen dfu-flash + @sudo make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=${AMPY_PORT} SD=s132 FROZEN_MPY_DIR=freeze dfu-gen dfu-flash micropython-build-teensy3.1: - @cp entrypoints/teensy31.py vendor/micropython/ports/teensy/memzip_files/main.py + #@cp entrypoints/teensy31.py vendor/micropython/ports/teensy/memzip_files/main.py @make -C vendor/micropython/ports/teensy/ BOARD=TEENSY_3.1 all micropython-flash-teensy3.1: micropython-build-teensy3.1 - @make -C vendor/micropython/ports/teensy/ BOARD=TEENSY_3.1 deploy + @sudo make -C vendor/micropython/ports/teensy/ BOARD=TEENSY_3.1 deploy micropython-build-pyboard: @make -j4 -C vendor/micropython/ports/stm32/ BOARD=PYBV11 FROZEN_MPY_DIR=freeze all micropython-flash-pyboard: micropython-build-pyboard - @make -j4 -C vendor/micropython/ports/stm32/ BOARD=PYBV11 FROZEN_MPY_DIR=freeze deploy + @sudo make -j4 -C vendor/micropython/ports/stm32/ BOARD=PYBV11 FROZEN_MPY_DIR=freeze deploy micropython-flash-pyboard-entrypoint: @echo "===> Flashing entrypoints if they doesn't already exist" @sleep 4 @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} rm /flash/main.py 2>/dev/null @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} rm /flash/boot.py 2>/dev/null - @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} put entrypoints/pyboard.py /flash/main.py - @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} put entrypoints/pyboard_boot.py /flash/boot.py + @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} put kmk/entrypoints/global.py /flash/main.py + @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} put kmk/entrypoints/handwire/pyboard_boot.py /flash/boot.py @echo "===> Flashed keyboard successfully!" circuitpy-flash-nrf-entrypoint: @@ -117,57 +117,57 @@ circuitpy-flash-nrf-entrypoint: @-timeout -k 5s 10s pipenv run ampy -p ${AMPY_PORT} -d ${AMPY_DELAY} -b ${AMPY_BAUD} put entrypoints/feather_nrf52832.py main.py @echo "===> Flashed keyboard successfully!" -ifndef BOARD +ifndef USER_KEYMAP build-feather-nrf52832: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" flash-feather-nrf52832: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" else build-feather-nrf52832: lint devdeps circuitpy-deps circuitpy-freeze-kmk-nrf @echo "===> Preparing keyboard script for bundling into CircuitPython" - @cp -av ${BOARD} vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py @$(MAKE) circuitpy-build-nrf flash-feather-nrf52832: lint devdeps circuitpy-deps circuitpy-freeze-kmk-nrf @echo "===> Preparing keyboard script for bundling into CircuitPython" - @cp -av ${BOARD} vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py @$(MAKE) circuitpy-flash-nrf circuitpy-flash-nrf-entrypoint endif -ifndef BOARD +ifndef USER_KEYMAP build-teensy-3.1: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" flash-teensy-3.1: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" else build-teensy-3.1: lint devdeps micropython-deps micropython-freeze-kmk-teensy3.1 @echo "===> Preparing keyboard script for bundling into MicroPython" - @cp -av ${BOARD} vendor/micropython/ports/teensy/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/micropython/ports/teensy/freeze/kmk_keyboard_user.py @$(MAKE) ARDUINO=${ARDUINO} micropython-build-teensy3.1 flash-teensy-3.1: lint devdeps micropython-deps micropython-freeze-kmk-teensy3.1 @echo "===> Preparing keyboard script for bundling into MicroPython" - @cp -av ${BOARD} vendor/micropython/ports/teensy/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/micropython/ports/teensy/freeze/kmk_keyboard_user.py @$(MAKE) ARDUINO=${ARDUINO} micropython-flash-teensy3.1 endif -ifndef BOARD +ifndef USER_KEYMAP build-pyboard: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" flash-pyboard: - @echo "===> Must provide a board (usually from boards/...) to build!" + @echo "===> Must provide a USER_KEYMAP (usually from user_keymaps/...) to build!" else build-pyboard: lint devdeps micropython-deps micropython-freeze-kmk-stm32 @echo "===> Preparing keyboard script for bundling into MicroPython" - @cp -av ${BOARD} vendor/micropython/ports/stm32/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/micropython/ports/stm32/freeze/kmk_keyboard_user.py @$(MAKE) AMPY_PORT=/dev/ttyACM0 AMPY_BAUD=115200 micropython-build-pyboard flash-pyboard: lint devdeps micropython-deps micropython-freeze-kmk-stm32 @echo "===> Preparing keyboard script for bundling into MicroPython" - @cp -av ${BOARD} vendor/micropython/ports/stm32/freeze/kmk_keyboard_user.py + @cp -av ${USER_KEYMAP} vendor/micropython/ports/stm32/freeze/kmk_keyboard_user.py @$(MAKE) AMPY_PORT=/dev/ttyACM0 AMPY_BAUD=115200 micropython-flash-pyboard micropython-flash-pyboard-entrypoint endif diff --git a/boards/klardotsh/threethree_matrix_pyboard.py b/boards/klardotsh/threethree_matrix_pyboard.py deleted file mode 100644 index a7af387..0000000 --- a/boards/klardotsh/threethree_matrix_pyboard.py +++ /dev/null @@ -1,46 +0,0 @@ -from logging import DEBUG - -import machine - -from kmk.common.consts import DiodeOrientation -from kmk.common.keycodes import KC -from kmk.firmware import Firmware -from kmk.micropython.pyb_hid import HIDHelper - - -def main(): - p = machine.Pin.board - - cols = (p.X10, p.X11, p.X12) - rows = (p.X1, p.X2, p.X3) - - diode_orientation = DiodeOrientation.COLUMNS - - keymap = [ - [ - [KC.MO(1), KC.H, KC.RESET], - [KC.MO(2), KC.I, KC.ENTER], - [KC.LCTRL, KC.SPACE, KC.LSHIFT], - ], - [ - [KC.TRNS, KC.B, KC.C], - [KC.NO, KC.D, KC.E], - [KC.F, KC.G, KC.H], - ], - [ - [KC.X, KC.Y, KC.Z], - [KC.TRNS, KC.N, KC.O], - [KC.R, KC.P, KC.Q], - ], - ] - - firmware = Firmware( - keymap=keymap, - row_pins=rows, - col_pins=cols, - diode_orientation=diode_orientation, - hid=HIDHelper, - log_level=DEBUG, - ) - - firmware.go() diff --git a/entrypoints/feather_nrf52832.py b/entrypoints/feather_nrf52832.py deleted file mode 100644 index a21b146..0000000 --- a/entrypoints/feather_nrf52832.py +++ /dev/null @@ -1,12 +0,0 @@ -import sys - -from kmk.circuitpython.util import feather_red_led_flash -from kmk_keyboard_user import main - -if __name__ == '__main__': - try: - main() - except Exception as e: - sys.print_exception(e) - feather_red_led_flash(duration=10, rate=0.5) - sys.exit(1) diff --git a/entrypoints/pyboard.py b/entrypoints/pyboard.py deleted file mode 100644 index 31df713..0000000 --- a/entrypoints/pyboard.py +++ /dev/null @@ -1,10 +0,0 @@ -import sys - -from kmk_keyboard_user import main - -if __name__ == '__main__': - try: - main() - except Exception as e: - sys.print_exception(e) - sys.exit(1) diff --git a/entrypoints/teensy31.py b/entrypoints/teensy31.py deleted file mode 100644 index 31df713..0000000 --- a/entrypoints/teensy31.py +++ /dev/null @@ -1,10 +0,0 @@ -import sys - -from kmk_keyboard_user import main - -if __name__ == '__main__': - try: - main() - except Exception as e: - sys.print_exception(e) - sys.exit(1) diff --git a/kmk/entrypoints/__init__.py b/kmk/entrypoints/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kmk/entrypoints/global.py b/kmk/entrypoints/global.py new file mode 100644 index 0000000..c07d83c --- /dev/null +++ b/kmk/entrypoints/global.py @@ -0,0 +1,4 @@ +from kmk_keyboard_user import main + +if __name__ == '__main__': + main() diff --git a/kmk/entrypoints/handwire/Pipfile b/kmk/entrypoints/handwire/Pipfile new file mode 100644 index 0000000..b9ba84f --- /dev/null +++ b/kmk/entrypoints/handwire/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/kmk/entrypoints/handwire/__init__.py b/kmk/entrypoints/handwire/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kmk/entrypoints/handwire/pyboard.py b/kmk/entrypoints/handwire/pyboard.py new file mode 100644 index 0000000..0a0f582 --- /dev/null +++ b/kmk/entrypoints/handwire/pyboard.py @@ -0,0 +1,24 @@ +import sys +from logging import DEBUG + +from kmk.firmware import Firmware +from kmk.micropython.pyb_hid import HIDHelper + + +def main(): + from kmk_keyboard_user import cols, diode_orientation, keymap, rows + + try: + firmware = Firmware( + keymap=keymap, + row_pins=rows, + col_pins=cols, + diode_orientation=diode_orientation, + hid=HIDHelper, + log_level=DEBUG, + ) + + firmware.go() + except Exception as e: + sys.print_exception(e) + sys.exit(1) diff --git a/entrypoints/pyboard_boot.py b/kmk/entrypoints/handwire/pyboard_boot.py similarity index 100% rename from entrypoints/pyboard_boot.py rename to kmk/entrypoints/handwire/pyboard_boot.py diff --git a/setup.cfg b/setup.cfg index 0c75357..9699245 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ exclude = .git,__pycache__,vendor,.venv max_line_length = 99 ignore = X100 per-file-ignores = - boards/**/*.py: E501 + user_keymaps/**/*.py: F401,E501 [isort] known_third_party = analogio,bitbangio,bleio,board,busio,digitalio,framebuf,gamepad,gc,microcontroller,micropython,pulseio,pyb,pydux,uio,ubluepy,machine,pyb diff --git a/boards/kdb424/handwire_planck_pyboard.py b/user_keymaps/kdb424/handwire_planck_pyboard.py similarity index 100% rename from boards/kdb424/handwire_planck_pyboard.py rename to user_keymaps/kdb424/handwire_planck_pyboard.py diff --git a/user_keymaps/klardotsh/threethree_matrix_pyboard.py b/user_keymaps/klardotsh/threethree_matrix_pyboard.py new file mode 100644 index 0000000..f95d0db --- /dev/null +++ b/user_keymaps/klardotsh/threethree_matrix_pyboard.py @@ -0,0 +1,29 @@ +import machine + +from kmk.common.consts import DiodeOrientation +from kmk.common.keycodes import KC +from kmk.entrypoints.handwire.pyboard import main + +p = machine.Pin.board +cols = (p.X10, p.X11, p.X12) +rows = (p.X1, p.X2, p.X3) + +diode_orientation = DiodeOrientation.COLUMNS + +keymap = [ + [ + [KC.MO(1), KC.H, KC.RESET], + [KC.MO(2), KC.I, KC.ENTER], + [KC.LCTRL, KC.SPACE, KC.LSHIFT], + ], + [ + [KC.TRNS, KC.B, KC.C], + [KC.NO, KC.D, KC.E], + [KC.F, KC.G, KC.H], + ], + [ + [KC.X, KC.Y, KC.Z], + [KC.TRNS, KC.N, KC.O], + [KC.R, KC.P, KC.Q], + ], +] diff --git a/boards/klardotsh/twotwo_matrix_feather.py b/user_keymaps/klardotsh/twotwo_matrix_feather.py similarity index 100% rename from boards/klardotsh/twotwo_matrix_feather.py rename to user_keymaps/klardotsh/twotwo_matrix_feather.py diff --git a/boards/noop.py b/user_keymaps/noop.py similarity index 100% rename from boards/noop.py rename to user_keymaps/noop.py