First some Disclaimers.
- I’m not native English speaker.
- It’s not tutorial, but i try to deliver as many hints i can.
- If you decide to replicate my idea, you do it on your own risk.
- I can drop this project anytime due lack of time, or lack of skill
- I’m not a writer or teacher, any suggestion about making it more clear will be welcome.
- I try to use as simple not-technical language as possible.
I’m playing flight simulator on xbox, and I’m disappointed by lack of support of ‘fancy’ peripherals…
That Discovery change everything…
I’m find a device, an micro-controller or uC for short. One named Rasspberry Pi pico which can witch minimal effort emulate keyboard. As we know FS on Xbox don’t have any problem which keyboard. There was two problems PID & VID. PID or Product ID and VID for Vendor ID. This two codes can’t be easy accrued. Thankful one company, Adafruit know for making stuff for meakers, make firmware for Rasspberry with build in keyboard emulation and PID & VID. That was beginning.
I’m trust every uC compatibile with circuitpython and native USB can be used, but Rasspberry was laying around handy, so i chose it.
Installation of circuitpython firmware is straightforward. Just download it (a stable one not beta) from Adafruit website (Here). Push and hold ‘bootsel’ buton on Raspberry Pi pico and while holding button connect it to computer. A USB dongle like drive should open. Drag and drop downloaded files into main folder. Reconnect Pi.
Next download necessary Libraries (I will be calling it later modules) from Adafruit website (Here) version must match your circuitpython firmware version. Uncompres and find ‘adafruit_hid’ folder inside ‘lib’ foder then copy it into ‘lib’ folder on your board. Last one save code posted below as ‘code.py’ on root folder of board.
For begining i wrote code:
#importing all important stuff to make THE RITUAL posible
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
#defining THE class, a heart of device, ritual cannot be done without THIS!
class switch:
def __init__(self, port, key1, key2): #This will setup our desired Button and bind it to uC!
self.switch = digitalio.DigitalInOut(port) #Make a psychical Anchorn a INPUT!
self.switch.switch_to_input(pull=digitalio.Pull.UP) #Make it HIGH!
self.key1 = key1
self.key2 = key2
self.state = True #This will prevent us from unnecesary repeting
def test_switch(self): #This will be testing state of Switch
if self.switch.value != self.state:
if self.switch.value == False: #This will execute if switch is ON!
keyboard.press(self.key1)
keyboard.release_all()
time.sleep(400/1000)
self.state = False
else: #This will execute if switch is OFF!
keyboard.press(self.key2)
keyboard.release_all()
time.sleep(400/1000)
self.state = True
#Start of preparing THE RITUAL!
#Preparing a virtual keyboard, device used to comunicate with THE THING!
time.sleep(1)
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
#Preparing a psihical switch, a anchorn in real world!
s1 = switch(board.GP2,Keycode.B,Keycode.C)
#Finally starting THE RITUAL!
while True:
s1.test_switch()
For hardware, nothing except usb and short jumpwire will not be used today.
(I will add here photos shortly)
Test if everything is working connect board to computer, open notepad and short wire pins labeled (they are on underside of board) as ‘GND’ and GP2 (as long you don’t change anything inside code), every single short wiring shuld send b into notepad and unshorting should send c.
Back to code.
#importing all important stuff to make THE RITUAL posible
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
This part load all necessary modules (aka prebuild parts). Ones starting with ‘from’ aren’t default one, but are provided by Adafruit and can be easily downloaded from adafruit website, more about leater.
class switch:
def __init__(self, port, key1, key2): #This will setup our desired Button and bind it to uC!
self.switch = digitalio.DigitalInOut(port) #Make a psychical Anchorn a INPUT!
self.switch.switch_to_input(pull=digitalio.Pull.UP) #Make it HIGH!
self.key1 = key1
self.key2 = key2
self.state = True #This will prevent us from unnecesary repeting
def test_switch(self): #This will be testing state of Switch
if self.switch.value != self.state:
if self.switch.value == False: #This will execute if switch is ON!
keyboard.press(self.key1)
keyboard.release_all()
time.sleep(400/1000)
self.state = False
else: #This will execute if switch is OFF!
keyboard.press(self.key2)
keyboard.release_all()
time.sleep(400/1000)
self.state = True
This part is sort of recipe for our program used for creating object with will define connection between physical switch and emulated keystroke.
def __init__(self, port, key1, key2):
will pass port name we use, key for ON state, key for OFF state when we create new connection.
keyboard.press(self.key1)
is used to push key on our virtual keyboard
keyboard.release_all()
releas all pushed keys, its important, if not done pushed keys will interfere with other interactions
time.sleep(400/1000)
adds a little delay to get rid of contact bouncing inside switch.
time.sleep(1)
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
Is used to creat virtual keyboard, its part of Adafruit module, and must be left as it is.
s1 = switch(board.GP2,Keycode.B,Keycode.C)
This will creat object which bound phisical part to software.
s1
it’s name of this conection and MUST be unique to all used switches and buttons
board.GP2
is name of uC pin we use to connect switch or button, in this case Port number 2
Keycode.B
is name of key B on keyboard, name is B but send b due of way in keyboard works.
Keycode.C
is like Keycode.B
B is used when switch is ON, C when is OFF
If you want more switches you must make this connection for every switches.
while True:
s1.test_switch()
it’s so called infinite loop, yes you guessed it it works infinite. It’s on end because we first must prepare everything to work. If any part of program will be put past this point, loop will don’t know it. It’s how python works. If you add another switch for example named s2 you must change it to:
while True:
s1.test_switch()
s2.test_switch()