|
| 1 | +# SPDX-FileCopyrightText: 2021 John Park |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# I2C rotary encoder multiple test example. |
| 5 | +# solder the A0 jumper on the second QT Rotary Encoder board |
| 6 | + |
| 7 | +import board |
| 8 | +from adafruit_seesaw import seesaw, rotaryio, digitalio, neopixel |
| 9 | + |
| 10 | +qt_enc1 = seesaw.Seesaw(board.I2C(), addr=0x36) |
| 11 | +qt_enc2 = seesaw.Seesaw(board.I2C(), addr=0x37) |
| 12 | + |
| 13 | +qt_enc1.pin_mode(24, qt_enc1.INPUT_PULLUP) |
| 14 | +button1 = digitalio.DigitalIO(qt_enc1, 24) |
| 15 | +button_held1 = False |
| 16 | + |
| 17 | +qt_enc2.pin_mode(24, qt_enc2.INPUT_PULLUP) |
| 18 | +button2 = digitalio.DigitalIO(qt_enc2, 24) |
| 19 | +button_held2 = False |
| 20 | + |
| 21 | +encoder1 = rotaryio.IncrementalEncoder(qt_enc1) |
| 22 | +last_position1 = None |
| 23 | + |
| 24 | +encoder2 = rotaryio.IncrementalEncoder(qt_enc2) |
| 25 | +last_position2 = None |
| 26 | + |
| 27 | +pixel1 = neopixel.NeoPixel(qt_enc1, 6, 1) |
| 28 | +pixel1.brightness = 0.2 |
| 29 | +pixel1.fill(0xff0000) |
| 30 | + |
| 31 | +pixel2 = neopixel.NeoPixel(qt_enc2, 6, 1) |
| 32 | +pixel2.brightness = 0.2 |
| 33 | +pixel2.fill(0x0000ff) |
| 34 | + |
| 35 | + |
| 36 | +while True: |
| 37 | + |
| 38 | + # negate the position to make clockwise rotation positive |
| 39 | + position1 = -encoder1.position |
| 40 | + position2 = -encoder2.position |
| 41 | + |
| 42 | + if position1 != last_position1: |
| 43 | + last_position1 = position1 |
| 44 | + print("Position 1: {}".format(position1)) |
| 45 | + |
| 46 | + if not button1.value and not button_held1: |
| 47 | + button_held1 = True |
| 48 | + pixel1.brightness = 0.5 |
| 49 | + print("Button 1 pressed") |
| 50 | + |
| 51 | + if button1.value and button_held1: |
| 52 | + button_held1 = False |
| 53 | + pixel1.brightness = 0.2 |
| 54 | + print("Button 1 released") |
| 55 | + |
| 56 | + |
| 57 | + if position2 != last_position2: |
| 58 | + last_position2 = position2 |
| 59 | + print("Position 2: {}".format(position2)) |
| 60 | + |
| 61 | + if not button2.value and not button_held2: |
| 62 | + button_held2 = True |
| 63 | + pixel2.brightness = 0.5 |
| 64 | + print("Button 2 pressed") |
| 65 | + |
| 66 | + if button2.value and button_held2: |
| 67 | + button_held2 = False |
| 68 | + pixel2.brightness = 0.2 |
| 69 | + print("Button 2 released") |
0 commit comments