|
| 1 | +"""Keypad and rotary encoder example for Adafruit MacroPad""" |
| 2 | +import board |
| 3 | +import digitalio |
| 4 | +import rotaryio |
| 5 | +import neopixel |
| 6 | +import keypad |
| 7 | + |
| 8 | +key_pins = (board.KEY1, board.KEY2, board.KEY3, board.KEY4, board.KEY5, board.KEY6, |
| 9 | + board.KEY7, board.KEY8, board.KEY9, board.KEY10, board.KEY11, board.KEY12) |
| 10 | +keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True) |
| 11 | + |
| 12 | +encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB) |
| 13 | +button = digitalio.DigitalInOut(board.BUTTON) |
| 14 | +button.switch_to_input(pull=digitalio.Pull.UP) |
| 15 | + |
| 16 | +pixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2) |
| 17 | + |
| 18 | + |
| 19 | +def colorwheel(color): |
| 20 | + if color < 0 or color > 255: |
| 21 | + return 0, 0, 0 |
| 22 | + if color < 85: |
| 23 | + return 255 - color * 3, color * 3, 0 |
| 24 | + if color < 170: |
| 25 | + color -= 85 |
| 26 | + return 0, 255 - color * 3, color * 3 |
| 27 | + color -= 170 |
| 28 | + return color * 3, 0, 255 - color * 3 |
| 29 | + |
| 30 | + |
| 31 | +last_position = None |
| 32 | +while True: |
| 33 | + if not button.value: |
| 34 | + pixels.brightness = 1.0 |
| 35 | + else: |
| 36 | + pixels.brightness = 0.2 |
| 37 | + |
| 38 | + position = encoder.position |
| 39 | + if last_position is None or position != last_position: |
| 40 | + print("Rotary:", position) |
| 41 | + last_position = position |
| 42 | + |
| 43 | + color_value = (position * 2) % 255 |
| 44 | + |
| 45 | + event = keys.events.get() |
| 46 | + if event: |
| 47 | + print(event) |
| 48 | + if event.pressed: |
| 49 | + pixels[event.key_number] = colorwheel(color_value) |
| 50 | + else: |
| 51 | + pixels[event.key_number] = 0 |
0 commit comments