Skip to content

Commit a31dc99

Browse files
authored
Merge pull request #8 from ladyada/master
fix text wrapping, image position and add splashscreens
2 parents 6aef8c8 + b5a68df commit a31dc99

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

adafruit_pyportal.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,42 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
180180
except OSError:
181181
self._uselocal = False
182182

183+
if self._debug:
184+
print("Init display")
185+
self.splash = displayio.Group(max_size=5)
186+
187+
if self._debug:
188+
print("Init background")
189+
self._bg_group = displayio.Group(max_size=1)
190+
self._bg_file = None
191+
self._default_bg = default_bg
192+
self.splash.append(self._bg_group)
193+
194+
# show thank you and bootup file if available
195+
for bootscreen in ("/thankyou.bmp", "/pyportal_startup.bmp"):
196+
try:
197+
os.stat(bootscreen)
198+
board.DISPLAY.show(self.splash)
199+
for i in range(100, -1, -1): # dim down
200+
self.set_backlight(i/100)
201+
time.sleep(0.005)
202+
self.set_background(bootscreen)
203+
board.DISPLAY.wait_for_frame()
204+
for i in range(100): # dim up
205+
self.set_backlight(i/100)
206+
time.sleep(0.005)
207+
time.sleep(2)
208+
except OSError:
209+
pass # they removed it, skip!
210+
211+
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
212+
self._speaker_enable.switch_to_output(False)
213+
self.audio = audioio.AudioOut(board.AUDIO_OUT)
214+
try:
215+
self.play_file("pyportal_startup.wav")
216+
except OSError:
217+
pass # they deleted the file, no biggie!
218+
183219
# Make ESP32 connection
184220
if self._debug:
185221
print("Init ESP32")
@@ -207,6 +243,9 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
207243
requests.set_interface(self._esp)
208244
self._connect_esp()
209245

246+
# set the default background
247+
self.set_background(self._default_bg)
248+
210249
if self._debug:
211250
print("Init SD Card")
212251
sd_cs = DigitalInOut(board.SD_CS)
@@ -218,27 +257,6 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
218257
except OSError as error:
219258
print("No SD card found:", error)
220259

221-
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
222-
self._speaker_enable.switch_to_output(False)
223-
self.audio = audioio.AudioOut(board.AUDIO_OUT)
224-
225-
try:
226-
self.play_file("pyportal_startup.wav")
227-
except OSError:
228-
pass # they deleted the file, no biggie!
229-
230-
if self._debug:
231-
print("Init display")
232-
self.splash = displayio.Group(max_size=5)
233-
board.DISPLAY.show(self.splash)
234-
235-
if self._debug:
236-
print("Init background")
237-
self._bg_group = displayio.Group(max_size=1)
238-
self._bg_file = None
239-
self._default_bg = default_bg
240-
self.set_background(self._default_bg)
241-
self.splash.append(self._bg_group)
242260

243261
self._qr_group = None
244262

@@ -318,7 +336,7 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
318336

319337
gc.collect()
320338

321-
def set_background(self, file_or_color):
339+
def set_background(self, file_or_color, position=None):
322340
"""The background image to a bitmap file.
323341
324342
:param file_or_color: The filename of the chosen background image, or a hex color.
@@ -328,6 +346,9 @@ def set_background(self, file_or_color):
328346
while self._bg_group:
329347
self._bg_group.pop()
330348

349+
if not position:
350+
position = (0, 0) # default in top corner
351+
331352
if not file_or_color:
332353
return # we're done, no background desired
333354
if self._bg_file:
@@ -337,7 +358,7 @@ def set_background(self, file_or_color):
337358
background = displayio.OnDiskBitmap(self._bg_file)
338359
self._bg_sprite = displayio.TileGrid(background,
339360
pixel_shader=displayio.ColorConverter(),
340-
position=(0, 0))
361+
position=position)
341362
elif isinstance(file_or_color, int):
342363
# Make a background color fill
343364
color_bitmap = displayio.Bitmap(320, 240, 1)
@@ -570,12 +591,13 @@ def _connect_esp(self):
570591
# secrets dictionary must contain 'ssid' and 'password' at a minimum
571592
print("Connecting to AP", secrets['ssid'])
572593
if secrets['ssid'] == 'CHANGE ME' or secrets['ssid'] == 'CHANGE ME':
573-
print("*"*45)
574-
print("Please update the 'secrets.py' file on your")
575-
print("CIRCUITPY drive to include your local access")
576-
print("point SSID name in 'ssid' and SSID password")
577-
print("in 'password'. Then save to reload!")
578-
print("*"*45)
594+
change_me = "\n"+"*"*45
595+
change_me += "\nPlease update the 'secrets.py' file on your\n"
596+
change_me += "CIRCUITPY drive to include your local WiFi\n"
597+
change_me += "access point SSID name in 'ssid' and SSID\n"
598+
change_me += "password in 'password'. Then save to reload!\n"
599+
change_me += "*"*45
600+
raise OSError(change_me)
579601
self.neo_status((100, 0, 0)) # red = not connected
580602
try:
581603
self._esp.connect(secrets)
@@ -688,7 +710,7 @@ def fetch(self):
688710
except OSError as error:
689711
print(error)
690712
raise OSError("""\n\nNo writable filesystem found for saving datastream. Insert an SD card or set internal filesystem to be unsafe by setting 'disable_concurrent_write_protection' in the mount options in boot.py""") # pylint: disable=line-too-long
691-
self.set_background(filename)
713+
self.set_background(filename, self._image_position)
692714
except ValueError as error:
693715
print("Error displaying cached image. " + error.args[0])
694716
self.set_background(self._default_bg)
@@ -717,7 +739,8 @@ def fetch(self):
717739
if self._text_wrap[i]:
718740
if self._debug:
719741
print("Wrapping text")
720-
string = '\n'.join(PyPortal.wrap_nicely(string, self._text_wrap[i]))
742+
lines = PyPortal.wrap_nicely(string, self._text_wrap[i])
743+
string = '\n'.join(lines)
721744
self.set_text(string, index=i)
722745
if len(values) == 1:
723746
return values[0]
@@ -807,6 +830,7 @@ def wrap_nicely(string, max_chars):
807830
:param int max_chars: The maximum number of characters on a line before wrapping.
808831
809832
"""
833+
string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
810834
words = string.split(' ')
811835
the_lines = []
812836
the_line = ""
@@ -818,4 +842,6 @@ def wrap_nicely(string, max_chars):
818842
the_line = ''+w
819843
if the_line: # last line remaining
820844
the_lines.append(the_line)
845+
# remove first space from first line:
846+
the_lines[0] = the_lines[0][1:]
821847
return the_lines

0 commit comments

Comments
 (0)