improvements

This commit is contained in:
Rudis Muiznieks 2022-03-22 16:09:50 -05:00
parent d630d4c08e
commit 247b6be6b7
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
3 changed files with 116 additions and 25 deletions

View File

@ -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.

42
code.py
View File

@ -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)

97
keytest.py Executable file
View File

@ -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