Inky Frame: New button test, old is now button demo.
This commit is contained in:
parent
9424ae7081
commit
9663be2787
|
@ -0,0 +1,85 @@
|
|||
# This example shows you a simple, non-interrupt way of reading Inky Frame's buttons with a loop that checks to see if buttons are pressed.
|
||||
|
||||
from pimoroni import ShiftRegister
|
||||
from picographics import PicoGraphics, DISPLAY_INKY_FRAME as DISPLAY # 5.7"
|
||||
# from picographics import PicoGraphics, DISPLAY_INKY_FRAME_4 as DISPLAY # 4.0"
|
||||
from machine import Pin
|
||||
|
||||
display = PicoGraphics(display=DISPLAY)
|
||||
|
||||
display.set_font("bitmap8")
|
||||
|
||||
# Inky Frame uses a shift register to read the buttons
|
||||
SR_CLOCK = 8
|
||||
SR_LATCH = 9
|
||||
SR_OUT = 10
|
||||
|
||||
sr = ShiftRegister(SR_CLOCK, SR_LATCH, SR_OUT)
|
||||
|
||||
# set up the button LEDs
|
||||
button_a_led = Pin(11, Pin.OUT)
|
||||
button_b_led = Pin(12, Pin.OUT)
|
||||
button_c_led = Pin(13, Pin.OUT)
|
||||
button_d_led = Pin(14, Pin.OUT)
|
||||
button_e_led = Pin(15, Pin.OUT)
|
||||
|
||||
|
||||
# a handy function we can call to clear the screen
|
||||
# display.set_pen(1) is white and display.set_pen(0) is black
|
||||
def clear():
|
||||
display.set_pen(1)
|
||||
display.clear()
|
||||
|
||||
|
||||
# set up
|
||||
clear()
|
||||
display.set_pen(0)
|
||||
display.text("Press any button!", 10, 10, scale=4)
|
||||
display.update()
|
||||
|
||||
while True:
|
||||
button_a_led.off()
|
||||
button_b_led.off()
|
||||
button_c_led.off()
|
||||
button_d_led.off()
|
||||
button_e_led.off()
|
||||
|
||||
# read the shift register
|
||||
# we can tell which button has been pressed by checking if a specific bit is 0 or 1
|
||||
result = sr.read()
|
||||
button_a = sr[7]
|
||||
button_b = sr[6]
|
||||
button_c = sr[5]
|
||||
button_d = sr[4]
|
||||
button_e = sr[3]
|
||||
|
||||
if button_a == 1: # if a button press is detected then...
|
||||
button_a_led.on()
|
||||
clear() # clear to white
|
||||
display.set_pen(4) # change the pen colour
|
||||
display.text("Button A pressed", 10, 10, scale=4) # display some text on the screen
|
||||
display.update() # update the display
|
||||
elif button_b == 1:
|
||||
button_b_led.on()
|
||||
clear()
|
||||
display.set_pen(6)
|
||||
display.text("Button B pressed", 10, 50, scale=4)
|
||||
display.update()
|
||||
elif button_c == 1:
|
||||
button_c_led.on()
|
||||
clear()
|
||||
display.set_pen(5)
|
||||
display.text("Button C pressed", 10, 90, scale=4)
|
||||
display.update()
|
||||
elif button_d == 1:
|
||||
button_d_led.on()
|
||||
clear()
|
||||
display.set_pen(2)
|
||||
display.text("Button D pressed", 10, 130, scale=4)
|
||||
display.update()
|
||||
elif button_e == 1:
|
||||
button_e_led.on()
|
||||
clear()
|
||||
display.set_pen(3)
|
||||
display.text("Button E pressed", 10, 170, scale=4)
|
||||
display.update()
|
|
@ -1,12 +1,10 @@
|
|||
# This example shows you a simple, non-interrupt way of reading Inky Frame's buttons with a loop that checks to see if buttons are pressed.
|
||||
# This example allows you to test Inky Frame's buttons
|
||||
# It does not update the screen.
|
||||
|
||||
from pimoroni import ShiftRegister
|
||||
from picographics import PicoGraphics, DISPLAY_INKY_FRAME
|
||||
from machine import Pin
|
||||
import time
|
||||
|
||||
display = PicoGraphics(display=DISPLAY_INKY_FRAME)
|
||||
|
||||
display.set_font("bitmap8")
|
||||
|
||||
# Inky Frame uses a shift register to read the buttons
|
||||
SR_CLOCK = 8
|
||||
|
@ -15,70 +13,47 @@ SR_OUT = 10
|
|||
|
||||
sr = ShiftRegister(SR_CLOCK, SR_LATCH, SR_OUT)
|
||||
|
||||
# set up the button LEDs
|
||||
button_a_led = Pin(11, Pin.OUT)
|
||||
button_b_led = Pin(12, Pin.OUT)
|
||||
button_c_led = Pin(13, Pin.OUT)
|
||||
button_d_led = Pin(14, Pin.OUT)
|
||||
button_e_led = Pin(15, Pin.OUT)
|
||||
|
||||
# Simple class to debounce button input and handle LED
|
||||
class Button:
|
||||
def __init__(self, idx, led, debounce=50):
|
||||
self.led = Pin(led, Pin.OUT) # LEDs are just regular IOs
|
||||
self.led.on()
|
||||
self._idx = idx
|
||||
self._debounce_time = debounce
|
||||
self._changed = time.ticks_ms()
|
||||
self._last_value = None
|
||||
|
||||
def debounced(self):
|
||||
return time.ticks_ms() - self._changed > self._debounce_time
|
||||
|
||||
def get(self, sr):
|
||||
value = sr[self._idx]
|
||||
if value != self._last_value and self.debounced():
|
||||
self._last_value = value
|
||||
self._changed = time.ticks_ms()
|
||||
return value
|
||||
|
||||
|
||||
# a handy function we can call to clear the screen
|
||||
# display.set_pen(1) is white and display.set_pen(0) is black
|
||||
def clear():
|
||||
display.set_pen(1)
|
||||
display.clear()
|
||||
button_a = Button(7, 11)
|
||||
button_b = Button(6, 12)
|
||||
button_c = Button(5, 13)
|
||||
button_d = Button(4, 14)
|
||||
button_e = Button(3, 15)
|
||||
|
||||
|
||||
# set up
|
||||
clear()
|
||||
display.set_pen(0)
|
||||
display.text("Press any button!", 10, 10, scale=4)
|
||||
display.update()
|
||||
|
||||
while True:
|
||||
button_a_led.off()
|
||||
button_b_led.off()
|
||||
button_c_led.off()
|
||||
button_d_led.off()
|
||||
button_e_led.off()
|
||||
sr.read()
|
||||
|
||||
# read the shift register
|
||||
# we can tell which button has been pressed by checking if a specific bit is 0 or 1
|
||||
result = sr.read()
|
||||
button_a = sr[7]
|
||||
button_b = sr[6]
|
||||
button_c = sr[5]
|
||||
button_d = sr[4]
|
||||
button_e = sr[3]
|
||||
if button_a.get(sr):
|
||||
button_a.led.toggle()
|
||||
if button_b.get(sr):
|
||||
button_b.led.toggle()
|
||||
if button_c.get(sr):
|
||||
button_c.led.toggle()
|
||||
if button_d.get(sr):
|
||||
button_d.led.toggle()
|
||||
if button_e.get(sr):
|
||||
button_e.led.toggle()
|
||||
|
||||
if button_a == 1: # if a button press is detected then...
|
||||
button_a_led.on()
|
||||
clear() # clear to white
|
||||
display.set_pen(4) # change the pen colour
|
||||
display.text("Button A pressed", 10, 10, scale=4) # display some text on the screen
|
||||
display.update() # update the display
|
||||
elif button_b == 1:
|
||||
button_b_led.on()
|
||||
clear()
|
||||
display.set_pen(6)
|
||||
display.text("Button B pressed", 10, 50, scale=4)
|
||||
display.update()
|
||||
elif button_c == 1:
|
||||
button_c_led.on()
|
||||
clear()
|
||||
display.set_pen(5)
|
||||
display.text("Button C pressed", 10, 90, scale=4)
|
||||
display.update()
|
||||
elif button_d == 1:
|
||||
button_d_led.on()
|
||||
clear()
|
||||
display.set_pen(2)
|
||||
display.text("Button D pressed", 10, 130, scale=4)
|
||||
display.update()
|
||||
elif button_e == 1:
|
||||
button_e_led.on()
|
||||
clear()
|
||||
display.set_pen(3)
|
||||
display.text("Button E pressed", 10, 170, scale=4)
|
||||
display.update()
|
||||
time.sleep(1.0 / 60) # Poll 60 times/second
|
||||
|
|
Loading…
Reference in New Issue