Skip to content

Commit 6755126

Browse files
committed
multimedia dial update
Multimeda dial update
1 parent cf6b7cf commit 6755126

1 file changed

Lines changed: 49 additions & 15 deletions

File tree

Crank_USB_HID/code.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
"""
2-
A CircuitPython 'crank' USB HID demo
3-
Uses a ItsyBitsy M0 + Rotary Encoder -> USB HID keyboard
2+
A CircuitPython 'multimedia' dial demo
3+
Uses a ItsyBitsy M0 + Rotary Encoder -> HID keyboard out with neopixel ring
44
"""
55

6-
import board
7-
from digitalio import DigitalInOut, Direction, Pull
6+
import time
7+
from digitalio import *
8+
from board import *
89
from adafruit_hid.keyboard import Keyboard
910
from adafruit_hid.keycode import Keycode
1011
from adafruit_hid.consumer_control import ConsumerControl
1112
from adafruit_hid.consumer_control_code import ConsumerControlCode
13+
import neopixel
14+
15+
DOT_COLOR = 0xFF0000 # set to your favorite webhex color
16+
PRESSED_DOT_COLOR = 0x008080 # set to your second-favorite color
17+
LIT_TIMEOUT = 15 # after n seconds, turn off ring
18+
19+
# NeoPixel LED ring on pin D1
20+
# Ring code will auto-adjust if not 16 so change to any value!
21+
ring = neopixel.NeoPixel(D5, 16, brightness=0.2)
22+
dot_location = 0 # what dot is currently lit
1223

1324
# Encoder button is a digital input with pullup on D9
14-
button = DigitalInOut(board.D9)
25+
button = DigitalInOut(D9)
1526
button.direction = Direction.INPUT
1627
button.pull = Pull.UP
1728

18-
# Rotary encoder inputs with pullup on D10 & D11 on ItsyBitsy
19-
rot_a = DigitalInOut(board.D10)
29+
# Rotary encoder inputs with pullup on D10 & D11
30+
rot_a = DigitalInOut(D10)
2031
rot_a.direction = Direction.INPUT
2132
rot_a.pull = Pull.UP
22-
rot_b = DigitalInOut(board.D11)
33+
rot_b = DigitalInOut(D11)
2334
rot_b.direction = Direction.INPUT
2435
rot_b.pull = Pull.UP
2536

2637
# Used to do HID output, see below
2738
kbd = Keyboard()
2839

40+
# time keeper, so we know when to turn off the LED
41+
timestamp = time.monotonic()
42+
2943
######################### MAIN LOOP ##############################
3044

3145
# the counter counts up and down, it can roll over! 16-bit value
@@ -97,21 +111,41 @@
97111

98112
# Check if rotary encoder went up
99113
if encoder_direction == 1:
100-
ConsumerControl().send(ConsumerControlCode.VOLUME_DECREMENT) #Volume Down
114+
ConsumerControl().send(ConsumerControlCode.VOLUME_DECREMENT) #Turn Down Volume
101115
# kbd.press(Keycode.LEFT_ARROW)
102116
# kbd.release_all()
103-
104117
# Check if rotary encoder went down
105118
if encoder_direction == -1:
106-
ConsumerControl().send(ConsumerControlCode.VOLUME_INCREMENT) #Volume Up
119+
ConsumerControl().send(ConsumerControlCode.VOLUME_INCREMENT) #Turn Up Volume
107120
# kbd.press(Keycode.RIGHT_ARROW)
108121
# kbd.release_all()
109-
110122
# Button was 'just pressed'
111123
if (not button.value) and last_button:
112124
print("Button pressed!")
113-
kbd.press(Keycode.SPACE) #Keycode for space bar
125+
kbd.press(Keycode.SPACE) #Keycode for spacebar
114126
kbd.release_all()
115-
127+
ring[dot_location] = PRESSED_DOT_COLOR # show it was pressed on ring
128+
timestamp = time.monotonic() # something happened!
116129
elif button.value and (not last_button):
117-
print("Button Released!")
130+
print("Button Released!")
131+
# kbd.press(Keycode.SHIFT, Keycode.SIX)
132+
# kbd.release_all()
133+
ring[dot_location] = DOT_COLOR # show it was released on ring
134+
timestamp = time.monotonic() # something happened!
135+
last_button = button.value
136+
137+
if encoder_direction != 0:
138+
timestamp = time.monotonic() # something happened!
139+
# spin neopixel LED around!
140+
previous_location = dot_location
141+
dot_location += encoder_direction # move dot in the direction
142+
dot_location += len(ring) # in case we moved negative, wrap around
143+
dot_location %= len(ring)
144+
if button.value:
145+
ring[dot_location] = DOT_COLOR # turn on new dot
146+
else:
147+
ring[dot_location] = PRESSED_DOT_COLOR # turn on new dot
148+
ring[previous_location] = 0 # turn off previous dot
149+
150+
if time.monotonic() > timestamp + LIT_TIMEOUT:
151+
ring[dot_location] = 0 # turn off ring light temporarily

0 commit comments

Comments
 (0)