Hi, everyone
I’m creating a conference badge which does the following
Button A name badge with a centred 75x100 jpg image with a black boarder around it and my name and positions centred as text beneath it
Button B = QR code 150x150 jpg
Button C = battery level
My jpg images are the correct size and are not progressive jpgs
However I keep getting errors with the script below specifically line 12, but tweaking the script before had errors on 7, 8 and 23.
Can someone help amend this script below or tell me what’s wrong with it.
import time
import micropython
from picographics import PicoGraphics, DISPLAY_TUFTY_2040
from pimoroni import Button
import jpegdec # Ensure you have this library installed
# Constants
WIDTH, HEIGHT = 320, 240
BUTTON_A = 7
BUTTON_B = 8
BUTTON_C = 9
INACTIVITY_TIMEOUT = micropython.const(120) # 2 minutes
OFF_TIMEOUT = micropython.const(300) # 5 minutes
# Set up display and buttons
display = PicoGraphics(display=DISPLAY_TUFTY_2040)
button_a = Button(BUTTON_A)
button_b = Button(BUTTON_B)
button_c = Button(BUTTON_C)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(200, 0, 0)
def show_name_badge():
display.set_backlight(1.0)
display.set_pen(WHITE)
display.clear()
display.set_pen(BLACK)
# Draw text
display.text("Andrew Hamilton", WIDTH // 2 - 90, 20, WIDTH, 3) # Centered name
display.text("Disability Rights Activist/Lobbyist", WIDTH // 2 - 150, 60, WIDTH, 2) # Position 1
display.text("Founder of Just Include", WIDTH // 2 - 130, 100, WIDTH, 2) # Position 2
# Draw image with border
image_x = (WIDTH - 75) // 2
image_y = (HEIGHT - 100) // 2
display.set_pen(BLACK)
display.rectangle(image_x - 1, image_y - 1, 77, 102) # Black border (width + 2, height + 2)
display.set_pen(WHITE)
display.rectangle(image_x, image_y, 75, 100) # Image background
try:
# Ensure JPEG image is named correctly and accessible
jpeg = jpegdec.JPEG(display)
jpeg.open_file("me.jpg") # Ensure this file is on the device
jpeg.decode(image_x, image_y, 75, 100) # Adjust size as needed
except OSError:
display.set_pen(RED)
display.text("Image not found", WIDTH // 2 - 70, HEIGHT // 2, WIDTH, 3)
display.update()
def show_qr_code():
display.set_backlight(1.0)
display.set_pen(WHITE)
display.clear()
display.set_pen(BLACK)
# Draw QR code
image_x = (WIDTH - 150) // 2
image_y = (HEIGHT - 150) // 2
display.set_pen(BLACK)
display.rectangle(image_x - 1, image_y - 1, 152, 152) # Black border
display.set_pen(WHITE)
display.rectangle(image_x, image_y, 150, 150) # QR code background
try:
# Ensure JPEG image is named correctly and accessible
jpeg = jpegdec.JPEG(display)
jpeg.open_file("qrcode.jpg") # Ensure this file is on the device
jpeg.decode(image_x, image_y, 150, 150) # Adjust size as needed
except OSError:
display.set_pen(RED)
display.text("QR Code not found", WIDTH // 2 - 70, HEIGHT // 2, WIDTH, 3)
display.update()
def show_battery_info():
# Implement battery info display logic here
pass
# Main loop
last_interaction = time.time()
while True:
if button_a.is_pressed:
show_name_badge()
last_interaction = time.time()
elif button_b.is_pressed:
show_qr_code()
last_interaction = time.time()
elif button_c.is_pressed:
show_battery_info()
last_interaction = time.time()
# Check for inactivity
current_time = time.time()
if current_time - last_interaction > OFF_TIMEOUT:
display.set_backlight(0) # Turn off display
elif current_time - last_interaction > INACTIVITY_TIMEOUT:
display.set_backlight(0.3) # Dim the display
else:
display.set_backlight(1.0) # Full brightness
time.sleep(0.1)