Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Analog module does not work #1062

Open
doman18 opened this issue Dec 22, 2024 · 4 comments
Open

[BUG] Analog module does not work #1062

doman18 opened this issue Dec 22, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@doman18
Copy link

doman18 commented Dec 22, 2024

My goal is to create gaming keypad with joystick and 26 keys similar to Razer Tartarus, Hori TAC, Azeron Cyborg, Saitek PZ31a etc. Im using Pi Pico (for prototyping) and later will use Pi Zero.

Im following this
https://github.com/LennartEd/Analog-WASD-Keyboard-kmk-pi-pico-rp2040/blob/main/README.md

I want to use my joystick the same way author did - by assigning WASD to it.
I adjusted my code so it looks like this:

import board

from kmk.kmk_keyboard import KMKKeyboard
from kmk.keys import KC
from kmk.scanners import DiodeOrientation
from kmk.modules.layers import Layers
from kmk.modules.analog import AnalogKey #handles analoginput

keyboard = KMKKeyboard()
layers = Layers()
ana = AnalogKey()

keyboard.modules = [layers,ana]

keyboard.col_pins = (board.GP18,board.GP19)
keyboard.row_pins = (board.GP12,board.GP13)
keyboard.diode_orientation = DiodeOrientation.COL2ROW

keyboard.keymap = [
    [
        KC.A, KC.B,
        KC.C, KC.D,
    ]
]

#ANALOG 
ana.pins = (board.A0,board.A1) #define analog pins

ana.reverse = False #flips direction(standard is: analog value goes down when key pressed) 

# define per layer if keypresses should be Threshhold(0) or Rapid trigger(1) explanation: https://wooting.io/rapid-trigger 
ana.keypType = (1,1)

#threshold: when analog value is passed key is pressed 
#rapit: when a certain distance is surpaced key is pressed (the lower the value the faster responce but at a certain point noise can trigger keypress)
    #use the provided "maxChangeInVal.py"+ 300 to find a good rapid trigger value
ana.threshold = (30000,1800) #default (30000,1800) 

ana.map = [
    [
        KC.W,KC.A,KC.S,KC.D
    ]
]

if __name__ == '__main__':
    keyboard.go()

Unfortunately im geting an error:

code.py output:
Starting
Traceback (most recent call last):
  File "code.py", line 9, in <module>
ImportError: no module named 'kmk.modules.analog'

Is there other way to achieve what i want?

@doman18 doman18 added the bug Something isn't working label Dec 22, 2024
@xs5871
Copy link
Collaborator

xs5871 commented Dec 22, 2024

This isn't a bug. The draft PR that implements that module isn't merged yet.

@piman13
Copy link

piman13 commented Dec 30, 2024

the project you are referencing anyway made their own module that seem to be very application specific. and while it is interesting it is not likely something that would be merged into kmk for everyone.
in the mean time though you could use the one provided by the project you are referencing

@piman13
Copy link

piman13 commented Dec 30, 2024

long term however the is work on analogio and gamepad HID so kmk being used for this application would definitely be a possibility in the future.

(I kinda think a hotas controller would be cool now.... one project at a time....)

@doman18
Copy link
Author

doman18 commented Jan 7, 2025

@piman13 AFAIK gamepad HID does not support matrixes. At least i havent found info about it. And in my case (26 keys) matrix is mandatory.

That the main problem here - to get some matrix library with joystick support.

After some research and help on adafruit forums i came out with this code (based on analogio and adafruit_hid library):

import board, keypad, usb_hid, time, analogio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

##### JOYSTICK

# Connect an analog two-axis joystick to A0 and A1.
# https://how2electronics.com/how-to-use-adc-in-raspberry-pi-pico-adc-example-code/
ax = analogio.AnalogIn(board.A0)
ay = analogio.AnalogIn(board.A1)

# Equivalent of Arduino's map() function.
def range_map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min

deadzoneSize=15

##### KEYBOARD
kbd = Keyboard(usb_hid.devices)

km = keypad.KeyMatrix(
    row_pins=(board.GP18,board.GP19),
    column_pins=(board.GP12,board.GP13),
)

mapping_list = [Keycode.W,Keycode.A,Keycode.S,Keycode.D]

xKey="none"
yKey="none"


while True:
    x=range_map(ax.value, 0, 65535, -127, 127)
    y=range_map(ay.value, 0, 65535, -127, 127)
 

    if x>deadzoneSize and xKey != "D": #
        kbd.press(Keycode.D)
        xKey="D"
    elif x<-deadzoneSize and xKey != "A":
        kbd.press(Keycode.A)
        xKey="A"
    elif abs(x)<=deadzoneSize and xKey != "none":
        xKey="none"
        kbd.release(Keycode.A)
        kbd.release(Keycode.D)

    if y>deadzoneSize and yKey != "W":
        kbd.press(Keycode.W)
        yKey="W"
    elif y<-deadzoneSize and yKey != "S":
        kbd.press(Keycode.S)
        yKey="S"
    elif abs(y)<=deadzoneSize and yKey != "none":
        yKey="none"
        kbd.release(Keycode.W)
        kbd.release(Keycode.S)

    print(" x", xKey, "y", yKey)


    event = km.events.get()
    if event:
        if event.pressed:
            kbd.press(mapping_list[event.key_number])
        else:
            kbd.release(mapping_list[event.key_number])

https://forums.adafruit.com/viewtopic.php?p=1040009#p1040009

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants