-
Notifications
You must be signed in to change notification settings - Fork 1
/
*DTMF
179 lines (156 loc) · 4.11 KB
/
*DTMF
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
//TO DO
//Add commands for telemetry & debug
//Add command for TX interval
//Add ability to change PIN and commands and store them in SRAM
//build ACK and ERROR signals for DTMF commands (800Hz tone?)
/*
PASS is a 4 digit PIN, each digit separated by a comma
KILL is a 2 digit command, each digit separated by a comma
'0' button is 10, * button is 11, # button is 12
*/
#define PASS 1, 2, 3, 4
#define KILL 0, 11 // 0 *
#define ENABLE 12, 1 // # 1
#define TIMEOUT 5000 //DTMF timeout in milliseconds
#define DTMF_IN 3 // MODULE RX FLAG, must be ISR pin
unsigned int dtmfTime = 0;
uint8_t dtmfPins[4] = {8, 9, 10, 11}; //Pins for 4 bit parallel input. LSB first
uint8_t dtmfPass[] = {PASS};
uint8_t dtmfKill[] = {KILL};
uint8_t dtmfEnable[] = {ENABLE};
uint8_t dtmfBits[4];
uint8_t passBuffer[5];
uint8_t commandBuffer[3];
uint8_t commandCount = 0;
uint8_t bufCount = 0;
uint8_t inByte = 0;
bool dtmfReceived = false;
bool passSuccess = false;
bool killSuccess = false;
bool enableSuccess = false;
bool dtmfAvailable = false;
//------------dtmfSetup------------//
//Set input pins and attach ISR to RX Flag pin
//Must be called in setup()
void dtmfSetup() {
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(DTMF_IN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(DTMF_IN), dtmfISR, FALLING);
}
//------------dtmfISR------------//
//Read 4 bit parallel into 4 ints for processing later
void dtmfISR() {
for (int d = 0; d <= 3; d++) {
dtmfBits[d] = digitalRead(dtmfPins[d]);
}
dtmfAvailable = 1;
}
//------------dtmfParse------------//
//Records timestamp for first tone received, parses 4 input bytes into a uint8_t,
//split PIN and command digits into separate arrays
void dtmfParse() {
if (bufCount == 0) { //Record first tone receive time
dtmfTime = millis();
}
inByte = 0;
if (dtmfBits[0] == 1) {
inByte++;
}
if (dtmfBits[1] == 1) {
inByte = inByte + 2;
}
if (dtmfBits[2] == 1) {
inByte = inByte + 4;
}
if (dtmfBits[3] == 1) {
inByte = inByte + 8;
}
if (inByte == 10) {
inByte = 0;
}
if (bufCount < 3) { //split PIN and command digits into separate arrays
passBuffer[bufCount] = inByte;
}
if (bufCount > 3) {
commandBuffer[commandCount] = inByte;
commandCount++;
}
bufCount++;
if (bufCount > 5) {
bufCount = 0;
commandCount = 0;
dtmfReceived = true;
}
dtmfAvailable = 0; //Clear byte RX flag
}
//------------passVerify------------//
bool passVerify(){
for (int c = 0; c < 4; c++) { //Compares password string with input string
if (dtmfPass[c] != passBuffer[c]) {
Serial.println("INCORRECT PIN");
return false;
}
else {
passSuccess = true;
Serial.println("PASSWORD ACCEPTED");
return true;
}
}
}
//------------killVerify------------//
bool killVerify() { //Checks to see if command string matches "Disable"
for (int c = 0; c < 2; c++) {
if (commandBuffer[c] != dtmfKill[c]) {
return false;
}
}
killSuccess = true;
return true;
}
//------------enableVerify------------//
bool enableVerify() { //Checks to see if command string matches "Enable"
for (int c = 0; c < 2; c++) {
if (commandBuffer[c] != dtmfEnable[c]) {
return false;
}
}
enableSuccess = true;
return true;
}
//------------dtmfExecute------------//
//Calls PIN and command parsing functions above & checks for timeout
void dtmfExecute() {
if (millis() - dtmfTime > TIMEOUT) { //Checks for timeout condition and exits if PIN + command took longer than TIMEOUT setting
Serial.println("TIMEOUT");
dtmfReceived = false;
return;
}
if (passVerify() == true) {
if (killVerify() == true) {
Serial.println("DISABLED");
}
else if (enableVerify() == true) {
Serial.println("ENABLED");
}
else {
Serial.println("UNKNOWN COMMAND");
}
}
dtmfReceived = false;
}
//-------------------------------------------//
void setup() {
Serial.begin(115200);
dtmfSetup();
}
void loop() {
if (dtmfReceived == true) {
dtmfExecute();
}
if (dtmfAvailable == 1) {
dtmfParse();
}
}