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