When last I wrote about the Feather RP2040 and the Featherwing OLED 128×32 ( /2022/02/05/tinkering-with-the-black-adafruit-feather-rp2040/ ), I’d mentioned I’d like to enable the three switches on the edge of the Featherwing labeled ‘A’, ‘B’, and ‘C’. The code posted herewith has that enabled. I now have both the Featherwing and the NeoPixel ring attached to the Feather RP2040. I had to make a slight change to what the NeoPixel ring’s DIN (data in) line was attached to because it turns out that the ‘A’ switch on the Featherwing wants to use that same pin as an input. You can see the code changes and additions in the highlighted sections below.
When the code is executing, pressing any of the buttons will print “Button … pressed” only once on the REPL. When that button is released then “Button … released” is printed. This is simple early code without anything substantial hooked into those button checks, but the framework presented here should inspire something more interesting.
The next stage will be to add code back in to manipulate the OLED display. I hope that code will be a bit more streamlined than my earlier example.
import timeimport boardimport neopixelfrom digitalio import DigitalInOut, Direction, Pull# Define all three buttons, A, B, and C.#a_pressed = b_pressed = c_pressed = Falsea_button = DigitalInOut(board.D9)a_button.direction = Direction.INPUTa_button.pull = Pull.UPb_button = DigitalInOut(board.D6)b_button.direction = Direction.INPUTb_button.pull = Pull.UPc_button = DigitalInOut(board.D5)c_button.direction = Direction.INPUTc_button.pull = Pull.UP# For the single neopixel on the Feather itself.#s_neopixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2, auto_write=False, pixel_order=neopixel.GRB)# For the 12 neopixel ring attached to the Feather.#neopixel_ring = neopixel.NeoPixel(board.D10, 12, brightness=0.2, auto_write=True, pixel_order=neopixel.GRB)def cycle_color(color):s_neopixel.fill(color)s_neopixel.show()for i in range(len(neopixel_ring)):neopixel_ring[i-1] = (0,0,0,0) # turn LED offneopixel_ring[i] = colorneopixel_ring.show()time.sleep(.05)while True:if (a_button.value is not True):if (not a_pressed):print('Button A pressed')a_pressed = Trueelse:if (a_pressed):print('Button A released')a_pressed = Falseif (b_button.value is not True):if (not b_pressed):print('Button B pressed')b_pressed = Trueelse:if (b_pressed):print('Button B released')b_pressed = Falseif (c_button.value is not True):if (not c_pressed):print('Button C pressed')c_pressed = Trueelse:if (c_pressed):print('Button C released')c_pressed = Falsecycle_color((64,0,0,0)) # redcycle_color((0,64,0,0)) # greencycle_color((0,0,64,0)) # bluecycle_color((64,16,0,0)) # orangecycle_color((0,32,32,0)) # cyancycle_color((0,0,0,0)) # black (LED off)