-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcode.cpp
More file actions
108 lines (85 loc) · 3.04 KB
/
code.cpp
File metadata and controls
108 lines (85 loc) · 3.04 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
//=====[Libraries]=============================================================
#include "mbed.h"
#include "arm_book_lib.h"
#include "code.h"
#include "user_interface.h"
#include "pc_serial_com.h"
#include "date_and_time.h"
#include "temperature_sensor.h"
#include "gas_sensor.h"
#include "matrix_keypad.h"
//=====[Declaration of private defines]========================================
//=====[Declaration of private data types]=====================================
//=====[Declaration and initialization of public global objects]===============
//=====[Declaration of external public global variables]=======================
extern char codeSequenceFromUserInterface[CODE_NUMBER_OF_KEYS];
extern char codeSequenceFromPcSerialCom[CODE_NUMBER_OF_KEYS];
//=====[Declaration and initialization of private global variables]============
static int numberOfIncorrectCodes = 0;
static char codeSequence[CODE_NUMBER_OF_KEYS] = { '1', '8', '0', '5' };
//=====[Declarations (prototypes) of private functions]========================
static bool codeMatch( char* codeToCompare );
static void codeDeactivate();
//=====[Implementations of public functions]===================================
void codeWrite( char* newCodeSequence )
{
int i;
for (i = 0; i < CODE_NUMBER_OF_KEYS; i++) {
codeSequence[i] = newCodeSequence[i];
}
}
bool codeMatchFrom( codeOrigin_t codeOrigin )
{
bool codeIsCorrect = false;
switch (codeOrigin) {
case CODE_KEYPAD:
if( userInterfaceCodeCompleteRead() ) {
codeIsCorrect = codeMatch(codeSequenceFromUserInterface);
userInterfaceCodeCompleteWrite(false);
if ( codeIsCorrect ) {
codeDeactivate();
} else {
incorrectCodeStateWrite(ON);
numberOfIncorrectCodes++;
}
}
break;
case CODE_PC_SERIAL:
if( pcSerialComCodeCompleteRead() ) {
codeIsCorrect = codeMatch(codeSequenceFromPcSerialCom);
pcSerialComCodeCompleteWrite(false);
if ( codeIsCorrect ) {
codeDeactivate();
pcSerialComStringWrite( "\r\nThe code is correct\r\n\r\n" );
} else {
incorrectCodeStateWrite(ON);
numberOfIncorrectCodes++;
pcSerialComStringWrite( "\r\nThe code is incorrect\r\n\r\n" );
}
}
break;
default:
break;
}
if ( numberOfIncorrectCodes >= 5 ) {
systemBlockedStateWrite(ON);
}
return codeIsCorrect;
}
//=====[Implementations of private functions]==================================
static bool codeMatch( char* codeToCompare )
{
int i;
for (i = 0; i < CODE_NUMBER_OF_KEYS; i++) {
if ( codeSequence[i] != codeToCompare[i] ) {
return false;
}
}
return true;
}
static void codeDeactivate()
{
systemBlockedStateWrite(OFF);
incorrectCodeStateWrite(OFF);
numberOfIncorrectCodes = 0;
}