forked from the-raspberry-pi-guy/lcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd_countdown.py
100 lines (87 loc) · 3.32 KB
/
lcd_countdown.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
import datetime
import lcddriver
import time
import sys
import getopt
from datetime import timedelta
# Load the driver and set it to "display"
display = lcddriver.lcd()
def show_message_blink(display, text , row_num , max_cols):
display.lcd_display_string(text,row_num)
time.sleep(0.4)
padding = ' ' * max_cols
display.lcd_display_string(padding,row_num)
time.sleep(0.4)
def show_usage():
print ("Usage: " + sys.argv[0] + ' -f <first row message> -b <blink first row> -c <center first row> -C <center second row> -d <date DD:MM:YYYY hh:mm:ss>')
display.lcd_clear()
display.lcd_backlight(0)
sys.exit()
def show_message_one_row(display, text, row_num , max_cols, center, blink):
if(len(text) > max_cols):
time.sleep(1)
#To allow show two special functions as blink and scoll at the same time this must be converted to multi thread execution
blink = 0;
padding = ' ' * max_cols
txt2show = padding + text + padding
for i in range(len(txt2show) - max_cols + 1):
text_to_print = txt2show[i:i+max_cols]
display.lcd_display_string(text_to_print,row_num)
time.sleep(0.2)
time.sleep(0.7)
else:
txt2show = text
if ((center == 1) and (len(text) < max_cols)):
padding = ' ' * int(round((max_cols - len(text))/2))
txt2show = padding + text + padding
if (blink == 1):
show_message_blink(display, txt2show , row_num , max_cols)
else :
display.lcd_display_string(txt2show,row_num)
def main(argv):
row1_msg = ''
date = ''
blink1 = 0
center1 = 0
display_cols = 15
row_num = 1
text = ""
date = '31-12-2020 23:59:59'
targetTime = datetime.datetime.strptime(date, '%d-%m-%Y %H:%M:%S')
timeNow = datetime.datetime.now()
try:
opts, args = getopt.getopt(argv,"hf:d:bc",["msg1=","msg2=","date=","blink1","center1"])
except getopt.GetoptError:
show_usage()
for opt, arg in opts:
if opt == '-h':
show_usage()
elif opt in ("-f", "--msg1"):
row1_msg = arg
elif opt in ("-d", "--date"):
date = int(arg)
elif opt in ("-b", "--blink1"):
blink1 = 1
elif opt in ("-c", "--center1"):
center1 = 1
try:
while timeNow <= targetTime:
timeNow = datetime.datetime.now() # the time now
remainingTime = (targetTime-timeNow)
days = remainingTime.days
secs = remainingTime.seconds
hrs, secs = divmod(secs, 3600)
mins, secs = divmod(secs, 60)
remain_time_str = str(days) + " " + str(hrs) + " " + str(mins)+ " " + str(secs)
show_message_one_row(display, row1_msg, 1, display_cols, center1, blink1)
show_message_one_row(display, remain_time_str, 2, display_cols, 1, 0)
time.sleep(1)
# Clear and Turn backlight off
display.lcd_clear()
display.lcd_backlight(0)
except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press CTRL + C), exit the program and cleanup
display.lcd_clear()
display.lcd_backlight(0)
if __name__ == "__main__":
main(sys.argv[1:])