-
Notifications
You must be signed in to change notification settings - Fork 1
Control the lights on the gamepad using Serial #1
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,24 @@ | ||
| # NeoTrellisGamepad | ||
| Have the Adafruit NeoTrellis M4 appear as a 32-button USB Gamepad. | ||
|
|
||
| This extends from the adafruit_hid examples, modifying them from 16-button to 32-button (which is the highest value the FRC Driver Station appears to support) | ||
| This extends from the adafruit_hid examples, modifying them from 16-button to 32-button (which is the highest value the FRC Driver Station appears to support) | ||
|
|
||
| FRC Driver Station cannot send data back to the controller directly, so listen for data over Serial to control the lights on the gamepad. | ||
|
|
||
| The gamepad has two serial connections: | ||
|
|
||
| 1. The console stream, provides debug data and the Python REPL | ||
| 2. The control stream, used for input from an application | ||
|
|
||
| The data format that the controller is expecting looks like: | ||
|
|
||
| `LLRRRGGGBBB\n` | ||
|
|
||
| - `LL` is the LED number to change, 01-32. | ||
| - `RRR` is the red portion of the color, 000-255. | ||
| - `GGG` is the green portion of the color, 000-255. | ||
| - `BBB` is the blue portion of the color, 000-255. | ||
|
|
||
| The console output will provide feedback if the gamepad doesn't understand the command. | ||
|
|
||
| Provide any non-numeric command with the correct length to clear all LEDs. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,22 +6,24 @@ | |
| from adafruit_hid import find_device | ||
| from hid_gamepad import Gamepad | ||
| import usb_hid | ||
| import usb_cdc | ||
|
|
||
| trellis = adafruit_trellism4.TrellisM4Express() | ||
|
|
||
| serial = usb_cdc.data | ||
| gp = Gamepad(usb_hid.devices) | ||
| gp.press_buttons(2) | ||
|
|
||
| """ | ||
| while True: | ||
| trellis.pixels[0, 0] = (0, 0, 0) | ||
| gp.press_buttons(2) | ||
| time.sleep(0.5) | ||
| trellis.pixels[0, 0] = (0, 0, 0) | ||
| gp.release_buttons(2) | ||
| time.sleep(0.5) | ||
| """ | ||
| if serial is None: | ||
| print("No serial connection!") | ||
| else: | ||
| print("Serial present") | ||
| serial.timeout = 0.1 | ||
|
|
||
| def clearLeds(trellis): | ||
| trellis.pixels.fill(0) | ||
|
|
||
| def inRange(value, min, max): | ||
| return value >= min and value <= max | ||
|
|
||
| while True: | ||
| pressed = trellis.pressed_keys | ||
|
|
@@ -30,14 +32,56 @@ | |
| for x, y in pressed: | ||
| button = (x + y * 8) + 1 | ||
| pressed_buttons.append(button) | ||
| trellis.pixels[x, y] = (255, 0, 0) # Set pressed buttons to red | ||
| print("Pressed buttons:", pressed_buttons) | ||
| gp.press_buttons(*pressed_buttons) | ||
| for i in range(1, 17): | ||
| if i not in pressed_buttons: | ||
| gp.release_buttons(i) | ||
| trellis.pixels[(i - 1) % 8, (i - 1) // 8] = (0, 0, 0) # Set released buttons to black | ||
| else: | ||
| gp.release_all_buttons() | ||
| trellis.pixels.fill((0, 0, 0)) # Set all buttons to black | ||
| time.sleep(0.001) | ||
| if serial.connected: | ||
| while serial.in_waiting >= 12: | ||
| # Serial byte packed format: LLRRRGGGBBB\n | ||
| # LL: Led number | ||
| # RRR: Red intensity | ||
| # GGG: Green intensity | ||
| # BBB: Blue intensity | ||
| command = serial.readline() | ||
| if command is None: | ||
| break | ||
| if (len(command)) != 12: | ||
| print("Unexpected command length: ", len(command)) | ||
| clearLeds(trellis) | ||
| serial.reset_input_buffer() | ||
| continue | ||
| commandString = str(command[:11], 'ascii') | ||
| print("Received command:", str(commandString)) | ||
|
|
||
| if not commandString.isdigit(): | ||
| print("Not a numeric command. Clearing!") | ||
| clearLeds(trellis) | ||
| continue | ||
|
|
||
| ledNumber = int(commandString[:2]) | ||
| redValue = int(commandString[2:5]) | ||
| greenValue = int(commandString[5:8]) | ||
| blueValue = int(commandString[8:11]) | ||
| if not inRange(ledNumber, 1, 32): | ||
|
||
| print("Unexpected led number:", ledNumber) | ||
| continue | ||
| if not inRange(redValue, 0, 255): | ||
| print("Unexpected red value:", redValue) | ||
| continue | ||
| if not inRange(greenValue, 0, 255): | ||
| print("Unexpected green value:", greenValue) | ||
| continue | ||
| if not inRange(blueValue, 0, 255): | ||
| print("Unexpected blue value:", blueValue) | ||
| continue | ||
|
|
||
| x = (ledNumber-1) % 8 | ||
| y = int((ledNumber-1) // 8) | ||
| trellis.pixels[(x, y)] = (redValue, greenValue, blueValue) | ||
| else: | ||
| print("Serial not connected") | ||
| time.sleep(0.001) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "CIRCUITPY"') do set myDrive=%%D | ||
| echo %myDrive% | ||
|
|
||
| set myDrive=D: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happened here? Did deploy.cmd not find the CIRCUITPY drive on your machine? Did it show up with a different name? |
||
| REM delete any existing python files so we know it's only going to run our new ones | ||
| del %myDrive%\*.py | ||
| REM copy over our python files | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.