Skip to content

Commit 8747da3

Browse files
committed
Merge branch 'master' of github.com:ladyada/Adafruit_CircuitPython_PyPortal
2 parents ab7fa0e + b5a68df commit 8747da3

File tree

1 file changed

+71
-38
lines changed

1 file changed

+71
-38
lines changed

adafruit_pyportal.py

Lines changed: 71 additions & 38 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=15)
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,10 @@ 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+
board.DISPLAY.show(self.splash)
249+
210250
if self._debug:
211251
print("Init SD Card")
212252
sd_cs = DigitalInOut(board.SD_CS)
@@ -218,28 +258,6 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
218258
except OSError as error:
219259
print("No SD card found:", error)
220260

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=15)
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)
242-
243261
self._qr_group = None
244262

245263
if self._debug:
@@ -571,19 +589,36 @@ def _connect_esp(self):
571589
# secrets dictionary must contain 'ssid' and 'password' at a minimum
572590
print("Connecting to AP", secrets['ssid'])
573591
if secrets['ssid'] == 'CHANGE ME' or secrets['ssid'] == 'CHANGE ME':
574-
print("*"*45)
575-
print("Please update the 'secrets.py' file on your")
576-
print("CIRCUITPY drive to include your local access")
577-
print("point SSID name in 'ssid' and SSID password")
578-
print("in 'password'. Then save to reload!")
579-
print("*"*45)
592+
change_me = "\n"+"*"*45
593+
change_me += "\nPlease update the 'secrets.py' file on your\n"
594+
change_me += "CIRCUITPY drive to include your local WiFi\n"
595+
change_me += "access point SSID name in 'ssid' and SSID\n"
596+
change_me += "password in 'password'. Then save to reload!\n"
597+
change_me += "*"*45
598+
raise OSError(change_me)
580599
self.neo_status((100, 0, 0)) # red = not connected
581600
try:
582601
self._esp.connect(secrets)
583602
except RuntimeError as error:
584603
print("Cound not connect to internet", error)
585604
print("Retrying in 3 seconds...")
586605
time.sleep(3)
606+
607+
@staticmethod
608+
def image_converter_url(image_url, width, height, color_depth=16):
609+
"""Generate a converted image url from the url passed in,
610+
with the given width and height. aio_username and aio_key must be
611+
set in secrets."""
612+
try:
613+
aio_username = secrets['aio_username']
614+
aio_key = secrets['aio_key']
615+
except KeyError:
616+
raise KeyError("\n\nOur image converter service require a login/password to rate-limit. Please register for a freeadafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
617+
618+
return IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
619+
width, height,
620+
color_depth, image_url)
621+
587622
def fetch(self):
588623
"""Fetch data from the url we initialized with, perfom any parsing,
589624
and display text or graphics. This function does pretty much everything"""
@@ -655,17 +690,11 @@ def fetch(self):
655690
gc.collect()
656691

657692
if image_url:
658-
try:
659-
aio_username = secrets['aio_username']
660-
aio_key = secrets['aio_key']
661-
except KeyError:
662-
raise KeyError("\n\nOur image converter service require a login/password to rate-limit. Please register for a freeadafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
663693
try:
664694
print("original URL:", image_url)
665-
image_url = IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
666-
self._image_resize[0],
667-
self._image_resize[1],
668-
16, image_url)
695+
image_url = self.image_converter_url(image_url,
696+
self._image_resize[0],
697+
self._image_resize[1])
669698
print("convert URL:", image_url)
670699
# convert image to bitmap and cache
671700
#print("**not actually wgetting**")
@@ -708,7 +737,8 @@ def fetch(self):
708737
if self._text_wrap[i]:
709738
if self._debug:
710739
print("Wrapping text")
711-
string = '\n'.join(PyPortal.wrap_nicely(string, self._text_wrap[i]))
740+
lines = PyPortal.wrap_nicely(string, self._text_wrap[i])
741+
string = '\n'.join(lines)
712742
self.set_text(string, index=i)
713743
if len(values) == 1:
714744
return values[0]
@@ -798,6 +828,7 @@ def wrap_nicely(string, max_chars):
798828
:param int max_chars: The maximum number of characters on a line before wrapping.
799829
800830
"""
831+
string = string.replace('\n', '').replace('\r', '') # strip confusing newlines
801832
words = string.split(' ')
802833
the_lines = []
803834
the_line = ""
@@ -811,4 +842,6 @@ def wrap_nicely(string, max_chars):
811842
the_line = ''+w
812843
if the_line: # last line remaining
813844
the_lines.append(the_line)
845+
# remove first space from first line:
846+
the_lines[0] = the_lines[0][1:]
814847
return the_lines

0 commit comments

Comments
 (0)