adafruit / Adafruit_CircuitPython_DisplayIO_Layout

A Circuitpython helper library for display element layout using displayio.
MIT License
10 stars 14 forks source link

Hotplug sensor examples #73

Closed PaulskPt closed 2 years ago

PaulskPt commented 2 years ago

This is my first PR. I was guided all the way towards this PR by @FoamyGuy. This PR adds two examples that both have a functionality to hotplug external sensors. Both examples use the new TabLayout class that @FoamyGuy recently has added to the Adafruit_CircuitPython_DisplayIO_Layout library. The second example uses the Touch capabilities of the PyPortal.

The particularities of the in this PR presented 'hotplug sensor' algorithm are:

The second example is an expanded version of the first example. As the first example also this script is able to read data from an external temperature sensor. This script is able to use an external RTC. The preferred setting is to use a NTP server. The script uses the Adafruit TouchScreen library. For the touch of the Tabs to be reliable a calibration is advised. I made use of the calibration script by CedarGrove Studios ( https://github.com/CedarGroveStudios/Touchscreen_Calibrator/blob/main/touch_calibrator_built_in.py). The calibration results have to be inserted at line 410 ( ts = adafruit_touchscreen.Touchscreen()), replacing what is there at 'calibration=((6815, 60095), (10520, 58007)),' Both example scripts use a gVars class to set/get globally used variables. This eliminates the need to use global variables. The gVars class has been created because we were confronted with pylint warnings about using global variables. Both example scripts show the date and time in the Tab 'Three'. Both examples have a gVars flag "temp_in_fahrenheit". When set to 'True' the temperature will be displayed in degrees Fahrenheit. When set to 'False' in degrees Celsius. See image #4 below. They display the temperature from the sensor in Tab 'Four'. The second example script will notify the user in Tab 'Three' how many mm:ss a next NTP synchronization is due. Current resync is every 5 minutes. This is done because we experienced that the internal clock runs ahead in time. Resync with NTP is crucial. The second example, because of connecting to the internet, needs user-filled-in data from the file secrets.py. A file secrets.py with default, unpersonalized, values is added to this PR. This example also uses as a background image the image: BGimage4.bmp which is inside the subfolder /images. About debugging: Both example scripts use a gVars variable "my_debug" which, when set to 'True' will have the script output more info about it's state/situation (I prefer to call it: 'debug information'). In the first example this flag is set in line 146. In the second example in line 172. Initial clock setting: in both examples the clock will be set at startup by setting the globally used gVars variable "default_dt". In the first example this is in line 149. In the second example in line 175. The second example, when not using an external RTC device (determined by the setting of gVars flag "use_ntp" in line 200), it will use the date and time from an NTP server. The internal clock is set in line 341: "ntp.set_time(tz_offset)". If exists a valid time from NTP then "default_dt" is set in line 363. About performance: The time for the second example script to become in a 'Ready...' state it takes about 54 seconds from powering up. Tapping on Tab 'Four' to display the temperature value - only the first time - takes about 5 seconds. After this one can tap from one Tab to the other with no significant delays. ToDo: investigate/experiment how to improve the time from boot to the 'Ready...' moment; During a recent Deep Dive with @FoamyGuy, viewers of this stream were enthousiastic about this sensor 'hotplug' feature. That is why @FoamyGuy advised me to create this PR.

The images below are taken while running the second example (the one using date and time from an NTP server).

IMG_5112

IMG_5113

IMG_5114

2022-05-16_temp_in_degs_F