code.py
1 # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """Rotary Trinkey NeoPixel color picker example""" 6 import rotaryio 7 import digitalio 8 import board 9 from rainbowio import colorwheel 10 import neopixel 11 12 print("Rotary Trinkey color picker example") 13 14 pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5) 15 encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB) 16 switch = digitalio.DigitalInOut(board.SWITCH) 17 switch.switch_to_input(pull=digitalio.Pull.DOWN) 18 19 last_position = -1 20 color = 0 # start at red 21 while True: 22 position = encoder.position 23 if last_position is None or position != last_position: 24 print(position) 25 if not switch.value: 26 # change the color 27 if position > last_position: # increase 28 color += 1 29 else: 30 color -= 1 31 color = (color + 256) % 256 # wrap around to 0-256 32 pixel.fill(colorwheel(color)) 33 else: 34 # change the brightness 35 if position > last_position: # increase 36 pixel.brightness = min(1.0, pixel.brightness + 0.1) 37 else: 38 pixel.brightness = max(0, pixel.brightness - 0.1) 39 last_position = position