50
50
import busio
51
51
import microcontroller
52
52
from digitalio import DigitalInOut
53
- import adafruit_touchscreen
54
53
import pulseio
54
+ import adafruit_touchscreen
55
55
import neopixel
56
56
57
57
from adafruit_esp32spi import adafruit_esp32spi
84
84
85
85
86
86
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."""
88
88
def __init__ (self , filename ):
89
89
self ._filename = filename
90
90
with open (filename , "r" ) as file :
91
91
self .text = file .read ()
92
92
93
93
def json (self ):
94
- """json for local requests."""
94
+ """json parsed version for local requests."""
95
95
import json
96
96
return json .loads (self .text )
97
97
@@ -100,38 +100,48 @@ class PyPortal:
100
100
"""Class representing the Adafruit PyPortal.
101
101
102
102
: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``.
119
129
: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``.
121
132
: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.
123
134
124
135
"""
125
136
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements
126
137
def __init__ (self , * , url = None , json_path = None , regexp_path = None ,
127
138
default_bg = None , status_neopixel = None ,
128
139
text_font = None , text_position = None , text_color = 0x808080 ,
129
- text_wrap = 0 , text_maxlen = 0 ,
140
+ text_wrap = False , text_maxlen = 0 ,
130
141
image_json_path = None , image_resize = None , image_position = None ,
131
- time_between_requests = 60 , success_callback = None ,
132
142
caption_text = None , caption_font = None , caption_position = None ,
133
143
caption_color = 0x808080 ,
134
- debug = False ):
144
+ success_callback = None , debug = False ):
135
145
136
146
self ._debug = debug
137
147
@@ -151,7 +161,6 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
151
161
self ._json_path = None
152
162
153
163
self ._regexp_path = regexp_path
154
- self ._time_between_requests = time_between_requests
155
164
self ._success_callback = success_callback
156
165
157
166
if status_neopixel :
@@ -170,16 +179,20 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
170
179
if self ._debug :
171
180
print ("Init ESP32" )
172
181
# pylint: disable=no-member
173
- esp32_cs = DigitalInOut (microcontroller .pin .PB14 ) # PB14
182
+ esp32_cs = DigitalInOut (microcontroller .pin .PB14 )
174
183
esp32_ready = DigitalInOut (microcontroller .pin .PB16 )
175
184
esp32_gpio0 = DigitalInOut (microcontroller .pin .PB15 )
176
185
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)
177
190
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
178
191
# pylint: enable=no-member
179
192
180
193
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 )
183
196
#self._esp._debug = 1
184
197
for _ in range (3 ): # retries
185
198
try :
@@ -280,7 +293,7 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
280
293
gc .collect ()
281
294
282
295
def set_background (self , filename ):
283
- """The background image.
296
+ """The background image to a bitmap file .
284
297
285
298
:param filename: The name of the chosen background image file.
286
299
@@ -311,7 +324,7 @@ def set_background(self, filename):
311
324
board .DISPLAY .wait_for_frame ()
312
325
313
326
def set_backlight (self , val ):
314
- """The backlight.
327
+ """Adjust the TFT backlight.
315
328
316
329
:param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
317
330
off, and ``1`` is 100% brightness.
@@ -325,34 +338,39 @@ def set_backlight(self, val):
325
338
board .DISPLAY .brightness = val
326
339
327
340
def preload_font (self , glyphs = None ):
341
+ # pylint: disable=line-too-long
328
342
"""Preload font.
329
343
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.
331
346
332
347
"""
348
+ # pylint: enable=line-too-long
333
349
if not glyphs :
334
350
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. "\' ?!'
335
351
print ("Preloading font glyphs:" , glyphs )
336
352
if self ._text_font :
337
353
self ._text_font .load_glyphs (glyphs )
338
354
339
355
def set_caption (self , caption_text , caption_position , caption_color ):
356
+ # pylint: disable=line-too-long
340
357
"""A caption. Requires setting ``caption_font`` in init!
341
358
342
359
:param caption_text: The text of the caption.
343
360
: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``.
346
363
347
364
"""
365
+ # pylint: enable=line-too-long
348
366
if self ._debug :
349
367
print ("Setting caption to" , caption_text )
350
368
351
369
if (not caption_text ) or (not self ._caption_font ) or (not caption_position ):
352
370
return # nothing to do!
353
371
354
372
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
356
374
board .DISPLAY .refresh_soon ()
357
375
board .DISPLAY .wait_for_frame ()
358
376
return
@@ -364,9 +382,9 @@ def set_caption(self, caption_text, caption_position, caption_color):
364
382
self .splash .append (self ._caption )
365
383
366
384
def set_text (self , val , index = 0 ):
367
- """Display text.
385
+ """Display text, with indexing into our list of text boxes .
368
386
369
- :param str val: The text to be displayed.
387
+ :param str val: The text to be displayed
370
388
:param index: Defaults to 0.
371
389
372
390
"""
@@ -402,7 +420,7 @@ def set_text(self, val, index=0):
402
420
self .splash .append (self ._text [index ])
403
421
404
422
def neo_status (self , value ):
405
- """The status NeoPixeel .
423
+ """The status NeoPixel .
406
424
407
425
:param value: The color to change the NeoPixel.
408
426
@@ -414,7 +432,7 @@ def neo_status(self, value):
414
432
def play_file (file_name ):
415
433
"""Play a wav file.
416
434
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 .
418
436
419
437
"""
420
438
#self._speaker_enable.value = True
@@ -435,11 +453,13 @@ def _json_traverse(json, path):
435
453
return value
436
454
437
455
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.
439
458
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 "``.
441
460
442
461
"""
462
+ # pylint: enable=line-too-long
443
463
self ._connect_esp ()
444
464
api_url = None
445
465
if not location :
@@ -469,10 +489,10 @@ def get_local_time(self, location=None):
469
489
gc .collect ()
470
490
471
491
def wget (self , url , filename ):
472
- """Obtain a stream .
492
+ """Download a url and save to filename location, like the command wget .
473
493
474
494
: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 .
476
496
477
497
"""
478
498
print ("Fetching stream from" , url )
@@ -514,7 +534,8 @@ def _connect_esp(self):
514
534
self ._esp .connect (settings )
515
535
516
536
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"""
518
539
json_out = None
519
540
image_url = None
520
541
values = []
@@ -618,11 +639,11 @@ def fetch(self):
618
639
return values
619
640
620
641
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
622
643
623
644
:param qr_data: The data for the QR code.
624
645
: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.
626
647
627
648
"""
628
649
import adafruit_miniqr
@@ -674,7 +695,7 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
674
695
675
696
for b in range (BLOCK_SIZE ):
676
697
# 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
678
699
# pylint: enable=invalid-name
679
700
680
701
# display the bitmap using our palette
@@ -694,7 +715,7 @@ def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=inval
694
715
# return a list of lines with wordwrapping
695
716
@staticmethod
696
717
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.
698
719
699
720
:param str string: The text to be wrapped.
700
721
:param int max_chars: The maximum number of characters on a line before wrapping.
0 commit comments