-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarmClock.c
382 lines (350 loc) · 12.5 KB
/
alarmClock.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "alarmClock.h"
//****************Define global variables and function****************
#define TRANSITION(newState) ((fsm->state = newState), RET_TRANSITION)
#define MILIS_HOURS_SCALER 3600000
#define MILIS_MINUTES_SCALER 60000
#define MILIS_SECONDS_SCALER 1000
#define HOURS_REMAINEDER 24
#define MINUTES_REMAINDER 60
#define SECONDS_REMAINDER 60
#define MILIS_REMAINDER 1000
#define MINUTES_HOURS_SCALER 60
#define GET_HOURS(current_time) ((current_time / MILIS_HOURS_SCALER) % HOURS_REMAINEDER)
#define GET_MINS(current_time) ((current_time / MILIS_MINUTES_SCALER)% MINUTES_REMAINDER)
#define GET_SECS(current_time) ((current_time / MILIS_SECONDS_SCALER) % SECONDS_REMAINDER)
#define GET_MILIS(current_time) (current_time % MILIS_REMAINDER)
#define GET_TOTAL_TIME_MILIS(hours,minutes) (((hours*MINUTES_HOURS_SCALER) + (minutes))*MILIS_MINUTES_SCALER) // convert total time to Milis
#define ALARM_EXPIRATION_TIME_SECS 5
#define MAX_HOURS 24
#define MAX_MINS 60
#define IDEAL_SECOND 1000
//*********** Internal events triggered by internal devices (i.e. Timers) ***********
static taskDescriptor time_dispatcher = {.task = timerDispatch, .param = NULL, .period = IDEAL_SECOND, .expire=0}; // Time updating event
static taskDescriptor alarm_dispatcher = {.task = alarmTimeoutDispatch, .param = NULL, .period = 125, .expire=0}; // Timeout check event
// Green red toggle event, period dynamically changed(sync.) with the second counter, updted in (timerDispatch()) function
static taskDescriptor green_led_toggler = {.task = led_greenToggle, .param = NULL, .period = IDEAL_SECOND, .expire=0};
Fsm theFsm;
static uint16_t alarm__elapsed_time;
static struct time_t global_system_time;
static bool alarm_time_set;
/************************ Clock *****************/
/*****Helper Functions*****/
// Set system time variable to the initialized clock time (ms)
void static clock_set_time(void)
{
systemTime_t strating_time = 0;
strating_time = GET_TOTAL_TIME_MILIS(theFsm.timeSet.hour, theFsm.timeSet.minute);
scheduler_setTime(strating_time);
}
// Display the clock time accodring the the current state (Flag)
void static clock_display(uint8_t digit)
{
uint8_t hours_value = theFsm.timeSet.hour;
uint8_t min_value = theFsm.timeSet.minute;
uint8_t sec_value = theFsm.timeSet.second;
if (digit == HOURS)
{
lcd_setCursor(0,0);
fprintf(lcdout,"%02d:",hours_value);
}
else if (digit == MINTUES)
{
lcd_setCursor(3,0);
fprintf(lcdout,"%02d:",min_value);
}
else
{
lcd_setCursor(0,0);
fprintf(lcdout,"%02d:",hours_value);
lcd_setCursor(3,0);
fprintf(lcdout,"%02d:",min_value);
lcd_setCursor(6,0);
fprintf(lcdout,"%02d",sec_value);
}
}
// Update the clock time variable whenever update event is triggered.
void static clock_update_time(void)
{
systemTime_t current_time = scheduler_getTime();
global_system_time.milli = GET_MILIS(current_time);
global_system_time.second = GET_SECS(current_time);
global_system_time.minute = GET_MINS(current_time);
global_system_time.hour = GET_HOURS(current_time);
// Update the clock display if the (Alarm is not enabled and the alarm time is not being set)
if ((!theFsm.isAlarmEnabled) && (!alarm_time_set))
{
theFsm.timeSet.milli = global_system_time.milli ;
theFsm.timeSet.second = global_system_time.second;
theFsm.timeSet.minute = global_system_time.minute;
theFsm.timeSet.hour = global_system_time.hour;
}
else if ((theFsm.isAlarmEnabled))
{
// Alarm is enabled, check for time match
if ((global_system_time.minute == theFsm.timeSet.minute) && (theFsm.timeSet.hour == global_system_time.hour))
{
alarmActivationDispatch();
return;
}
}
clock_display(ALL);
}
// Handle alarm flag, and led stats.
void static check_alarm_setting(void)
{
theFsm.isAlarmEnabled = !theFsm.isAlarmEnabled;
if (theFsm.isAlarmEnabled)
{
led_yellowOn();
if (theFsm.timeSet.hour < global_system_time.hour)
{
lcd_setCursor(0,2);
fprintf(lcdout, "Warning:alarm will");
lcd_setCursor(0,3);
fprintf(lcdout, "fire the next day");
}
else if ((theFsm.timeSet.hour == global_system_time.hour))
{
if (theFsm.timeSet.minute < global_system_time.minute)
{
lcd_setCursor(0,2);
fprintf(lcdout, "Warning:alarm will");
lcd_setCursor(0,3);
fprintf(lcdout, "fire the next day");
}
}
}
else
{
led_yellowOff();
}
}
//*********** Events passed when internal events trigger by the scheduler ***********
/**
* Tasks(Events), that will be added to the scheduler as a Qeue
* @param param is set to void pointer to match scheduler task discriptor
*/
void timerDispatch(void * param)
{
green_led_toggler.period = IDEAL_SECOND - GET_MILIS(scheduler_getTime());
Event e = {.signal = UPDATE_CLOCK};
fsm_dispatch(&theFsm, &e);
}
void alarmTimeoutDispatch(void * param)
{
clock_update_time();
led_redToggle();
if ((global_system_time.second - alarm__elapsed_time) >= ALARM_EXPIRATION_TIME_SECS)
{
Event e = {.signal = ALARM_EXPIRED};
fsm_dispatch(&theFsm, &e);
}
}
void alarmActivationDispatch(void)
{
Event e = {.signal = ALARM_CLOCKTIME_MATCH};
fsm_dispatch(&theFsm, &e);
}
static void returnToRunning(void)
{
Event e = {.signal = RETURN};
fsm_dispatch(&theFsm, &e);
}
//*********** System States***********
fsmReturnStatus clock_set_hours(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
theFsm.timeSet.hour = 0;
lcd_setCursor(0,0);
fprintf(lcdout,"HH:MM\nSet clock hours ");
return RET_HANDLED;
case ROTARY_PRESSED:
theFsm.timeSet.hour = (theFsm.timeSet.hour + 1) % MAX_HOURS;
clock_display(HOURS);
return RET_HANDLED;
case ROTARY_LEFT_PRESSED:
if (theFsm.timeSet.hour > 0)
{
theFsm.timeSet.hour--;
}
clock_display(HOURS);
return RET_HANDLED;
case JOYSTICK_PRESSED:
return TRANSITION(clock_set_minutes);
case EXIT:
return RET_IGNORED;
default:
return RET_IGNORED;
}
}
fsmReturnStatus clock_set_minutes(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
theFsm.timeSet.minute = 0;
lcd_setCursor(3,0);
fprintf(lcdout, "00",theFsm.timeSet.hour);
lcd_setCursor(0,1);
fprintf(lcdout,"Set clock minutes ");
return RET_HANDLED;
case ROTARY_PRESSED:
theFsm.timeSet.minute++;
if (theFsm.timeSet.minute >= 60)
{
theFsm.timeSet.hour++;
theFsm.timeSet.minute = 0;
}
clock_display(MINTUES);
return RET_HANDLED;
case ROTARY_LEFT_PRESSED:
if (theFsm.timeSet.minute > 0)
{
theFsm.timeSet.minute--;
}
clock_display(MINTUES);
return RET_HANDLED;
case JOYSTICK_PRESSED:
return TRANSITION(clock_running);
case EXIT:
clock_set_time();
return RET_HANDLED;
default:
return RET_IGNORED;
}
}
fsmReturnStatus clock_running(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
lcd_setCursor(0,1);
fprintf(lcdout," ");
scheduler_add(&time_dispatcher);
scheduler_add(&green_led_toggler);
return RET_HANDLED;
case UPDATE_CLOCK:
clock_update_time();
return RET_HANDLED;
case ROTARY_PRESSED:
return TRANSITION(clock_alarm_enable);
case JOYSTICK_PRESSED:
return TRANSITION(alarm_set_hours);
case ALARM_CLOCKTIME_MATCH:
return TRANSITION(clock_alarm_activated);
case EXIT:
scheduler_remove(&time_dispatcher);
return RET_HANDLED;
default:
return RET_IGNORED;
}
}
fsmReturnStatus clock_alarm_enable(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
check_alarm_setting();
returnToRunning();
return RET_HANDLED;
case RETURN:
return TRANSITION(clock_running);
case EXIT:
default:
return RET_IGNORED;
}
}
fsmReturnStatus alarm_set_hours(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
theFsm.timeSet.hour = 0;
lcd_clear();
lcd_setCursor(0,0);
fprintf(lcdout,"00:MM\nSet alaram hours ");
return RET_HANDLED;
case ROTARY_PRESSED:
theFsm.timeSet.hour = (theFsm.timeSet.hour + 1) % MAX_HOURS;
clock_display(HOURS);
return RET_HANDLED;
case JOYSTICK_PRESSED:
return TRANSITION(alarm_set_minutes);
case ROTARY_LEFT_PRESSED:
if (theFsm.timeSet.hour > 0)
{
theFsm.timeSet.hour--;
}
clock_display(HOURS);
return RET_HANDLED;
case EXIT:return RET_IGNORED;
default:
return RET_IGNORED;
}
}
fsmReturnStatus alarm_set_minutes(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
theFsm.timeSet.minute = 0;
lcd_setCursor(3,0);
fprintf(lcdout, "00",theFsm.timeSet.hour);
lcd_setCursor(0,1);
fprintf(lcdout,"Set alarm minutes ");
return RET_HANDLED;
case ROTARY_PRESSED:
theFsm.timeSet.minute++;
if (theFsm.timeSet.minute >= 60)
{
theFsm.timeSet.hour++;
theFsm.timeSet.minute = 0;
}
clock_display(MINTUES);
return RET_HANDLED;
case JOYSTICK_PRESSED:
return TRANSITION(clock_running);
case ROTARY_LEFT_PRESSED:
if (theFsm.timeSet.minute > 0)
{
theFsm.timeSet.minute--;
}
clock_display(MINTUES);
return RET_HANDLED;
case EXIT:
theFsm.timeSet.second =0;
alarm_time_set = true;
return RET_HANDLED;
default:
return RET_IGNORED;
}
}
fsmReturnStatus clock_alarm_activated(Fsm * fsm, const Event * event)
{
switch(event->signal)
{
case ENTRY:
fsm->isAlarmEnabled = false;
alarm__elapsed_time = global_system_time.second;
led_redToggle();
led_greenOff();
led_yellowOff();
scheduler_add(&alarm_dispatcher);
return RET_HANDLED;
case JOYSTICK_PRESSED:
return TRANSITION(clock_running);
case ROTARY_PRESSED:
return TRANSITION(clock_running);
case ALARM_EXPIRED:
return TRANSITION(clock_running);
case EXIT:
alarm_time_set = false;
scheduler_remove(&alarm_dispatcher);
led_redOff();
return RET_HANDLED;
default:
return RET_IGNORED;
}
}