-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathdscKeybusInterface.cpp
More file actions
705 lines (589 loc) · 24.6 KB
/
dscKeybusInterface.cpp
File metadata and controls
705 lines (589 loc) · 24.6 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
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 "dscKeybus.h"
#if defined(ESP32)
portMUX_TYPE dscKeybusInterface::timer1Mux = portMUX_INITIALIZER_UNLOCKED;
hw_timer_t * dscKeybusInterface::timer1 = NULL;
#endif // ESP32
dscKeybusInterface::dscKeybusInterface(byte setClockPin, byte setReadPin, byte setWritePin) {
dscClockPin = setClockPin;
dscReadPin = setReadPin;
dscWritePin = setWritePin;
if (dscWritePin != 255) virtualKeypad = true;
writeReady = false;
processRedundantData = true;
displayTrailingBits = false;
processModuleData = false;
writePartition = 1;
pauseStatus = false;
}
void dscKeybusInterface::begin(Stream &_stream) {
pinMode(dscClockPin, INPUT);
pinMode(dscReadPin, INPUT);
if (virtualKeypad) pinMode(dscWritePin, OUTPUT);
stream = &_stream;
// Platform-specific timers trigger a read of the data line 250us after the Keybus clock changes
// Arduino/AVR Timer1 calls ISR(TIMER1_OVF_vect) from dscClockInterrupt() and is disabled in the ISR for a one-shot timer
#if defined(__AVR__)
TCCR1A = 0;
TCCR1B = 0;
TIMSK1 |= (1 << TOIE1);
// esp8266 timer1 calls dscDataInterrupt() from dscClockInterrupt() as a one-shot timer
#elif defined(ESP8266)
timer1_isr_init();
timer1_attachInterrupt(dscDataInterrupt);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
// esp32 timer1 calls dscDataInterrupt() from dscClockInterrupt()
#elif defined(ESP32)
//timer1 = timerBegin(1, 80, true);
//timerStop(timer1);
//timerAttachInterrupt(timer1, &dscDataInterrupt, true);
//timerAlarmWrite(timer1, 250, true);
//timerAlarmEnable(timer1);
timer1 = timerBegin(1000000); // 1 MHz = 1 µs per tick
timerStop(timer1);
timerAttachInterrupt(timer1, &dscDataInterrupt);
timerAlarm(timer1, 250, true, 0); // 1000 ticks = 1 ms interval
#endif
// Generates an interrupt when the Keybus clock rises or falls - requires a hardware interrupt pin on Arduino/AVR
attachInterrupt(digitalPinToInterrupt(dscClockPin), dscClockInterrupt, CHANGE);
}
void dscKeybusInterface::stop() {
// Disables Arduino/AVR Timer1 interrupts
#if defined(__AVR__)
TIMSK1 = 0;
// Disables esp8266 timer1
#elif defined(ESP8266)
timer1_disable();
timer1_detachInterrupt();
// Disables esp32 timer1
#elif defined(ESP32)
//timerAlarmDisable(timer1);
//timerEnd(timer1);
timerDetachInterrupt(timer1); // Detach the interrupt handler
timerStop(timer1); // Stop counting
timerEnd(timer1); // Free the timer resource
#endif
// Disables the Keybus clock pin interrupt
detachInterrupt(digitalPinToInterrupt(dscClockPin));
// Resets the panel capture data and counters
panelBufferLength = 0;
for (byte i = 0; i < dscReadSize; i++) isrPanelData[i] = 0;
isrPanelBitTotal = 0;
isrPanelBitCount = 0;
isrPanelByteCount = 0;
// Resets the keypad and module capture data
for (byte i = 0; i < dscReadSize; i++) isrModuleData[i] = 0;
}
bool dscKeybusInterface::loop() {
#if defined(ESP8266) || defined(ESP32)
yield();
#endif
// Checks if Keybus data is detected and sets a status flag if data is not detected for 3s
#if defined(ESP32)
portENTER_CRITICAL(&timer1Mux);
#else
noInterrupts();
#endif
if (millis() - keybusTime > 3000) keybusConnected = false; // keybusTime is set in dscDataInterrupt() when the clock resets
else keybusConnected = true;
#if defined(ESP32)
portEXIT_CRITICAL(&timer1Mux);
#else
interrupts();
#endif
if (previousKeybus != keybusConnected) {
previousKeybus = keybusConnected;
keybusChanged = true;
if (!pauseStatus) statusChanged = true;
if (!keybusConnected) return true;
}
// Writes keys when multiple keys are sent as a char array
if (writeKeysPending) writeKeys(writeKeysArray);
// Skips processing if the panel data buffer is empty
if (panelBufferLength == 0) return false;
// Copies data from the buffer to panelData[]
static byte panelBufferIndex = 1;
byte dataIndex = panelBufferIndex - 1;
for (byte i = 0; i < dscReadSize; i++) panelData[i] = panelBuffer[dataIndex][i];
panelBitCount = panelBufferBitCount[dataIndex];
panelByteCount = panelBufferByteCount[dataIndex];
panelBufferIndex++;
// Resets counters when the buffer is cleared
#if defined(ESP32)
portENTER_CRITICAL(&timer1Mux);
#else
noInterrupts();
#endif
if (panelBufferIndex > panelBufferLength) {
panelBufferIndex = 1;
panelBufferLength = 0;
}
#if defined(ESP32)
portEXIT_CRITICAL(&timer1Mux);
#else
interrupts();
#endif
// Waits at startup for the 0x05 status command or a command with valid CRC data to eliminate spurious data.
static bool startupCycle = true;
if (startupCycle) {
if (panelData[0] == 0) return false;
else if (panelData[0] == 0x05 || panelData[0] == 0x1B) {
if (panelByteCount == 6) keybusVersion1 = true;
startupCycle = false;
writeReady = true;
}
else if (!validCRC()) return false;
}
// Sets writeReady status
if (!writeKeyPending && !writeKeysPending) writeReady = true;
else writeReady = false;
// Skips redundant data sent constantly while in installer programming
static byte previousCmd0A[dscReadSize];
static byte previousCmd0F[dscReadSize];
static byte previousCmdE6_20[dscReadSize];
static byte previousCmdE6_21[dscReadSize];
switch (panelData[0]) {
case 0x0A: // Partition 1 status in programming
if (redundantPanelData(previousCmd0A, panelData)) return false;
break;
case 0x0F: // Partition 2 status in programming
if (redundantPanelData(previousCmd0F, panelData)) return false;
break;
case 0xE6:
if (panelData[2] == 0x20 && redundantPanelData(previousCmdE6_20, panelData)) return false; // Partition 1 status in programming, zone lights 33-64
if (panelData[2] == 0x21 && redundantPanelData(previousCmdE6_21, panelData)) return false; // Partition 2 status in programming
break;
}
if (dscPartitions > 4) {
static byte previousCmdE6_03[dscReadSize];
if (panelData[0] == 0xE6 && panelData[2] == 0x03 && redundantPanelData(previousCmdE6_03, panelData, 8)) return false; // Status in alarm/programming, partitions 5-8
}
// Processes valid panel data
switch (panelData[0]) {
case 0x05: // Panel status: partitions 1-4
case 0x1B: processPanelStatus(); break; // Panel status: partitions 5-8
case 0x16: processPanel_0x16(); break; // Panel configuration
case 0x27: processPanel_0x27(); break; // Panel status and zones 1-8 status
case 0x2D: processPanel_0x2D(); break; // Panel status and zones 9-16 status
case 0x34: processPanel_0x34(); break; // Panel status and zones 17-24 status
case 0x3E: processPanel_0x3E(); break; // Panel status and zones 25-32 status
case 0x87: processPanel_0x87(); break; // PGM outputs
case 0xA5: processPanel_0xA5(); break; // Date, time, system status messages - partitions 1-2
case 0xE6: if (dscPartitions > 2) processPanel_0xE6(); break; // Extended status command split into multiple subcommands to handle up to 8 partitions/64 zones
case 0xEB: if (dscPartitions > 2) processPanel_0xEB(); break; // Date, time, system status messages - partitions 1-8
}
return true;
}
bool dscKeybusInterface::handleModule() {
if (!moduleDataCaptured) return false;
moduleDataCaptured = false;
if (moduleBitCount < 8) return false;
// Determines if a keybus message is a response to a panel command
switch (moduleCmd) {
case 0x11:
case 0x28:
case 0xD5: queryResponse = true; break;
default: queryResponse = false; break;
}
return true;
}
// Sets up writes for a single key
void dscKeybusInterface::write(const char receivedKey) {
// Blocks if a previous write is in progress
while(writeKeyPending || writeKeysPending) {
loop();
#if defined(ESP8266)
yield();
#endif
}
setWriteKey(receivedKey);
}
// Sets up writes for multiple keys sent as a char array
void dscKeybusInterface::write(const char *receivedKeys, bool blockingWrite) {
// Blocks if a previous write is in progress
while(writeKeyPending || writeKeysPending) {
loop();
#if defined(ESP8266)
yield();
#endif
}
writeKeysArray = receivedKeys;
if (writeKeysArray[0] != '\0') {
writeKeysPending = true;
writeReady = false;
}
// Optionally blocks until the write is complete, necessary if the received keys char array is ephemeral
if (blockingWrite) {
while (writeKeysPending) {
writeKeys(writeKeysArray);
loop();
#if defined(ESP8266)
yield();
#endif
}
}
else writeKeys(writeKeysArray);
}
// Writes multiple keys from a char array
void dscKeybusInterface::writeKeys(const char *writeKeysArray) {
static byte writeCounter = 0;
if (!writeKeyPending && writeKeysPending && writeCounter < strlen(writeKeysArray)) {
if (writeKeysArray[writeCounter] != '\0') {
setWriteKey(writeKeysArray[writeCounter]);
writeCounter++;
if (writeKeysArray[writeCounter] == '\0') {
writeKeysPending = false;
writeCounter = 0;
}
}
}
}
// Specifies the key value to be written by dscClockInterrupt() and selects the write partition. This includes a 500ms
// delay after alarm keys to resolve errors when additional keys are sent immediately after alarm keys.
void dscKeybusInterface::setWriteKey(const char receivedKey) {
static unsigned long previousTime;
static bool setPartition;
// Sets the write partition if set by virtual keypad key '/'
if (setPartition) {
setPartition = false;
if (receivedKey >= '1' && receivedKey <= '8') {
writePartition = receivedKey - 48;
}
return;
}
// Sets the binary to write for virtual keypad keys
if (!writeKeyPending && (millis() - previousTime > 500 || millis() <= 500)) {
bool validKey = true;
// Skips writing to disabled partitions or partitions not specified in dscKeybusInterface.h
if (disabled[writePartition - 1] || dscPartitions < writePartition) {
switch (receivedKey) {
case '/': setPartition = true; validKey = false; break;
}
return;
}
// Sets binary for virtual keypad keys
else {
switch (receivedKey) {
case '/': setPartition = true; validKey = false; break;
case '0': writeKey = 0x00; break;
case '1': writeKey = 0x05; break;
case '2': writeKey = 0x0A; break;
case '3': writeKey = 0x0F; break;
case '4': writeKey = 0x11; break;
case '5': writeKey = 0x16; break;
case '6': writeKey = 0x1B; break;
case '7': writeKey = 0x1C; break;
case '8': writeKey = 0x22; break;
case '9': writeKey = 0x27; break;
case '*': writeKey = 0x28; if (status[writePartition - 1] < 0x9E) starKeyCheck = true; break;
case '#': writeKey = 0x2D; break;
case 'f': case 'F': writeKey = 0xBB; writeAlarm = true; break; // Keypad fire alarm
case 'b': case 'B': writeKey = 0x82; break; // Enter event buffer
case '>': writeKey = 0x87; break; // Event buffer right arrow
case '<': writeKey = 0x88; break; // Event buffer left arrow
case 'l': case 'L': writeKey = 0xA5; break; // LCD keypad data request
case 's': case 'S': writeKey = 0xAF; writeAccessCode[writePartition - 1] = true; break; // Arm stay
case 'w': case 'W': writeKey = 0xB1; writeAccessCode[writePartition - 1] = true; break; // Arm away
case 'n': case 'N': writeKey = 0xB6; writeAccessCode[writePartition - 1] = true; break; // Arm with no entry delay (night arm)
case 'a': case 'A': writeKey = 0xDD; writeAlarm = true; break; // Keypad auxiliary alarm
case 'c': case 'C': writeKey = 0xBB; break; // Door chime
case 'r': case 'R': writeKey = 0xDA; break; // Reset
case 'p': case 'P': writeKey = 0xEE; writeAlarm = true; break; // Keypad panic alarm
case 'x': case 'X': writeKey = 0xE1; break; // Exit
case '[': writeKey = 0xD5; writeAccessCode[writePartition - 1] = true; break; // Command output 1
case ']': writeKey = 0xDA; writeAccessCode[writePartition - 1] = true; break; // Command output 2
case '{': writeKey = 0x70; writeAccessCode[writePartition - 1] = true; break; // Command output 3
case '}': writeKey = 0xEC; writeAccessCode[writePartition - 1] = true; break; // Command output 4
default: {
validKey = false;
break;
}
}
}
// Sets the writing position in dscClockInterrupt() for the currently set partition
switch (writePartition) {
case 1:
case 5: {
writeByte = 2;
writeBit = 9;
break;
}
case 2:
case 6: {
writeByte = 3;
writeBit = 17;
break;
}
case 3:
case 7: {
writeByte = 8;
writeBit = 57;
break;
}
case 4:
case 8: {
writeByte = 9;
writeBit = 65;
break;
}
default: {
writeByte = 2;
writeBit = 9;
break;
}
}
if (writeAlarm) previousTime = millis(); // Sets a marker to time writes after keypad alarm keys
if (validKey) {
writeKeyPending = true; // Sets a flag indicating that a write is pending, cleared by dscClockInterrupt()
writeReady = false;
}
}
}
#if defined(__AVR__)
bool dscKeybusInterface::redundantPanelData(byte previousCmd[], volatile byte currentCmd[], byte checkedBytes) {
#elif defined(ESP8266)
bool ICACHE_RAM_ATTR dscKeybusInterface::redundantPanelData(byte previousCmd[], volatile byte currentCmd[], byte checkedBytes) {
#elif defined(ESP32)
bool IRAM_ATTR dscKeybusInterface::redundantPanelData(byte previousCmd[], volatile byte currentCmd[], byte checkedBytes) {
#endif
bool redundantData = true;
for (byte i = 0; i < checkedBytes; i++) {
if (previousCmd[i] != currentCmd[i]) {
redundantData = false;
break;
}
}
if (redundantData) return true;
else {
for (byte i = 0; i < dscReadSize; i++) previousCmd[i] = currentCmd[i];
return false;
}
}
bool dscKeybusInterface::validCRC() {
byte byteCount = (panelBitCount - 1) / 8;
int dataSum = 0;
for (byte panelByte = 0; panelByte < byteCount; panelByte++) {
if (panelByte != 1) dataSum += panelData[panelByte];
}
if (dataSum % 256 == panelData[byteCount]) return true;
else return false;
}
// Called as an interrupt when the DSC clock changes to write data for virtual keypad and setup timers to read
// data after an interval.
#if defined(__AVR__)
void dscKeybusInterface::dscClockInterrupt() {
#elif defined(ESP8266)
void ICACHE_RAM_ATTR dscKeybusInterface::dscClockInterrupt() {
#elif defined(ESP32)
void IRAM_ATTR dscKeybusInterface::dscClockInterrupt() {
#endif
// Data sent from the panel and keypads/modules has latency after a clock change (observed up to 160us for
// keypad data). The following sets up a timer for each platform that will call dscDataInterrupt() in
// 250us to read the data line.
// AVR Timer1 calls dscDataInterrupt() via ISR(TIMER1_OVF_vect) when the Timer1 counter overflows
#if defined(__AVR__)
TCNT1=61535; // Timer1 counter start value, overflows at 65535 in 250us
TCCR1B |= (1 << CS10); // Sets the prescaler to 1
// esp8266 timer1 calls dscDataInterrupt() in 250us
#elif defined(ESP8266)
timer1_write(1250);
// esp32 timer1 calls dscDataInterrupt() in 250us
#elif defined(ESP32)
timerStart(timer1);
portENTER_CRITICAL(&timer1Mux);
#endif
static unsigned long previousClockHighTime;
static bool skipData = false;
// Panel sends data while the clock is high
if (digitalRead(dscClockPin) == HIGH) {
if (virtualKeypad) digitalWrite(dscWritePin, LOW); // Restores the data line after a virtual keypad write
previousClockHighTime = micros();
}
// Keypads and modules send data while the clock is low
else {
clockHighTime = micros() - previousClockHighTime; // Tracks the clock high time to find the reset between commands
// Saves data and resets counters after the clock cycle is complete (high for at least 1ms)
if (clockHighTime > 1000) {
keybusTime = millis();
// Skips incomplete and redundant data from status commands - these are sent constantly on the keybus at a high
// rate, so they are always skipped. Checking is required in the ISR to prevent flooding the buffer.
if (isrPanelBitTotal < 8) skipData = true;
else switch (isrPanelData[0]) {
static byte previousCmd05[dscReadSize];
static byte previousCmd1B[dscReadSize];
case 0x05: // Status: partitions 1-4
if (redundantPanelData(previousCmd05, isrPanelData, isrPanelByteCount)) skipData = true;
break;
case 0x1B: // Status: partitions 5-8
if (redundantPanelData(previousCmd1B, isrPanelData, isrPanelByteCount)) skipData = true;
break;
}
// Stores new panel data in the panel buffer
currentCmd = isrPanelData[0];
if (panelBufferLength == dscBufferSize) bufferOverflow = true;
else if (!skipData && panelBufferLength < dscBufferSize) {
for (byte i = 0; i < dscReadSize; i++) panelBuffer[panelBufferLength][i] = isrPanelData[i];
panelBufferBitCount[panelBufferLength] = isrPanelBitTotal;
panelBufferByteCount[panelBufferLength] = isrPanelByteCount;
panelBufferLength++;
}
if (processModuleData) {
// Stores new keypad and module data - this data is not buffered
if (moduleDataDetected) {
moduleCmd = isrPanelData[0];
moduleSubCmd = isrPanelData[2];
moduleDataDetected = false;
moduleDataCaptured = true; // Sets a flag for handleModule()
for (byte i = 0; i < dscReadSize; i++) moduleData[i] = isrModuleData[i];
moduleBitCount = isrPanelBitTotal;
moduleByteCount = isrPanelByteCount;
}
// Resets the keypad and module capture data
for (byte i = 0; i < dscReadSize; i++) isrModuleData[i] = 0;
}
// Resets the panel capture data and counters
for (byte i = 0; i < dscReadSize; i++) isrPanelData[i] = 0;
isrPanelBitTotal = 0;
isrPanelBitCount = 0;
isrPanelByteCount = 0;
skipData = false;
}
// Virtual keypad
if (virtualKeypad) {
static bool writeStart = false;
static bool writeRepeat = false;
static bool writeCmd = false;
if (writePartition <= 4 && statusCmd == 0x05) writeCmd = true;
else if (writePartition >= 5 && statusCmd == 0x1B) writeCmd = true;
else writeCmd = false;
// Writes a F/A/P alarm key and repeats the key on the next immediate command from the panel (0x1C verification)
if ((writeAlarm && writeKeyPending) || writeRepeat) {
// Writes the first bit by shifting the alarm key data right 7 bits and checking bit 0
if (isrPanelBitTotal == 0) {
if (!((writeKey >> 7) & 0x01)) {
digitalWrite(dscWritePin, HIGH);
}
writeStart = true; // Resolves a timing issue where some writes do not begin at the correct bit
}
// Writes the remaining alarm key data
else if (writeStart && isrPanelBitTotal <= 7) {
if (!((writeKey >> (7 - isrPanelBitTotal)) & 0x01)) digitalWrite(dscWritePin, HIGH);
// Resets counters when the write is complete
if (isrPanelBitTotal == 7) {
writeKeyPending = false;
writeStart = false;
writeAlarm = false;
// Sets up a repeated write for alarm keys
if (!writeRepeat) writeRepeat = true;
else writeRepeat = false;
}
}
}
// Writes a regular key unless waiting for a response to the '*' key or the panel is sending a query command
else if (writeKeyPending && !starKeyWait[writePartition - 1] && isrPanelByteCount == writeByte && writeCmd) {
// Writes the first bit by shifting the key data right 7 bits and checking bit 0
if (isrPanelBitTotal == writeBit) {
if (!((writeKey >> 7) & 0x01)) digitalWrite(dscWritePin, HIGH);
writeStart = true; // Resolves a timing issue where some writes do not begin at the correct bit
}
// Writes the remaining key data
else if (writeStart && isrPanelBitTotal > writeBit && isrPanelBitTotal <= writeBit + 7) {
if (!((writeKey >> (7 - isrPanelBitCount)) & 0x01)) digitalWrite(dscWritePin, HIGH);
// Resets counters when the write is complete
if (isrPanelBitTotal == writeBit + 7) {
if (starKeyCheck) starKeyWait[writePartition - 1] = true; // Handles waiting until the panel is ready after pressing '*'
else writeKeyPending = false;
writeStart = false;
}
}
}
}
}
#if defined(ESP32)
portEXIT_CRITICAL(&timer1Mux);
#endif
}
// Interrupt function called by AVR Timer1, esp8266 timer1, and esp32 timer1 after 250us to read the data line
#if defined(__AVR__)
void dscKeybusInterface::dscDataInterrupt() {
#elif defined(ESP8266)
void ICACHE_RAM_ATTR dscKeybusInterface::dscDataInterrupt() {
#elif defined(ESP32)
void IRAM_ATTR dscKeybusInterface::dscDataInterrupt() {
timerStop(timer1);
portENTER_CRITICAL(&timer1Mux);
#endif
// Panel sends data while the clock is high
if (digitalRead(dscClockPin) == HIGH) {
// Reads panel data and sets data counters
if (isrPanelByteCount < dscReadSize) { // Limits Keybus data bytes to dscReadSize
if (isrPanelBitCount < 8) {
// Data is captured in each byte by shifting left by 1 bit and writing to bit 0
isrPanelData[isrPanelByteCount] <<= 1;
if (digitalRead(dscReadPin) == HIGH) {
isrPanelData[isrPanelByteCount] |= 1;
}
}
// Tests for a status command, used in dscClockInterrupt() to ensure keys are only written during a status command
if (isrPanelBitTotal == 7) {
switch (isrPanelData[0]) {
case 0x05:
case 0x0A: statusCmd = 0x05; break;
case 0x1B: statusCmd = 0x1B; break;
default: statusCmd = 0; break;
}
}
// Stores the stop bit by itself in byte 1 - this aligns the Keybus bytes with panelData[] bytes
if (isrPanelBitTotal == 8) {
isrPanelBitCount = 0;
isrPanelByteCount++;
}
// Increments the bit counter if the byte is incomplete
else if (isrPanelBitCount < 7) {
isrPanelBitCount++;
}
// Byte is complete, set the counters for the next byte
else {
isrPanelBitCount = 0;
isrPanelByteCount++;
}
isrPanelBitTotal++;
}
}
// Keypads and modules send data while the clock is low
else {
// Keypad and module data is not buffered and skipped if the panel data buffer is filling
if (processModuleData && isrPanelByteCount < dscReadSize && panelBufferLength <= 1) {
// Data is captured in each byte by shifting left by 1 bit and writing to bit 0
if (isrPanelBitCount < 8) {
isrModuleData[isrPanelByteCount] <<= 1;
if (digitalRead(dscReadPin) == HIGH) {
isrModuleData[isrPanelByteCount] |= 1;
}
else {
moduleDataDetected = true; // Keypads and modules send data by pulling the data line low
}
}
// Stores the stop bit by itself in byte 1 - this aligns the Keybus bytes with moduleData[] bytes
if (isrPanelBitTotal == 8) {
isrModuleData[1] = 1; // Sets the stop bit manually to 1 in byte 1
}
}
}
#if defined(ESP32)
portEXIT_CRITICAL(&timer1Mux);
#endif
}