# Show value of readings from distance sensor on the display
# Uses CircuitPython
# learn more:
# https://learn.adafruit.com/ultrasonic-sonar-distance-sensors/python-circuitpython
import time
import board
import busio
import adafruit_hcsr04
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_ssd1306
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.GP17, echo_pin=board.GP16)
displayio.release_displays()
i2c = busio.I2C(board.GP3, board.GP2)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)
# Make the display context
splash = displayio.Group()
display.root_group = splash
# Draw a label
text = "Initializing..."
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00, x=5, y=5)
splash.append(text_area)
while True:
try:
text = str(sonar.distance)
text_area.text = text
print(text)
except RuntimeError:
print("Retrying...")
time.sleep(1)