Adafruit_Learning_System_Gu.../CircuitPython_Essentials/CircuitPython_HID_Mouse.py
2018-03-25 18:39:23 -04:00

64 lines
1.4 KiB
Python

import time
from analogio import AnalogIn
import board
import digitalio
from adafruit_hid.mouse import Mouse
from adafruit_hid.keyboard import Keyboard
mouse = Mouse()
keyboard = Keyboard()
x_axis = AnalogIn(board.A0)
y_axis = AnalogIn(board.A1)
select = digitalio.DigitalInOut(board.A2)
select.direction = digitalio.Direction.INPUT
select.pull = digitalio.Pull.UP
pot_max = 3.29
pot_min = 0.00
step = (pot_max - pot_min) / 20.0
def get_voltage(pin):
return (pin.value * 3.3) / 65536
def steps(axis): # Maps the potentiometer voltage range from 0-20 in whole numbers
return round((axis - pot_min) / step)
while True:
x = get_voltage(x_axis)
y = get_voltage(y_axis)
if select.value is False:
mouse.click(Mouse.LEFT_BUTTON)
time.sleep(0.2) # Debounce delay
if steps(x) > 11.0:
# print(steps(x))
mouse.move(x=1)
if steps(x) < 9.0:
# print(steps(x))
mouse.move(x=-1)
if steps(x) > 19.0:
# print(steps(x))
mouse.move(x=8)
if steps(x) < 1.0:
# print(steps(x))
mouse.move(x=-8)
if steps(y) > 11.0:
# print(steps(y))
mouse.move(y=-1)
if steps(y) < 9.0:
# print(steps(y))
mouse.move(y=1)
if steps(y) > 19.0:
# print(steps(y))
mouse.move(y=-7)
if steps(y) < 1.0:
# print(steps(y))
mouse.move(y=7)