code.py
  1  # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  import math
  7  import board
  8  import neopixel
  9  import usb_hid
 10  from adafruit_hid.keyboard import Keyboard
 11  from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
 12  from adafruit_hid.keycode import Keycode  # pylint: disable=unused-import
 13  from digitalio import DigitalInOut, Pull
 14  
 15  # create the pixel and turn it off
 16  pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
 17  pixel.fill(0x0)
 18  
 19  time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
 20  keyboard = Keyboard(usb_hid.devices)
 21  keyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)
 22  
 23  # create the switch, add a pullup, start it with not being pressed
 24  button = DigitalInOut(board.SWITCH)
 25  button.switch_to_input(pull=Pull.DOWN)
 26  button_state = False
 27  
 28  ssid = ""
 29  password = ""
 30  
 31  account = ""
 32  account_password = ""
 33  
 34  DELAY = 0.05
 35  key_output = (
 36      {"keys": None, "delay": 2},  # Wifi setup
 37      {"keys": Keycode.TAB, "delay": DELAY},
 38      {"keys": Keycode.TAB, "delay": DELAY},
 39      {"keys": Keycode.TAB, "delay": DELAY},
 40      {"keys": Keycode.TAB, "delay": DELAY},
 41      {"keys": Keycode.ENTER, "delay": 1},
 42      {"keys": Keycode.TAB, "delay": DELAY},
 43      {"keys": Keycode.TAB, "delay": DELAY},
 44      {"keys": Keycode.TAB, "delay": DELAY},
 45      {"keys": Keycode.ENTER, "delay": 0.5},
 46      {"keys": ssid, "delay": 0.5},
 47      {"keys": Keycode.TAB, "delay": 0.5},
 48      {"keys": Keycode.DOWN_ARROW, "delay": DELAY},
 49      {"keys": Keycode.DOWN_ARROW, "delay": 0.5},
 50      {"keys": Keycode.TAB, "delay": DELAY},
 51      {"keys": password, "delay": 0.5},
 52      {"keys": Keycode.ENTER, "delay": 9},  # Long pause while connection is established
 53      {"keys": Keycode.TAB, "delay": DELAY},  # Go Through first run setup
 54      {"keys": Keycode.TAB, "delay": DELAY},
 55      {"keys": Keycode.ENTER, "delay": 0.5},
 56      {"keys": Keycode.TAB, "delay": DELAY},
 57      {"keys": Keycode.TAB, "delay": DELAY},
 58      {"keys": Keycode.TAB, "delay": DELAY},
 59      {"keys": Keycode.ENTER, "delay": 0.5},
 60      {"keys": (Keycode.CONTROL, Keycode.ALT, Keycode.E), "delay": DELAY},
 61      {"keys": Keycode.TAB, "delay": DELAY},
 62      {"keys": Keycode.TAB, "delay": DELAY},
 63      {"keys": Keycode.TAB, "delay": DELAY},
 64      {"keys": Keycode.TAB, "delay": DELAY},
 65      {"keys": Keycode.TAB, "delay": DELAY},  # Part Two: Enrollment
 66      {"keys": Keycode.ENTER, "delay": 4},
 67      {"keys": account, "delay": DELAY},
 68      {"keys": Keycode.ENTER, "delay": 5},
 69      {"keys": account_password, "delay": 0.5},
 70      {"keys": Keycode.ENTER, "delay": 8},  # Long pause while device is enrolled
 71      {"keys": Keycode.ENTER, "delay": DELAY},
 72  )
 73  
 74  
 75  def make_keystrokes(keys, delay):
 76      start = time.monotonic()
 77      if isinstance(keys, str):  # If it's a string...
 78          keyboard_layout.write(keys)  # ...Print the string
 79          print(keys)
 80      elif isinstance(keys, int):  # If its a single key
 81          keyboard.press(keys)  # "Press"...
 82          pixel.fill((math.sin(keys) * 255, math.cos(keys) * 255, math.tan(keys) * 255))
 83          keyboard.release_all()  # ..."Release"!
 84          print(keys)
 85      elif isinstance(keys, (list, tuple)):  # If its multiple keys
 86          keyboard.press(*keys)  # "Press"...
 87          for key in keys:
 88              pixel.fill((math.sin(key) * 255, math.cos(key) * 255, math.tan(key) * 255))
 89          keyboard.release_all()  # ..."Release"!
 90          print(*keys)
 91      while time.monotonic() < start + delay:
 92          if button.value:
 93              print("Stopped")
 94              return
 95  
 96  
 97  activated = False
 98  while True:
 99      if button.value:
100          activated = True
101          time.sleep(0.5)
102          pixel.fill(0x000000)
103      if activated:
104          print("Starting")
105          for k in key_output:
106              if button.value:
107                  activated = False
108                  time.sleep(0.2)
109                  break
110              make_keystrokes(k["keys"], k["delay"])
111          print("Done!")
112          pixel.fill(0xFFFFFF)
113          time.sleep(0.1)
114          pixel.fill(0x000000)
115          time.sleep(0.1)
116          pixel.fill(0xFFFFFF)
117          time.sleep(0.1)
118          pixel.fill(0x000000)
119          time.sleep(0.1)
120          pixel.fill(0xFFFFFF)
121          break