Code: Select all
@reboot python3 /home/pi/scroll_clock.py &
If I run
Code: Select all
python3 /home/pi/scroll_clock.py
Raspberry Pi Zero running the latest Raspberry Pi OS with a Unicorn Hat Mini attached.
The following code displays the day, date, time in a repeating scrolling message. The brightness up down works as does the shutdown when button X is pressed.
Code: Select all
#!/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)
#GPIO.add_event_detect(24, 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()
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
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 &