Skip to content

Commit 3e9b24c

Browse files
authored
Merge pull request #1072 from FoamyGuy/temp_sensor_fallback
PyPortal User Interface - fallback if we don't have the temperature sensor
2 parents e1a1d09 + f022cc3 commit 3e9b24c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

PyPortal_User_Interface/code.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
import board
3+
import microcontroller
34
import displayio
45
import busio
56
from analogio import AnalogIn
@@ -12,10 +13,13 @@
1213
from adafruit_pyportal import PyPortal
1314

1415
# ------------- Inputs and Outputs Setup ------------- #
15-
# init. the temperature sensor
16-
i2c_bus = busio.I2C(board.SCL, board.SDA)
17-
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
18-
adt.high_resolution = True
16+
try: # attempt to init. the temperature sensor
17+
i2c_bus = busio.I2C(board.SCL, board.SDA)
18+
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
19+
adt.high_resolution = True
20+
except ValueError:
21+
# Did not find ADT7410. Probably running on Titano or Pynt
22+
adt = None
1923

2024
# init. the light sensor
2125
light_sensor = AnalogIn(board.LIGHT)
@@ -332,9 +336,13 @@ def switch_view(what_view):
332336
while True:
333337
touch = ts.touch_point
334338
light = light_sensor.value
335-
tempC = round(adt.temperature)
336-
tempF = tempC * 1.8 + 32
337339

340+
if adt: # Only if we have the temperature sensor
341+
tempC = round(adt.temperature)
342+
else: # No temperature sensor
343+
tempC = round(microcontroller.cpu.temperature)
344+
345+
tempF = tempC * 1.8 + 32
338346
sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF)
339347

340348
# ------------- Handle Button Press Detection ------------- #

0 commit comments

Comments
 (0)