|
1 | 1 | /* |
2 | | - * DSC Keybus Reader 1.0 (Arduino) |
| 2 | + * DSC Keybus Reader 1.2 (Arduino) |
3 | 3 | * |
4 | 4 | * Decodes and prints data from the Keybus to a serial interface, including reading from serial for the virtual |
5 | | - * keypad. This is primarily to help decode the Keybus protocol - see the Status examples to put the interface |
| 5 | + * keypad. This is primarily to help decode the Keybus protocol - see the Status example to put the interface |
6 | 6 | * to productive use. |
7 | 7 | * |
| 8 | + * Release notes: |
| 9 | + * 1.2 - Handle spurious data while keybus is disconnected |
| 10 | + * Removed redundant data processing |
| 11 | + * 1.0 - Initial release |
| 12 | + * |
8 | 13 | * Wiring: |
9 | 14 | * DSC Aux(+) --- Arduino Vin pin |
10 | 15 | * |
|
39 | 44 | // Configures the Keybus interface with the specified pins - dscWritePin is optional, leaving it out disables the |
40 | 45 | // virtual keypad. |
41 | 46 | #define dscClockPin 3 // Arduino Uno hardware interrupt pin: 2,3 |
42 | | -#define dscReadPin 5 // Arduino Uno: 2-12 |
| 47 | +#define dscReadPin 5 // Arduino Uno: 2-12 |
43 | 48 | #define dscWritePin 6 // Arduino Uno: 2-12 |
| 49 | + |
| 50 | +// Initialize components |
44 | 51 | dscKeybusInterface dsc(dscClockPin, dscReadPin, dscWritePin); |
45 | 52 |
|
46 | 53 |
|
47 | 54 | void setup() { |
48 | 55 | Serial.begin(115200); |
| 56 | + delay(1000); |
49 | 57 | Serial.println(); |
50 | 58 | Serial.println(); |
51 | 59 |
|
52 | 60 | // Optional configuration |
53 | | - dsc.hideKeypadDigits = false; // Controls if keypad digits are hidden for publicly posted logs (default: false) |
54 | | - dsc.processRedundantData = false; // Controls if repeated periodic commands are processed and displayed (default: true) |
55 | | - dsc.processModuleData = true; // Controls if keypad and module data is processed and displayed (default: false) |
56 | | - dsc.displayTrailingBits = false; // Controls if bits read as the clock is reset are displayed, appears to be spurious data (default: false) |
| 61 | + dsc.hideKeypadDigits = false; // Controls if keypad digits are hidden for publicly posted logs |
| 62 | + dsc.processModuleData = true; // Controls if keypad and module data is processed and displayed |
| 63 | + dsc.displayTrailingBits = false; // Controls if bits read as the clock is reset are displayed, appears to be spurious data |
57 | 64 |
|
58 | 65 | // Starts the Keybus interface and optionally specifies how to print data. |
59 | 66 | // begin() sets Serial by default and can accept a different stream: begin(Serial1), etc. |
60 | 67 | dsc.begin(); |
61 | | - |
62 | 68 | Serial.println(F("DSC Keybus Interface is online.")); |
63 | 69 | } |
64 | 70 |
|
65 | 71 |
|
66 | 72 | void loop() { |
67 | 73 |
|
68 | 74 | // Reads from serial input and writes to the Keybus as a virtual keypad |
69 | | - if (Serial.available() > 0 && dsc.writeReady) { |
70 | | - dsc.write(Serial.read()); |
71 | | - } |
| 75 | + if (Serial.available() > 0) dsc.write(Serial.read()); |
72 | 76 |
|
73 | | - if (dsc.handlePanel()) { |
| 77 | + if (dsc.loop()) { |
| 78 | + |
| 79 | + if (dsc.statusChanged) { // Checks if the security system status has changed |
| 80 | + dsc.statusChanged = false; // Reset the status tracking flag |
| 81 | + |
| 82 | + // Checks if the interface is connected to the Keybus |
| 83 | + if (dsc.keybusChanged) { |
| 84 | + dsc.keybusChanged = false; // Resets the Keybus data status flag |
| 85 | + if (dsc.keybusConnected) Serial.println(F("Keybus connected")); |
| 86 | + else Serial.println(F("Keybus disconnected")); |
| 87 | + } |
| 88 | + } |
74 | 89 |
|
75 | 90 | // If the Keybus data buffer is exceeded, the sketch is too busy to process all Keybus commands. Call |
76 | | - // handlePanel() more often, or increase dscBufferSize in the library: src/dscKeybusInterface.h |
77 | | - if (dsc.bufferOverflow) Serial.println(F("Keybus buffer overflow")); |
78 | | - dsc.bufferOverflow = false; |
| 91 | + // loop() more often, or increase dscBufferSize in the library: src/dscKeybusInterface.h |
| 92 | + if (dsc.bufferOverflow) { |
| 93 | + Serial.println(F("Keybus buffer overflow")); |
| 94 | + dsc.bufferOverflow = false; |
| 95 | + } |
79 | 96 |
|
80 | 97 | // Prints panel data |
81 | | - printTimestamp(); |
82 | | - Serial.print(" "); |
83 | | - dsc.printPanelBinary(); // Optionally prints without spaces: printPanelBinary(false); |
84 | | - Serial.print(" ["); |
85 | | - dsc.printPanelCommand(); // Prints the panel command as hex |
86 | | - Serial.print("] "); |
87 | | - dsc.printPanelMessage(); // Prints the decoded message |
88 | | - Serial.println(); |
89 | | - |
90 | | - // Prints keypad and module data when valid panel data is printed |
91 | | - if (dsc.handleModule()) { |
| 98 | + if (dsc.keybusConnected) { |
92 | 99 | printTimestamp(); |
93 | 100 | Serial.print(" "); |
94 | | - dsc.printModuleBinary(); // Optionally prints without spaces: printKeybusBinary(false); |
95 | | - Serial.print(" "); |
96 | | - dsc.printModuleMessage(); // Prints the decoded message |
| 101 | + dsc.printPanelBinary(); // Optionally prints without spaces: printPanelBinary(false); |
| 102 | + Serial.print(" ["); |
| 103 | + dsc.printPanelCommand(); // Prints the panel command as hex |
| 104 | + Serial.print("] "); |
| 105 | + dsc.printPanelMessage(); // Prints the decoded message |
97 | 106 | Serial.println(); |
| 107 | + |
| 108 | + // Prints keypad and module data when valid panel data is printed |
| 109 | + if (dsc.handleModule()) printModule(); |
98 | 110 | } |
99 | 111 | } |
100 | 112 |
|
101 | 113 | // Prints keypad and module data when valid panel data is not available |
102 | | - else if (dsc.handleModule()) { |
103 | | - printTimestamp(); |
104 | | - Serial.print(" "); |
105 | | - dsc.printModuleBinary(); // Optionally prints without spaces: printKeybusBinary(false); |
106 | | - Serial.print(" "); |
107 | | - dsc.printModuleMessage(); |
108 | | - Serial.println(); |
109 | | - } |
| 114 | + else if (dsc.keybusConnected && dsc.handleModule()) printModule(); |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +// Prints keypad and module data |
| 119 | +void printModule() { |
| 120 | + printTimestamp(); |
| 121 | + Serial.print(" "); |
| 122 | + dsc.printModuleBinary(); // Optionally prints without spaces: printKeybusBinary(false); |
| 123 | + Serial.print(" "); |
| 124 | + dsc.printModuleMessage(); // Prints the decoded message |
| 125 | + Serial.println(); |
110 | 126 | } |
111 | 127 |
|
| 128 | + |
112 | 129 | // Prints a timestamp in seconds (with 2 decimal precision) - this is useful to determine when |
113 | 130 | // the panel sends a group of messages immediately after each other due to an event. |
114 | 131 | void printTimestamp() { |
|
0 commit comments