Example #1
0
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
Example #2
0
cycle_clock = 0.000006  # 6µs according to spec
cycle_pause = 0.000006  # 6µs according to spec

while True:
	# toggle the latch to indicate we're polling for current button states
	latch.value = True
	sleep(cycle_latch)
	latch.value = False
	sleep(cycle_pause)

	# the A button is always sent immediately
	pressed[0] = data.value

	# now cycle through the rest of the buttons in order
	for x in range(1, 8):
		# each clock pulse tells the controller to send the next button in its internal order
		clock.value = True
		sleep(cycle_clock)
		clock.value = False
		sleep(cycle_pause)

		pressed[x] = data.value

	# and since we know the state of all buttons, let's press 'em
	for index in pressed:
		# Data pulls up (grounds when pressed), which means we actually press on False
		if pressed[index] == True:
			gamepad.release_buttons(gamepad_buttons[nes_buttons[index]])
		else:
			gamepad.press_buttons(gamepad_buttons[nes_buttons[index]])
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)
    for i, button in enumerate(buttons):
        buttons = ss.digital_read_bulk(button_mask)
        if not (buttons & (1 << button) and not button_state[i]):
            g.press_buttons(i + 1)
            print("Press", i + 1)
            button_state[i] = True
        elif button_state[i]:
            g.release_buttons(i + 1)
            print("Release", i + 1)
            button_state[i] = False

    time.sleep(0.01)
Example #4
0
        sleep(delaytime)
        clock.value = False
        sleep(delaytime)
        button_status[buttons[x + 1]] = data.value
    if button_status['select'] == False and button_status[
            'start'] == False and button_status['up'] == False:
        if mode == 2:
            mode = 1
        else:
            mode = 2
        keyboard.release_all()
        print(mode)
        sleep(0.3)
    press_buttons = []
    release_buttons = []
    for x in buttons:
        if mode == 2:
            if button_status[buttons[x]] == True:
                gamepad.release_buttons(x + 1)
            else:
                gamepad.press_buttons(x + 1)
        else:
            if button_status[buttons[x]] == True:
                release_buttons.append(keyboard_buttons[buttons[x]])
            else:
                press_buttons.append(keyboard_buttons[buttons[x]])

    if mode == 1:
        keyboard.press(*press_buttons)
        keyboard.release(*release_buttons)
Example #5
0
data = [up, down, left, right, ab, c]
buttons = len(data)
for pin in data:
	pin.direction = digitalio.Direction.INPUT
	pin.pull = digitalio.Pull.UP

select = digitalio.DigitalInOut(board.GP22)
select.direction = digitalio.Direction.OUTPUT

wait = .00001

while True:
	select.value = True
	sleep(wait)
	for h, btn in enumerate(data):
		pressed[h] = data[h].value

	select.value = False
	sleep(wait)
	pressed[buttons] = ab.value
	pressed[buttons+1] = c.value

	# and since we know the state of all buttons, let's press 'em
	for index in pressed:
		# Data pulls up (grounds when pressed), which means we actually press on False
		if pressed[index] == True:
			gamepad.release_buttons(gamepad_buttons[genesis_buttons[index]])
		else:
			gamepad.press_buttons(gamepad_buttons[genesis_buttons[index]])
# reverse thrust (TODO: maybe buttons?)
#rthr1 = analogio.AnalogIn(board.RCC1)
#rthr2 = analogio.AnalogIn(board.RCC2)
#rthr3 = analogio.AnalogIn(board.RCC3)
#rthr4 = analogio.AnalogIn(board.RCC4)


# 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[i]
        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="")

    # Convert range[0, 65535] to -127 to 127
    gp.move_joysticks(
        x=range_map(ax.value, 0, 65535, -127, 127),
        y=range_map(ay.value, 0, 65535, -127, 127),
    )
    print(" x", ax.value, "y", ay.value)
Example #7
0
def whammyDeadZone(whammyVal):
    whammyDeadZoneVal = -90
    whammyVal = setToJoyStickRange(whammyVal)
    if(whammyVal < whammyDeadZoneVal):
        return whammyDeadZoneVal
    else:
        return whammyVal

# rounds down the joystick value to the value specified
def roundStickVal(stickVal):
    roundDownVal = 20
    return int(stickVal / roundDownVal) * roundDownVal
while True:
    for gamenum, button in buttons.items():
        if button.value:
            gamepad.press_buttons(gamenum)
        else:
            gamepad.release_buttons(gamenum)

    # the limit on each analog stick is by the first value
    # make limit -127 to 127
    # adjust your joystick until limits are -127 to 127

    gamepad.move_joysticks(
        setToJoyStickRange(ana_joy_x.value/64000),
        setToJoyStickRange(ana_joy_y.value/65000),
        roundStickVal(whammyDeadZone((ana_whammy.value-30000)/25000)),
        None)

    #time.sleep(0.1)
    #print(roundStickVal(whammyDeadZone((ana_whammy.value-30000)/25000)))
Example #8
0
         switchCode |= (1 << num)
 if (switchCode != 0):
     tempSwitchCode = switchCode
     for i in range(num):
         if (int(tempSwitchCode/pow(2,num-i))==1):
             tempSwitchCode = tempSwitchCode%pow(2,num-i)
             singleSwitchCode=pow(2,num-i)
         else:
             singleSwitchCode=0
         if (singleSwitchCode in currentMode.actions):
             a = currentMode.actions[singleSwitchCode]
             if a[0] == Mode.BUTTON_PRESS:
                 (actionType, buttonNum) = a
                 if type(buttonNum) is int:
                     print("Button=",buttonNum)
                     gp.press_buttons(buttonNum)
                 else:
                     for b in buttonNum:
                         print("Button=",b)
                         gp.press_buttons(b)
             elif a[0] == Mode.DPAD_MOVE:
                 (actionType, x, y) = a
                 print("x=",x,",y=",y)
                 gp.move_joysticks(dpadMap(x),dpadMap(-y))
     if (switchCode != lastCode):
         print("NewCode")
         lastCode = switchCode
         codeStartTime = readTime
     else:
         if (readTime > (codeStartTime + modeDelay)):
             print("longPress")
Example #9
0
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:
    # Buttons are grounded when pressed (.value = False).
    for i, button in enumerate(buttons):
        gamepad_button_num = gamepad_buttons[i]
        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='')

    # Convert range[0, 65535] to -127 to 127
    gp.move_joysticks(x=range_map(ax.value, 0, 65535, -127, 127),
                      y=range_map(ay.value, 0, 65535, -127, 127))
    print(" x", ax.value, "y", ay.value)
Example #10
0
def get_wheel_position(device: AnalogIn) -> int:
    """Returns the current Wheel position, value between -127 and 127

    Args:
        device (AnalogIn): Analog device representing the wheel

    Returns:
        int: Scaled wheel value, between -127 and 127
    """
    return scale_voltage(device.value, min_voltage=3520, max_voltage=61712)


# calibrate_analog_input(steering_wheel)
while True:
    for idx, button in buttons.items():
        if not button["device"].value and button["pressed"]:
            button["pressed"] = False
            print(f"Button {idx} Released!")
            gp.release_buttons(idx)

        if button["device"].value and not button["pressed"]:
            button["pressed"] = True
            print(f"Button {idx} pressed!")
            gp.press_buttons(idx)

    gear_position = get_gear_position(gear_lever)
    wheel_position = get_wheel_position(steering_wheel)

    gp.move_joysticks(x=wheel_position, y=gear_position)