code.py
 1  # SPDX-FileCopyrightText: 2018 John Edgar Park for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  # Gemma Radio Tuning Knob
 6  # for fine tuning Software Defined Radio CubicSDR software
 7  # 10k pot hooked to 3v, A2, and D2 acting as GND
 8  
 9  import time
10  
11  import board
12  import usb_hid
13  from adafruit_hid.keyboard import Keyboard
14  from adafruit_hid.keycode import Keycode
15  from analogio import AnalogIn
16  from digitalio import DigitalInOut, Direction
17  
18  d2_ground = DigitalInOut(board.D2)
19  d2_ground.direction = Direction.OUTPUT
20  d2_ground.value = False
21  analog2in = AnalogIn(board.A2)
22  
23  led = DigitalInOut(board.D13)
24  led.direction = Direction.OUTPUT
25  
26  pot_max = 3.29
27  pot_min = 0.00
28  step = (pot_max - pot_min) / 10.0
29  last_knob = 0
30  
31  
32  def steps(x):
33      return round((x - pot_min) / step)
34  
35  
36  def getVoltage(pin):
37      return (pin.value * 3.3) / 65536
38  
39  
40  def spamKey(code):
41      knobkeys = [Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET,
42                  Keycode.RIGHT_BRACKET, Keycode.RIGHT_BRACKET,
43                  Keycode.RIGHT_BRACKET, Keycode.SPACE,
44                  Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET,
45                  Keycode.LEFT_BRACKET, Keycode.LEFT_BRACKET,
46                  Keycode.LEFT_BRACKET]
47      spamRate = [0.01, 0.05, 0.125, 0.25, 0.5,
48                  0.5, 0.5, 0.25, 0.125, 0.05, 0.01]
49      kbd = Keyboard(usb_hid.devices)
50      kbd.press(knobkeys[code])  # which keycode is entered
51      kbd.release_all()
52      time.sleep(spamRate[code])  # how fast the key is spammed
53  
54  
55  while True:
56      knob = (getVoltage(analog2in))
57      if steps(knob) == 5:  # the center position is active
58          led.value = True
59      elif steps(knob) != 5:
60          led.value = False
61          spamKey(steps(knob))