I have some new code to test. I have a hunch as to what might be happening.
With the old code the text / image was being written to the buffer over and over while it was simultaneously being read out and displayed. Which I think corrupted the saved image.
With this code the text= and draw image only happens just before it starts to show the message. And doesn’t get updated again until the end when it starts over. I’m hoping that’s what’s happening, its running now on mine and keeping correct time.
It should, save date time > display date and time > update date and time > display date and time > etc.
#!/usr/bin/env python3
import time, datetime
import sys
from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini
unicornhatmini = UnicornHATMini()
rotation = 180
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()
print("{}x{}".format(display_width, display_height))
unicornhatmini.set_brightness(0.5)
font = ImageFont.truetype("5x7.ttf", 8)
offset_x = 0
while True:
if offset_x == 0:
text = time.strftime("%A %B %-d %-I:%M %p")
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
else:
for y in range(display_height):
for x in range(display_width):
if image.getpixel((x + offset_x, y)) == 255:
unicornhatmini.set_pixel(x, y, 0, 255, 0)
else:
unicornhatmini.set_pixel(x, y, 0, 0, 0)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
unicornhatmini.show()
time.sleep(0.05)
EDIT: So far so good, its been running since about 10:30 AM this morning and its now 3 PM with no issues. I’m going to leave it running until I hit the sack tonight for further testing.
EDIT:2 Forum software won’t let me do more than three posts in a row.
New code with shutdown via the X button. Button A set global brightness to 0.5 and the B button sets it to 1.0
#!/usr/bin/env python3
import sys
import os
import time, datetime
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw, ImageFont
from unicornhatmini import UnicornHATMini
unicornhatmini = UnicornHATMini()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# button_map
# 5: "A",
# 6: "B",
# 16: "X",
# 24: "Y"}
X = 0
def Dim(channel):
unicornhatmini.set_brightness(0.5)
def Bright(channel):
unicornhatmini.set_brightness(1.0)
def Shutdown(channel):
global X
X = 1
GPIO.add_event_detect(5, GPIO.FALLING, callback = Dim, bouncetime = 2000)
GPIO.add_event_detect(6, GPIO.FALLING, callback = Bright, bouncetime = 2000)
GPIO.add_event_detect(16, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
rotation = 180
if len(sys.argv) > 1:
try:
rotation = int(sys.argv[1])
except ValueError:
print("Usage: {} <rotation>".format(sys.argv[0]))
sys.exit(1)
unicornhatmini.set_rotation(rotation)
display_width, display_height = unicornhatmini.get_shape()
unicornhatmini.set_brightness(0.5)
font = ImageFont.truetype("5x7.ttf", 8)
offset_x = 0
while True:
if offset_x == 0:
text = time.strftime("%A %B %-d %-I:%M %p")
text_width, text_height = font.getsize(text)
image = Image.new('P', (text_width + display_width + display_width, display_height), 0)
draw = ImageDraw.Draw(image)
draw.text((display_width, -1), text, font=font, fill=255)
else:
for y in range(display_height):
for x in range(display_width):
if image.getpixel((x + offset_x, y)) == 255:
unicornhatmini.set_pixel(x, y, 0, 255, 0)
else:
unicornhatmini.set_pixel(x, y, 0, 0, 0)
offset_x += 1
if offset_x + display_width > image.size[0]:
offset_x = 0
if X == 1:
unicornhatmini.set_all(0, 0, 0)
unicornhatmini.show()
os.system("sudo shutdown now -P")
time.sleep(30)
unicornhatmini.show()
time.sleep(0.05)
# Last edited on June 6th 2020
# added shutdown via button X
# also added Dim and Bright function to button A and B
# run sudo crontab -e
# add
# @reboot python3 /home/pi/scroll_clock.py &