-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclock.py
More file actions
122 lines (103 loc) · 4.05 KB
/
clock.py
File metadata and controls
122 lines (103 loc) · 4.05 KB
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
from apps import App
from constants import APP_CLOCK, SCHEDULER_CLOCK_SECOND
from display import Display
from rtc import RTC
from buttons import Buttons
from configuration import Configuration
from wifi import WLAN
import helpers
import time
import ntptime
import localPTZtime
class Clock(App):
def __init__(self, scheduler):
App.__init__(self, APP_CLOCK)
self.config = Configuration()
self.wifi_config = Configuration().wifi_config
self.wlan = WLAN(scheduler)
self.display = Display(scheduler)
self.rtc = RTC()
self.enabled = True
self.buttons = Buttons(scheduler)
self.hour = 0
self.minute = 0
self.second = 0
scheduler.schedule(SCHEDULER_CLOCK_SECOND, 1000, self.secs_callback)
async def enable(self):
self.enabled = True
self.buttons.add_callback(2, self.temp_callback, max=500)
self.buttons.add_callback(
2, self.switch_temperature_callback, min=500, max=5000)
self.buttons.add_callback(3, self.backlight_callback, max=500)
self.buttons.add_callback(
3, self.switch_blink_callback, min=500, max=5000)
await self.update_time()
await self.show_time()
self.display.show_temperature_icon()
def disable(self):
self.enabled = False
async def secs_callback(self):
if self.enabled:
await self.update_time()
if self.should_blink():
if self.second % 2 == 0:
# makes : display
self.display.show_char(":", pos=10)
else:
# makes : not display
self.display.show_char(" :", pos=10)
def should_blink(self):
return self.config.blink_time_colon and not self.display.animating and self.display.showing_time
def ntp_sync(self):
if self.wifi_config.enabled and self.wifi_config.ntp_enabled:
try:
ntptime.settime()
except:
print("NTP time sync failed")
return False
local_time= localPTZtime.tztime(time.time(), self.wifi_config.ntp_ptz)
self.rtc.save_time(local_time[:8])
return True
return False
async def update_time(self):
t = self.rtc.get_time()
self.second = t[5]
if self.hour != t[3] or self.minute != t[4]:
self.hour = t[3]
self.minute = t[4]
if self.minute % 5 == 0 and self.second == 0:
if not self.wlan.wifi_connected():
self.wlan.connect_to_wifi() # Reconnect to WiFi every 5 minutes if not connected
if self.minute == 10 and self.second == 0:
if self.ntp_sync(): # Sync time via NTP every hour at HH:10:00
print("NTP time sync successful")
self.show_time_icon()
self.display.show_day(t[6])
await self.show_time()
elif t[5] == 20 and self.config.show_temp:
await self.show_temperature()
async def show_time(self):
hour = self.hour
if self.config.clock_type == "12":
hour = helpers.convert_twenty_four_to_twelve_hour(hour)
await self.display.show_time("%02d:%02d" % (hour, self.minute))
def show_time_icon(self):
if self.config.clock_type == "12":
if self.hour >= 12:
self.display.show_icon("PM")
self.display.hide_icon("AM")
else:
self.display.show_icon("AM")
self.display.hide_icon("PM")
async def show_temperature(self):
temp = self.rtc.get_temperature()
await self.display.show_temperature(temp)
async def temp_callback(self):
await self.show_temperature()
async def switch_temperature_callback(self):
self.config.switch_temp_value()
self.display.show_temperature_icon()
async def backlight_callback(self):
self.display.switch_backlight()
async def switch_blink_callback(self):
self.config.switch_blink_time_colon_value()