|
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +from digitalio import DigitalInOut, Direction, Pull |
| 7 | +from adafruit_debouncer import Debouncer |
| 8 | +import usb_hid |
| 9 | +from adafruit_hid.keyboard import Keyboard |
| 10 | +from adafruit_hid.keycode import Keycode |
| 11 | +from adafruit_hid.mouse import Mouse |
| 12 | +from rainbowio import colorwheel |
| 13 | +from adafruit_seesaw.seesaw import Seesaw |
| 14 | +from adafruit_seesaw.analoginput import AnalogInput |
| 15 | +from adafruit_seesaw import neopixel |
| 16 | + |
| 17 | +# LED for button |
| 18 | +led = DigitalInOut(board.A0) |
| 19 | +led.direction = Direction.OUTPUT |
| 20 | + |
| 21 | +# button setup |
| 22 | +key_pin = DigitalInOut(board.A1) |
| 23 | +key_pin.direction = Direction.INPUT |
| 24 | +key_pin.pull = Pull.UP |
| 25 | +switch = Debouncer(key_pin) |
| 26 | + |
| 27 | +# button to toot in the game (can be any key or a mouse click) |
| 28 | +key_pressed = Keycode.SPACE |
| 29 | + |
| 30 | +# keyboard object |
| 31 | +time.sleep(1) # Sleep for a bit to avoid a race condition on some systems |
| 32 | +keyboard = Keyboard(usb_hid.devices) |
| 33 | + |
| 34 | +# mouse object |
| 35 | +mouse = Mouse(usb_hid.devices) |
| 36 | + |
| 37 | +# NeoSlider Setup |
| 38 | +neoslider = Seesaw(board.STEMMA_I2C(), 0x30) |
| 39 | +pot = AnalogInput(neoslider, 18) |
| 40 | +pixels = neopixel.NeoPixel(neoslider, 14, 4, pixel_order=neopixel.GRB) |
| 41 | + |
| 42 | +# scale pot value to rainbow colors |
| 43 | +def potentiometer_to_color(value): |
| 44 | + return value / 1023 * 255 |
| 45 | + |
| 46 | +# last value of the potentiometer |
| 47 | +last_value = 0 |
| 48 | +# difference between last_value and current value |
| 49 | +diff = 0 |
| 50 | +# mouse movement sensitivity |
| 51 | +# increase value for faster movement, decrease for slower |
| 52 | +mouse_sensitivity = 8 |
| 53 | + |
| 54 | +while True: |
| 55 | + # read the slide pot's value (range of 0 to 1023) |
| 56 | + y = pot.value |
| 57 | + # debouncer update |
| 58 | + switch.update() |
| 59 | + # colorwheel for neoslider neopixels |
| 60 | + pixels.fill(colorwheel(potentiometer_to_color(pot.value))) |
| 61 | + # if button released |
| 62 | + if switch.rose: |
| 63 | + # turn off button LED |
| 64 | + led.value = False |
| 65 | + # release all keyboard keys |
| 66 | + keyboard.release_all() |
| 67 | + # if button pressed |
| 68 | + if switch.fell: |
| 69 | + # turn on button LED |
| 70 | + led.value = True |
| 71 | + # press the space bar |
| 72 | + keyboard.press(key_pressed) |
| 73 | + # if the current value of the pot is different from the last value |
| 74 | + if y != last_value: |
| 75 | + # if the last value was bigger |
| 76 | + if last_value > y: |
| 77 | + # find the difference |
| 78 | + diff = abs(last_value - y) |
| 79 | + # divide by 10 |
| 80 | + diff = diff / 10 |
| 81 | + # move cursor negative for range of difference |
| 82 | + for i in range(diff): |
| 83 | + mouse.move(y=-(mouse_sensitivity)) |
| 84 | + # if last value was smaller |
| 85 | + if last_value < y: |
| 86 | + # find the difference |
| 87 | + diff = abs(last_value - y) |
| 88 | + # divide by 10 |
| 89 | + diff = diff / 10 |
| 90 | + # move cursor positive for range of difference |
| 91 | + for i in range(diff): |
| 92 | + mouse.move(y=mouse_sensitivity) |
| 93 | + # reset last value |
| 94 | + last_value = y |
| 95 | + # if value is 0 |
| 96 | + if y == 0: |
| 97 | + # slight movement |
| 98 | + mouse.move(y=-2) |
| 99 | + # if value is 1023 |
| 100 | + if y == 1023: |
| 101 | + # slight movement |
| 102 | + mouse.move(y=2) |
0 commit comments