Last active
August 2, 2021 21:50
-
-
Save extrasleepy/440634c91915fd3d79a60628cfb6db20 to your computer and use it in GitHub Desktop.
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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries | |
# SPDX-License-Identifier: Unlicense | |
#Based on code by Author: Mark Roberts (mdroberts1243) from Adafruit code | |
#Adapted in July 2021 by Andrew Kleindolph | |
import board | |
import time | |
import displayio | |
import terminalio | |
import adafruit_ahtx0 | |
# can try import bitmap_label below for alternative | |
from adafruit_display_text import label | |
import adafruit_displayio_sh1107 | |
# Create sensor object, communicating over the board's default I2C bus | |
i2c = board.I2C() # uses board.SCL and board.SDA | |
sensor = adafruit_ahtx0.AHTx0(i2c) | |
displayio.release_displays() | |
# oled_reset = board.D9 | |
# Use for I2C | |
i2c = board.I2C() | |
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C) | |
# SH1107 is vertically oriented 64x128 | |
WIDTH = 128 | |
HEIGHT = 64 | |
BORDER = 2 | |
display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT) | |
# Make the display context | |
splash = displayio.Group(max_size=10) | |
display.show(splash) | |
color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1) | |
color_palette = displayio.Palette(1) | |
color_palette[0] = 0xFFFFFF # White | |
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) | |
splash.append(bg_sprite) | |
# Draw a smaller inner rectangle in black | |
inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1) | |
inner_palette = displayio.Palette(1) | |
inner_palette[0] = 0x000000 # Black | |
inner_sprite = displayio.TileGrid( | |
inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER | |
) | |
splash.append(inner_sprite) | |
# Draw some white squares | |
# sm_bitmap = displayio.Bitmap(8, 8, 1) | |
# sm_square = displayio.TileGrid(sm_bitmap, pixel_shader=color_palette, x=58, y=17) | |
# splash.append(sm_square) | |
# med_bitmap = displayio.Bitmap(16, 16, 1) | |
# med_square = displayio.TileGrid(med_bitmap, pixel_shader=color_palette, x=71, y=15) | |
# splash.append(med_square) | |
# lrg_bitmap = displayio.Bitmap(32, 32, 1) | |
# lrg_square = displayio.TileGrid(lrg_bitmap, pixel_shader=color_palette, x=91, y=28) | |
# splash.append(lrg_square) | |
# Draw some label text | |
# text1 = "0123456789ABCDEF123456789AB" # overly long to see where it clips | |
# text_area = label.Label(terminalio.FONT, text=text1, color=0xFFFFFF, x=8, y=8) | |
# splash.append(text_area) | |
# text2 = "SHHHITTTT" | |
# text_area2 = label.Label( | |
# terminalio.FONT, text=text2, scale=2, color=0xFFFFFF, x=9, y=44 | |
# ) | |
# splash.append(text_area2) | |
# time.sleep(5) | |
# splash.pop(5) | |
while True: | |
print("\nTemperature: %0.1f C" % sensor.temperature) | |
print("Humidity: %0.1f %%" % sensor.relative_humidity) | |
text2 = str("%0.1f F" % ((sensor.temperature * 1.8) + 32)) | |
text_area2 = label.Label( | |
terminalio.FONT, text=text2, scale=2, color=0xFFFFFF, x=9, y=15 | |
) | |
splash.append(text_area2) | |
text2 = str("%0.1f C" % sensor.temperature) | |
text_area2 = label.Label( | |
terminalio.FONT, text=text2, scale=2, color=0xFFFFFF, x=9, y=45 | |
) | |
splash.append(text_area2) | |
display.show(splash) | |
time.sleep(5) | |
splash.pop(-1) | |
splash.pop(-1) | |
text2 = str("H: %0.1f %%" % sensor.relative_humidity) | |
text_area2 = label.Label( | |
terminalio.FONT, text=text2, scale=2, color=0xFFFFFF, x=9, y=10 | |
) | |
splash.append(text_area2) | |
display.show(splash) | |
time.sleep(5) | |
splash.pop(-1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment