Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created August 9, 2022 14:03
Show Gist options
  • Save benevpi/d9b41427fc3b5aa83cc671806a6bc9b7 to your computer and use it in GitHub Desktop.
Save benevpi/d9b41427fc3b5aa83cc671806a6bc9b7 to your computer and use it in GitHub Desktop.
surfing controller
import board
#from hx711.hx711_pio import HX711_PIO
from hx711.hx711_gpio import HX711_GPIO
import time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from digitalio import DigitalInOut, Direction, Pull
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
#firing key
fire = DigitalInOut(board.GP16)
fire.direction = Direction.INPUT
fire.pull = Pull.UP
print("initialising")
pin_data = board.GP2
pin_clk = board.GP3
'''
hx1 = HX711_PIO(board.GP2, board.GP3, tare=True)
hx2 = HX711_PIO(board.GP6, board.GP7, tare=True)
hx3 = HX711_PIO(board.GP10, board.GP11, tare=True)
hx4 = HX711_PIO(board.GP21, board.GP20, tare=True)
'''
hx1 = HX711_GPIO(DigitalInOut(board.GP2), DigitalInOut(board.GP3), tare=True)
hx2 = HX711_GPIO(DigitalInOut(board.GP6), DigitalInOut(board.GP7), tare=True)
hx3 = HX711_GPIO(DigitalInOut(board.GP10), DigitalInOut(board.GP11), tare=True)
hx4 = HX711_GPIO(DigitalInOut(board.GP21), DigitalInOut(board.GP20), tare=True)
sensors = [hx1, hx2, hx3, hx4]
list1 = []
list2 = []
list3 = []
list4 = []
lists = [list1, list2, list3, list4]
list_total = []
SIZE = 10
THRESHOLD = 200000
DEBOUNCE = 0.4
last_jump_time = 0
for i in range(SIZE):
list1.append(hx1.read_raw())
list2.append(hx2.read_raw())
list3.append(hx3.read_raw())
list4.append(hx4.read_raw())
averages = [0]*4
for i in range(SIZE):
averages[0] += list1[i]
averages[1] += list2[i]
averages[2] += list3[i]
averages[3] += list4[i]
for i in range(4):
averages[i] = int(averages[i]/SIZE)
print("starting")
#left = sensors 0 and 2, right 1 and 3?
left=[0,2]
right=[1,3]
loop_time = 0
right_pressed = False
left_pressed = True
firing = False
while True:
loop_time = time.monotonic()
left_total = 0
right_total=0
#note -- this loop is kinda slow so probably no b ounc
if not fire.value:
if not firing:
keyboard.press(Keycode.SPACE)
firing = True
else:
if firing:
keyboard.release(Keycode.SPACE)
firing = False
for number, sensor in enumerate(sensors):
value = int(sensor.read_raw())
if number in left:
left_total += value-averages[number]
else:
right_total += value-averages[number]
if(left_total < right_total*1.5):
if(left_pressed):
keyboard.release(Keycode.LEFT_ARROW)
left_pressed = False
if not right_pressed:
keyboard.press(Keycode.RIGHT_ARROW)
right_pressed = True
print("right")
elif (right_total <left_total*1.5):
if(right_pressed):
keyboard.release(Keycode.RIGHT_ARROW)
right_pressed = False
if not left_pressed:
keyboard.press(Keycode.LEFT_ARROW)
print("lefT")
left_pressed = True
else:
if(right_pressed):
keyboard.release(Keycode.RIGHT_ARROW)
right_pressed = False
if(left_pressed):
keyboard.release(Keycode.LEFT_ARROW)
left_pressed = False
#print(time.monotonic()-loop_time)
#print(left_total - right_total)
#time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment