Tufty2040 buttons

Hi i’m Saymon,
I have new Tufty2040,
Ive installed Thonny, maked connection, updated file to 1.21.0,
Tufty works well, buttons works, i checked it with " button test.py"

After this basic things, i maked some tests,
learned how to display simple text,
then how to display one simple image,


from picographics import PicoGraphics, DISPLAY_TUFTY_2040
from jpegdec import JPEG

display = PicoGraphics(display=DISPLAY_TUFTY_2040)
j = JPEG(display)

j.open_file(“your_image.jpeg”)
j.decode()
display.update()


this mini code above works well,
then i tested it how to display 3 images with delay, also works good.


from picographics import PicoGraphics, DISPLAY_TUFTY_2040
from jpegdec import JPEG
import time

display = PicoGraphics(display=DISPLAY_TUFTY_2040)
j = JPEG(display)

List of image filenames

image_filenames = [“your_image01.jpeg”, “. your_image02.jpeg”, “your_image03.jpeg”]

Delay between image displays (adjust as needed, in seconds)

delay_between_images = 5 # Change this value to set the delay

for image_filename in image_filenames:
# Open and decode each image in the list
try:
j.open_file(image_filename)
j.decode()
display.update()
time.sleep(delay_between_images) # Introduce a delay between images
except OSError as e:
print(f"Error opening {image_filename}: {e}")


Now what was my goal: how to make images works with buttons?,
after a lot of tries, nothing works,
all factory mini programs where buttons are used works well.

How to write simple code to jumps between list of for example 8 images,
using UP and DOWN buttons?

Thanks for any help…

from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
from pimoroni import Button
from jpegdec import JPEG
from time import sleep

button_a = Button(7, invert=False)
button_b = Button(8, invert=False)
button_c = Button(9, invert=False)
button_up = Button(22, invert=False)
button_down = Button(6, invert=False)

display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_RGB332)
j = JPEG(display)

image_filenames = [“Image_01”, “Image_02”, “Image_03”, “Image_04”]
max_array = len (image_filenames)
arr_position = 0

while True:

if button_up.is_pressed:
arr_position += 1
arr_position %=max_array
sleep(.25)
display.clear()

if button_down.is_pressed:
arr_position -= 1
arr_position %=1
sleep(.25)
display.clear()

picture = image_filenames[arr_position]
sleep (.25)
j.open_file(image_filenames[arr_position])
j.decode()
display.update()

This is my quick and dirty – am I sure there are others on this forum whom can program at the drop of a pin. My hats off to them.

Hope this helps you somewhat.

Cheers!!!

Thanks, but console shows this after run this code:

MPY: soft reboot
Traceback (most recent call last):
File “”, line 21
SyntaxError: invalid syntax

image

Sorry about that – you will need to indent the lines as such --^

My copy and paste removed them - apologies…

@Bootboy @Mad_Monk Code formatting does work here - use the code formatting device in the editor.

Code goes in here
    and respects indentation
        just fine

No problemo 😂
I made indentations,
now console says there is a some problem with image names:

File “”, line 15, in
NameError: name ‘“Image_01”’ isn’t defined

I have 4 images on Tufty…
named: Tor01.jpeg, Tor02.jpeg etc.

They are displayed correctly on earlier tries on Tufty screen…

Sorry Halfer – I knew about that. But did the quick CRTL-C // CRTL-V action, as I swapped to another window.

Hi Bootboy!!

You will need to update the following line with your named files:

image_filenames = [“Image_01”, “Image_02”, “Image_03”, “Image_04”]

TO

image_filenames = [“Tor01.jpeg”, “Tor02.jpeg”, “Tor03.jpeg”, “Tor04.jpeg”]

I did it, console stay the same, and screen is all black,

File “”, line 15, in
NameError: name ‘“Tor01.jpeg”’ isn’t defined

from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
from pimoroni import Button
from jpegdec import JPEG
from time import sleep


# Setup Buttons
button_a = Button(7, invert=False)
button_b = Button(8, invert=False)
button_c = Button(9, invert=False)
button_up = Button(22, invert=False)
button_down = Button(6, invert=False)


display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_RGB332)
j = JPEG(display)

image_filenames = ["Tor01.jpeg", "Tor02.jpeg", "Tor03.jpeg", "Tor04.jpeg"]
max_array = len (image_filenames)
arr_position = 0

while True:

  if button_up.is_pressed:
	arr_position += 1
	arr_position %=max_array
	sleep(.25)
	display.clear()
       
  if button_down.is_pressed:
	arr_position = arr_position - 1
	sleep(.25)
	display.clear()
	
  if arr_position < 0:
     arr_position = (max_array-1) 
  
  picture = image_filenames[arr_position]
  j.open_file(image_filenames[arr_position])
  j.decode()
  display.update()
  


Also fixed the down button to cycle back, like the forward button does when you keep pushing up. Previously the 0 killed it. –

Hopefully this formatting works… guessing the system may have altered it when you copied it out of my previous script.

I am assuming your photos are in the root of Tufty? - Otherwise you will need to add the directory as such “Images/Tor01.jpeg” –

NOTE:
All names are case-sensitive

Not a problem - just click on the Edit feature against the appropriate paste, and it can be fixed in a jiffy. I think the code button won’t help here - jut put code fences top and bottom around the code. This is usually three backticks in sequence, on their own line.

Seems to be working very well,
No need to create new folder for images,
They on root.

BIG THANKS!!! for all of YOU…

1 Like

Hi Bootboy!!

Just a note:

You can comment out the following line with a " # ":

picture = image_filenames[arr_position]

I added it previously for testing in print statements (to identify the name of files names). But as you can see, it is no longer used. [never called hence after]

Cheers,
Tomas