-
Notifications
You must be signed in to change notification settings - Fork 0
/
clocky.py
executable file
·59 lines (49 loc) · 1.89 KB
/
clocky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
import time
import datetime
from Adafruit_7Segment import SevenSegment
from Adafruit_LEDBackpack import LEDBackpack
import signal
import sys
segment = SevenSegment(address=0x70)
seven = LEDBackpack(address=0x70)
# Kinda pack-rat-ish.. these are crumbs I'd like to implement
seven.setBrightness(15)
def exit_gracefully(signum, frame):
# let's restore the original signal handlers
signal.signal(signal.SIGTERM, original_sigterm)
signal.signal(signal.SIGINT, original_sigint)
signal.signal(signal.SIGHUP, original_sighup)
# clean up gracefully here. bail when done.
seven.clear()
sys.exit(0)
#just in case we do something during cleanup that means we *shouldn't" exit, we want our handler to stay intact.
signal.signal(signal.SIGTERM, exit_gracefully)
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGHUP, exit_gracefully)
def run_this():
# Continually update the time on a 4 char, 7-segment display
while(True):
now = datetime.datetime.now()
hour = now.hour
minute = now.minute
second = now.second
# Set hours
segment.writeDigit(0, int(hour / 10)) # Tens
segment.writeDigit(1, hour % 10) # Ones
# Set minutes
segment.writeDigit(3, int(minute / 10)) # Tens
segment.writeDigit(4, minute % 10) # Ones
# Toggle colon
segment.setColon(second % 2) # Toggle colon at 1Hz
# Wait one second
time.sleep(1)
if __name__ == '__main__':
# store the original SIGTERM
original_sigterm = signal.getsignal(signal.SIGTERM)
original_sigint = signal.getsignal(signal.SIGINT)
original_sighup = signal.getsignal(signal.SIGHUP)
signal.signal(signal.SIGTERM, exit_gracefully)
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGHUP, exit_gracefully)
run_this()