Started on events and actions
[wakeup.git] / doc / design.xml
1 <?xml version="1.0"?>
2
3 <doc style="style.css">
4
5   <book>
6   <titlepage>
7       <title>Bedroom light control</title>
8    </titlepage>
9
10     <chapter>
11     <heading>Requirements</heading>
12     <section>
13     <heading>Top level requirements</heading>
14
15 <description>
16   <item tag='REQ1.01'>
17     Wake up lights fade in to specified color or slightly open curtains.
18   </item>
19
20   <item tag='REQ1.02'>
21     wake up light starts at preset alarm time
22   </item>
23
24   <item tag='REQ1.03'>
25     if outside light open curtain else fade to light level
26   </item>
27
28   <item tag='REQ1.04'>
29     open curtain and fade out light at sunrise
30   </item>
31
32   <item tag='REQ1.05'>
33     input from sunrise calculation and outside light sensor
34   </item>
35
36   <item tag='REQ1.06'>
37     user interface for alarm times
38   </item>
39
40   <item tag='REQ1.07'>
41     user interface for wake up light level, sleep light level, walk-in light level and fade times
42   </item>
43
44   <item tag='REQ1.08'>
45     light on at walking in and no outside light
46   </item>
47
48   <item tag='REQ1.09'>
49     lights fade to dark at sleep time
50   </item>
51
52   <item tag='REQ1.10'>
53     lights fade on to specified color at movement in the night.
54   </item>
55
56   <item tag='REQ1.11'>
57     curtains fully open if outside light and after alarm
58   </item>
59
60   <item tag='REQ1.12'>
61     curtains close ar outside dark
62   </item>
63
64   <item tag='REQ1.13'>
65     wake up times can be specified with a recurrance pattern
66   </item>
67
68   <item tag='REQ1.14'>
69     recurrence patterns can be n days, weeks, months, years, weekday, weekendday
70   </item>
71
72   <item tag='REQ1.15'>
73     wake up times can be optained from a online calander
74   </item>
75 </description>
76
77     </section>
78     <section>
79     <heading>Manual controls</heading>
80
81 <description>
82   <item tag='REQ2.01'>
83     manual controls override automatic controls
84   </item>
85
86   <item tag='REQ2.02'>
87     manual control for light levels
88   </item>
89
90   <item tag='REQ2.03'>
91     manual control for light on and off
92   </item>
93
94   <item tag='REQ2.04'>
95     manual control for curtains open and close
96   </item>
97
98   <item tag='REQ2.05'>
99     manual control from switches
100   </item>
101
102   <item tag='REQ2.06'>
103     manual control from remote, online
104   </item>
105 </description>
106
107     </section>
108
109     <section>
110     <heading>Security requirements</heading>
111
112 <description>
113   <item tag='REQ3.01'>
114     Accessible by authorized persons and devices only
115   </item>
116
117   <item tag='REQ3.02'>
118     All remote inputs are logged.
119   </item>
120 </description>
121
122     </section>
123     <section>
124     <heading>Testability requirements</heading>
125
126 <description>
127   <item tag='REQ4.01'>
128     Time is obtained from actual time or simulated time
129   </item>
130
131   <item tag='REQ4.02'>
132     A simulated time base is used for testing
133   </item>
134 </description>
135
136     </section>
137     </chapter>
138
139     <chapter>
140     <heading>Test scenarios</heading>
141
142 <itemize>
143   <item>
144     Wake up sequence when dark outside followed by sunrise
145     <enumerate>
146     <item>Set the date to dec 21, 23:00.</item>
147     <item>Set a wakeup event at dec 22, 06:00</item>
148     <item>Start wakeup daemon</item>
149     <item>Verify lights on at 06:00</item>
150     <item>Verify lights off at sunrise</item>
151     </enumerate>
152   </item>
153   <item>
154     wake up sequence when light outside
155   </item>
156   <item>
157     wake up with a single alarm event
158   </item>
159   <item>
160     wake up with recurring alarm events
161   </item>
162   <item>
163     sleep time fade out
164   </item>
165   <item>
166     walk in when dark
167   </item>
168   <item>
169     walk in when light
170   </item>
171   <item>
172     curtains close at dusk
173   </item>
174 </itemize>
175
176     </chapter>
177
178     <chapter>
179     <heading>Interfaces</heading>
180
181 <para>
182     inputs:
183
184 <itemize>
185   <item>
186     alarm time for wake up
187   </item>
188   <item>
189     calculated sunrise and sunset time
190   </item>
191   <item>
192     outside light level
193   </item>
194   <item>
195     manual switches
196   </item>
197   <item>
198     remote control inputs
199   </item>
200   <item>
201     pir sensor
202   </item>
203 </itemize>
204 </para>
205
206 <para>
207     outputs:
208
209 <itemize>
210   <item>
211     light levels
212   </item>
213   <item>
214     curtain open and close
215   </item>
216 </itemize>
217 </para>
218
219     </chapter>
220
221     <chapter>
222     <heading>Modules</heading>
223 <para>
224 The dataflow diagram shows the high level design.
225 </para>
226 <svg src='dataflow.svg'/>
227     <section>
228     <heading>PWM generator</heading>
229 <para>
230 The PWM generator is controlled by a list of <emph>pwm</emph> structures:
231 <verbatim>
232
233     struct pwm
234     {
235        int interval;
236        int output;
237     };
238
239 </verbatim>
240 Each structure holds an interval in microseconds and an output that is to be switched off after
241 that interval of time has passed.
242 An interval defines the width of the pulse of the PWM output signal relative to the previous interval in the list.
243 The pulse width of a specifc output is the addition of all intervals up to and including the interval for that output.
244 The last entry in the list has an output of -1, which deniotes the end of the list.
245 After that final interval has passed, all outputs are switched ON and the PWM generator returns to the first entry in the list.
246 </para>
247 <svg src='pwm-algorithm.svg'/>
248 <para>
249 This allows the PWM genrator run run with a minimum of calculations.
250 The algorithm of the PWM generator is shown in the figure below:
251 </para>
252 <svg src='pwm-psd.svg'/>
253 <para>
254 The list of PWM signal intervals and associated outputs are store in shared memory.
255 The content of this shared memroy is written by <emph>lightcontrol</emph>.
256 The PWM generator reads the list continuously to generate the output signals.
257 </para>
258     </section>
259
260     <section>
261     <heading>lightcontrol</heading>
262 <para>
263 The program <emph>lightcontrol</emph> is used to control the levels of the red, green, blue and white LEDs.
264 The levels specified on the command line are converted into PWM signals and passed to the PWM generator.
265 The PWM generator uses a list of incremental intervals, as described in the previous section.
266 These intervals are calculated by <emph>lightcontrol</emph> and stored in the shared memory interface
267 for the PWM generator.
268 The algorithm is shown is the figure below:
269 </para>
270 <svg src='light_to_pwm.svg'/>
271 <para>
272 The following figure shows the algorithm to fade the lights:
273 </para>
274 <svg src='lightfade.svg'/>
275     <subsection>
276     <heading>Command line interface</heading>
277 <para>
278 Command line options specify the operation of the LEDs:
279 <verbatim>
280
281    lightcontrol [-l] [-V] [-r red] [-g green] [-b blue] [-w white] [-f fadetime]
282
283 </verbatim>
284 Not all levels need to be specified. If the desired level for a LED is not specified, it will not be changed.
285 For example if only the option "-r 50" is given,
286 the red LED will light at 50% but the green, blue and white levels will be unchanged.
287 <description>
288   <item tag="-r red">
289     Set the level of the red LED. The level is an integer number between 0 and 100, 0 meaning fully off and 100 meaning fully on.
290   </item>
291   <item tag="-g green">
292     Set the level of the green LED. The level is an integer number between 0 and 100, 0 meaning fully off and 100 meaning fully on.
293   </item>
294   <item tag="-b blue">
295     Set the level of the blue LED. The level is an integer number between 0 and 100, 0 meaning fully off and 100 meaning fully on.
296   </item>
297   <item tag="-w white">
298     Set the level of the white LED. The level is an integer number between 0 and 100, 0 meaning fully off and 100 meaning fully on.
299   </item>
300   <item tag="-f fadetime">
301     Do not set the levels of the LEDs immediately but fade from the current levels to the desired levels
302     in <emph>fadetime</emph> seconds.
303     The default fade time is 0, which will immeditely change the light levels.
304   </item>
305   <item tag="-l">
306     List the current light levels.
307     The current levels are printed to standard output on a single line of 4 numbers.
308     The numbers are the current levels in the order red, green, blue and white.
309   </item>
310   <item tag="-V">
311     Print the version of the program and exit.
312   </item>
313 </description>
314
315 <remark>TODO:</remark> Option -p to set the PWM period, default 10000 microseconds.
316 </para>
317     </subsection>
318     <subsection>
319     <heading>Execution interface</heading>
320 <para>
321 Create a run file when fading lights to the desired level.
322 The run file holds the process id and the Tachyon name.
323 A fade can be interrupted by a signal. This allows another lightcontrol process to override a running fade.
324 The Tachyon name can be used to control the time base used by the fade process.
325 This is mainly used for test purpooses.
326 The run file is removed on exit.
327 </para>
328     </subsection>
329
330     </section>
331     <section>
332     <heading>Wakeup</heading>
333
334 <para>
335 The primary function of the wakeup process is to gradually increase the light at wakeup time in the morning.
336 Either by fading in the lights to a specified level or by (slightly) opening the curtains.
337 Controlling the curtains is the secondary function of wakeup.
338 This means fully opening the curtains in the morning and closing the curtains in the evening,
339 depending on the times of sunrise and sunset.
340 </para>
341
342 <para>
343 The wakeup times are specified like calendar events, possibly with a recurrence pattern and an end date.
344 Elements in an event are:
345 <itemize>
346    <item> Label</item>
347    <item> Action</item>
348    <item> Start time</item>
349    <item> Recurrence pattern</item>
350    <item> Number of recurrences</item>
351    <item> End time</item>
352 </itemize>
353 A recurrence pattern can be specified with a number of days, weeks or months as well as a set of weekdays.
354 A set of weekdays implies the recurrence will be weekly.
355 The action for an event can be a light sequence or a curtain control.
356 </para>
357
358 <para>
359 An event is read from an XML element or created dynamically, for example calculated from the time of sunrise.
360 From any event, the next occurance is calculated, which is an event that happens after a certain time.
361 When the next event does happen, meaning the time of the occurance is the current time, the sequence of actions is executed.
362 Each action can for example be a change in lights or the opening or closing of curtains.
363 Event methods:
364
365 FromXML
366 ToXML
367 add_recurrance
368 next_occurance
369 add_action
370 execute_actions
371 </para>
372     </section>
373     </chapter>
374
375   </book>
376
377 </doc>