Skip to content

Commit b5a68df

Browse files
committed
add startup splash screens if they exist, and oserror on unset ssid/pass
1 parent c713981 commit b5a68df

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

adafruit_pyportal.py

Lines changed: 51 additions & 28 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

@@ -573,12 +591,13 @@ def _connect_esp(self):
573591
# secrets dictionary must contain 'ssid' and 'password' at a minimum
574592
print("Connecting to AP", secrets['ssid'])
575593
if secrets['ssid'] == 'CHANGE ME' or secrets['ssid'] == 'CHANGE ME':
576-
print("*"*45)
577-
print("Please update the 'secrets.py' file on your")
578-
print("CIRCUITPY drive to include your local access")
579-
print("point SSID name in 'ssid' and SSID password")
580-
print("in 'password'. Then save to reload!")
581-
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)
582601
self.neo_status((100, 0, 0)) # red = not connected
583602
try:
584603
self._esp.connect(secrets)
@@ -720,7 +739,8 @@ def fetch(self):
720739
if self._text_wrap[i]:
721740
if self._debug:
722741
print("Wrapping text")
723-
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)
724744
self.set_text(string, index=i)
725745
if len(values) == 1:
726746
return values[0]
@@ -810,6 +830,7 @@ def wrap_nicely(string, max_chars):
810830
:param int max_chars: The maximum number of characters on a line before wrapping.
811831
812832
"""
833+
string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
813834
words = string.split(' ')
814835
the_lines = []
815836
the_line = ""
@@ -821,4 +842,6 @@ def wrap_nicely(string, max_chars):
821842
the_line = ''+w
822843
if the_line: # last line remaining
823844
the_lines.append(the_line)
845+
# remove first space from first line:
846+
the_lines[0] = the_lines[0][1:]
824847
return the_lines

0 commit comments

Comments
 (0)