Skip to content

Commit db2e8a2

Browse files
author
GrumpyTechie
committed
Added interrupt and exception handling, improved commenting.
1 parent 4c88254 commit db2e8a2

File tree

1 file changed

+88
-61
lines changed

1 file changed

+88
-61
lines changed

7seg_clock.py

+88-61
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# A simple Raspberry Pi project that uses shift registers to
44
# display time on 7-segment displays.
55
#
6-
# Copyright 2017 GrumpyTechie
6+
# Copyright 2017 GrumpyTechie.net
77
# Licensed under the EUPL
88

99
import RPi.GPIO as GPIO
@@ -27,6 +27,18 @@
2727
GPIO.output(LatchPin,GPIO.LOW)
2828

2929
# Segments definitions for SparkFun Large Digit Driver
30+
#
31+
# Standard 7-segment layout is the following
32+
#
33+
# **aaaa**
34+
# *f****b*
35+
# *f****b*
36+
# **gggg**
37+
# *e****c*
38+
# *e****c*
39+
# **dddd**DP
40+
#
41+
# Segments are arranged as a,f,g,e,d,c,b,DP
3042
Nums = {'0':[1,1,0,1,1,1,1,0],
3143
'1':[0,0,0,0,0,1,1,0],
3244
'2':[1,0,1,1,1,0,1,0],
@@ -38,63 +50,78 @@
3850
'8':[1,1,1,1,1,1,1,0],
3951
'9':[1,1,1,0,1,1,1,0]}
4052

41-
print ('the current time is:')
42-
print (time.strftime('%H:%M:%S'))
43-
44-
# main loop
45-
while True:
46-
47-
# First timed loop to shift out current time every tenth of a second with DP off.
48-
# Timer1 sets the time the DP is on and off, in 100ms increments
49-
Timer1 = 9
50-
while Timer1 >= 0:
51-
Timestr = time.strftime('%H:%M:%S')
52-
Data1 = Nums[Timestr[0]] + Nums[Timestr[1]] + Nums[Timestr[3]] + Nums[Timestr[4]] + Nums[Timestr[6]] + Nums[Timestr[7]]
53-
54-
# Send data to the shift registers
55-
Shift = 47
56-
while Shift >= 0:
57-
GPIO.output(DataPin, GPIO.LOW)
58-
59-
# Determine if bit is set or clear
60-
if Data1[Shift] == 1: GPIO.output(DataPin, GPIO.HIGH)
61-
62-
# Advance the clock
63-
GPIO.output(ClockPin, GPIO.LOW)
64-
GPIO.output(ClockPin, GPIO.HIGH)
65-
Shift -= 1
66-
67-
# Latch and display the output
68-
GPIO.output(LatchPin, GPIO.LOW)
69-
GPIO.output(LatchPin, GPIO.HIGH)
70-
time.sleep(.1)
71-
Timer1 -= 1
72-
73-
# Second timed loop for 1 sec with decimal points (DP) turned on between hours and minutes, and minutes and seconds
74-
Timer1 = 9
75-
while Timer1 >= 0:
76-
Timestr = time.strftime('%H:%M:%S')
77-
Data1 = Nums[Timestr[0]] + Nums[Timestr[1]] + Nums[Timestr[3]] + Nums[Timestr[4]] + Nums[Timestr[6]] + Nums[Timestr[7]]
78-
79-
# Set bits 16 and 32 high to turn on DPs
80-
Data1[15] = 1
81-
Data1[31] = 1
82-
83-
# Send data to the shift registers
84-
Shift = 47
85-
while Shift >= 0:
86-
GPIO.output(DataPin, GPIO.LOW)
87-
88-
# Determine if bit is high or low
89-
if Data1[Shift] == 1: GPIO.output(DataPin, GPIO.HIGH)
90-
91-
# Advance the clock
92-
GPIO.output(ClockPin, GPIO.LOW)
93-
GPIO.output(ClockPin, GPIO.HIGH)
94-
Shift -= 1
95-
96-
# Latch and display the output
97-
GPIO.output(LatchPin, GPIO.LOW)
98-
GPIO.output(LatchPin, GPIO.HIGH)
99-
time.sleep(.1)
100-
Timer1 -= 1
53+
print('Starting ntpclock')
54+
print('The current time is:')
55+
print(time.strftime('%H:%M:%S'))
56+
57+
try:
58+
while True:
59+
60+
# First timed loop to shift out current time every tenth of a second with DP off.
61+
# Timer1 sets the time the DP is on and off, in 100ms increments
62+
Timer1 = 9
63+
while Timer1 >= 0:
64+
Timestr = time.strftime('%H:%M:%S')
65+
Data1 = Nums[Timestr[0]] + Nums[Timestr[1]] + Nums[Timestr[3]] + Nums[Timestr[4]] + Nums[Timestr[6]] + Nums[Timestr[7]]
66+
67+
# Send data to the shift registers
68+
Shift = 47
69+
while Shift >= 0:
70+
GPIO.output(DataPin, GPIO.LOW)
71+
72+
# Determine if bit is set or clear
73+
if Data1[Shift] == 1: GPIO.output(DataPin, GPIO.HIGH)
74+
75+
# Advance the clock
76+
GPIO.output(ClockPin, GPIO.LOW)
77+
GPIO.output(ClockPin, GPIO.HIGH)
78+
Shift -= 1
79+
80+
# Latch and display the output
81+
GPIO.output(LatchPin, GPIO.LOW)
82+
GPIO.output(LatchPin, GPIO.HIGH)
83+
time.sleep(.1)
84+
Timer1 -= 1
85+
86+
# Second timed loop for 1 sec with decimal points (DP)
87+
# turned on between hours and minutes, and minutes and seconds
88+
Timer1 = 9
89+
while Timer1 >= 0:
90+
Timestr = time.strftime('%H:%M:%S')
91+
Data1 = Nums[Timestr[0]] + Nums[Timestr[1]] + Nums[Timestr[3]] + Nums[Timestr[4]] + Nums[Timestr[6]] + Nums[Timestr[7]]
92+
93+
# Set bits 16 and 32 high to turn on DPs
94+
Data1[15] = 1
95+
Data1[31] = 1
96+
97+
# Send data to the shift registers
98+
Shift = 47
99+
while Shift >= 0:
100+
GPIO.output(DataPin, GPIO.LOW)
101+
102+
# Determine if bit is high or low
103+
if Data1[Shift] == 1: GPIO.output(DataPin, GPIO.HIGH)
104+
105+
# Advance the clock
106+
GPIO.output(ClockPin, GPIO.LOW)
107+
GPIO.output(ClockPin, GPIO.HIGH)
108+
Shift -= 1
109+
110+
# Latch and display the output
111+
GPIO.output(LatchPin, GPIO.LOW)
112+
GPIO.output(LatchPin, GPIO.HIGH)
113+
time.sleep(.1)
114+
Timer1 -= 1
115+
116+
except KeyboardInterrupt:
117+
# Handling of CTRL+C cleanly
118+
print('Interupted by user, exiting at ')
119+
print(time.strftime('%H:%M:%S'))
120+
121+
except:
122+
# Handling any other exceptions
123+
print('Unknown error occured, exiting at ')
124+
print(time.strftime('%H:%M:%S'))
125+
126+
finally:
127+
GPIO.cleanup() # this ensures a clean exit

0 commit comments

Comments
 (0)