Struggling with images displaying on tufy2040

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)

If you wrap your code in ``` then it will be a lot easier to see what’s going on.

Also, the specific error message might make it easier to diagnose.

Done, it just said syntax line 12, nothing else except the script exited

Ok, line 12 looks to have something about micropython.const, which… looks odd?

I’d change it to

INACTIVITY_TIMEOUT = 120  # 2 minutes
OFF_TIMEOUT = 300  # 5 minutes

(I mean you could leave const() in there if you want…) and see if that moves you on to a different error ;-)

Can you see any other obvious errors with my script?

Not immediately, but I confess I didn’t try running it (what with not actually owning a Tufty).

I’m sure Thonny will let you know where any other fatal errors are. Logic errors are easier to spot when you know what things are / aren’t happening.

I unfortunately can’t use thonny, it still didn’t work and when I removed it, a new syntax appears on line 24 about setting backlight of name badge to white

What exactly are you using instead of Thonny? And could you show us the real output of your error (with context) using cut&paste?

Ardunio lab for micropython

Screen shot below:

Ok, well the code you provided doesn’t seem to set the backlight to white (because… that would make no sense) and it’s hard to tell what line 24 is because… that doesn’t seem to be the backlight stuff.

Where has this code actually come from? Is it published anywhere? It’s exceedingly hard to debug one forum message at a time.

The line number 12 in the error message does not match the line-numbers in the screenshot of the code.

One reason for the failure could be an error in the transport layer: it seems that the editor is using the raw REPL to send code to the device. I had this kind of problem with a different device (and a different tool): somehow the transfer via raw REPL failed and the interpreter received invalid code.

But this is only speculating. Maybe you could verify your code is working with a different tool. Thonny would be one option, others have used mpremote or rshell.