55from io import BytesIO
66from base_circuitpython import base_cp_constants as CONSTANTS
77import time
8- import collections
98from random import shuffle
109import common
1110import board
@@ -165,6 +164,7 @@ def __init__(
165164
166165 self ._order = order
167166 self ._curr_img = ""
167+ self ._current_image_index = None
168168
169169 # load images into main queue
170170 self .__load_images ()
@@ -219,41 +219,57 @@ def update(self):
219219
220220 def __get_next_img (self ):
221221
222- # handle empty queue
223- if not len (self .pic_queue ):
224- if self .loop :
225- self .__load_images ()
222+ if self .direction == PlayBackDirection .FORWARD :
223+ if self ._current_image_index == None :
224+ self ._current_image_index = 0
226225 else :
227- return ""
226+ self ._current_image_index += 1
227+
228+ if self ._current_image_index >= len (self .dir_imgs ):
229+
230+ if self .loop :
231+ self ._current_image_index = 0
232+ self .__load_images ()
233+ else :
234+ self ._current_image_index = len (self .dir_imgs ) - 10
235+ return ""
228236
229- if self .direction == PlayBackDirection .FORWARD :
230- return self .pic_queue .popleft ()
231237 else :
232- return self .pic_queue .pop ()
238+ if self ._current_image_index == None :
239+ self ._current_image_index = len (self .dir_imgs ) - 1
240+ else :
241+ self ._current_image_index -= 1
242+
243+ if self ._current_image_index < 0 :
244+ if self .loop :
245+ self ._current_image_index = len (self .dir_imgs ) - 1
246+ self .__load_images ()
247+ else :
248+ self ._current_image_index = 0
249+ return ""
250+
251+ img = self .dir_imgs [self ._current_image_index ]
252+ return img
233253
234254 def __load_images (self ):
235- dir_imgs = []
255+ self . dir_imgs = []
236256 for d in self .dirs :
237257 try :
238258 new_path = os .path .join (self .folder , d )
239259
240260 # only add bmp imgs
241- if os .path .splitext (new_path )[1 ] == CONSTANTS .BMP_IMG_ENDING :
242- dir_imgs .append (new_path )
261+ if os .path .splitext (new_path )[- 1 ] == CONSTANTS .BMP_IMG_ENDING :
262+ self . dir_imgs .append (new_path )
243263 except Image .UnidentifiedImageError as e :
244264 continue
245265
246- if not len (dir_imgs ):
266+ if not len (self . dir_imgs ):
247267 raise RuntimeError (CONSTANTS .NO_VALID_IMGS_ERR )
248268
249269 if self ._order == PlayBackOrder .RANDOM :
250- shuffle (dir_imgs )
270+ shuffle (self . dir_imgs )
251271 else :
252- dir_imgs .sort ()
253-
254- # convert list to queue
255- # (must be list beforehand for potential randomization)
256- self .pic_queue = collections .deque (dir_imgs )
272+ self .dir_imgs .sort ()
257273
258274 def __advance_with_fade (self ):
259275 if board .DISPLAY .active_group != self :
@@ -265,6 +281,7 @@ def __advance_with_fade(self):
265281 while not advance_sucessful :
266282 new_path = self .__get_next_img ()
267283 if new_path == "" :
284+ self ._img_start = time .monotonic ()
268285 return False
269286
270287 try :
@@ -323,6 +340,7 @@ def __advance_no_fade(self):
323340 while not advance_sucessful :
324341 new_path = self .__get_next_img ()
325342 if new_path == "" :
343+ self ._img_start = time .monotonic ()
326344 return False
327345
328346 try :
0 commit comments