# 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)
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)
    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)
Beispiel #3
0
game_thresh=1
middleLimit = 10

gp.reset_all()
while True:
#    baseVal = get_voltage(base)
    horVal = get_voltage(hor) - baseVal
    vertVal = get_voltage(vert) - baseVal
    horAvg.addValue(horVal)
    vertAvg.addValue(vertVal)

    game_x = range_map(horDirection * horAvg.average(), lowVal, highVal, 0, 255)
    game_y = range_map(vertDirection * vertAvg.average(), lowVal, highVal, 0, 255)

    if (abs(game_x - center) < middleLimit):
        game_x = center
    if (abs(game_y - center) < middleLimit):
        game_y = center

    if (abs(last_game_x - game_x) > game_thresh):
        last_game_x = game_x
#        print(game_x, game_y)
        gp.move_joysticks(x=game_x)
    if (abs(last_game_y - game_y) > game_thresh):
        last_game_y = game_y
#        print(game_x, game_y)
        gp.move_joysticks(y=game_y)
#    print((horAvg.average(),vertAvg.average(),))

    print((game_x, game_y,))
    time.sleep(0.025)
#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


#initial x joystick direction
xDir = 0
#initial y joystick direction
yDir = 0

while True:
    #mapping function to find ranges
    #specifically for the josysticks with their min and max ranges
    gp.move_joysticks(x=range_map(xPin.value, 0, 65535, -127, 127),
                      y=range_map(yPin.value, 0, 65535, -127, 127))

    #if x joystick is in the forward position, set x direction to 1
    if xPin.value > 0 and xPin.value < 500:
        #print('x is up')
        xDir = 1

    #if x joystick is in the back position, set x direction to 2
    elif xPin.value > 30000 and xPin.value < 35000:
        #print('x is down')
        xDir = 2

    #else x joystick is up
    else:
        xDir = 0
Beispiel #5
0
        if (debug): print("ch1 was empty")
        ch1in = 1500

    if len(ch2) > 0:
        ch2in = ch2[0]
        if (debug): print("ch2 was ", ch2in)
    else:
        if (debug): print("ch2 was empty")
        ch2in = 1500

#    and xin > allowed_min and xin < allowed_max and yin > allowed_min and yin < allowed_max:
    try:
        if ch1in < ch1min: ch1min = ch1in
        if ch1in > ch1max and ch1in < 2200: ch1max = ch1in
        if ch2in < ch2min: ch2min = ch2in
        if ch2in > ch2max and ch2in < 2200: ch2max = ch2in
        x = range_map(ch1in, ch1min, ch1max, -127, 127)
        y = range_map(ch2in, ch2min, ch2max, -127, 127) * -1
        gp.move_joysticks(x, y)
        if (debug):
            print("ch1min", ch1min, "ch1in", ch1in, "ch1max", ch1max, "ch2min",
                  ch2min, "ch2in", ch2in, "ch2max", ch2max, "joy_x", x,
                  "joy_y", y)
        print("x", x, ", y", y)
        sleep(0.001)
    except Exception as e:
        if (debug): print("ax", ch1in, "ay", ch2in, e)
        pass

    # if(debug): sleep(0.5)
Beispiel #6
0
    else:
        if (debug): print("ch3 was empty")
        ch3in = 1500

#    and xin > allowed_min and xin < allowed_max and yin > allowed_min and yin < allowed_max:
    try:
        if ch1in < ch1min: ch1min = ch1in
        if ch1in > ch1max and ch1in < 2200: ch1max = ch1in
        if ch2in < ch2min: ch2min = ch2in
        if ch2in > ch2max and ch2in < 2200: ch2max = ch2in
        if ch3in < ch3min: ch3min = ch3in
        if ch3in > ch3max and ch3in < 2200: ch3max = ch3in

        x = range_map(ch1in, ch1min, ch1max, -127, 127)
        y = range_map(ch2in, ch2min, ch2max, -127, 127) * -1
        z = range_map(ch3in, ch3min, ch3max, -127, 127) * -1

        gp.move_joysticks(x, y, z)
        if (debug):
            print("ch1min", ch1min, "ch1in", ch1in, "ch1max", ch1max, "ch2min",
                  ch2min, "ch2in", ch2in, "ch2max", ch2max, "ch3min", ch3min,
                  "ch3in", ch3in, "ch3max", ch3max, "joy_x", x, "joy_y", y,
                  "joy_z", z)
        if (not shush): print("x", x, ", y", y, ", z", z)
        sleep(0.005)
    except Exception as e:
        if (debug): print("ax", ch1in, "ay", ch2in, "az", ch3in, e)
        pass

    # if(debug): sleep(0.5)
Beispiel #7
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)
# 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)
Beispiel #9
0
    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)))

Beispiel #10
0
             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")
             if (switchCode == modeSwitchCode):
                 numOfModes = len(settings.modes)
                 modeNum = (modeNum + 1) % numOfModes
                 currentMode = settings.modes[modeNum]
                 modeColor=hex2rgb(currentMode.color)
                 dot[0]=modeColor
             codeStartTime = readTime
 else:
    try:
        if ch1in < ch1min and ch1in > allowed_min: ch1min = ch1in
        if ch1in > ch1max and ch1in < allowed_max: ch1max = ch1in
        if ch2in < ch2min and ch2in > allowed_min: ch2min = ch2in
        if ch2in > ch2max and ch2in < allowed_max: ch2max = ch2in
        if ch3in < ch3min and ch3in > allowed_min: ch3min = ch3in
        if ch3in > ch3max and ch3in < allowed_max: ch3max = ch3in
        if ch4in < ch4min and ch4in > allowed_min: ch4min = ch4in
        if ch4in > ch4max and ch4in < 480: ch4max = ch4in

        x = range_map(ch1in, ch1min, ch1max, -127, 127)
        y = range_map(ch2in, ch2min, ch2max, -127, 127) * -1
        z = range_map(ch3in, ch3min, ch3max, -127, 127) * -1
        r = range_map(ch4in, ch4min, ch4max, -127, 127) * -1

        gamepad.move_joysticks(x, y, z, r)
        if (debug):
            print("ch1 {:04d}/{:04d}/{:04d} ".format(ch1min, ch1in, ch1max),
                  end=" ")
        if (debug):
            print("ch2 {:04d}/{:04d}/{:04d} ".format(ch2min, ch2in, ch2max),
                  end=" ")
        if (debug):
            print("ch3 {:04d}/{:04d}/{:04d} ".format(ch3min, ch3in, ch3max),
                  end=" ")
        if (debug):
            print("ch4 {:04d}/{:04d}/{:04d} ".format(ch4min, ch4in, ch4max),
                  end=" ")
        if (debug):
            print("x[{:>4}] y[{:>4}] z[{:>4}] r[{:>4}] ".format(x, y, z, r))
Beispiel #12
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)
Beispiel #13
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)