Skip to content

Commit 538745f

Browse files
committed
Adding rotary QT NeoPixel example.
1 parent 5812855 commit 538745f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

examples/seesaw_rotary_neopixel.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""I2C rotary encoder NeoPixel color picker and brightness setting example."""
2+
import board
3+
from adafruit_seesaw import seesaw, neopixel, rotaryio, digitalio
4+
try:
5+
import _pixelbuf
6+
except ImportError:
7+
import adafruit_pypixelbuf as _pixelbuf
8+
9+
# For use with the STEMMA connector on QT Py RP2040
10+
# import busio
11+
# i2c = busio.I2C(board.SCL1, board.SDA1)
12+
# seesaw = seesaw.Seesaw(i2c, 0x36)
13+
14+
seesaw = seesaw.Seesaw(board.I2C(), 0x36)
15+
16+
encoder = rotaryio.IncrementalEncoder(seesaw)
17+
switch = digitalio.DigitalIO(seesaw, 24)
18+
19+
pixel = neopixel.NeoPixel(seesaw, 6, 1)
20+
pixel.brightness = 0.5
21+
22+
last_position = -1
23+
color = 0 # start at red
24+
25+
while True:
26+
position = encoder.position
27+
28+
if position != last_position:
29+
print(position)
30+
31+
if switch.value:
32+
# Change the LED color.
33+
if position > last_position: # Advance forward through the colorwheel.
34+
color += 1
35+
else:
36+
color -= 1 # Advance backward through the colorwheel.
37+
color = (color + 256) % 256 # wrap around to 0-256
38+
pixel.fill(_pixelbuf.colorwheel(color))
39+
40+
else: # If the button is pressed...
41+
# ...change the brightness.
42+
if position > last_position: # Increase the brightness.
43+
pixel.brightness = min(1.0, pixel.brightness + 0.1)
44+
else: # Decrease the brightness.
45+
pixel.brightness = max(0, pixel.brightness - 0.1)
46+
47+
last_position = position

0 commit comments

Comments
 (0)