code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """CircuitPython Essentials HID Mouse example"""
 6  import time
 7  import analogio
 8  import board
 9  import digitalio
10  import usb_hid
11  from adafruit_hid.mouse import Mouse
12  
13  mouse = Mouse(usb_hid.devices)
14  
15  x_axis = analogio.AnalogIn(board.A0)
16  y_axis = analogio.AnalogIn(board.A1)
17  select = digitalio.DigitalInOut(board.A2)
18  select.direction = digitalio.Direction.INPUT
19  select.pull = digitalio.Pull.UP
20  
21  pot_min = 0.00
22  pot_max = 3.29
23  step = (pot_max - pot_min) / 20.0
24  
25  
26  def get_voltage(pin):
27      return (pin.value * 3.3) / 65536
28  
29  
30  def steps(axis):
31      """ Maps the potentiometer voltage range to 0-20 """
32      return round((axis - pot_min) / step)
33  
34  
35  while True:
36      x = get_voltage(x_axis)
37      y = get_voltage(y_axis)
38  
39      if select.value is False:
40          mouse.click(Mouse.LEFT_BUTTON)
41          time.sleep(0.2)  # Debounce delay
42  
43      if steps(x) > 11.0:
44          # print(steps(x))
45          mouse.move(x=1)
46      if steps(x) < 9.0:
47          # print(steps(x))
48          mouse.move(x=-1)
49  
50      if steps(x) > 19.0:
51          # print(steps(x))
52          mouse.move(x=8)
53      if steps(x) < 1.0:
54          # print(steps(x))
55          mouse.move(x=-8)
56  
57      if steps(y) > 11.0:
58          # print(steps(y))
59          mouse.move(y=-1)
60      if steps(y) < 9.0:
61          # print(steps(y))
62          mouse.move(y=1)
63  
64      if steps(y) > 19.0:
65          # print(steps(y))
66          mouse.move(y=-8)
67      if steps(y) < 1.0:
68          # print(steps(y))
69          mouse.move(y=8)