Raspberry Pi Pico – Joystick to Keyboard (WASZ + Space)
=================================================

Function
--------
Reads a digital joystick and appears to the PC as a USB keyboard only.

Joystick to key mapping:
Up     -> W
Left   -> A
Down   -> S
Right  -> Z
Button -> Space

Keys are held while the joystick is held.

-------------------------------------------------

Hardware Requirements
---------------------
- Raspberry Pi Pico (RP2040)
- Digital joystick (4 direction switches + 1 button)
- USB cable
- Hookup wire

Joystick switches must be active-low (pressed = GND).

-------------------------------------------------

Wiring
------
All joystick switches share a common GND.

Joystick Switch   Pico GPIO
---------------------------
Up                GP2
Down              GP3
Left              GP4
Right             GP5
Button / Fire     GP6
Common            GND

Internal pull-ups are enabled in software.

-------------------------------------------------

Software Setup
--------------

1. Install CircuitPython
- Download CircuitPython for Raspberry Pi Pico
- Hold BOOTSEL while plugging in USB
- Copy the UF2 file to the Pico
- Pico reboots as CIRCUITPY drive

-------------------------------------------------

2. USB Keyboard Only Configuration

Create file: boot.py

import usb_hid

usb_hid.enable(
    (usb_hid.Device.KEYBOARD,)
)

Save, unplug, and reconnect the Pico.

-------------------------------------------------

3. Main Program

Create file: code.py

import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

kbd = Keyboard(usb_hid.devices)

def make_input(pin):
    d = digitalio.DigitalInOut(pin)
    d.direction = digitalio.Direction.INPUT
    d.pull = digitalio.Pull.UP
    return d

joy_up     = make_input(board.GP2)
joy_down   = make_input(board.GP3)
joy_left   = make_input(board.GP4)
joy_right  = make_input(board.GP5)
joy_button = make_input(board.GP6)

state = {
    "up": False,
    "down": False,
    "left": False,
    "right": False,
    "button": False
}

while True:
    if not joy_up.value:
        if not state["up"]:
            kbd.press(Keycode.W)
            state["up"] = True
    else:
        if state["up"]:
            kbd.release(Keycode.W)
            state["up"] = False

    if not joy_down.value:
        if not state["down"]:
            kbd.press(Keycode.S)
            state["down"] = True
    else:
        if state["down"]:
            kbd.release(Keycode.S)
            state["down"] = False

    if not joy_left.value:
        if not state["left"]:
            kbd.press(Keycode.A)
            state["left"] = True
    else:
        if state["left"]:
            kbd.release(Keycode.A)
            state["left"] = False

    if not joy_right.value:
        if not state["right"]:
            kbd.press(Keycode.Z)
            state["right"] = True
    else:
        if state["right"]:
            kbd.release(Keycode.Z)
            state["right"] = False

    if not joy_button.value:
        if not state["button"]:
            kbd.press(Keycode.SPACE)
            state["button"] = True
    else:
        if state["button"]:
            kbd.release(Keycode.SPACE)
            state["button"] = False

    time.sleep(0.01)

-------------------------------------------------

Notes
-----
- Supports diagonals and multiple keys
- No auto-repeat (handled by OS)
- Works on Windows, Linux, macOS, DOSBox

End of file
