-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathdscClassicKeypad.cpp
More file actions
395 lines (331 loc) · 10.8 KB
/
dscClassicKeypad.cpp
File metadata and controls
395 lines (331 loc) · 10.8 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
/*
DSC Keybus Interface
https://github.com/taligentx/dscKeybusInterface
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dscClassicKeypad.h"
#if defined(ESP32)
portMUX_TYPE dscClassicKeypadInterface::timer1Mux = portMUX_INITIALIZER_UNLOCKED;
hw_timer_t * dscClassicKeypadInterface::timer1 = NULL;
#endif // ESP32
dscClassicKeypadInterface::dscClassicKeypadInterface(byte setClockPin, byte setReadPin, byte setWritePin) {
dscClockPin = setClockPin;
dscReadPin = setReadPin;
dscWritePin = setWritePin;
commandReady = true;
keyData = 0xFF;
clockInterval = 50000; // Sets AVR timer 1 to trigger an overflow interrupt every ~1ms to generate a 500Hz clock signal
keyInterval = 150;
alarmKeyInterval = 1000;
}
void dscClassicKeypadInterface::begin(Stream &_stream) {
pinMode(dscClockPin, OUTPUT);
pinMode(dscReadPin, INPUT);
pinMode(dscWritePin, OUTPUT);
digitalWrite(dscClockPin, LOW);
digitalWrite(dscWritePin, LOW);
stream = &_stream;
// Platform-specific timers setup the Keybus 1kHz clock signal
// Arduino/AVR Timer1 calls ISR(TIMER1_OVF_vect)
#if defined(__AVR__)
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = clockInterval;
TCCR1B |= (1 << CS10);
// esp8266 timer1 calls dscClockInterrupt()
#elif defined(ESP8266)
timer1_isr_init();
timer1_attachInterrupt(dscClockInterrupt);
timer1_write(5000);
// esp32 timer1 calls dscClockInterrupt()
#elif defined(ESP32)
timer1 = timerBegin(1, 80, true);
timerStop(timer1);
timerAttachInterrupt(timer1, &dscClockInterrupt, true);
timerAlarmWrite(timer1, 1000, true);
timerAlarmEnable(timer1);
#endif
intervalStart = millis();
unsigned long keybusTime = millis();
while (millis() - keybusTime < 100) { // Waits for the keypad to be powered on
if (!digitalRead(dscReadPin)) keybusTime = millis();
#if defined(ESP8266) || defined(ESP32)
yield();
#endif
}
}
bool dscClassicKeypadInterface::loop() {
// Sets up the next panel command once the previous command is complete
if (commandReady && millis() - intervalStart >= commandInterval) {
commandReady = false;
// Sets lights
if (panelLights != previousLights) {
previousLights = panelLights;
classicCommand[1] = panelLights;
}
// Sets zones
if (panelZones != previousZones) {
previousZones = panelZones;
classicCommand[0] = panelZones;
}
// Key beep
if (keyBeep) {
if (!beepStart) {
beepStart = true;
beepInterval = millis();
bitWrite(classicCommand[1], 0, 1);
}
else if (millis() - beepInterval > 100) {
beepStart = false;
keyBeep = false;
bitWrite(classicCommand[1], 0, 0);
}
}
// Sets next panel command
for (byte i = 0; i < 2; i++) panelCommand[i] = classicCommand[i];
panelCommandByteTotal = 2;
clockCycleCount = 0;
clockCycleTotal = panelCommandByteTotal * 16;
#if defined(__AVR__)
TIMSK1 |= (1 << TOIE1); // Enables AVR Timer 1 interrupt
#elif defined(ESP8266)
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
#elif defined(ESP32)
timerStart(timer1);
#endif
}
else if (!commandReady) intervalStart = millis();
// Sets panel lights
panelLight(lightReady, 7);
panelLight(lightArmed, 6);
panelLight(lightMemory, 5);
panelLight(lightBypass, 4);
panelLight(lightTrouble, 3);
panelLight(lightProgram, 2);
panelLight(lightFire, 1);
// Sets zone lights
zoneLight(lightZone1, 7);
zoneLight(lightZone2, 6);
zoneLight(lightZone3, 5);
zoneLight(lightZone4, 4);
zoneLight(lightZone5, 3);
zoneLight(lightZone6, 2);
zoneLight(lightZone7, 1);
zoneLight(lightZone8, 0);
// Skips key processing if the key buffer is empty
if (keyBufferLength == 0) return false;
// Copies data from the buffer to keyData
static byte keyBufferIndex = 1;
byte dataIndex = keyBufferIndex - 1;
keyData = keyBuffer[dataIndex];
keyBufferIndex++;
// Resets counters when the buffer is cleared
#if defined(ESP32)
portENTER_CRITICAL(&timer1Mux);
#else
noInterrupts();
#endif
if (keyBufferIndex > keyBufferLength) {
keyBufferIndex = 1;
keyBufferLength = 0;
}
#if defined(ESP32)
portEXIT_CRITICAL(&timer1Mux);
#else
interrupts();
#endif
if (keyData != 0xFF) {
keyAvailable = true;
keyBeep = true;
switch (keyData) {
case 0xD7: key = 0x00; break; // 0
case 0xBE: key = 0x05; break; // 1
case 0xDE: key = 0x0A; break; // 2
case 0xEE: key = 0x0F; break; // 3
case 0xBD: key = 0x11; break; // 4
case 0xDD: key = 0x16; break; // 5
case 0xED: key = 0x1B; break; // 6
case 0xBB: key = 0x1C; break; // 7
case 0xDB: key = 0x22; break; // 8
case 0xEB: key = 0x27; break; // 9
case 0xB7: key = 0x28; break; // *
case 0xE7: key = 0x2D; break; // #
case 0x3F: key = 0x0B; break; // Fire alarm
case 0x5F: key = 0x0D; break; // Aux alarm
case 0x6F: key = 0x0E; break; // Panic alarm
default: keyAvailable = false; keyBeep = false; break; // Skips other DSC key values and invalid data
}
keyData = 0xFF;
}
return true;
}
void dscClassicKeypadInterface::panelLight(Light lightPanel, byte zoneBit) {
if (lightPanel == on) {
bitWrite(panelLights, zoneBit, 1);
bitWrite(panelBlink, zoneBit, 0);
}
else if (lightPanel == blink) bitWrite(panelBlink, zoneBit, 1);
else {
bitWrite(panelLights, zoneBit, 0);
bitWrite(panelBlink, zoneBit, 0);
}
}
void dscClassicKeypadInterface::zoneLight(Light lightZone, byte zoneBit) {
if (lightZone == on ) {
bitWrite(panelZones, zoneBit, 1);
bitWrite(panelZonesBlink, zoneBit, 0);
}
else if (lightZone == blink) bitWrite(panelZonesBlink, zoneBit, 1);
else {
bitWrite(panelZones, zoneBit, 0);
bitWrite(panelZonesBlink, zoneBit, 0);
}
}
void dscClassicKeypadInterface::beep(byte beeps) {
(void) beeps;
}
void dscClassicKeypadInterface::tone(byte beep, bool tone, byte interval) {
(void) beep;
(void) tone;
(void) interval;
}
void dscClassicKeypadInterface::buzzer(byte seconds) {
(void) seconds;
}
#if defined(__AVR__)
void dscClassicKeypadInterface::dscClockInterrupt() {
#elif defined(ESP8266)
void ICACHE_RAM_ATTR dscClassicKeypadInterface::dscClockInterrupt() {
#elif defined(ESP32)
void IRAM_ATTR dscClassicKeypadInterface::dscClockInterrupt() {
#endif
// Toggles the clock pin for the length of a panel command
if (clockCycleCount < clockCycleTotal) {
static bool clockHigh = true;
if (clockHigh) {
clockHigh = false;
digitalWrite(dscClockPin, HIGH);
digitalWrite(dscWritePin, LOW);
}
else {
clockHigh = true;
digitalWrite(dscClockPin, LOW);
if (isrModuleByteCount < dscReadSize) {
// Data is captured in each byte by shifting left by 1 bit and writing to bit 0
if (isrModuleBitCount < 8) {
isrModuleData[isrModuleByteCount] <<= 1;
if (digitalRead(dscReadPin) == HIGH) {
isrModuleData[isrModuleByteCount] |= 1;
}
else {
moduleDataDetected = true; // Keypads and modules send data by pulling the data line low
}
}
// Increments the bit counter if the byte is incomplete
if (isrModuleBitCount < 7) {
isrModuleBitCount++;
}
// Byte is complete, set the counters for the next byte
else {
isrModuleBitCount = 0;
isrModuleByteCount++;
}
isrModuleBitTotal++;
}
// Write panel data
if (isrPanelBitCount == 7) {
if (!bitRead(panelCommand[panelCommandByteCount], 0)) digitalWrite(dscWritePin, HIGH);
isrPanelBitCount = 0;
isrPanelBitTotal++;
panelCommandByteCount++;
}
// Panel command bytes bits 0-6
else if (panelCommandByteCount < panelCommandByteTotal) {
byte bitCount = 0;
for (byte i = 7; i > 0; i--) {
if (isrPanelBitCount == bitCount && !bitRead(panelCommand[panelCommandByteCount], i)) digitalWrite(dscWritePin, HIGH);
bitCount++;
}
isrPanelBitCount++;
isrPanelBitTotal++;
}
}
clockCycleCount++;
}
// Panel command complete
else {
digitalWrite(dscClockPin, LOW);
// Checks for module data
if (moduleDataDetected) {
moduleDataDetected = false;
for (byte i = 0; i < dscReadSize; i++) moduleData[i] = isrModuleData[i];
if (isrModuleData[0] != 0xFF) {
// Checks that alarm keys are pressed continuously for the alarmKeyInterval before setting the key
if (isrModuleData[0] == 0x3F || isrModuleData[0] == 0x5F || isrModuleData[0] == 0x6F) {
if (!alarmKeyDetected) {
alarmKeyDetected = true;
alarmKeyTime = millis();
}
else if (millis() - alarmKeyTime > alarmKeyInterval) {
keyBuffer[keyBufferLength] = isrModuleData[0];
keyBufferLength++;
alarmKeyDetected = false;
}
else {
keyBuffer[keyBufferLength] = 0xFF;
keyBufferLength++;
}
}
// Checks for regular keys and debounces for keyInterval
else {
alarmKeyDetected = false;
alarmKeyTime = millis();
// Skips the debounce interval if a different key is pressed
if (keyBuffer[keyBufferLength] != isrModuleData[0]) {
keyBuffer[keyBufferLength] = isrModuleData[0];
keyBufferLength++;
repeatInterval = millis();
}
// Sets the key
else if (millis() - repeatInterval > keyInterval) {
keyBuffer[keyBufferLength] = isrModuleData[0];
keyBufferLength++;
repeatInterval = millis();
}
}
}
}
else {
alarmKeyDetected = false;
alarmKeyTime = millis();
}
// Resets counters
for (byte i = 0; i < dscReadSize; i++) isrModuleData[i] = 0;
isrModuleBitTotal = 0;
isrModuleBitCount = 0;
isrModuleByteCount = 0;
panelCommandByteCount = 0;
isrPanelBitTotal = 0;
isrPanelBitCount = 0;
commandReady = true;
#if defined(__AVR__)
TIMSK1 = 0; // Disables AVR Timer 1 interrupt
#elif defined(ESP8266)
timer1_disable();
#elif defined(ESP32)
timerStop(timer1);
#endif
}
#if defined(__AVR__)
TCNT1 = clockInterval;
#endif
}