From 247b6be6b7525ff331f96284f36d83a9d01b33a7 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Tue, 22 Mar 2022 16:09:50 -0500 Subject: [PATCH] improvements --- README.md | 2 +- code.py | 42 ++++++++++------------- keytest.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 25 deletions(-) create mode 100755 keytest.py diff --git a/README.md b/README.md index a91c60b..06de4db 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## Button Buddy -This is the code I run on my 3x3 macro-pad. The main reason I built it is to provide mouse click buttons next to my [Ploopy Nano](https://ploopy.co/nano-trackball/). I use the [maddie](https://github.com/qmk/qmk_firmware/tree/e7931289918221081cbe2a7ea5df27a5d86324db/keyboards/ploopyco/trackball_nano/keymaps/maddie) keymap which includes code that turns the Ploopy into a scroll wheel when num-lock is toggled twice in 25ms so a couple of the buttons are mapped to that (one hold-to-scroll button and a toggle scroll-mode button). +This is the code I run on my 3x3 macro-pad. The main reason I built it is to provide mouse click buttons next to my [Ploopy Nano](https://ploopy.co/nano-trackball/). I use the [maddie](https://github.com/qmk/qmk_firmware/tree/e7931289918221081cbe2a7ea5df27a5d86324db/keyboards/ploopyco/trackball_nano/keymaps/maddie) keymap which includes code that turns the Ploopy into a scroll wheel when num-lock is toggled twice in 25ms which is why one of the buttons is programmed to do that. I 3D-printed the macropad using [this model](https://www.thingiverse.com/thing:4816077), used some left over Kailh midnight jade switches left over from other keyboard projects, hand-wired to a Raspberry Pi Pico. The switches are wired to GPIO pins 0 through 8 with 0-2 across the top row, 3-5 across the middle row, and 6-8 across the bottom. diff --git a/code.py b/code.py index c50e354..36a3e08 100755 --- a/code.py +++ b/code.py @@ -27,44 +27,38 @@ pins = [ board.GP8, ] -# toggles scroll mode in ploopy nano using maddie layout -def toggle_scroll(): - kbd.press(Keycode.KEYPAD_NUMLOCK) - kbd.release(Keycode.KEYPAD_NUMLOCK) - kbd.press(Keycode.KEYPAD_NUMLOCK) - kbd.release(Keycode.KEYPAD_NUMLOCK) - def press(key): - if key == 0: - kbd.press(Keycode.F13) - elif key == 1: - kbd.press(Keycode.F14) + if key == 0: # xf86copy + ctl.send(0x21B) + elif key == 1: # xf86paste + ctl.send(0x21D) elif key == 2: mse.press(Mouse.MIDDLE_BUTTON) elif key == 3: - ctl.send(0x224) # back + ctl.send(0x224) # back (browser) elif key == 4: - ctl.send(0x225) # back + ctl.send(0x225) # forward (browser) elif key == 5: mse.press(Mouse.RIGHT_BUTTON) - elif key == 6: # sticky scroll mode - toggle_scroll() - elif key == 7: # press and hold scroll mode - toggle_scroll() + elif key == 6: # ctrl-w + kbd.press(Keycode.CONTROL) + kbd.press(Keycode.W) + kbd.release(Keycode.W) + kbd.release(Keycode.CONTROL) + elif key == 7: + # toggles scroll mode in ploopy nano using maddie layout + kbd.press(Keycode.KEYPAD_NUMLOCK) + kbd.release(Keycode.KEYPAD_NUMLOCK) + kbd.press(Keycode.KEYPAD_NUMLOCK) + kbd.release(Keycode.KEYPAD_NUMLOCK) elif key == 8: mse.press(Mouse.LEFT_BUTTON) def release(key): - if key == 0: - kbd.release(Keycode.F13) - elif key == 1: - kbd.release(Keycode.F14) - elif key == 2: + if key == 2: mse.release(Mouse.MIDDLE_BUTTON) elif key == 5: mse.release(Mouse.RIGHT_BUTTON) - elif key == 7: - toggle_scroll() elif key == 8: mse.release(Mouse.LEFT_BUTTON) diff --git a/keytest.py b/keytest.py new file mode 100755 index 0000000..84d82ed --- /dev/null +++ b/keytest.py @@ -0,0 +1,97 @@ +import time +import board +from digitalio import DigitalInOut, Direction, Pull +import usb_hid +from adafruit_hid.keyboard import Keyboard +from adafruit_hid.keycode import Keycode +from adafruit_hid.mouse import Mouse +from adafruit_hid.consumer_control import ConsumerControl + +led = DigitalInOut(board.LED) +led.direction = Direction.OUTPUT +led.value = True + +kbd = Keyboard(usb_hid.devices) +mse = Mouse(usb_hid.devices) +ctl = ConsumerControl(usb_hid.devices) + +pins = [ + board.GP0, # 0 1 2 + board.GP1, # 3 4 5 + board.GP2, # 6 7 8 + board.GP3, + board.GP4, + board.GP5, + board.GP6, + board.GP7, + board.GP8, +] + +testcode = 0x180 + +def press(key): + if key == 0: # xf86select + global testcode + ctl.send(testcode) + print("Keycode is % s" % testcode) + testcode += 1 + elif key == 1: # xf86print + ctl.send(0x208) + elif key == 2: + mse.press(Mouse.MIDDLE_BUTTON) + elif key == 3: + ctl.send(0x224) # back (browser) + elif key == 4: + ctl.send(0x225) # forward (browser) + elif key == 5: + mse.press(Mouse.RIGHT_BUTTON) + elif key == 6: # ctrl-w + kbd.press(Keycode.CONTROL) + kbd.press(Keycode.W) + kbd.release(Keycode.W) + kbd.release(Keycode.CONTROL) + elif key == 7: + # toggles scroll mode in ploopy nano using maddie layout + kbd.press(Keycode.KEYPAD_NUMLOCK) + kbd.release(Keycode.KEYPAD_NUMLOCK) + kbd.press(Keycode.KEYPAD_NUMLOCK) + kbd.release(Keycode.KEYPAD_NUMLOCK) + elif key == 8: + mse.press(Mouse.LEFT_BUTTON) + +def release(key): + if key == 2: + mse.release(Mouse.MIDDLE_BUTTON) + elif key == 5: + mse.release(Mouse.RIGHT_BUTTON) + elif key == 8: + mse.release(Mouse.LEFT_BUTTON) + +switches = [0, 1, 2, 3, 4, 5, 6, 7, 8] + +for i in range(9): + switches[i] = DigitalInOut(pins[i]) + switches[i].direction = Direction.INPUT + switches[i].pull = Pull.UP + +switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +while True: + for button in range(9): + if switch_state[button] == 0: + if not switches[button].value: + try: + press(button) + except ValueError: # deals w six key limit + pass + switch_state[button] = 1 + + if switch_state[button] == 1: + if switches[button].value: + try: + release(button) + except ValueError: + pass + switch_state[button] = 0 + + time.sleep(0.01) # debounce