Another Unicorn Hat Clock


#1

My Unicorn Hat Clock displays Hours, Minutes and Seconds on two rows each of the Hat.

Tens are brighter than units. So this is 13:16 and 58 seconds:

It looks better than that in real life, the contrast overwhelmed my phone camera’s sensor.

The clock leaves two rows of LEDs available for other purposes. I have a version of the script which occasionally tests the WiFi connection and shows this as red/green at the bottom. Unfortunately the test sometimes takes a couple of seconds so it messes up the time display.

#!/usr/bin/env python
#
# Script to display the current time on Unicorn Hat as Hours, Minutes, Seconds
# eg 11:59:00 is shown as 1 bright blue and 1 dim blue (11)
#                         5 bright green and 9 dim green (59)
#                         0 bright red and 0 dim red (00)
#
# Uses 6 rows of the Unicorn Hat
# For UK summertime make sure you have the timezone set in raspi-config
#
##########################################################

import unicornhat as unicorn
import time

unicorn.rotation(270)
unicorn.brightness(0.8)

# Define the led colours
RGBHRS = (0,0,180)   # Tens column for hours
RGBhrs = (0,0,90)    # Units column for hours
RGBMIN = (180,0,0)   # Tens column for minutes
RGBmin = (80,0,0)    # Units column for minutes
RGBSEC = (0,180,0)   # Tens column for seconds
RGBsec = (0,80,0)    # Units column for seconds

##########################################################

# Matrix representation of the display
mx = [[(0,0,0) for y in range(8)] for x in range(8)]

def clear_clock() :
  for y in range(6):    # Dont touch the two unused rows
    for x in range(8):
      mx[x][y] = (0,0,0)

def setpixel(x,y,r,g,b):
  mx[7-x][7-y] = (r,g,b)

def lightup(digit, xoffset, yoffset, rgb) :
# Test each digit to see if pixel should be lit
# NB even when digit can only be 0,1,2 (first digit of hours) it will test 10 times
  for x in range(6) :
    for y in range(2) :
      test = 2* x + y
      if test < digit :
        setpixel (x+xoffset, y+yoffset, rgb[0], rgb[1], rgb[2])

##########################################################

while True :
  clear_clock()

  hrs = time.strftime("%H")  # string representation, leading zero
  min = time.strftime("%M")
  sec = time.strftime("%S")

  # hours # first digit 0 to 2
  lightup(int(hrs[0]), 0, 6, RGBHRS)
  # second digit 0 to 9
  lightup(int(hrs[1]), 3, 6, RGBhrs)

  # minutes # first digit 0 to 5
  lightup(int(min[0]),0,4,RGBMIN)
  # second digit 0 to 9
  lightup(int(min[1]),3,4,RGBmin)
				
  # seconds # first digit 0 to 5
  lightup(int(sec[0]),0,2,RGBSEC)
  # second digit 0 to 9
  lightup(int(sec[1]),3,2,RGBsec)
				
  unicorn.set_pixels(mx)
  unicorn.show()
  time.sleep(0.1)