Skip to content

Commit b9fd8cf

Browse files
Merge pull request #1 from adafruit/master
updates since my previous PR
2 parents d2dec13 + 1416bc0 commit b9fd8cf

File tree

3 files changed

+102
-75
lines changed

3 files changed

+102
-75
lines changed

README.rst

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,52 +39,3 @@ Contributions are welcome! Please read our `Code of Conduct
3939
<https://github.com/adafruit/Adafruit_CircuitPython_PyPortal/blob/master/CODE_OF_CONDUCT.md>`_
4040
before contributing to help this project stay welcoming.
4141

42-
Building locally
43-
================
44-
45-
Zip release files
46-
-----------------
47-
48-
To build this library locally you'll need to install the
49-
`circuitpython-build-tools <https://github.com/adafruit/circuitpython-build-tools>`_ package.
50-
51-
.. code-block:: shell
52-
53-
python3 -m venv .env
54-
source .env/bin/activate
55-
pip install circuitpython-build-tools
56-
57-
Once installed, make sure you are in the virtual environment:
58-
59-
.. code-block:: shell
60-
61-
source .env/bin/activate
62-
63-
Then run the build:
64-
65-
.. code-block:: shell
66-
67-
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-pyportal --library_location .
68-
69-
Sphinx documentation
70-
-----------------------
71-
72-
Sphinx is used to build the documentation based on rST files and comments in the code. First,
73-
install dependencies (feel free to reuse the virtual environment from above):
74-
75-
.. code-block:: shell
76-
77-
python3 -m venv .env
78-
source .env/bin/activate
79-
pip install Sphinx sphinx-rtd-theme
80-
81-
Now, once you have the virtual environment activated:
82-
83-
.. code-block:: shell
84-
85-
cd docs
86-
sphinx-build -E -W -b html . _build/html
87-
88-
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
89-
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
90-
locally verify it will pass.

adafruit_pyportal.py

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,32 @@
5050
import busio
5151
from digitalio import DigitalInOut
5252
import pulseio
53-
import adafruit_touchscreen
5453
import neopixel
54+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
55+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
56+
import adafruit_requests as requests
57+
import storage
58+
import displayio
59+
import audioio
60+
import rtc
61+
import supervisor
62+
from adafruit_bitmap_font import bitmap_font
63+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
64+
import adafruit_sdcard
65+
66+
67+
68+
if hasattr(board, 'TOUCH_XL'):
69+
import adafruit_touchscreen
70+
elif hasattr(board, 'BUTTON_CLOCK'):
71+
from adafruit_cursorcontrol.cursorcontrol import Cursor
72+
from adafruit_cursorcontrol.cursorcontrol_cursormanager import CursorManager
5573

56-
from adafruit_esp32spi import adafruit_esp32spi
57-
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
5874
try:
5975
from adafruit_display_text.text_area import TextArea # pylint: disable=unused-import
6076
print("*** WARNING ***\nPlease update your library bundle to the latest 'adafruit_display_text' version as we've deprecated 'text_area' in favor of 'label'") # pylint: disable=line-too-long
6177
except ImportError:
6278
from adafruit_display_text.Label import Label
63-
from adafruit_bitmap_font import bitmap_font
64-
65-
import storage
66-
import adafruit_sdcard
67-
import displayio
68-
import audioio
69-
import rtc
70-
import supervisor
7179

7280
try:
7381
from secrets import secrets
@@ -165,7 +173,10 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
165173
self._debug = debug
166174

167175
try:
168-
self._backlight = pulseio.PWMOut(board.TFT_BACKLIGHT) # pylint: disable=no-member
176+
if hasattr(board, 'TFT_BACKLIGHT'):
177+
self._backlight = pulseio.PWMOut(board.TFT_BACKLIGHT) # pylint: disable=no-member
178+
elif hasattr(board, 'TFT_LITE'):
179+
self._backlight = pulseio.PWMOut(board.TFT_LITE) # pylint: disable=no-member
169180
except ValueError:
170181
self._backlight = None
171182
self.set_backlight(1.0) # turn on backlight
@@ -225,7 +236,12 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
225236

226237
self._speaker_enable = DigitalInOut(board.SPEAKER_ENABLE)
227238
self._speaker_enable.switch_to_output(False)
228-
self.audio = audioio.AudioOut(board.AUDIO_OUT)
239+
if hasattr(board, 'AUDIO_OUT'):
240+
self.audio = audioio.AudioOut(board.AUDIO_OUT)
241+
elif hasattr(board, 'SPEAKER'):
242+
self.audio = audioio.AudioOut(board.SPEAKER)
243+
else:
244+
raise AttributeError('Board does not have a builtin speaker!')
229245
try:
230246
self.play_file("pyportal_startup.wav")
231247
except OSError:
@@ -261,11 +277,14 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
261277
self._esp.reset()
262278
else:
263279
raise RuntimeError("Was not able to find ESP32")
264-
requests.set_interface(self._esp)
280+
requests.set_socket(socket, self._esp)
265281

266282
if url and not self._uselocal:
267283
self._connect_esp()
268284

285+
if self._debug:
286+
print("My IP address is", self._esp.pretty_ip(self._esp.ip_address))
287+
269288
# set the default background
270289
self.set_background(self._default_bg)
271290
board.DISPLAY.show(self.splash)
@@ -353,20 +372,35 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
353372
self._image_position = (0, 0) # default to top corner
354373
if not self._image_resize:
355374
self._image_resize = (320, 240) # default to full screen
375+
if hasattr(board, 'TOUCH_XL'):
376+
if self._debug:
377+
print("Init touchscreen")
378+
# pylint: disable=no-member
379+
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
380+
board.TOUCH_YD, board.TOUCH_YU,
381+
calibration=((5200, 59000),
382+
(5800, 57000)),
383+
size=(320, 240))
384+
# pylint: enable=no-member
385+
386+
self.set_backlight(1.0) # turn on backlight
387+
elif hasattr(board, 'BUTTON_CLOCK'):
388+
if self._debug:
389+
print("Init cursor")
390+
self.mouse_cursor = Cursor(board.DISPLAY, display_group=self.splash, cursor_speed=8)
391+
self.mouse_cursor.hide()
392+
self.cursor = CursorManager(self.mouse_cursor)
393+
else:
394+
raise AttributeError('PyPortal module requires either a touchscreen or gamepad.')
356395

357-
if self._debug:
358-
print("Init touchscreen")
359-
# pylint: disable=no-member
360-
self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
361-
board.TOUCH_YD, board.TOUCH_YU,
362-
calibration=((5200, 59000),
363-
(5800, 57000)),
364-
size=(320, 240))
365-
# pylint: enable=no-member
396+
gc.collect()
366397

367-
self.set_backlight(1.0) # turn on backlight
398+
def set_headers(self, headers):
399+
"""Set the headers used by fetch().
368400
369-
gc.collect()
401+
:param headers: The new header dictionary
402+
"""
403+
self._headers = headers
370404

371405
def set_background(self, file_or_color, position=None):
372406
"""The background image to a bitmap file.
@@ -673,6 +707,47 @@ def image_converter_url(image_url, width, height, color_depth=16):
673707
width, height,
674708
color_depth, image_url)
675709

710+
def push_to_io(self, feed_key, data):
711+
# pylint: disable=line-too-long
712+
"""Push data to an adafruit.io feed
713+
714+
:param str feed_key: Name of feed key to push data to.
715+
:param data: data to send to feed
716+
717+
"""
718+
# pylint: enable=line-too-long
719+
720+
try:
721+
aio_username = secrets['aio_username']
722+
aio_key = secrets['aio_key']
723+
except KeyError:
724+
raise KeyError("Adafruit IO secrets are kept in secrets.py, please add them there!\n\n")
725+
726+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None)
727+
io_client = IO_HTTP(aio_username, aio_key, wifi)
728+
729+
while True:
730+
try:
731+
feed_id = io_client.get_feed(feed_key)
732+
except AdafruitIO_RequestError:
733+
# If no feed exists, create one
734+
feed_id = io_client.create_new_feed(feed_key)
735+
except RuntimeError as exception:
736+
print("An error occured, retrying! 1 -", exception)
737+
continue
738+
break
739+
740+
while True:
741+
try:
742+
io_client.send_data(feed_id['key'], data)
743+
except RuntimeError as exception:
744+
print("An error occured, retrying! 2 -", exception)
745+
continue
746+
except NameError as exception:
747+
print(feed_id['key'], data, exception)
748+
continue
749+
break
750+
676751
def fetch(self, refresh_url=None):
677752
"""Fetch data from the url we initialized with, perfom any parsing,
678753
and display text or graphics. This function does pretty much everything

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
autodoc_mock_imports = ["rtc", "supervisor", "pulseio", "audioio", "displayio", "neopixel",
2424
"microcontroller", "adafruit_touchscreen", "adafruit_bitmap_font",
2525
"adafruit_display_text", "adafruit_esp32spi", "secrets",
26-
"adafruit_sdcard", "storage"]
26+
"adafruit_sdcard", "storage", "adafruit_io", "adafruit_cursorcontrol",
27+
"adafruit_requests"]
2728

2829

2930
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

0 commit comments

Comments
 (0)