-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerControl.c
More file actions
226 lines (203 loc) · 6.79 KB
/
timerControl.c
File metadata and controls
226 lines (203 loc) · 6.79 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
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
/*
* timerControl.c
*
* Created: 06-Apr-18 8:20:04 PM
* Copyright: ©2018 Alsayed Alsisi
* Author: Alsayed Alsisi
* License:
You have the complete freedom to use the code in any educational or commercial purposes under the following conditions:
- You don't remove my name under any circumstances.
- You use the code as is. And if you need to edit it before using it you may contact me first.
* Contact Details:
- E-mail: alsayed.alsisi@gmail.com
- Phone Number: +201066769510
*/
/*----------------------------------------------------------------
--------------------- File Inclusions ----------------------------
----------------------------------------------------------------*/
#include "timerControl.h"
#include "buzzer.h"
#include "sw.h"
#include "timer0.h"
#include "lcd4bit.h"
#include "swingControl.h"
#include "fanPowerControl.h"
#include "ssd.h"
#include <stdint.h>
/*----------------------------------------------------------------
--------------------- Private Constants ---------------------------
----------------------------------------------------------------*/
/*----------------------------------------------------------------
--------------------- Private Data Types -------------------------
----------------------------------------------------------------*/
typedef enum{
NO_STOP, //the state in which the fan is working continuously without time limit.
TURN_OFF_AFTER_1HOUR, //the state in which the fan will stop after 1 hour.
TURN_OFF_AFTER_2HOUR, //the state in which the fan will stop after 2 hours.
TURN_OFF_AFTER_4HOUR, //the state in which the fan will stop after 4 hours.
TIMER_CONTROL_STOPPED //the state in which the fan is stopped.
} timer_control_state_t;
/*----------------------------------------------------------------
--------------------- Private Variable Definitions ---------------
----------------------------------------------------------------*/
static timer_control_state_t timer_control_current_state = NO_STOP; //fan default state is working continuously.
static uint16_t timer_milliseconds_counter = 0;
static uint8_t timer_seconds_couter = 0;
static uint8_t timer_minutes_counter = 0;
static uint8_t timer_hours_counter = 0;
/*----------------------------------------------------------------
--------------------- Public Function Definitions ----------------
----------------------------------------------------------------*/
void timer_control_update(void)
{
sw_state_t timer_control_sw_state = timer_sw_state_get(); //reading the state of the timer control sw
switch (timer_control_current_state)
{
case NO_STOP:
//keep checking for a press on timer control sw:
if (timer_control_sw_state == FALLING_EDGE) //if the sw is pressed, then
{
buzzer_start(); //start the 2 seconds buzz, and
timer_control_current_state = TURN_OFF_AFTER_1HOUR; //go to next state.
ssd_minutes_var_set(0);
ssd_hours_var_set(1);
}
break;
case TURN_OFF_AFTER_1HOUR:
timer_milliseconds_counter +=TIMER_TICK; //start counting time
if(timer_milliseconds_counter == 1000)
{
timer_milliseconds_counter = 0;
timer_seconds_couter++;
if (timer_seconds_couter == 60)
{
timer_seconds_couter = 0;
timer_minutes_counter++;
if (timer_minutes_counter == 1)
{ssd_hours_var_decrement();}
if (timer_minutes_counter == 60)
{
timer_minutes_counter = 0;
timer_hours_counter++;
}
}
}
if (timer_hours_counter == 1)
{
timer_hours_counter = 0;
fan_turn_off();
}
//keep checking for a press on timer control sw:
else if (timer_control_sw_state == FALLING_EDGE) //if the sw is pressed, then
{
buzzer_start(); //start the 2 seconds buzz, and
timer_control_current_state = TURN_OFF_AFTER_2HOUR; //go to next state.
timer_seconds_couter = 0;
timer_minutes_counter = 0;
timer_hours_counter = 0;
ssd_minutes_var_set(0);
ssd_hours_var_set(2);
}
break;
case TURN_OFF_AFTER_2HOUR:
timer_milliseconds_counter +=TIMER_TICK; //start counting time
if(timer_milliseconds_counter == 1000)
{
timer_milliseconds_counter = 0;
timer_seconds_couter++;
if (timer_seconds_couter == 60)
{
timer_seconds_couter = 0;
timer_minutes_counter++;
if (timer_minutes_counter == 1)
{ssd_hours_var_decrement();}
if (timer_minutes_counter == 60)
{
timer_minutes_counter = 0;
timer_hours_counter++;
}
}
}
if (timer_hours_counter == 2)
{
timer_hours_counter = 0;
fan_turn_off();
}
//keep checking for a press on timer control sw:
else if (timer_control_sw_state == FALLING_EDGE) //if the sw is pressed, then
{
buzzer_start(); //start the 2 seconds buzz, and
timer_control_current_state = TURN_OFF_AFTER_4HOUR; //go to next state.
timer_seconds_couter = 0;
timer_minutes_counter = 0;
timer_hours_counter = 0;
ssd_minutes_var_set(0);
ssd_hours_var_set(4);
}
break;
case TURN_OFF_AFTER_4HOUR:
timer_milliseconds_counter +=TIMER_TICK; //start counting time
if(timer_milliseconds_counter == 1000)
{
timer_milliseconds_counter = 0;
timer_seconds_couter++;
if (timer_seconds_couter == 60)
{
timer_seconds_couter = 0;
timer_minutes_counter++;
if (timer_minutes_counter == 1)
{ssd_hours_var_decrement();}
if (timer_minutes_counter == 60)
{
timer_minutes_counter = 0;
timer_hours_counter++;
}
}
}
if (timer_hours_counter == 4)
{
timer_hours_counter = 0;
fan_turn_off();
}
//keep checking for a press on timer control sw:
else if (timer_control_sw_state == FALLING_EDGE)//if the sw is pressed,
{
timer_control_current_state = NO_STOP; //go again to the start state
timer_seconds_couter = 0;
timer_minutes_counter = 0;
timer_hours_counter = 0;
buzzer_start(); //starting the 2 seconds buzz
ssd_minutes_var_set(0);
ssd_hours_var_set(0);
}
break;
case TIMER_CONTROL_STOPPED:
timer_seconds_couter = 0;
timer_minutes_counter = 0;
timer_hours_counter = 0;
ssd_minutes_var_set(0);
ssd_hours_var_set(0);
break;
default:
break;
}
}
uint8_t timer_control_minutes_counter_value_get(void)
{
return timer_minutes_counter;
}
uint8_t timer_control_hours_counter_value_get(void)
{
return timer_hours_counter;
}
extern void timer_control_stop(void)
{
timer_control_current_state = TIMER_CONTROL_STOPPED;
}
extern void timer_control_start(void)
{
timer_control_current_state = NO_STOP;
}
/*----------------------------------------------------------------
--------------------- End of File --------------------------------
----------------------------------------------------------------*/