Skip to content

Commit 970569a

Browse files
authored
Merge pull request #2 from ladyada/master
some more comments!
2 parents 58ffe2b + 9bdbfca commit 970569a

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

adafruit_pyportal.py

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
import busio
5151
import microcontroller
5252
from digitalio import DigitalInOut
53-
import adafruit_touchscreen
5453
import pulseio
54+
import adafruit_touchscreen
5555
import neopixel
5656

5757
from adafruit_esp32spi import adafruit_esp32spi
@@ -84,14 +84,14 @@
8484

8585

8686
class Fake_Requests:
87-
"""For requests using a local file instead of the network."""
87+
"""For faking 'requests' using a local file instead of the network."""
8888
def __init__(self, filename):
8989
self._filename = filename
9090
with open(filename, "r") as file:
9191
self.text = file.read()
9292

9393
def json(self):
94-
"""json for local requests."""
94+
"""json parsed version for local requests."""
9595
import json
9696
return json.loads(self.text)
9797

@@ -100,38 +100,48 @@ class PyPortal:
100100
"""Class representing the Adafruit PyPortal.
101101
102102
:param url: The URL of your data source. Defaults to ``None``.
103-
:param json_path: Defaults to ``None``.
104-
:param regexp_path: Defaults to ``None``.
105-
:param default_bg: The path to your default background file. Defaults to ``None``.
106-
:param status_neopixel: The pin for the status NeoPixel. Use ``board.NeoPixel`` for the
107-
on-board NeoPixel. Defaults to ``None``.
108-
:param str text_font: The path to your font file for your text.
109-
:param text_position: The position of your text on the display.
110-
:param text_color: The color of the text. Defaults to ``None``.
111-
:param text_wrap: The location where the text wraps. Defaults to ``None``.
112-
:param text_maxlen: The max length of the text. Defaults to ``None``.
113-
:param image_json_path: Defaults to ``None``.
114-
:param image_resize: Defaults to ``None``.
115-
:param image_position: The position of the image on the display. Defaults to ``None``.
116-
:param time_between_requests: Defaults to ``None``.
117-
:param success_callback: Defaults to ``None``.
118-
:param str caption_text: The text of your caption. Defaults to ``None``.
103+
:param json_path: The list of json traversal to get data out of. Can be list of lists for
104+
multiple data points. Defaults to ``None`` to not use json.
105+
:param regexp_path: The list of regexp strings to get data out (use a single regexp group). Can
106+
be list of regexps for multiple data points. Defaults to ``None`` to not
107+
use regexp.
108+
:param default_bg: The path to your default background image file. Defaults to ``None``.
109+
:param status_neopixel: The pin for the status NeoPixel. Use ``board.NEOPIXEL`` for the on-board
110+
NeoPixel. Defaults to ``None``, no status LED
111+
:param str text_font: The path to your font file for your data text display.
112+
:param text_position: The position of your extracted text on the display in an (x, y) tuple.
113+
Can be a list of tuples for when there's a list of json_paths, for example
114+
:param text_color: The color of the text, in 0xRRGGBB format. Can be a list of colors for when
115+
there's multiple texts. Defaults to ``None``.
116+
:param text_wrap: Whether or not to wrap text (for long text data chunks). Defaults to
117+
``False``, no wrapping.
118+
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
119+
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
120+
``None``.
121+
:param image_resize: What size to resize the image we got from the json_path, make this a tuple
122+
of the width and height you want. Defaults to ``None``.
123+
:param image_position: The position of the image on the display as an (x, y) tuple. Defaults to
124+
``None``.
125+
:param success_callback: A function we'll call if you like, when we fetch data successfully.
126+
Defaults to ``None``.
127+
:param str caption_text: The text of your caption, a fixed text not changed by the data we get.
128+
Defaults to ``None``.
119129
:param str caption_font: The path to the font file for your caption. Defaults to ``None``.
120-
:param caption_position: The position of your caption on the display. Defaults to ``None``.
130+
:param caption_position: The position of your caption on the display as an (x, y) tuple.
131+
Defaults to ``None``.
121132
:param caption_color: The color of your caption. Must be a hex value, e.g. ``0x808000``.
122-
:param debug: Turn on debug features. Defaults to False.
133+
:param debug: Turn on debug print outs. Defaults to False.
123134
124135
"""
125136
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
126137
def __init__(self, *, url=None, json_path=None, regexp_path=None,
127138
default_bg=None, status_neopixel=None,
128139
text_font=None, text_position=None, text_color=0x808080,
129-
text_wrap=0, text_maxlen=0,
140+
text_wrap=False, text_maxlen=0,
130141
image_json_path=None, image_resize=None, image_position=None,
131-
time_between_requests=60, success_callback=None,
132142
caption_text=None, caption_font=None, caption_position=None,
133143
caption_color=0x808080,
134-
debug=False):
144+
success_callback=None, debug=False):
135145

136146
self._debug = debug
137147

@@ -151,7 +161,6 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
151161
self._json_path = None
152162

153163
self._regexp_path = regexp_path
154-
self._time_between_requests = time_between_requests
155164
self._success_callback = success_callback
156165

157166
if status_neopixel:
@@ -170,16 +179,20 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
170179
if self._debug:
171180
print("Init ESP32")
172181
# pylint: disable=no-member
173-
esp32_cs = DigitalInOut(microcontroller.pin.PB14) # PB14
182+
esp32_cs = DigitalInOut(microcontroller.pin.PB14)
174183
esp32_ready = DigitalInOut(microcontroller.pin.PB16)
175184
esp32_gpio0 = DigitalInOut(microcontroller.pin.PB15)
176185
esp32_reset = DigitalInOut(microcontroller.pin.PB17)
186+
#esp32_ready = DigitalInOut(board.ESP_BUSY)
187+
#esp32_gpio0 = DigitalInOut(board.ESP_GPIO0)
188+
#esp32_reset = DigitalInOut(board.ESP_RESET)
189+
#esp32_cs = DigitalInOut(board.ESP_CS)
177190
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
178191
# pylint: enable=no-member
179192

180193
if not self._uselocal:
181-
self._esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset,
182-
esp32_gpio0)
194+
self._esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready,
195+
esp32_reset, esp32_gpio0)
183196
#self._esp._debug = 1
184197
for _ in range(3): # retries
185198
try:
@@ -280,7 +293,7 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
280293
gc.collect()
281294

282295
def set_background(self, filename):
283-
"""The background image.
296+
"""The background image to a bitmap file.
284297
285298
:param filename: The name of the chosen background image file.
286299
@@ -311,7 +324,7 @@ def set_background(self, filename):
311324
board.DISPLAY.wait_for_frame()
312325

313326
def set_backlight(self, val):
314-
"""The backlight.
327+
"""Adjust the TFT backlight.
315328
316329
:param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
317330
off, and ``1`` is 100% brightness.
@@ -325,34 +338,39 @@ def set_backlight(self, val):
325338
board.DISPLAY.brightness = val
326339

327340
def preload_font(self, glyphs=None):
341+
# pylint: disable=line-too-long
328342
"""Preload font.
329343
330-
:param glyphs: The font glyphs to load. Defaults to ``None``, uses built in glyphs if None.
344+
:param glyphs: The font glyphs to load. Defaults to ``None``, uses alphanumeric glyphs if
345+
None.
331346
332347
"""
348+
# pylint: enable=line-too-long
333349
if not glyphs:
334350
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. "\'?!'
335351
print("Preloading font glyphs:", glyphs)
336352
if self._text_font:
337353
self._text_font.load_glyphs(glyphs)
338354

339355
def set_caption(self, caption_text, caption_position, caption_color):
356+
# pylint: disable=line-too-long
340357
"""A caption. Requires setting ``caption_font`` in init!
341358
342359
:param caption_text: The text of the caption.
343360
:param caption_position: The position of the caption text.
344-
:param caption_color: The color of your caption text. Must be a hex value,
345-
e.g. ``0x808000``.
361+
:param caption_color: The color of your caption text. Must be a hex value, e.g.
362+
``0x808000``.
346363
347364
"""
365+
# pylint: enable=line-too-long
348366
if self._debug:
349367
print("Setting caption to", caption_text)
350368

351369
if (not caption_text) or (not self._caption_font) or (not caption_position):
352370
return # nothing to do!
353371

354372
if self._caption:
355-
self._caption._update_text(str(caption_text)) # pylint: disable=protected-access, undefined-variable
373+
self._caption._update_text(str(caption_text)) # pylint: disable=protected-access
356374
board.DISPLAY.refresh_soon()
357375
board.DISPLAY.wait_for_frame()
358376
return
@@ -364,9 +382,9 @@ def set_caption(self, caption_text, caption_position, caption_color):
364382
self.splash.append(self._caption)
365383

366384
def set_text(self, val, index=0):
367-
"""Display text.
385+
"""Display text, with indexing into our list of text boxes.
368386
369-
:param str val: The text to be displayed.
387+
:param str val: The text to be displayed
370388
:param index: Defaults to 0.
371389
372390
"""
@@ -402,7 +420,7 @@ def set_text(self, val, index=0):
402420
self.splash.append(self._text[index])
403421

404422
def neo_status(self, value):
405-
"""The status NeoPixeel.
423+
"""The status NeoPixel.
406424
407425
:param value: The color to change the NeoPixel.
408426
@@ -414,7 +432,7 @@ def neo_status(self, value):
414432
def play_file(file_name):
415433
"""Play a wav file.
416434
417-
:param str file_name: The name of the wav file.
435+
:param str file_name: The name of the wav file to play on the speaker.
418436
419437
"""
420438
#self._speaker_enable.value = True
@@ -435,11 +453,13 @@ def _json_traverse(json, path):
435453
return value
436454

437455
def get_local_time(self, location=None):
438-
"""The local time.
456+
# pylint: disable=line-too-long
457+
"""Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
439458
440-
:param str location: Your city and state, e.g. ``"New York, New York"``.
459+
:param str location: Your city and country, e.g. ``"New York, US"``.
441460
442461
"""
462+
# pylint: enable=line-too-long
443463
self._connect_esp()
444464
api_url = None
445465
if not location:
@@ -469,10 +489,10 @@ def get_local_time(self, location=None):
469489
gc.collect()
470490

471491
def wget(self, url, filename):
472-
"""Obtain a stream.
492+
"""Download a url and save to filename location, like the command wget.
473493
474494
:param url: The URL from which to obtain the data.
475-
:param filename: The name of the file to save the data.
495+
:param filename: The name of the file to save the data to.
476496
477497
"""
478498
print("Fetching stream from", url)
@@ -514,7 +534,8 @@ def _connect_esp(self):
514534
self._esp.connect(settings)
515535

516536
def fetch(self):
517-
"""Fetch data."""
537+
"""Fetch data from the url we initialized with, perfom any parsing,
538+
and display text or graphics. This function does pretty much everything"""
518539
json_out = None
519540
image_url = None
520541
values = []
@@ -618,11 +639,11 @@ def fetch(self):
618639
return values
619640

620641
def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=invalid-name
621-
"""Display a QR code.
642+
"""Display a QR code on the TFT
622643
623644
:param qr_data: The data for the QR code.
624645
:param int qr_size: The size of the QR code in pixels.
625-
:param position: The position of the QR code on the display.
646+
:param position: The (x, y) tuple position of the QR code on the display.
626647
627648
"""
628649
import adafruit_miniqr
@@ -674,7 +695,7 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
674695

675696
for b in range(BLOCK_SIZE):
676697
# load this line of data in, as many time as block size
677-
qr_bitmap._load_row(Y_OFFSET + y*BLOCK_SIZE+b, line) #pylint: disable=protected-access
698+
qr_bitmap._load_row(Y_OFFSET + y*BLOCK_SIZE+b, line) # pylint: disable=protected-access
678699
# pylint: enable=invalid-name
679700

680701
# display the bitmap using our palette
@@ -694,7 +715,7 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
694715
# return a list of lines with wordwrapping
695716
@staticmethod
696717
def wrap_nicely(string, max_chars):
697-
"""A list of lines with word wrapping.
718+
"""A helper that will return a list of lines with word-break wrapping.
698719
699720
:param str string: The text to be wrapped.
700721
:param int max_chars: The maximum number of characters on a line before wrapping.

0 commit comments

Comments
 (0)