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