-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleepy.py
executable file
·298 lines (248 loc) · 9.87 KB
/
sleepy.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import Tkconstants as TkC
from Tkinter import Tk, Frame, Label
from PIL import ImageTk as itk
#import sensor
import sim_sensor as sensor
import glob
import json
import os
import sys
import time
import tkFont
import urllib
import urllib2
import thread
import datetime
import math
class Palette:
def __init__(self):
pass
background = "#192D2B"
primary = "#694330"
secondary = "#948358"
class Weather(Frame):
widgets = {}
blank = None
images = {}
def __init__(self, parent, w, h):
"""
Create the GUI elements that display the weather data
"""
Frame.__init__(self, parent)
self.parent = parent
self.config(
height=h,
width=w,
bg="gray",
)
self.load_images()
# to be able to size labels in pixels, they need an image assigned
self.blank = itk.PhotoImage(file="pix/Blank.gif")
font10 = tkFont.Font(family='Droid Sans', size=9)
font8 = tkFont.Font(family='Droid Sans', size=8)
for i in range(0, 5):
self.widgets[i] = {}
self.widgets[i]['frame'] = Frame(self, width=(w / 5),
height=h, bg=Palette.background)
self.widgets[i]['frame'].place(x=((w / 5) * i), y=0)
self.widgets[i]['iconholder'] = Label(self.widgets[i]['frame'],
image=self.images['none'],
bg=Palette.background,
borderwidth=0)
self.widgets[i]['iconholder'].place(x=8, y=0)
self.widgets[i]['line1'] = Label(self.widgets[i]['frame'],
text="??h ??°",
bg=Palette.background,
fg=Palette.secondary,
width=(w / 5),
compound="center",
image=self.blank, font=font10)
self.widgets[i]['line1'].place(x=0, y=48)
self.widgets[i]['line2'] = Label(self.widgets[i]['frame'],
text="???", bg=Palette.background,
fg=Palette.secondary, width=(w / 5),
compound="center", image=self.blank,
font=font8)
self.widgets[i]['line2'].place(x=0, y=64)
def load_images(self):
"""
Preloads the weather images
"""
for f in glob.glob('pix/weather/*.png'):
base = os.path.splitext(os.path.basename(f))[0]
self.images[base] = itk.PhotoImage(file=f)
def update_data(self, weather_data):
"""
Update the weather display with the given data
"""
for i in range(0, 5):
self.widgets[i]['line1'].config(
text="%d° %sh" % (
weather_data['list'][i]['main']['temp'] - 273.15,
datetime.datetime.fromtimestamp(
weather_data['list'][i]['dt']).strftime('%H').lstrip('0')
)
)
self.widgets[i]['line2'].config(
text=weather_data['list'][i]['weather'][0]['main']
)
self.widgets[i]['iconholder'].config(
image=self.images[weather_data['list'][i]['weather'][0]['icon']]
)
class Sleepy(Frame):
clock = None
calendar = None
weather = None
temperature = None
sleep_phase = None
def __init__(self, parent):
Frame.__init__(self, parent, background=Palette.background)
self.parent = parent
self.pack(fill=TkC.BOTH, expand=1)
# init the clock
clock_font = tkFont.Font(family='Droid Sans', size=52, weight='bold')
self.clock = Label(self, text="??:??", fg=Palette.primary,
bg=Palette.background, font=clock_font)
self.clock.place(x=0, y=0)
# init the calendar
calendar_font = tkFont.Font(family='Droid Sans', size=12)
self.calendar = Label(self, text="?? ?????, ???",
fg=Palette.secondary, bg=Palette.background,
font=calendar_font)
self.calendar.place(x=4, y=70)
#init the sleep phase indicator
sleep_phase_font = tkFont.Font(family='Droid Sans', size=14)
self.sleep_phase = Label(self, text="Heavy sleep",
fg=Palette.primary, bg=Palette.background,
font=sleep_phase_font)
self.sleep_phase.place(x=4, y=90)
# init the weather
self.weather = Weather(self, 320, 82)
self.weather.place(x=0, y=(240 - 82))
# init the temperature
temperature_font = tkFont.Font(family='Droid Sans', size=12)
self.temperature = Label(self, text="?? °C", fg=Palette.secondary,
bg=Palette.background, font=temperature_font)
self.temperature.place(x=240, y=50)
# print tkFont.families()
''' ('Century Schoolbook L', 'Droid Sans Mono', 'Droid Sans Ethiopic',
'Droid Sans Thai', 'DejaVu Sans Mono', 'URW Palladio L',
'Droid Arabic Naskh', 'URW Gothic L', 'Dingbats', 'URW Chancery L',
'FreeSerif', 'DejaVu Sans', 'Droid Sans Japanese', 'Droid Sans Georgian',
'Nimbus Sans L', 'Droid Serif', 'Droid Sans Hebrew',
'Droid Sans Fallback', 'Standard Symbols L', 'Nimbus Mono L',
'Nimbus Roman No9 L', 'FreeSans', 'DejaVu Serif', 'Droid Sans Armenian',
'FreeMono', 'URW Bookman L', 'Droid Sans') '''
# start working
self.update_clock()
self.update_temperature()
self.fetch_weather_thread()
self.monitor_sleep_phase_thread()
def update_clock(self):
"""
Updates the clock every ten seconds (less precision = less stuff to do)
"""
now = time.strftime("%H:%M")
self.clock.configure(text=now)
cal = time.strftime("%A, %B %d")
self.calendar.configure(text=cal)
self.parent.after(10000, self.update_clock)
def update_sleep_phase(self,sleep_phase):
"""
Change the current sleep phase indication
"""
self.sleep_phase.configure(text=sleep_phase)
def update_temperature(self):
"""
Update the temperature every minute
"""
t = sensor.DS18B20()
temp = "%0.2f °C" % (t.read_temp())
self.temperature.configure(text=temp)
self.parent.after(60*1000, self.update_temperature)
def fetch_weather_thread(self):
"""
start a thread to fetch new weather data
"""
thread.start_new_thread(self.fetch_weather, ())
def fetch_weather(self):
"""
fetch new weather data
"""
apikey = "f4b0ead5b6a13da37d586a327730ae0b" # FIXME move to config
location = "Berlin,de"
values = {
'q': location,
'APPID': apikey,
'mode': json
}
try:
response = urllib2.urlopen(
'http://api.openweathermap.org/data/2.5/forecast?' +
urllib.urlencode(values))
data = response.read()
weather_data = json.loads(data)
self.weather.update_data(weather_data)
# call again in 1 hour
self.parent.after(1000 * 60 * 60, self.fetch_weather_thread)
except urllib2.URLError as e:
print e.reason
# call again in 1 minute
self.parent.after(1000 * 60 * 1, self.fetch_weather_thread)
def monitor_sleep_phase_thread(self):
"""
start a thread to monitor the sleep phase
"""
thread.start_new_thread(self.monitor_sleep_phase, ())
def monitor_sleep_phase(self):
"""
monitor the sleep status of the sleeper
"""
#speedup_factor speeds up the process for testing
#it should be set to 1 for normal operation
speedup_factor=100
#constants
#FIXME: add to configuration file
accelerometer_calibrated_one=1.02
accelerometer_time_resolution=1.0/speedup_factor
awake_threshold=0.2
light_sleep_threshold=0.05
awake_time_constant=600/speedup_factor
light_sleep_time_constant=600/speedup_factor
#assume at beginning that sleeper is fast asleep and has been forever
sleep_phase='heavy sleep'
last_awake_spike_time=float("-inf")
last_light_sleep_spike_time=float("-inf")
accelerometer=sensor.ADXL345()
while True:
prev_status=sleep_phase
now=time.time()
(x,y,z)=accelerometer.getAxes(True)
resultant=math.sqrt(x**2+y**2+z**2)
spike=math.fabs(resultant-accelerometer_calibrated_one)
#print(spike)
if spike>awake_threshold:
last_awake_spike_time=now
elif spike>light_sleep_threshold:
last_light_sleep_spike_time=now
if now - last_awake_spike_time < awake_time_constant:
sleep_phase='awake'
elif now - last_light_sleep_spike_time < light_sleep_time_constant:
sleep_phase='light sleep'
else:
sleep_phase='heavy sleep'
if sleep_phase != prev_status:
self.update_sleep_phase(sleep_phase)
time.sleep(accelerometer_time_resolution)
def main():
root = Tk()
root.geometry("320x240")
root.wm_title('sleepy')
if len(sys.argv) > 1 and sys.argv[1] == 'fs':
root.wm_attributes('-fullscreen', True)
app = Sleepy(root)
root.mainloop()
if __name__ == '__main__':
main()