-
Notifications
You must be signed in to change notification settings - Fork 1
/
LcdThread.py
130 lines (99 loc) · 3.8 KB
/
LcdThread.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
from LCDControl.LCDControl import LCDControl
import gaugette.rotary_encoder
import time
import datetime
import pytz
import threading
import MenuControl
import Settings
from InputWorker import InputWorker
import logging
log = logging.getLogger('root')
#
# Date convenience methods
#
def suffix(d):
return 'th' if 11 <= d <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(d % 10, 'th')
def formatDate(dateObj):
message = dateObj.strftime("%a ")
message += dateObj.strftime("%d").lstrip("0")
message += suffix(dateObj.day)
message += dateObj.strftime(" %B")
return message
#
# Class dealing with displaying relevant information on the LCD screen
#
class LcdThread(threading.Thread):
def __init__(self, alarmThread, settings, weather, media, shutdownCallback):
threading.Thread.__init__(self)
self.alarmThread = alarmThread
self.stopping = False
self.message = ""
self.settings = settings
self.weather = weather
self.menu = MenuControl.MenuControl(alarmThread, settings, media, shutdownCallback)
self.menu.setDaemon(True)
self.lcd = LCDControl()
self.lcd.white()
self.setMessage("Booting up...")
if self.settings.getInt('use_rotor') == 1:
self.rotor = InputWorker(self)
self.rotor.start()
def setBrightness(self, brightness):
# We get passes a value from 0 - 15, which we need to scale to 0-255 before passing to LCDControl
colVal = int(255 * (float(brightness) / 15))
self.lcd.setColour(colVal, colVal, colVal)
def setMessage(self, newMessage, center=False):
if newMessage != self.message:
self.message = newMessage
self.lcd.setMessage(self.message, center)
def scroll(self, direction):
self.menu.scroll(direction)
# Called by InputWorker on press of the select button
def select(self):
if self.alarmThread.isAlarmSounding():
# Lets snooze for a while
self.alarmThread.snooze()
return
self.menu.select()
# Called by InputWorker on press of the cancel button
def cancel(self):
if self.alarmThread.isAlarmSounding() or self.alarmThread.isSnoozing():
# Stop the alarm!
self.alarmThread.stopAlarm()
return
if self.alarmThread.alarmInSeconds() < self.settings.getInt('preempt_cancel') and not self.menu.isActive():
# We're in the allowable window for pre-empting a cancel alarm, and we're not in the menu
log.info("Pre-empt cancel triggered")
self.alarmThread.stopAlarm()
return
self.menu.cancel()
def stop(self):
self.stopping = True
def run(self):
self.menu.start()
self.lcd.setMessage("Boot finished")
while (not self.stopping):
time.sleep(0.1)
try:
if self.alarmThread.isAlarmSounding():
self.setMessage("Wakey wakey!", True)
continue
if self.menu.isActive():
message = self.menu.getMessage()
elif self.menu.backgroundRadioActive():
message = "Radio player on"
else:
now = datetime.datetime.now(pytz.timezone(self.settings.get('timezone')))
message = formatDate(now)
message += "\n"
message += self.weather.getWeather().display()
message += "\n"
message += self.alarmThread.getMenuLine()
self.setMessage(message, True)
except:
log.exception("Error in LcdThread loop")
# end while not stopping
self.setMessage("Shutting down")
self.lcd.shutdown()
self.menu.stop()