Hi everyone,
Newbie here - I'm trying to use the CPB as a bluetooth game controller, connecting pins A0 through A7 as GPIO pins.
The plan is to use limit switches as A,B, Select, Start, UP, DOWN, LEFT & RIGHT using limit switches, connected to ground.
Does this code look correct?:
# Gamepad coding using Digital Input pins for Circuit Playground Express Bluefruit, pins A0 through A7
import board
import digitalio
import usb_hid
from gamepad import Gamepad
gp = Gamepad(usb_hid.devices)
# Create some buttons. The physical buttons are connected
# to ground on one side and these and these pins on the other.
button_pins = (board.A0, board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7)
# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons will send that button number when buttons
# is pushed.
gamepad_buttons = (1, 2, 3, 4, 5, 6, 7, 8)
buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
# 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
while True:
# Buttons are grounded when pressed (.value = False).
for i, button in enumerate(buttons):
gamepad_button_num = gamepad_buttons
if button.value:
gp.release_buttons(gamepad_button_num)
print(" release", gamepad_button_num, end="")
else:
gp.press_buttons(gamepad_button_num)
print(" press", gamepad_button_num, end="")
Thanks!
Bluetooth game controller - code help needed
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Duffy_2021
- Posts: 6
- Joined: Fri Jul 30, 2021 11:25 am
- adafruit_support_mike
- Posts: 67485
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Bluetooth game controller - code help needed
You’ll get lots of redundant signals that way. The code will emit either a press or release message for every pin every time through the loop.
You need to capture the moment the signal ffrom the key changes, not just its value. There are several ways to do that, but I prefer the shift-register method:
Make an array of integers for each input pin. On each pass through the loop, multiply the intger by 2, then add 1 if the pin is high. Finally, truncate the value to 255.
What that does is create a numeric record of the last 8 readings. It also turns button behavior into numbers that are easy to identify.
For each key, call .press_button() when the integer value is 128. That’s a 1 followed by seven zeros, meaning the loop had to see the pin high at least once, then the next seven readings were low.
Call .release_button() when the integer value is 1. That means the pervious seven readings were low, but the most recent reading was high.
I find that easier and more robust that trying to compare the latest reading to the previous one.
You need to capture the moment the signal ffrom the key changes, not just its value. There are several ways to do that, but I prefer the shift-register method:
Make an array of integers for each input pin. On each pass through the loop, multiply the intger by 2, then add 1 if the pin is high. Finally, truncate the value to 255.
What that does is create a numeric record of the last 8 readings. It also turns button behavior into numbers that are easy to identify.
For each key, call .press_button() when the integer value is 128. That’s a 1 followed by seven zeros, meaning the loop had to see the pin high at least once, then the next seven readings were low.
Call .release_button() when the integer value is 1. That means the pervious seven readings were low, but the most recent reading was high.
I find that easier and more robust that trying to compare the latest reading to the previous one.
- Duffy_2021
- Posts: 6
- Joined: Fri Jul 30, 2021 11:25 am
Re: Bluetooth game controller - code help needed
Hi Mike,
Thanks for the reply! I'm new to coding, so I'd need some help to do what you suggest.
Are there any tutorials on how to do what you are saying? Maybe some sample code I can take a look at?
Thanks again!
Mike
Thanks for the reply! I'm new to coding, so I'd need some help to do what you suggest.
Are there any tutorials on how to do what you are saying? Maybe some sample code I can take a look at?
Thanks again!
Mike
- adafruit_support_mike
- Posts: 67485
- Joined: Thu Feb 11, 2010 2:51 pm
Re: Bluetooth game controller - code help needed
I don’t know of any tutorials, but this post has some sample code using the technique for multiple buttons:
viewtopic.php?f=19&t=126732#p632143
It will probably help to work through the instructions by hand, writing the values down on paper for a few passes through the loop. There’s no substitute for a working mental model of what code is doing.. without that, trying to program is like trying to speak another language without a translation dictionary.
viewtopic.php?f=19&t=126732#p632143
It will probably help to work through the instructions by hand, writing the values down on paper for a few passes through the loop. There’s no substitute for a working mental model of what code is doing.. without that, trying to program is like trying to speak another language without a translation dictionary.
Please be positive and constructive with your questions and comments.