forked from zador-blood-stained/RPLCD-i2c
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.py
executable file
·300 lines (259 loc) · 8.58 KB
/
clock.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
import sys
import bme280
from RPLCD_i2c import CharLCD
from RPLCD_i2c import Alignment, CursorMode, ShiftMode
from RPLCD_i2c import cursor, cleared
from time import strftime, sleep
from datetime import datetime
# build big digits
def disp_number(lcd_char, position):
if lcd_char=="0" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(4))
lcd.write_string(unichr(0))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(7))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="1" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(0))
lcd.write_string(unichr(5))
lcd.write_string(unichr(254))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(1))
lcd.write_string(unichr(255))
lcd.write_string(unichr(1))
elif lcd_char=="2" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(2))
lcd.write_string(unichr(2))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(255))
lcd.write_string(unichr(1))
lcd.write_string(unichr(1))
elif lcd_char=="3" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(0))
lcd.write_string(unichr(2))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(1))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="4" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(255))
lcd.write_string(unichr(1))
lcd.write_string(unichr(255))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
lcd.write_string(unichr(6))
elif lcd_char=="5" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(255))
lcd.write_string(unichr(2))
lcd.write_string(unichr(2))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(1))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="6" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(4))
lcd.write_string(unichr(2))
lcd.write_string(unichr(2))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(7))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="7" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(0))
lcd.write_string(unichr(0))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
lcd.write_string(unichr(6))
elif lcd_char=="8" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(4))
lcd.write_string(unichr(2))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(7))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="9" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(255))
lcd.write_string(unichr(2))
lcd.write_string(unichr(5))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(1))
lcd.write_string(unichr(1))
lcd.write_string(unichr(6))
elif lcd_char=="C" :
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(255))
lcd.write_string(unichr(2))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
else:
lcd.cursor_pos = (0, position)
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
lcd.cursor_pos = (1, position)
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
lcd.write_string(unichr(254))
return lcd_char;
# scroll effect
def shift(direction):
if direction=="left" :
for x in range(0, 16):
lcd.shift_display(-1)
sleep(0.05)
else :
for x in range(0, 16):
lcd.shift_display(1)
sleep(0.05)
def read_temp():
temperature,pressure,humidity = bme280.readBME280All()
return temperature
def clock_dots():
# display two dots
lcd.cursor_pos = (0, 8)
lcd.write_string(unichr(3))
lcd.cursor_pos = (1, 8)
lcd.write_string(unichr(3))
sleep(0.5)
# remove the two dots
lcd.cursor_pos = (0, 8)
lcd.write_string(unichr(254))
lcd.cursor_pos = (1, 8)
lcd.write_string(unichr(254))
sleep(0.5)
def clock_date(digits, month, day_name):
# previous text leaving to left
shift("left")
# preparing date buffer
tens_day = disp_number(digits[4], 0)
day = disp_number(digits[5], 4)
lcd.cursor_pos = (0, 8)
lcd.write_string(' ')
lcd.cursor_pos = (1, 8)
lcd.write_string(' ')
lcd.cursor_pos = (0, 8)
lcd.write_string(month[:8])
lcd.cursor_pos = (1, 8)
lcd.write_string(day_name[:8])
# date entering from left
shift("right")
sleep(2)
def clock_temp():
# previous text leaving to left
shift("left")
# preparing temp buffer
lcd.cursor_pos = (0, 0)
lcd.write_string('Temp: ')
lcd.cursor_pos = (1, 0)
lcd.write_string(' ')
temp_digits = str(read_temp());
disp_number(temp_digits[0], 6)
disp_number(temp_digits[1], 10)
disp_number("C", 14)
lcd.cursor_pos = (0, 13)
lcd.write_string(unichr(223))
# date entering from left
shift("right")
sleep(5)
def clock_hour(digits):
# date leaving to left
shift("left")
# preparing hour buffer
lcd.cursor_pos = (0, 0)
lcd.write_string(' ')
lcd.cursor_pos = (1, 0)
lcd.write_string(' ')
disp_number(digits[0], 0)
disp_number(digits[1], 4)
disp_number(digits[2], 9)
disp_number(digits[3], 13)
# hour entering from left
shift("right")
def main():
try:
input = raw_input
except NameError:
pass
try:
unichr = unichr
except NameError:
unichr = chr
old_time = 0
counter = 0
# custom symbols
lcd.clear()
top_line = (0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000)
bottom_line = (0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111)
both_lines = (0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111)
dot = (0b00000, 0b00000, 0b00000, 0b11000, 0b11000, 0b00000, 0b00000, 0b00000)
up_left = (0b00111, 0b01111, 0b01111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111)
up_right = (0b11100, 0b11110, 0b11110, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111)
bot_right = (0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11110, 0b11110, 0b11100)
bot_left = (0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b01111, 0b01111, 0b00111)
lcd.create_char(0, top_line)
lcd.create_char(1, bottom_line)
lcd.create_char(2, both_lines)
lcd.create_char(3, dot)
lcd.create_char(4, up_left)
lcd.create_char(5, up_right)
lcd.create_char(6, bot_right)
lcd.create_char(7, bot_left)
#main loop
while True:
counter += 1
new_time = datetime.now().strftime('%H%M%d')
month = datetime.now().strftime('%B')
day_name = datetime.now().strftime('%A')
# time changed, update LCD buffer
if new_time!=old_time :
digits = str(new_time)
tens_hour = disp_number(digits[0], 0)
hour = disp_number(digits[1], 4)
tens_minutes = disp_number(digits[2], 9)
minutes = disp_number(digits[3], 13)
old_time = new_time
cur_hour = int(tens_hour)*10 + int(hour)
# enable backlight by night
lcd.set_backlight(cur_hour>=19 or cur_hour<9)
# dots blink
clock_dots()
if counter==15 :
counter=0
# date display
clock_date(digits, month, day_name)
# temperature display
clock_temp()
# temperature display
clock_hour(digits)
if __name__ == '__main__':
lcd = CharLCD(address=0x3F, port=1, cols=16, rows=2, dotsize=8)
try:
main()
except KeyboardInterrupt:
pass
finally:
lcd.clear()
lcd.set_backlight(False)
lcd.home()