-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathimage_gallery.py
More file actions
251 lines (187 loc) · 6.47 KB
/
image_gallery.py
File metadata and controls
251 lines (187 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# ICON photo-library
# NAME Photo Frame
# DESC A touch enabled image gallery
"""
An image gallery demo to turn your Pimoroni Presto into a desktop photo frame!
- Create a folder called 'gallery' on the root of your SD card and fill it with JPEGs.
- The image will change automatically every 5 minutes
- You can also tap the right side of the screen to skip next image and left side to go to the previous :)
"""
import os
import time
import jpegdec
import machine
import sdcard
import uos
from presto import Presto
# The total number of LEDs to set, the Presto has 7
NUM_LEDS = 7
# Seconds between changing the image on screen
# This interval shows us a new image every 5 minutes
INTERVAL = 60 * 5
LEDS_LEFT = [4, 5, 6]
LEDS_RIGHT = [0, 1, 2]
# Setup for the Presto display
presto = Presto()
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
BACKGROUND = display.create_pen(1, 1, 1)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
# We'll need this for the touch element of the screen
touch = presto.touch
# JPEG Dec
j = jpegdec.JPEG(display)
# Where our images are located
directory = "gallery"
# Stores the total number of images in the user gallery
total_image_count = 0
# Store our current location within the user gallery
current_image = 0
lfsr = 1
tap = 0xdc29
# Display an error msg on screen and keep it looping
def display_error(text):
while 1:
for i in range(2):
display.set_layer(i)
display.set_pen(BACKGROUND)
display.clear()
display.set_pen(WHITE)
display.text(f"Error: {text}", 10, 10, WIDTH - 10, 1)
presto.update()
time.sleep(1)
try:
# Setup for SD Card
sd_spi = machine.SPI(0, sck=machine.Pin(34, machine.Pin.OUT), mosi=machine.Pin(35, machine.Pin.OUT), miso=machine.Pin(36, machine.Pin.OUT))
sd = sdcard.SDCard(sd_spi, machine.Pin(39))
# Mount the SD to the directory 'sd'
uos.mount(sd, "/sd")
# if the gallery folder exists on the SD card we want to use the images in there!
if os.stat("sd/gallery"):
directory = "sd/gallery"
except OSError:
pass
def numberedfiles(k):
try:
return int(k[:-4])
except ValueError:
pass
return 0
try:
files = [file for file in sorted(os.listdir(directory), key=numberedfiles) if file.endswith(".jpg")]
except OSError:
display_error("Problem loading images.\n\nEnsure that your Presto or SD card contains a 'gallery' folder in the root")
total_image_count = len(files) - 1
def return_point():
global lfsr
x = lfsr & 0x00ff
y = (lfsr & 0xff00) >> 8
lsb = lfsr & 1
lfsr >>= 1
if lsb:
lfsr ^= tap
if x - 1 < 240 and y < 240:
return x - 1, y
return -1, -1
def fizzlefade():
display.set_pen(BLACK)
display.set_layer(1)
while True:
for _ in range(2000):
x, y = return_point()
if x > -1 and y > -1:
display.pixel(x, y)
if lfsr == 1:
break
presto.update()
if lfsr == 1:
break
def show_image(show_next=False, show_previous=False):
global current_image
global total_image_count
# Get the next image in the gallery
# If we're at the end of the gallery, loop back and start from 1.
if show_next:
if current_image < total_image_count:
current_image += 1
else:
current_image = 0
if show_previous:
if current_image > 0:
current_image -= 1
else:
current_image = total_image_count
# Open the index file and read lines until we're at the correct position
try:
img = f"{directory}/{files[current_image]}"
j.open_file(img)
img_height, img_width = j.get_height(), j.get_width()
img_x = 0
img_y = 0
# if the image isn't exactly 240x240 then we'll try to centre the image
if img_width < WIDTH:
img_x = (WIDTH // 2) - (img_width // 2)
if img_height < HEIGHT:
img_y = (HEIGHT // 2) - (img_height // 2)
display.set_layer(0)
display.set_pen(BACKGROUND)
display.clear()
j.decode(img_x, img_y, jpegdec.JPEG_SCALE_FULL, dither=True)
fizzlefade()
# Now draw the current image to Layer 1
display.set_layer(1)
# Decode the JPEG
j.decode(img_x, img_y, jpegdec.JPEG_SCALE_FULL, dither=True)
except OSError:
display_error("Unable to find/read file.\n\nCheck that the 'gallery' folder in the root of your SD card contains JPEG images!")
except IndexError:
display_error("Unable to read images in the 'gallery' folder.\n\nCheck the files are present and are in JPEG format.")
def clear():
display.set_pen(BACKGROUND)
display.set_layer(0)
display.clear()
display.set_layer(1)
display.clear()
# Store the last time the screen was updated
last_updated = time.time()
# Show the first image on the screen so it's not just noise :)
# We're not passing the arg for 'show_next' or 'show_previous' so it'll show whichever image is current
clear()
show_image()
presto.update()
while True:
# Poll the touch so we can see if anything changed since the last time
touch.poll()
# Check if it's time to update the image!
if time.time() - last_updated > INTERVAL:
last_updated = time.time()
show_image(show_next=True)
presto.update()
# if the screen is reporting that there is touch we want to handle that here
if touch.state:
# Right half of the screen moves to the next image
# The LEDs on the right side of the presto light up to show it is working
if touch.x > WIDTH // 2:
for i in LEDS_RIGHT:
presto.set_led_rgb(i, 255, 255, 255)
show_image(show_next=True)
presto.update()
last_updated = time.time()
for i in LEDS_RIGHT:
presto.set_led_rgb(i, 0, 0, 0)
time.sleep(0.01)
# Left half of the screen moves to the previous image
elif touch.x < WIDTH // 2:
for i in LEDS_LEFT:
presto.set_led_rgb(i, 255, 255, 255)
show_image(show_previous=True)
presto.update()
last_updated = time.time()
for i in LEDS_LEFT:
presto.set_led_rgb(i, 0, 0, 0)
time.sleep(0.01)
# Wait here until the user stops touching the screen
while touch.state:
touch.poll()
time.sleep(0.02)