|
56 | 56 | total_image_count = 0 |
57 | 57 |
|
58 | 58 | # Store our current location within the user gallery |
59 | | -current_image = 1 |
| 59 | +current_image = 0 |
60 | 60 |
|
| 61 | +lfsr = 1 |
| 62 | +tap = 0xdc29 |
61 | 63 |
|
62 | | -# We might not have enough space to load the entire directory list into RAM |
63 | | -# so we iterate though 'os.ilistdir' and store each file name in a .txt file for later |
64 | | -def create_index(dir): |
65 | | - |
66 | | - # check to see if contents.txt exists and skip the indexing if it does. |
67 | | - if not file_exists(dir + "/index.txt"): |
68 | | - |
69 | | - f = open(dir + "/index.txt", "a") |
70 | | - |
71 | | - for file in os.ilistdir(dir): |
72 | | - # We're only looking for jpeg images, we don't want to index any other file type |
73 | | - if ".jpg" in file[0] or ".jpeg" in file[0]: |
74 | | - f.write(str(file[0]) + "\n") |
75 | | - |
76 | | - f.close() |
77 | 64 |
|
78 | | - print("'index.txt' generated successfully!") |
| 65 | +# Display an error msg on screen and keep it looping |
| 66 | +def display_error(text): |
| 67 | + while 1: |
| 68 | + display.set_pen(BACKGROUND) |
| 69 | + display.clear() |
| 70 | + display.set_pen(WHITE) |
| 71 | + display.text(f"Error: {text}", 10, 10, WIDTH - 10, 1) |
| 72 | + presto.update() |
| 73 | + time.sleep(1) |
79 | 74 |
|
80 | | - else: |
81 | | - print("\nindex.txt already generated! \nIf you have changed the files in the current directory you will need to delete 'content.txt' and run the program again.\n\n") |
82 | 75 |
|
| 76 | +try: |
| 77 | + # Setup for SD Card |
| 78 | + 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)) |
| 79 | + sd = sdcard.SDCard(sd_spi, machine.Pin(39)) |
83 | 80 |
|
84 | | -def count_files(dir): |
85 | | - c = 0 |
86 | | - for file in os.ilistdir(dir): |
87 | | - c += 1 |
88 | | - return c - 1 |
| 81 | + # Mount the SD to the directory 'sd' |
| 82 | + uos.mount(sd, "/sd") |
| 83 | +except OSError: |
| 84 | + display_error("Unable to mount SD card") |
89 | 85 |
|
90 | 86 |
|
91 | | -# Delete the current index file if it exists |
92 | | -# This will force it to recreate it on the next boot. |
93 | | -def delete_index(dir): |
94 | | - print("Removing 'index.txt'") |
95 | | - os.remove(dir + "/index.txt") |
| 87 | +def numberedfiles(k): |
| 88 | + try: |
| 89 | + return int(k[:-4]) |
| 90 | + except ValueError: |
| 91 | + pass |
| 92 | + return 0 |
96 | 93 |
|
97 | 94 |
|
98 | | -lfsr = 1 |
99 | | -tap = 0xdc29 |
| 95 | +files = list(sorted(os.listdir("sd/gallery"), key=numberedfiles)) |
| 96 | +total_image_count = len(files) - 1 |
100 | 97 |
|
101 | 98 |
|
102 | 99 | def return_point(): |
@@ -145,22 +142,18 @@ def show_image(show_next=False, show_previous=False): |
145 | 142 | if current_image < total_image_count: |
146 | 143 | current_image += 1 |
147 | 144 | else: |
148 | | - current_image = 1 |
| 145 | + current_image = 0 |
149 | 146 | if show_previous: |
150 | | - if current_image > 1: |
| 147 | + if current_image > 0: |
151 | 148 | current_image -= 1 |
152 | 149 | else: |
153 | 150 | current_image = total_image_count |
154 | 151 |
|
155 | 152 | # Open the index file and read lines until we're at the correct position |
156 | 153 | try: |
157 | | - f = open(DIR + "/index.txt", 'r') |
158 | | - for i in range(current_image - 1): |
159 | | - f.readline() |
160 | | - file = f.readline() |
161 | | - f.close() |
| 154 | + img = f"sd/gallery/{files[current_image]}" |
162 | 155 |
|
163 | | - j.open_file(f"{DIR}/{file}") |
| 156 | + j.open_file(img) |
164 | 157 |
|
165 | 158 | img_height, img_width = j.get_height(), j.get_width() |
166 | 159 |
|
@@ -198,51 +191,6 @@ def clear(): |
198 | 191 | display.clear() |
199 | 192 |
|
200 | 193 |
|
201 | | -# Display an error msg on screen and keep it looping |
202 | | -def display_error(text): |
203 | | - while 1: |
204 | | - display.set_pen(BACKGROUND) |
205 | | - display.clear() |
206 | | - display.set_pen(WHITE) |
207 | | - display.text(f"Error: {text}", 10, 10, WIDTH - 10, 1) |
208 | | - presto.update() |
209 | | - time.sleep(1) |
210 | | - |
211 | | - |
212 | | -try: |
213 | | - # Setup for SD Card |
214 | | - 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)) |
215 | | - sd = sdcard.SDCard(sd_spi, machine.Pin(39)) |
216 | | - |
217 | | - # Mount the SD to the directory 'sd' |
218 | | - uos.mount(sd, "/sd") |
219 | | -except OSError: |
220 | | - display_error("Unable to mount SD card") |
221 | | - |
222 | | - |
223 | | -# Function to check if a file is present on the filesystem |
224 | | -def file_exists(filename): |
225 | | - try: |
226 | | - return (os.stat(filename)[0] & 0x4000) == 0 |
227 | | - except OSError: |
228 | | - return False |
229 | | - |
230 | | - |
231 | | -try: |
232 | | - # Delete the index to force it to recreate the file in the next step |
233 | | - delete_index(DIR) |
234 | | -except OSError: |
235 | | - print("Unable to delete index") |
236 | | - |
237 | | -# Create the index |
238 | | -# And count the images |
239 | | -try: |
240 | | - create_index(DIR) |
241 | | - total_image_count = count_files(DIR) |
242 | | -except OSError: |
243 | | - display_error("Unable to create index file. \nCheck that your file names do not contain non unicode characters.") |
244 | | - |
245 | | - |
246 | 194 | # Store the last time the screen was updated |
247 | 195 | last_updated = time.time() |
248 | 196 |
|
|
0 commit comments