I have to thank you again @Shoe for helping me sort this out. I would have never figured out that simple fix.
Here is the my full code that shows the Day, Date, Time, Temperature, Humidity and Barometric Pressure, an endlessly repeating message. The color of each message changes based on conditions. The temperature text turns Orange at 25c then red at 30c for example. I just figured you’d like to see the end result. There likely is some stuff that could go above the while True to clean it up but it works, so I’ll play with that another day.
#!/usr/bin/env python
import time
from sys import exit
from PIL import Image, ImageDraw, ImageFont
import unicornhathd
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
from bme280 import BME280
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
unicornhathd.rotation(90)
unicornhathd.brightness(0.6)
FONT = ('/usr/share/fonts/truetype/freefont/FreeSans.ttf', 12)
while True:
text = time.strftime("It's %A %B %-d %-I:%M:%p")
#print("time updated")
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
text = time.strftime("It's %A %B %-d %-I:%M:%p")
w, h = font.getsize(text)
text_width += w + width
text_height = max(text_height, h)
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
r, g, b =(0, 255, 255)
offset_left = 0
draw.text((text_x + offset_left, text_y), text, font=font, fill=(r, g, b))
#draw.text((text_x + offset_left, text_y), text, font=font)
offset_left += font.getsize(text)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
T = bme280.get_temperature()
T = round(T)
if T <= 0:
r, g, b = [0, 0, 255] # Blue
text = ("and its very Cold @ %sc") % (T)
elif T > 0 and T < 13:
r, g, b, = [255, 255, 0] # Yellow
text = ("and its cool at %sc") % (T)
elif T >= 13 and T < 25:
r, g, b = [0, 255, 0] # Green
text = ("and the Temperature is %sc") % (T)
elif T >= 25 and T < 30:
r, g, b = [255, 50, 0] # Orange
text = ("and its Hot @ %sc") % (T)
elif T >= 30:
r, g, b = [255, 0, 0] # Red
text = ("and its very Hot @ %sc") % (T)
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
w, h = font.getsize(text)
text_width += w + width
text_height = max(text_height, h)
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
offset_left = 0
draw.text((text_x + offset_left, text_y), text, font=font, fill=(r, g, b))
#draw.text((text_x + offset_left, text_y), text, font=font)
offset_left += font.getsize(text)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
H = bme280.get_humidity()
H = round(H)
if H < 30:
r, g, b = [255, 0, 0] # Red
text = ("with Low %s%% Humidity") % (H)
elif H >= 30 and H <= 60:
r, g, b = [0, 255, 0] # Green
text = ("with %s%% Humidity") % (H)
elif H > 60 and H < 80:
r, g, b = [255, 255, 0] # Yellow
text = ("with High %s%% Humidity") % (H)
elif H >= 80:
r, g, b = [255, 0, 0] # Red
text = ("with Very High %s%% Humidity") % (H)
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
w, h = font.getsize(text)
text_width += w + width
text_height = max(text_height, h)
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
offset_left = 0
draw.text((text_x + offset_left, text_y), text, font=font, fill=(r, g, b))
#draw.text((text_x + offset_left, text_y), text, font=font)
offset_left += font.getsize(text)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
p = bme280.get_pressure()
P = round(p)
if P > 0 and P < 982: # Very Low
r, g, b = [255, 0, 0] # Red
text = ("Baromiter is Very Low@ %smb : Storm Watch") % (P)
elif P >= 982 and P < 1004: # Low
r, g, b = [255, 255, 0] # Yellow
text = ("Barometer is Low @ %smb : Posible Precipitation") % (P)
elif P >= 1004 and P < 1026: # Mid Range
r, g, b = [0, 255, 0] # Green
text = ("Barometer is @ %smb") % (P)
elif P >= 1026 and P < 1048: # High
r, g, b = [0, 0, 255] # Blue
text = ("Barometer is High @ %smb") % (P)
elif P >= 1048: # Very High
r, g, b = [255, 50, 0] # Orange
text = ("Barometer is Very High @ %smb") % (P)
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
w, h = font.getsize(text)
text_width += w + width
text_height = max(text_height, h)
image = Image.new('RGB', (text_width, max(16, text_height)), (0, 0, 0))
draw = ImageDraw.Draw(image)
offset_left = 0
draw.text((text_x + offset_left, text_y), text, font=font, fill=(r, g, b))
#draw.text((text_x + offset_left, text_y), text, font=font)
offset_left += font.getsize(text)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x + scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)