|
| 1 | +import board |
| 2 | +import busio |
| 3 | +import analogio |
| 4 | +import neopixel |
| 5 | + |
| 6 | +from adafruit_mcp3426 import MCP3426 |
| 7 | +from adafruit_mcp23008 import MCP23008 |
| 8 | +from kmk.kmk_keyboard import KMKKeyboard |
| 9 | +from kmk.keys import KC |
| 10 | +from kmk.scanners import DiodeOrientation |
| 11 | + |
| 12 | +import usb_hid |
| 13 | +from adafruit_hid.consumer_control import ConsumerControl |
| 14 | +from adafruit_hid.consumer_control_code import ConsumerControlCode |
| 15 | + |
| 16 | +import usb_hid |
| 17 | + |
| 18 | +# Source: Adafruit |
| 19 | +# TODO: Test with mac to find the correct report descriptor |
| 20 | +# This is only one example of a gamepad report descriptor, |
| 21 | +# and may not suit your needs. |
| 22 | +GAMEPAD_REPORT_DESCRIPTOR = bytes(( |
| 23 | + 0x05, 0x01, # Usage Page (Generic Desktop Ctrls) |
| 24 | + 0x09, 0x05, # Usage (Game Pad) |
| 25 | + 0xA1, 0x01, # Collection (Application) |
| 26 | + 0x85, 0x04, # Report ID (4) |
| 27 | + 0x05, 0x09, # Usage Page (Button) |
| 28 | + 0x19, 0x01, # Usage Minimum (Button 1) |
| 29 | + 0x29, 0x10, # Usage Maximum (Button 16) |
| 30 | + 0x15, 0x00, # Logical Minimum (0) |
| 31 | + 0x25, 0x01, # Logical Maximum (1) |
| 32 | + 0x75, 0x01, # Report Size (1) |
| 33 | + 0x95, 0x10, # Report Count (16) |
| 34 | + 0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) |
| 35 | + 0x05, 0x01, # Usage Page (Generic Desktop Ctrls) |
| 36 | + 0x15, 0x81, # Logical Minimum (-127) |
| 37 | + 0x25, 0x7F, # Logical Maximum (127) |
| 38 | + 0x09, 0x30, # Usage (X) |
| 39 | + 0x09, 0x31, # Usage (Y) |
| 40 | + 0x09, 0x32, # Usage (Z) |
| 41 | + 0x09, 0x35, # Usage (Rz) |
| 42 | + 0x75, 0x08, # Report Size (8) |
| 43 | + 0x95, 0x04, # Report Count (4) |
| 44 | + 0x81, 0x02, # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) |
| 45 | + 0xC0, # End Collection |
| 46 | +)) |
| 47 | + |
| 48 | +gamepad = usb_hid.Device( |
| 49 | + report_descriptor=GAMEPAD_REPORT_DESCRIPTOR, |
| 50 | + usage_page=0x01, # Generic Desktop Control |
| 51 | + usage=0x05, # Gamepad |
| 52 | + report_ids=(4,), # Descriptor uses report ID 4. |
| 53 | + in_report_lengths=(6,), # This gamepad sends 6 bytes in its report. |
| 54 | + out_report_lengths=(0,), # It does not receive any reports. |
| 55 | +) |
| 56 | + |
| 57 | +usb_hid.enable( |
| 58 | + (usb_hid.Device.CONSUMER_CONTROL, |
| 59 | + gamepad) |
| 60 | +) |
| 61 | + |
| 62 | +cc = ConsumerControl(usb_hid.devices) |
| 63 | +gp1 = gamepad(usb_hid.devices) |
| 64 | +gp2 = gamepad(usb_hid.devices) |
| 65 | + |
| 66 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 67 | + |
| 68 | +adc = MCP3426(i2c) |
| 69 | + |
| 70 | +mcp = MCP23008(i2c) |
| 71 | + |
| 72 | +joystick_x_left = analogio.AnalogIn(board.GP26) |
| 73 | +joystick_y_left = analogio.AnalogIn(board.GP27) |
| 74 | +joystick_x_right = analogio.AnalogIn(board.GP28) |
| 75 | +joystick_y_right = analogio.AnalogIn(board.GP29) |
| 76 | + |
| 77 | +keyboard = KMKKeyboard() |
| 78 | + |
| 79 | +for pin in range(8): |
| 80 | + mcp.setup(pin, MCP23008.IN) |
| 81 | + |
| 82 | +keyboard.col_pins = [mcp.get_pin(i) for i in range(3)] |
| 83 | +keyboard.row_pins = [mcp.get_pin(i + 3) for i in range(3)] |
| 84 | + |
| 85 | +keyboard.keymap = [ |
| 86 | + [ |
| 87 | + [KC.MUTE, KC.VOLD, KC.VOLU], # Media controls: Mute, Volume Down, Volume Up |
| 88 | + [KC.PLAY_PAUSE, KC.PREV_TRACK, KC.NEXT_TRACK], # Media playback controls |
| 89 | + [KC.WEB_SEARCH, KC.NEW_TAB, KC.FIND], # Web/Browser shortcuts |
| 90 | + ], |
| 91 | + [ |
| 92 | + [KC.COPY, KC.PASTE, KC.CUT], # Editing controls |
| 93 | + [KC.UNDO, KC.REDO, KC.SELECT_ALL], # Editing shortcuts |
| 94 | + [KC.LCTRL, KC.LALT, KC.LSFT], # Modifier keys |
| 95 | + ], |
| 96 | + [ |
| 97 | + [KC.SCRN, KC.PRTSC, KC.CALC], # Screenshot and other utilities |
| 98 | + [KC.LGUI, KC.RGUI, KC.SPC], # GUI keys and space |
| 99 | + [KC.ESC, KC.TAB, KC.ENTER], # Control keys |
| 100 | + ], |
| 101 | +] |
| 102 | + |
| 103 | +keyboard.diode_orientation = DiodeOrientation.COL2ROW |
| 104 | + |
| 105 | +def read_joystick(joystick): |
| 106 | + return int((joystick.value / 65535) * 255) - 127 |
| 107 | + |
| 108 | +def update_joystick(): |
| 109 | + # Read the joystick values (left and right) |
| 110 | + x_left = read_joystick(joystick_x_left) |
| 111 | + y_left = read_joystick(joystick_y_left) |
| 112 | + x_right = read_joystick(joystick_x_right) |
| 113 | + y_right = read_joystick(joystick_y_right) |
| 114 | + |
| 115 | + gp1.move_joysticks( |
| 116 | + x=x_left, |
| 117 | + y=y_left, |
| 118 | + ) |
| 119 | + |
| 120 | + gp2.move_joysticks( |
| 121 | + x=x_right, |
| 122 | + y=y_right, |
| 123 | + ) |
| 124 | + |
| 125 | +old_volume = 0 |
| 126 | + |
| 127 | +def update_volume(): |
| 128 | + slider_0_value = adc.read_voltage(channel=0) |
| 129 | + volume_level = int((slider_0_value / 65535) * 100) |
| 130 | + # TODO: make this better |
| 131 | + if volume_level > old_volume: |
| 132 | + cc.send(ConsumerControlCode.VOLUME_INCREMENT) |
| 133 | + elif volume_level < old_volume: |
| 134 | + cc.send(ConsumerControlCode.VOLUME_DECREMENT) |
| 135 | + old_volume = volume_level |
| 136 | + |
| 137 | +old_brightness = 0 |
| 138 | + |
| 139 | +def update_brightness(): |
| 140 | + slider_1_value = adc.read_voltage(channel=1) |
| 141 | + brightness_level = int((slider_1_value / 65535) * 100) |
| 142 | + if brightness_level > old_brightness: |
| 143 | + cc.send(ConsumerControlCode.BRIGHTNESS_INCREMENT) |
| 144 | + elif brightness_level < old_brightness: |
| 145 | + cc.send(ConsumerControlCode.BRIGHTNESS_DECREMENT) |
| 146 | + old_brightness = brightness_level |
| 147 | + |
| 148 | +pixel = neopixel.NeoPixel(board.GP2, 9, brightness=0.5, auto_write=True) |
| 149 | + |
| 150 | +def fade_colors(): |
| 151 | + colors = [ |
| 152 | + (255, 0, 0), # Red |
| 153 | + (0, 255, 0), # Green |
| 154 | + (0, 0, 255), # Blue |
| 155 | + (255, 255, 0), # Yellow |
| 156 | + (0, 255, 255), # Cyan |
| 157 | + (255, 0, 255), # Magenta |
| 158 | + (255, 255, 255) # White |
| 159 | + ] |
| 160 | + while True: |
| 161 | + for color in colors: |
| 162 | + for i in range(256): |
| 163 | + faded_color = tuple(int(c * i / 255) for c in color) |
| 164 | + pixel.fill(faded_color) |
| 165 | + time.sleep(0.01) |
| 166 | + |
| 167 | +def update_inputs(): |
| 168 | + update_joystick() |
| 169 | + update_volume() |
| 170 | + update_brightness() |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + while True: |
| 174 | + update_inputs() |
| 175 | + keyboard.go() |
| 176 | + fade_colors() |
0 commit comments