Last active
November 11, 2023 17:03
-
-
Save todbot/5fc9c930285863486b54211d1c242fbc to your computer and use it in GitHub Desktop.
Diagnosing ssd1306 & asyncio : https://old.reddit.com/r/circuitpython/comments/17snve6/trouble_with_ssd1306_and_asyncio/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# diagnose https://old.reddit.com/r/circuitpython/comments/17snve6/trouble_with_ssd1306_and_asyncio/ | |
# 11 Nov 2023 | |
import asyncio | |
import random # for creating synthetic data | |
import time | |
import board | |
import digitalio | |
import busio | |
import displayio | |
import adafruit_displayio_ssd1306 | |
import terminalio | |
from adafruit_display_text import label | |
displayio.release_displays() # release display so we can alloc its resources | |
#i2c0 = busio.I2C(sda=board.GP20, scl=board.GP21, frequency=100000) | |
#i2c1 = busio.I2C(sda=board.GP14, scl=board.GP15, frequency=100000) | |
i2c1 = busio.I2C(sda=board.GP14, scl=board.GP15) # use default faster bus speed | |
#SEA_LEVEL_PRESSURE = float('1013.25') | |
#bme680_sensor = adafruit_bme680.Adafruit_BME680_I2C(i2c0) | |
#bme680_sensor.sea_level_pressure = SEA_LEVEL_PRESSURE | |
bme680_temperature = 0 # can't have None type in formatting, so guard against that | |
bme680_humidity = 0 | |
bme680_pressure = 0 | |
bme680_gas = 0 | |
bme680_altitude = 0 | |
#oled_reset = board.GP28 # not needed for most I2C displays | |
WIDTH = 128 | |
HEIGHT = 64 | |
#BORDER = 5 | |
display_bus = displayio.I2CDisplay(i2c1, device_address=0x3c) # reset=oled_reset) | |
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT) | |
bitmap = displayio.Bitmap(WIDTH, HEIGHT, 2) | |
palette = displayio.Palette(2) | |
palette[0] = 0x000000 | |
palette[1] = 0xFFFFFF | |
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) | |
group = displayio.Group() | |
group.append(tile_grid) | |
temperature_label = label.Label(terminalio.FONT, text="Temp: ", color=0xFFFFFF, x=0, y=8) | |
humidity_label = label.Label(terminalio.FONT, text="Humidity: ", color=0xFFFFFF, x=0, y=20) | |
pressure_label = label.Label(terminalio.FONT, text="Pressure: ", color=0xFFFFFF, x=0, y=32) | |
group.append(temperature_label) | |
group.append(humidity_label) | |
group.append(pressure_label) | |
display.show(group) | |
async def read_bme680(): | |
global bme680_temperature, bme680_humidity, bme680_pressure, bme680_gas, bme680_altitude | |
while True: | |
# we don't have bme680 so synthesize plausible but fake data | |
bme680_temperature = random.uniform( 30,80 ) # bme680_sensor.temperature | |
bme680_humidity = random.uniform( 20, 100) # bme680_sensor.humidity | |
bme680_pressure = random.uniform( 500, 1000) # bme680_sensor.pressure | |
bme680_gas = random.uniform( 800, 1200 ) # bme680_sensor.gas | |
bme680_altitude = random.uniform( 100, 2000 ) # bme680_sensor.altitude | |
await asyncio.sleep(1) | |
async def update_display(): | |
while True: | |
print(f"Updating Display - Temp: {bme680_temperature}, Humidity: {bme680_humidity}, Pressure: {bme680_pressure}") | |
temperature_label.text = f"Temp: {bme680_temperature:.2f}C" | |
humidity_label.text = f"Humidity: {bme680_humidity:.2f}%" | |
pressure_label.text = f"Pressure: {bme680_pressure:.2f}hPa" | |
#display.refresh() # don't need this since auto-refresh is enabled | |
await asyncio.sleep(1) | |
async def main(): | |
task1 = asyncio.create_task(read_bme680()) | |
task2 = asyncio.create_task(update_display()) | |
await asyncio.gather(task1,task2) # this is preferred method to wait | |
asyncio.run(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Video of it doing its thing
IMG_3127.mp4