示例#1
0
# https://github.com/adafruit/Adafruit_CircuitPython_HID

import usb_hid
from adafruit_hid.gamepad import Gamepad

gp = Gamepad(usb_hid.devices)

# Test on Linux with:
#   jstest-gtk
#   https://jstest-gtk.gitlab.io/

# click gamepad buttons
# button index is 1-based
# icons in jstest-gtk will flash briefly
# jstest-gtk maps to (0, 2, 3, 6)
gp.click_buttons(1, 3, 4, 7)

# move joysticks in range [-127, 127]
# jstest-gtk maps to [-32767, 32767]
gp.move_joysticks(x = 90, y = 70, z = -80, r_z = 60)
示例#2
0
import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.gamepad import Gamepad
from digitalio import DigitalInOut, Direction, Pull
import mfrc522

last_value = "FIRST_VALUE_TO_BE_ERASED"
no_value_count = 0
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
gp = Gamepad(usb_hid.devices)

switchPins = [board.D10, board.D11, board.D12]
switches = []
switchPressedValues = [] 
for switchPin in switchPins: 
    switch = DigitalInOut(switchPin)
    switch.direction = Direction.INPUT
    switch.pull = Pull.UP
    switches.append(switch)
    switchPressedValues.append(False)

def send_value(val):
    print("sending {}".format(val.lower()))
    # kbd.send(Keycode.CONTROL, Keycode.ONE)
    # layout.write(val.lower())
    # kbd.send(Keycode.CONTROL, Keycode.TWO)
    val = val.lower()
#designate the chip select and reset pins
CS = digitalio.DigitalInOut(board.D4)
RESET = digitalio.DigitalInOut(board.D3)

#initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

#initialze RFM radio
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)

#optionally set an encryption key (16 byte AES key). MUST match both
#on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'

#set gamepad library equal to gp
gp = Gamepad(usb_hid.devices)

#set 1st joystick equal to xPin
xPin = analogio.AnalogIn(board.A0)
#set 2nd joystick equal to yPin
yPin = analogio.AnalogIn(board.A1)


#mapping function to find ranges
#parameters from beginning to end are as follows:
#x - the device, in_min - x value, in_max - y value,
#out_min - z value, out_max - z rotation value
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

示例#4
0
# Write your code here :-)
import time
import board
import array
from analogio import AnalogIn
from adafruit_hid.gamepad import Gamepad

from settings import *


time.sleep(1.0)
gp = Gamepad()

class RollingAverage:
    def __init__(self, size):
        self.size=size
        self.buffer = array.array('d')
        for i in range(size):
            self.buffer.append(0.0)
        self.pos = 0
    def addValue(self,val):
        self.buffer[self.pos] = val
        self.pos = (self.pos + 1) % self.size
    def average(self):
        return (sum(self.buffer) / self.size)

base = AnalogIn(board.A2)
if (not swapAxes):
    hor = AnalogIn(board.A3)
    vert = AnalogIn(board.A4)
示例#5
0
# | 2 5 |      3: Latch
# | 3 6 |      4: Data
# | 4 7 |      5: VCC
#  -----

# and you want to wire it up like this:
#
# NES          Pico
# ------------------
# Ground       GND (any)
# Clock        GP4
# Latch        GP5
# Data         GP6
# VCC          3V3

gamepad = Gamepad(usb_hid.devices)

clock = digitalio.DigitalInOut(board.GP4)
latch = digitalio.DigitalInOut(board.GP5)
data = digitalio.DigitalInOut(board.GP6)

latch.direction = digitalio.Direction.OUTPUT
clock.direction = digitalio.Direction.OUTPUT
data.direction = digitalio.Direction.INPUT

data.pull = digitalio.Pull.UP

latch.value = False
clock.value = False

# these are the SNES timings, and apparently NES can be made to run much faster between cycles, buuuuut....
示例#6
0
import board
import digitalio
import analogio

from adafruit_hid.gamepad import Gamepad

gp = Gamepad()

# Create some buttons. The physical buttons are connected
# to ground on one side and these and these pins on the other.
button_pins = (board.D2, board.D3, board.D4, board.D5)

# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons[i] will send that button number when buttons[i]
# is pushed.
gamepad_buttons = (1, 2, 8, 15)

buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP

# Connect an analog two-axis joystick to A4 and A5.
ax = analogio.AnalogIn(board.A4)
ay = analogio.AnalogIn(board.A5)

# 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:
示例#7
0
from adafruit_hid.gamepad import Gamepad
from time import sleep
import sys

startpause = 3
print(
    "usb-rc - an adapter from RC car receiver to USB-gamepad/joystick. https://github.com/colzilla/usb-rc"
)
print("starting in {} sec...".format(startpause))
sleep(startpause)

debug = False
shush = False

#create the gampepad object
gp = Gamepad()

#setup the pulse readers
ch1 = pulseio.PulseIn(board.A3, 5)
ch2 = pulseio.PulseIn(board.A4, 5)
ch3 = pulseio.PulseIn(board.A2, 5)

#used in auto-calibration
ch1min = 1499
ch1max = 1501
ch2min = 1499
ch2max = 1501
ch3min = 1499
ch3max = 1501
allowed_min = 800
allowed_max = 2200
import board
import digitalio
import analogio
import usb_hid

# Run using Robo HAT MM1

from adafruit_hid.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.
# Engine Ignition
button_pins = (board.SERVO1, board.SERVO2, board.SERVO3, board.SERVO4)

# Map the buttons to button numbers on the Gamepad.
# gamepad_buttons[i] will send that button number when buttons[i]
# is pushed.
gamepad_buttons = (1, 2, 8, 15)

buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP

# Center console controls for 747 (6 levers)
thr1 = analogio.AnalogIn(board.RCC1)
thr2 = analogio.AnalogIn(board.RCC2)
thr3 = analogio.AnalogIn(board.RCC3)
thr4 = analogio.AnalogIn(board.RCC4)
button_mask = const((1 << BUTTON_RIGHT) |
                    (1 << BUTTON_DOWN) |
                    (1 << BUTTON_LEFT) |
                    (1 << BUTTON_UP) |
                    (1 << BUTTON_SEL))

i2c = busio.I2C(board.SCL, board.SDA)

ss = adafruit_seesaw.Seesaw(i2c)

ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)

last_game_x = 0
last_game_y = 0

g = Gamepad()

while True:
    x = ss.analog_read(2)
    y = ss.analog_read(3)

    game_x = range_map(x, 0, 1023, -127, 127)
    game_y = range_map(y, 0, 1023, -127, 127)
    if last_game_x != game_x or last_game_y != game_y:
        last_game_x = game_x
        last_game_y = game_y
        print(game_x, game_y)
        g.move_joysticks(x=game_x, y=game_y)

    buttons = (BUTTON_RIGHT, BUTTON_DOWN, BUTTON_LEFT, BUTTON_UP, BUTTON_SEL)
    button_state = [False] * len(buttons)
示例#10
0
from config import *

from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_hid.mouse import Mouse
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.gamepad import Gamepad

# Init HIDs
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
cc = ConsumerControl(usb_hid.devices)
m = Mouse(usb_hid.devices)
g = Gamepad(usb_hid.devices)


def sanitise(line, cmd, remove_space=False):
    line = line.replace(cmd, "")
    if remove_space == True:
        line = line.strip()
    return line


def argument(line):
    x = re.search(r"\[(.*?)\]", line.lower())
    return x.group(1)


def ext_payload(payload):
示例#11
0
import time
import usb_hid

from button import button

from adafruit_hid.mouse import Mouse
from adafruit_hid.gamepad import Gamepad
from adafruit_hid.consumer_control import ConsumerControl

gp = Gamepad(usb_hid.devices)
m = Mouse(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)

gp.move_joysticks(0, 0, 0, 0)
btn = button(oled=True)
btn.show_text("Welcome")
btn.record(gp, cc, m)
示例#12
0
import usb_hid

from adafruit_hid.gamepad import Gamepad
from analogio import AnalogIn
# checks if the pico is connected, could be removed later

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

for x in range(0, 3):
    time.sleep(0.1)
    led.value = False
    time.sleep(0.1)
    led.value = True

gamepad = Gamepad(usb_hid.devices)

# frets
btn_green = digitalio.DigitalInOut(board.GP2)
btn_green.direction = digitalio.Direction.INPUT
btn_green.pull = digitalio.Pull.DOWN

btn_red = digitalio.DigitalInOut(board.GP3)
btn_red.direction = digitalio.Direction.INPUT
btn_red.pull = digitalio.Pull.DOWN

btn_yellow = digitalio.DigitalInOut(board.GP4)
btn_yellow.direction = digitalio.Direction.INPUT
btn_yellow.pull = digitalio.Pull.DOWN

btn_blue = digitalio.DigitalInOut(board.GP5)
示例#13
0
from time import sleep, monotonic

from adafruit_hid.gamepad import Gamepad
from mode import Mode

def hex2rgb(hexcode):
    #added 1 to all for #
    red = int("0x"+hexcode[1:3], 16)
    green = int("0x"+hexcode[3:5], 16)
    blue = int("0x"+hexcode[5:7], 16)
    rgb = (red, green, blue)
    return rgb

dot = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)

gp = Gamepad()

import settings

print(settings.modes)

modeDelay = settings.modeDelay
repeatDelay = settings.repeatDelay

modeSwitchCode = 0x0

for num in settings.modeSwitches:
    modeSwitchCode |= (1 << num)

modeNum = 0
currentMode = settings.modes[modeNum]
示例#14
0
import board
import digitalio
import pulseio
from adafruit_hid.gamepad import Gamepad
from time import sleep
import sys

version = "20200331-1933"
print(
    "usb-rc version [{}] - an adapter from RC car receiver to USB-gamepad/joystick. https://github.com/colzilla/usb-rc"
    .format(version))

debug = False

gamepad = Gamepad()

#setup the pulse readers
ch1 = pulseio.PulseIn(board.A1, 1)
ch2 = pulseio.PulseIn(board.A2, 1)
ch3 = pulseio.PulseIn(board.A3, 1)
ch4 = pulseio.PulseIn(board.A4, 1)

#used in auto-calibration
ch1min = ch2min = ch3min = ch4min = 279
ch1max = ch2max = ch3max = ch4max = 281

allowed_min = 100
allowed_max = 480


def range_map(x, in_min, in_max, out_min, out_max):
button_mask = const((1 << BUTTON_RIGHT)
                    | (1 << BUTTON_DOWN)
                    | (1 << BUTTON_LEFT)
                    | (1 << BUTTON_UP)
                    | (1 << BUTTON_SEL))

i2c = busio.I2C(board.SCL, board.SDA)

ss = adafruit_seesaw.Seesaw(i2c)

ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)

last_game_x = 0
last_game_y = 0

g = Gamepad(usb_hid.devices)

while True:
    x = ss.analog_read(2)
    y = ss.analog_read(3)

    game_x = range_map(x, 0, 1023, -127, 127)
    game_y = range_map(y, 0, 1023, -127, 127)
    if last_game_x != game_x or last_game_y != game_y:
        last_game_x = game_x
        last_game_y = game_y
        print(game_x, game_y)
        g.move_joysticks(x=game_x, y=game_y)

    buttons = (BUTTON_RIGHT, BUTTON_DOWN, BUTTON_LEFT, BUTTON_UP, BUTTON_SEL)
    button_state = [False] * len(buttons)
示例#16
0
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.gamepad import Gamepad
from adafruit_hid.keycode import Keycode
from time import sleep
import board

gamepad = Gamepad(usb_hid.devices)

button_status = {
    'up': 0,
    'down': 0,
    'left': 0,
    'right': 0,
    'select': 0,
    'start': 0,
    'a': 0,
    'b': 0
}
buttons = {
    0: 'a',
    1: 'b',
    2: 'select',
    3: 'start',
    4: 'up',
    5: 'down',
    6: 'left',
    7: 'right'
}
示例#17
0
文件: code.py 项目: Qest333/ShiftPico
import board
import digitalio
import usb_hid

from adafruit_hid.gamepad import Gamepad

shifter = Gamepad(usb_hid.devices)
button_pins = (board.GP18, board.GP19, board.GP20, board.GP21, board.GP22,
               board.GP26, board.GP27)
shifter_buttons = (1, 2, 3, 4, 5, 6, 7)
buttons = [digitalio.DigitalInOut(pin) for pin in button_pins]
for button in buttons:
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP

pressedButtons = [False, False, False, False, False, False, False]
while True:
    for i, button in enumerate(buttons):
        if pressedButtons[i] != button.value:
            if button.value:
                shifter.release_buttons(shifter_buttons[i])
            else:
                shifter.press_buttons(shifter_buttons[i])
            pressedButtons[i] = button.value
示例#18
0
"""
import time

import board
import usb_hid
from adafruit_hid.gamepad import Gamepad
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
led.value = True

print("Initializing the gamepad")
# Create a gamepad for HID simulation
gp = Gamepad(usb_hid.devices)

# Other pins can also be set to output
# for detecting buttons.
out = DigitalInOut(board.GP16)
out.direction = Direction.OUTPUT
out.value = True

buttons = {
    i + 1: {
        "device": DigitalInOut(device),
        "pressed": False
    }
    for i, device in enumerate(
        [
            # Buttons in the steering wheel, from 1 to 6