Created
March 2, 2018 14:54
-
-
Save ali1234/cb40e1710368d9a1d958212f71e4a70b to your computer and use it in GitHub Desktop.
scroll phat hd clock
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
#!/usr/bin/env python | |
import time | |
import signal | |
import math | |
import scrollphathd | |
from envirophat import light, motion, weather | |
""" | |
[Unit] | |
Description=Scroll Hat HD Clock | |
[Service] | |
Environment=TZ=Europe/London | |
ExecStart=/home/pi/clock.py | |
Restart=on-abort | |
[Install] | |
WantedBy=multi-user.target | |
""" | |
print(""" | |
Scroll pHAT HD: Clock | |
Displays hours and minutes in text, | |
plus a seconds progress bar. | |
Press Ctrl+C to exit! | |
""") | |
class Font(object): | |
def __init__(self, width, height, data): | |
self.width = width | |
self.height = height | |
self.data = data | |
font3x5 = Font(5, 5, { | |
0x00000030: [[0xff,0xff,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x00000031: [[0x00,0xff,0x00], | |
[0xff,0xff,0x00], | |
[0x00,0xff,0x00], | |
[0x00,0xff,0x00], | |
[0xff,0xff,0xff]], | |
0x00000032: [[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0xff,0xff,0xff], | |
[0xff,0x00,0x00], | |
[0xff,0xff,0xff]], | |
0x00000033: [[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0x00,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x00000034: [[0xff,0x00,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0x00,0x00,0xff]], | |
0x00000035: [[0xff,0xff,0xff], | |
[0xff,0x00,0x00], | |
[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x00000036: [[0xff,0xff,0xff], | |
[0xff,0x00,0x00], | |
[0xff,0xff,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x00000037: [[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0x00,0xff,0x00], | |
[0x00,0xff,0x00], | |
[0x00,0xff,0x00]], | |
0x00000038: [[0xff,0xff,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x00000039: [[0xff,0xff,0xff], | |
[0xff,0x00,0xff], | |
[0xff,0xff,0xff], | |
[0x00,0x00,0xff], | |
[0xff,0xff,0xff]], | |
0x0000003a: [[0x00], | |
[0xff], | |
[0x00], | |
[0xff]], | |
} ) | |
class Widget(object): | |
def __init__(self, font): | |
self.font = font | |
def draw(self, brightness=0.25, rotation=0): | |
scrollphathd.rotate(90*rotation) | |
scrollphathd.clear() | |
if rotation%2: | |
self.draw_portrait(brightness, rotation) | |
else: | |
self.draw_landscape(brightness, rotation) | |
scrollphathd.show() | |
class Clock(Widget): | |
def draw_landscape(self, brightness, rotation): | |
float_sec = (time.time() % 60) / 60.0 | |
bar_sec = float_sec * 16 | |
bar_int = math.floor(bar_sec) | |
bar_float = (bar_sec - bar_int)**2 | |
if rotation == 0: | |
bar_pos = 6 | |
clk_pos = 0 | |
else: | |
bar_pos = 0 | |
clk_pos = 2 | |
scrollphathd.set_pixel(bar_int,bar_pos,(1.0-bar_float)*brightness) | |
scrollphathd.set_pixel(bar_int+1,bar_pos,bar_float*brightness) | |
str_time = time.strftime("%H:%M") | |
scrollphathd.write_string(str_time, x=0, y=clk_pos, font=self.font, brightness=brightness) | |
if int(time.time()) % 2 == 0: | |
scrollphathd.clear_rect(8,clk_pos,1,5) | |
def draw_portrait(self, brightness, rotation): | |
str_hours = time.strftime("%H") | |
scrollphathd.write_string(str_hours, x=0, y=0, font=self.font, brightness=brightness) | |
str_minutes = time.strftime("%M") | |
scrollphathd.write_string(str_minutes, x=0, y=6, font=self.font, brightness=brightness) | |
str_seconds = time.strftime("%S") | |
scrollphathd.write_string(str_seconds, x=0, y=12, font=self.font, brightness=brightness) | |
clock = Clock(font3x5) | |
rotate = 0 | |
while True: | |
x, y, z = motion.accelerometer() | |
if y < -0.5: | |
rotate = 0 | |
elif y > 0.5: | |
rotate = 2 | |
elif x < -0.5: | |
rotate = 1 | |
elif x > 0.5: | |
rotate = 3 | |
brightness = 0.2 + ((light.light()/512.0)**2) | |
if brightness > 1.0: | |
brightness = 1.0 | |
clock.draw(brightness, rotate) | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment