Skip to content

Commit 12d922a

Browse files
Catch alloc error on image load
1 parent 3b585aa commit 12d922a

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

CLUE_Light_Painter/code.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
# pylint: disable=import-error
99
import gc
1010
from math import modf
11-
from time import monotonic
11+
from time import monotonic, sleep
1212
import board
1313
import busio
1414
import digitalio
1515
import displayio
1616
import ulab
17-
from ubmp import UBMP
17+
from ubmp import UBMP, BMPError
1818
from neopixel_write import neopixel_write
1919
from richbutton import RichButton
2020
from adafruit_display_text import label
@@ -87,6 +87,7 @@ def __init__(self, flip, path, num_pixels, pixel_order, pixel_pin, gamma):
8787
self.config_mode = 0 # Current setting being changed
8888
self.rect = None # Multipurpose progress/setting rect
8989
self.speed = 0.6 # Paint speed, 0.0 (slow) to 1.0 (fast)
90+
self.columns = [] # Empty image data
9091

9192
if isinstance(pixel_pin, (tuple, list)):
9293
# Using DotStar LEDs. The SPI peripheral is locked and config'd
@@ -193,9 +194,15 @@ def load_image(self):
193194
group.append(self.rect)
194195
board.DISPLAY.show(group)
195196

196-
self.columns = self.neobmp.load(self.path + '/' +
197-
self.images[self.image_num],
198-
self.load_progress)
197+
try:
198+
self.columns = self.neobmp.load(self.path + '/' +
199+
self.images[self.image_num],
200+
self.load_progress)
201+
except (MemoryError, BMPError):
202+
group = displayio.Group()
203+
group.append(centered_label('TOO BIG', 40, 3))
204+
board.DISPLAY.show(group)
205+
sleep(4)
199206

200207
board.DISPLAY.show(displayio.Group()) # Clear display
201208

@@ -207,9 +214,12 @@ def paint(self):
207214
the nifty image processing.
208215
"""
209216

217+
if not self.columns: # If no image loaded
218+
return # Go back to config, can try another
219+
210220
board.DISPLAY.brightness = 0 # Screen backlight OFF
211221
painting = False
212-
duration = 10.0 - self.speed * 9.0 # 1 to 10 seconds
222+
duration = 5.0 - self.speed * 4.5 # 0.5 to 5 seconds
213223
err = 0 # Clear the 'error term' used for diffusion dithering
214224

215225
gc.collect() # Helps make playback a little smoother
@@ -351,7 +361,7 @@ def config_select(self):
351361
group = self.make_ui_group(True, strings[self.config_mode])
352362
board.DISPLAY.brightness = 1 # Screen on
353363
prev_mode = self.config_mode
354-
reload_image = False
364+
reload_image = not self.columns
355365

356366
while True:
357367
action_left, action_right = (self.button_left.action(),

0 commit comments

Comments
 (0)