Skip to content

Commit ffb8f9b

Browse files
committed
fallback if we don't have the temperature sensor
1 parent 812002d commit ffb8f9b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

PyPortal_User_Interface/code.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
from adafruit_pyportal import PyPortal
1313

1414
# ------------- 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
15+
try: # attempt to 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
19+
except ValueError:
20+
# Did not find ADT7410. Probably running on Titano or Pynt
21+
adt = None
1922

2023
# init. the light sensor
2124
light_sensor = AnalogIn(board.LIGHT)
@@ -332,10 +335,14 @@ def switch_view(what_view):
332335
while True:
333336
touch = ts.touch_point
334337
light = light_sensor.value
335-
tempC = round(adt.temperature)
336-
tempF = tempC * 1.8 + 32
337338

338-
sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF)
339+
if adt: # Only if we have the temperature sensor
340+
tempC = round(adt.temperature)
341+
tempF = tempC * 1.8 + 32
342+
sensor_data.text = 'Touch: {}\nLight: {}\n Temp: {}°F'.format(touch, light, tempF)
343+
344+
else: # No temperature sensor on Titano and Pynt
345+
sensor_data.text = 'Touch: {}\nLight: {}'.format(touch, light)
339346

340347
# ------------- Handle Button Press Detection ------------- #
341348
if touch: # Only do this if the screen is touched

0 commit comments

Comments
 (0)