Skip to content

Instantly share code, notes, and snippets.

@caitlinsdad
Created February 24, 2018 15:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save caitlinsdad/5d2f6521cc2e97b170f84a333105c9a5 to your computer and use it in GitHub Desktop.
Blade Runner Memory Maker Controller
# started with code from Adafruit CircuitPlayground demo - Keyboard emu
# using servo and HID mouse emulation - Adafruit Circuit Python
from digitalio import DigitalInOut, Direction, Pull
import board
import time
import simpleio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.mouse import Mouse
# A simple neat keyboard demo in circuitpython
# The button pins we'll use, each will have an internal pullup
buttonpins = [board.A0, board.A1, board.A4, board.A5, board.A6]
# our array of button objects
buttons = []
# The keycode sent for each button, will be paired with a control key
buttonkeys = ["Up.", "Down.", "Right.", "Left", "LeftClick"]
controlkey = Keycode.SHIFT
# the keyboard object!
# sleep for a bit to avoid a race condition on some systems
time.sleep(1)
kbd = Keyboard()
m = Mouse()
# we're americans :)
layout = KeyboardLayoutUS(kbd)
servo = simpleio.Servo(board.A2)
servo2 = simpleio.Servo(board.A3)
# make all pin objects, make them inputs w/pullups
for pin in buttonpins:
button = DigitalInOut(pin)
button.direction = Direction.INPUT
button.pull = Pull.UP
buttons.append(button)
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
leftmpress = 1
print("00 Waiting for button presses")
while True:
# check each button
for button in buttons:
if not button.value: # pressed?
i = buttons.index(button)
# print("Button #%d Pressed" % i)
# turn on the LED
led.value = True
# Printout code for diagnostics and user cue commented out
# while not button.value:
# pass # wait for it to be released!
# type the keycode or string
# k = buttonkeys[i] # get the corresp. keycode/str
# if type(k) is str:
# layout.write(k)
# else:
# kbd.press(controlkey, k) # press...
# kbd.release_all() # release!
if i == 0:
# use modulo math to see if left mouse button toggled on or off
if not leftmpress % 2:
# print("XX 00 move UP")
# Press the left button.
m.press(Mouse.LEFT_BUTTON)
m.move(y=25)
else:
# print("00 00 move UP")
# mouse LEFT
m.move(y=25)
m.release_all()
if i == 1:
if not leftmpress % 2:
# print("XX 00 move DOWN")
# Press the left button.
m.press(Mouse.LEFT_BUTTON)
m.move(y=-25)
else:
# print("00 00 move DOWN")
# mouse LEFT
m.move(y=-25)
m.release_all()
if i == 2:
if not leftmpress % 2:
# print("XX 00 move RIGHT")
# Press the left button.
m.press(Mouse.LEFT_BUTTON)
m.move(x=25)
else:
# print("00 00 move RIGHT")
# mouse LEFT
m.move(x=25)
m.release_all()
if i == 3:
if not leftmpress % 2:
# print("XX 00 move LEFT")
# Press the left button.
m.press(Mouse.LEFT_BUTTON)
m.move(x=-25)
else:
# print("00 00 move LEFT")
# mouse LEFT
m.move(x=-25)
m.release_all()
if i == 4:
leftmpress += 1
# print("LeftMouse value = %d " % leftmpress)
for angle in range(0, 120, 5): # 0-180 deg, 5 deg at a time
servo.angle = angle
time.sleep(0.05)
for angle in range(0, 120, 5): # 0-180 deg, 5 deg at a time
servo2.angle = angle
time.sleep(0.06)
for angle in range(120, 0, -5): # 180-0 deg, 5 deg at a time
servo2.angle = angle
time.sleep(0.07)
for angle in range(120, 0, -5): # 180-0 deg, 5 deg at a time
servo.angle = angle
time.sleep(0.05)
# turn off the LED
led.value = False
# adjust delay if key/mouse repeat too fast
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment