Skip to content

Instantly share code, notes, and snippets.

@jedgarpark
Last active May 25, 2021 20:42
Show Gist options
  • Save jedgarpark/bf4457cf07ba69d5a687df395f43d667 to your computer and use it in GitHub Desktop.
Save jedgarpark/bf4457cf07ba69d5a687df395f43d667 to your computer and use it in GitHub Desktop.
import time
import board
import rotaryio
import touchio
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
import neopixel
# ------- Set up the inputs ------ #
button = digitalio.DigitalInOut(board.SWITCH)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.DOWN
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
touch = touchio.TouchIn(board.TOUCH)
button_state = None
last_position = encoder.position
# ------- Set up NeoPixel ------ #
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1.0)
# ------- Set up HID ------ #
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
# cc = ConsumerControl(usb_hid.devices)
# ------- Hue/Sat/Val function to convert to RGB ------ #
def hsv2rgb(h, s, v):
"""
Convert H,S,V in 0-255,0-255,0-255 format
to R,G,B in 0-255,0-255,0-255 format
Converts an integer HSV (value range from 0 to 255) to an RGB tuple
"""
if s == 0: return v, v, v
h = h/255
s = s/255
v = v/255
i = int(h * 6.0) # XXX assume int() truncates!
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0: r,g,b = v,t,p
if i == 1: r,g,b = q,v,p
if i == 2: r,g,b = p,v,t
if i == 3: r,g,b = p,q,v
if i == 4: r,g,b = t,p,v
if i == 5: r,g,b = v,p,q
return int(r*255), int(g*255), int(b*255)
hue = 180
sat = 255
val = 255
color = hsv2rgb(hue, sat, val)
pixel[0] = color
pixel.show()
while True:
# ------- Check encoder for change ------ #
current_position = encoder.position
position_change = current_position - last_position
# ------- Clockwise send . ------ #
if position_change > 0:
for _ in range(position_change):
#cc.send(ConsumerControlCode.VOLUME_INCREMENT)
keyboard.send(Keycode.PERIOD)
hue = current_position*10
pixel[0] = hsv2rgb(hue, sat, val)
pixel.show()
print(current_position)
# ------- CCW send , ------ #
elif position_change < 0:
for _ in range(-position_change):
#cc.send(ConsumerControlCode.VOLUME_DECREMENT)
keyboard.send(Keycode.COMMA)
hue = current_position*10
pixel[0] = hsv2rgb(hue, sat, val)
pixel.show()
print(current_position)
last_position = current_position
# ------- Button send c ------ #
if not button.value and button_state is None:
button_state = "pressed"
if button.value and button_state == "pressed":
print("Button pressed.")
keyboard.send(Keycode.C)
#cc.send(ConsumerControlCode.PLAY_PAUSE)
button_state = None
# ------- Touch send spacebar play/pause ------ #
if touch.value:
while touch.value: # Wait for release
time.sleep(0.1)
keyboard.send(Keycode.SPACEBAR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment