Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroPython: Create inky_frame module for buttons. #569

Merged
merged 1 commit into from
Dec 16, 2022

Conversation

Gadgetoid
Copy link
Member

Add a new module "inky_frame.py" that handles setting up the shift register and reading the buttons.

A close analog to the "Button" module and Pico Zero's button.

Import the module:

import inky_frame

Use the is_pressed property to read the current button state:

inky_frame.button_a.is_pressed

Use the read() method to get a debounced button state reading:

inky_frame.button_a.read()

@helgibbons
Copy link
Contributor

Thanks - is there a nicer way of lighting up the LEDs when the buttons are pressed than this?

# An offline image gallery that switches between five jpg images
# Copy them into your Pico's flash using Thonny.
# Sample images are taken by the Webb Space Telescope
# Credit: NASA and the Space Telescope Science Institute (STScI).
# More images and info @ https://webbtelescope.org/
# If you want to use your own images they must be 600 x 448 pixels or smaller
# and saved as *non-progressive* jpgs

import inky_frame
from picographics import PicoGraphics, DISPLAY_INKY_FRAME
import jpegdec
from machine import Pin

# you can change your file names here
IMAGE_A = "jwst1.jpg"
IMAGE_B = "jwst2.jpg"
IMAGE_C = "jwst3.jpg"
IMAGE_D = "jwst4.jpg"
IMAGE_E = "jwst5.jpg"

# set up the display
display = PicoGraphics(display=DISPLAY_INKY_FRAME)

# set up the button LEDs
button_a_led = Pin(inky_frame.LED_A, Pin.OUT)
button_b_led = Pin(inky_frame.LED_B, Pin.OUT)
button_c_led = Pin(inky_frame.LED_C, Pin.OUT)
button_d_led = Pin(inky_frame.LED_D, Pin.OUT)
button_e_led = Pin(inky_frame.LED_E, Pin.OUT)

# and the activity LED
activity_led = Pin(6, Pin.OUT)

# set up and enable vsys hold so we don't go to sleep
HOLD_VSYS_EN_PIN = 2

hold_vsys_en_pin = Pin(HOLD_VSYS_EN_PIN, Pin.OUT)
hold_vsys_en_pin.value(True)

# Create a new JPEG decoder for our PicoGraphics
j = jpegdec.JPEG(display)

# setup
activity_led.on()
# update the image on Inky every time it's powered up
# comment these lines out if running on battery power
# button_a_led.on()
# display_image(IMAGE_A)


def display_image(filename):
    
    # Open the JPEG file
    j.open_file(filename)
    
    # Decode the JPEG
    j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
    
    # Display the result
    display.update()


while True:   
    button_a_led.off()
    button_b_led.off()
    button_c_led.off()
    button_d_led.off()
    button_e_led.off()
    
    # light up the activity LED when Inky is awake
    activity_led.on()
    
    if inky_frame.button_a.is_pressed:
        button_a_led.on()
        display_image(IMAGE_A)
    elif inky_frame.button_b.is_pressed:
        button_b_led.on()
        display_image(IMAGE_B)
    elif inky_frame.button_c.is_pressed:
        button_c_led.on()
        display_image(IMAGE_C)
    elif inky_frame.button_d.is_pressed:
        button_d_led.on()
        display_image(IMAGE_D)
    elif inky_frame.button_e.is_pressed:
        button_e_led.on()
        display_image(IMAGE_E)
    
    # Go to sleep if on battery power
    # Activity LED will be always on when running on USB
    activity_led.off()
    hold_vsys_en_pin.init(Pin.IN)
    ```

@Gadgetoid
Copy link
Member Author

Yes, each button has a "led" property which is just a Pin instance, so you can use button_a.led.off() etc.

@Gadgetoid Gadgetoid merged commit fe55d9b into main Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants