-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain.cpp
More file actions
458 lines (391 loc) · 13.2 KB
/
main.cpp
File metadata and controls
458 lines (391 loc) · 13.2 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//=====[Libraries]=============================================================
#include "mbed.h"
#include "arm_book_lib.h"
//=====[Defines]===============================================================
#define NUMBER_OF_KEYS 4
#define BLINKING_TIME_GAS_ALARM 1000
#define BLINKING_TIME_OVER_TEMP_ALARM 500
#define BLINKING_TIME_GAS_AND_OVER_TEMP_ALARM 100
#define NUMBER_OF_AVG_SAMPLES 100
#define OVER_TEMP_LEVEL 50
#define TIME_INCREMENT_MS 10
#define DEBOUNCE_KEY_TIME_MS 40
#define KEYPAD_NUMBER_OF_ROWS 4
#define KEYPAD_NUMBER_OF_COLS 4
//=====[Declaration of public data types]======================================
typedef enum {
MATRIX_KEYPAD_SCANNING,
MATRIX_KEYPAD_DEBOUNCE,
MATRIX_KEYPAD_KEY_HOLD_PRESSED
} matrixKeypadState_t;
//=====[Declaration and initialization of public global objects]===============
DigitalIn alarmTestButton(BUTTON1);
DigitalIn mq2(PE_12);
DigitalOut alarmLed(LED1);
DigitalOut incorrectCodeLed(LED3);
DigitalOut systemBlockedLed(LED2);
DigitalInOut sirenPin(PE_10);
UnbufferedSerial uartUsb(USBTX, USBRX, 115200);
AnalogIn lm35(A1);
DigitalOut keypadRowPins[KEYPAD_NUMBER_OF_ROWS] = {PB_3, PB_5, PC_7, PA_15};
DigitalIn keypadColPins[KEYPAD_NUMBER_OF_COLS] = {PB_12, PB_13, PB_15, PC_6};
//=====[Declaration and initialization of public global variables]=============
bool alarmState = OFF;
bool incorrectCode = false;
bool overTempDetector = OFF;
int numberOfIncorrectCodes = 0;
int numberOfHashKeyReleasedEvents = 0;
int keyBeingCompared = 0;
int keyBeingConfigured = 0;
char codeSequence[NUMBER_OF_KEYS] = { '1', '8', '0', '5' };
char keysPressed[NUMBER_OF_KEYS] = { '0', '0', '0', '0' };
int accumulatedTimeAlarm = 0;
bool gasDetectorState = OFF;
bool overTempDetectorState = OFF;
float potentiometerReading = 0.0;
float lm35ReadingsAverage = 0.0;
float lm35ReadingsSum = 0.0;
float lm35ReadingsArray[NUMBER_OF_AVG_SAMPLES];
float lm35TempC = 0.0;
int accumulatedDebounceMatrixKeypadTime = 0;
int matrixKeypadCodeIndex = 0;
char matrixKeypadLastKeyPressed = '\0';
char matrixKeypadIndexToCharArray[] = {
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D',
};
matrixKeypadState_t matrixKeypadState;
//=====[Declarations (prototypes) of public functions]=========================
void inputsInit();
void outputsInit();
void alarmActivationUpdate();
void alarmDeactivationUpdate();
void uartTask();
void availableCommands();
bool areEqual();
float celsiusToFahrenheit( float tempInCelsiusDegrees );
float analogReadingScaledWithTheLM35Formula( float analogReading );
void lm35ReadingsArrayInit();
void matrixKeypadInit();
char matrixKeypadScan();
char matrixKeypadUpdate();
//=====[Main function, the program entry point after power on or reset]========
int main()
{
inputsInit();
outputsInit();
while (true) {
alarmActivationUpdate();
alarmDeactivationUpdate();
uartTask();
delay(TIME_INCREMENT_MS);
}
}
//=====[Implementations of public functions]===================================
void inputsInit()
{
lm35ReadingsArrayInit();
sirenPin.mode(OpenDrain);
sirenPin.input();
matrixKeypadInit();
}
void outputsInit()
{
alarmLed = OFF;
incorrectCodeLed = OFF;
systemBlockedLed = OFF;
}
void alarmActivationUpdate()
{
static int lm35SampleIndex = 0;
int i = 0;
lm35ReadingsArray[lm35SampleIndex] = lm35.read();
lm35SampleIndex++;
if ( lm35SampleIndex >= NUMBER_OF_AVG_SAMPLES) {
lm35SampleIndex = 0;
}
lm35ReadingsSum = 0.0;
for (i = 0; i < NUMBER_OF_AVG_SAMPLES; i++) {
lm35ReadingsSum = lm35ReadingsSum + lm35ReadingsArray[i];
}
lm35ReadingsAverage = lm35ReadingsSum / NUMBER_OF_AVG_SAMPLES;
lm35TempC = analogReadingScaledWithTheLM35Formula ( lm35ReadingsAverage );
if ( lm35TempC > OVER_TEMP_LEVEL ) {
overTempDetector = ON;
} else {
overTempDetector = OFF;
}
if( !mq2) {
gasDetectorState = ON;
alarmState = ON;
}
if( overTempDetector ) {
overTempDetectorState = ON;
alarmState = ON;
}
if( alarmTestButton ) {
overTempDetectorState = ON;
gasDetectorState = ON;
alarmState = ON;
}
if( alarmState ) {
accumulatedTimeAlarm = accumulatedTimeAlarm + TIME_INCREMENT_MS;
sirenPin.output();
sirenPin = LOW;
if( gasDetectorState && overTempDetectorState ) {
if( accumulatedTimeAlarm >= BLINKING_TIME_GAS_AND_OVER_TEMP_ALARM ) {
accumulatedTimeAlarm = 0;
alarmLed = !alarmLed;
}
} else if( gasDetectorState ) {
if( accumulatedTimeAlarm >= BLINKING_TIME_GAS_ALARM ) {
accumulatedTimeAlarm = 0;
alarmLed = !alarmLed;
}
} else if ( overTempDetectorState ) {
if( accumulatedTimeAlarm >= BLINKING_TIME_OVER_TEMP_ALARM ) {
accumulatedTimeAlarm = 0;
alarmLed = !alarmLed;
}
}
} else{
alarmLed = OFF;
gasDetectorState = OFF;
overTempDetectorState = OFF;
sirenPin.input();
}
}
void alarmDeactivationUpdate()
{
if ( numberOfIncorrectCodes < 5 ) {
char keyReleased = matrixKeypadUpdate();
if( keyReleased != '\0' && keyReleased != '#' ) {
keysPressed[matrixKeypadCodeIndex] = keyReleased;
if( matrixKeypadCodeIndex >= NUMBER_OF_KEYS ) {
matrixKeypadCodeIndex = 0;
} else {
matrixKeypadCodeIndex++;
}
}
if( keyReleased == '#' ) {
if( incorrectCodeLed ) {
numberOfHashKeyReleasedEvents++;
if( numberOfHashKeyReleasedEvents >= 2 ) {
incorrectCodeLed = OFF;
numberOfHashKeyReleasedEvents = 0;
matrixKeypadCodeIndex = 0;
keysPressed[0] = '0';
keysPressed[1] = '0';
keysPressed[2] = '0';
keysPressed[3] = '0';
}
} else {
if ( alarmState ) {
if ( areEqual() ) {
alarmState = OFF;
numberOfIncorrectCodes = 0;
matrixKeypadCodeIndex = 0;
keysPressed[0] = '0';
keysPressed[1] = '0';
keysPressed[2] = '0';
keysPressed[3] = '0';
} else {
incorrectCodeLed = ON;
numberOfIncorrectCodes++;
}
}
}
}
} else {
systemBlockedLed = ON;
}
}
void uartTask()
{
char receivedChar = '\0';
char str[100];
int stringLength;
if( uartUsb.readable() ) {
uartUsb.read( &receivedChar, 1 );
switch (receivedChar) {
case '1':
if ( alarmState ) {
uartUsb.write( "The alarm is activated\r\n", 24);
} else {
uartUsb.write( "The alarm is not activated\r\n", 28);
}
break;
case '2':
if ( !mq2 ) {
uartUsb.write( "Gas is being detected\r\n", 22);
} else {
uartUsb.write( "Gas is not being detected\r\n", 27);
}
break;
case '3':
if ( overTempDetector ) {
uartUsb.write( "Temperature is above the maximum level\r\n", 40);
} else {
uartUsb.write( "Temperature is below the maximum level\r\n", 40);
}
break;
case '4':
uartUsb.write( "Please enter the four digits numeric code ", 42 );
uartUsb.write( "to deactivate the alarm: ", 25 );
incorrectCode = false;
for ( keyBeingCompared = 0;
keyBeingCompared < NUMBER_OF_KEYS;
keyBeingCompared++) {
uartUsb.read( &receivedChar, 1 );
uartUsb.write( "*", 1 );
if ( codeSequence[keyBeingCompared] != receivedChar ) {
incorrectCode = true;
}
}
if ( incorrectCode == false ) {
uartUsb.write( "\r\nThe code is correct\r\n\r\n", 25 );
alarmState = OFF;
incorrectCodeLed = OFF;
numberOfIncorrectCodes = 0;
} else {
uartUsb.write( "\r\nThe code is incorrect\r\n\r\n", 27 );
incorrectCodeLed = ON;
numberOfIncorrectCodes++;
}
break;
case '5':
uartUsb.write( "Please enter the new four digits numeric code ", 46 );
uartUsb.write( "to deactivate the alarm: ", 25 );
for ( keyBeingConfigured = 0;
keyBeingConfigured < NUMBER_OF_KEYS;
keyBeingConfigured++) {
uartUsb.read( &receivedChar, 1 );
uartUsb.write( "*", 1 );
codeSequence[keyBeingConfigured] = receivedChar;
}
uartUsb.write( "\r\nNew code generated\r\n\r\n", 24 );
break;
case 'c':
case 'C':
sprintf ( str, "Temperature: %.2f \xB0 C\r\n", lm35TempC );
stringLength = strlen(str);
uartUsb.write( str, stringLength );
break;
case 'f':
case 'F':
sprintf ( str, "Temperature: %.2f \xB0 F\r\n",
celsiusToFahrenheit( lm35TempC ) );
stringLength = strlen(str);
uartUsb.write( str, stringLength );
break;
default:
availableCommands();
break;
}
}
}
void availableCommands()
{
uartUsb.write( "Available commands:\r\n", 21 );
uartUsb.write( "Press '1' to get the alarm state\r\n", 34 );
uartUsb.write( "Press '2' to get the gas detector state\r\n", 41 );
uartUsb.write( "Press '3' to get the over temperature detector state\r\n", 54 );
uartUsb.write( "Press '4' to enter the code sequence\r\n", 38 );
uartUsb.write( "Press '5' to enter a new code\r\n", 31 );
uartUsb.write( "Press 'f' or 'F' to get lm35 reading in Fahrenheit\r\n", 52 );
uartUsb.write( "Press 'c' or 'C' to get lm35 reading in Celsius\r\n\r\n", 51 );
}
bool areEqual()
{
int i;
for (i = 0; i < NUMBER_OF_KEYS; i++) {
if (codeSequence[i] != keysPressed[i]) {
return false;
}
}
return true;
}
float analogReadingScaledWithTheLM35Formula( float analogReading )
{
return ( analogReading * 3.3 / 0.01 );
}
float celsiusToFahrenheit( float tempInCelsiusDegrees )
{
return ( tempInCelsiusDegrees * 9.0 / 5.0 + 32.0 );
}
void lm35ReadingsArrayInit()
{
int i;
for( i=0; i<NUMBER_OF_AVG_SAMPLES ; i++ ) {
lm35ReadingsArray[i] = 0;
}
}
void matrixKeypadInit()
{
matrixKeypadState = MATRIX_KEYPAD_SCANNING;
int pinIndex = 0;
for( pinIndex=0; pinIndex<KEYPAD_NUMBER_OF_COLS; pinIndex++ ) {
(keypadColPins[pinIndex]).mode(PullUp);
}
}
char matrixKeypadScan()
{
int row = 0;
int col = 0;
int i = 0;
for( row=0; row<KEYPAD_NUMBER_OF_ROWS; row++ ) {
for( i=0; i<KEYPAD_NUMBER_OF_ROWS; i++ ) {
keypadRowPins[i] = ON;
}
keypadRowPins[row] = OFF;
for( col=0; col<KEYPAD_NUMBER_OF_COLS; col++ ) {
if( keypadColPins[col] == OFF ) {
return matrixKeypadIndexToCharArray[row*KEYPAD_NUMBER_OF_ROWS + col];
}
}
}
return '\0';
}
char matrixKeypadUpdate()
{
char keyDetected = '\0';
char keyReleased = '\0';
switch( matrixKeypadState ) {
case MATRIX_KEYPAD_SCANNING:
keyDetected = matrixKeypadScan();
if( keyDetected != '\0' ) {
matrixKeypadLastKeyPressed = keyDetected;
accumulatedDebounceMatrixKeypadTime = 0;
matrixKeypadState = MATRIX_KEYPAD_DEBOUNCE;
}
break;
case MATRIX_KEYPAD_DEBOUNCE:
if( accumulatedDebounceMatrixKeypadTime >=
DEBOUNCE_KEY_TIME_MS ) {
keyDetected = matrixKeypadScan();
if( keyDetected == matrixKeypadLastKeyPressed ) {
matrixKeypadState = MATRIX_KEYPAD_KEY_HOLD_PRESSED;
} else {
matrixKeypadState = MATRIX_KEYPAD_SCANNING;
}
}
accumulatedDebounceMatrixKeypadTime =
accumulatedDebounceMatrixKeypadTime + TIME_INCREMENT_MS;
break;
case MATRIX_KEYPAD_KEY_HOLD_PRESSED:
keyDetected = matrixKeypadScan();
if( keyDetected != matrixKeypadLastKeyPressed ) {
if( keyDetected == '\0' ) {
keyReleased = matrixKeypadLastKeyPressed;
}
matrixKeypadState = MATRIX_KEYPAD_SCANNING;
}
break;
default:
matrixKeypadInit();
break;
}
return keyReleased;
}