├── CRC32.cpp ├── CRC32.h ├── README.md ├── connections.png └── dsc_1832.ino /CRC32.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "CRC32.h" 3 | static uint32_t crc_table[16] = { 4 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 5 | 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 6 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 7 | 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 8 | }; 9 | 10 | CRC32::CRC32(){} 11 | 12 | /*CRC32::~CRC32(){}*/ 13 | 14 | unsigned long CRC32::crc_update(unsigned long crc, byte data) 15 | { 16 | byte tbl_idx; 17 | tbl_idx = crc ^ (data >> (0 * 4)); 18 | crc = *(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); 19 | tbl_idx = crc ^ (data >> (1 * 4)); 20 | crc = *(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); 21 | return crc; 22 | } 23 | 24 | unsigned long CRC32::crc_string(const char *s) 25 | { 26 | unsigned long crc = ~0L; 27 | while (*s) 28 | crc = crc_update(crc, *s++); 29 | crc = ~crc; 30 | return crc; 31 | } -------------------------------------------------------------------------------- /CRC32.h: -------------------------------------------------------------------------------- 1 | #ifndef CRC32_H 2 | #define CRC32_H 3 | 4 | #include "Arduino.h" 5 | 6 | class CRC32 { 7 | public: 8 | CRC32(); 9 | /*~CRC32();*/ 10 | unsigned long crc_string(const char *s); 11 | private: 12 | unsigned long crc_update(unsigned long crc, byte data); 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-Keybus 2 | Reads data from the DSC Keybus communication lines. Derived from http://www.avrfreaks.net/comment/864193 and cleaned up for successful compilation. 3 | 4 | ### How To Connect 5 | 6 | ![alt text][logo] 7 | 8 | [logo]: https://raw.githubusercontent.com/emcniece/Arduino-Keybus/master/connections.png "Connect DSC Keybus to Arduino" 9 | 10 | ### How To Run 11 | Be sure to change the #include path to the CRC32.h to match your current working directory before compiling. After uploading to your board, open the serial monitor to begin watching data. -------------------------------------------------------------------------------- /connections.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/emcniece/Arduino-Keybus/2f950c16af5c5b5dcf687539931cdaaded1ef71b/connections.png -------------------------------------------------------------------------------- /dsc_1832.ino: -------------------------------------------------------------------------------- 1 | #define CLK 3 // Keybus Yellow 2 | #define DTA 4 // Keybus Green 3 | #include 4 | #include 5 | #include "C:\path\to\local\folder\CRC32.h" 6 | #define DEVICEID "0952" 7 | 8 | char hex[] = "0123456789abcdef"; 9 | String st, old; 10 | unsigned long lastData; 11 | char buf[100]; 12 | 13 | PROGMEM const uint32_t crc_table[16] = { 14 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 15 | 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 16 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 17 | 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c 18 | }; 19 | 20 | void setup() 21 | { 22 | pinMode(CLK,INPUT); 23 | pinMode(DTA,INPUT); 24 | Serial.begin(9600); 25 | Serial.println("Booting"); 26 | 27 | // Interrupt 1 = PIN 3 external 28 | attachInterrupt(1,clkCalled,RISING); 29 | 30 | Serial.println("Ready!"); 31 | } 32 | void loop() 33 | { 34 | if (millis() - lastData > 500) 35 | digitalWrite(13,0); 36 | else 37 | digitalWrite(13,1); 38 | 39 | if (waitCLKchange(1) < 2000) return; 40 | String stc = st; 41 | st = ""; 42 | 43 | int cmd = getBinaryData(stc,0,8); 44 | if (cmd == 0x05) lastData = millis(); // Reset led counter 45 | 46 | if (stc == old) return; 47 | if (cmd == 0) return; 48 | 49 | old = stc; 50 | 51 | String msg; 52 | 53 | // Interpreter les donnees 54 | if (cmd == 0x05) 55 | { 56 | if (getBinaryData(stc,12,1)) msg += "Error"; 57 | if (getBinaryData(stc,13,1)) msg += "Bypass"; 58 | if (getBinaryData(stc,14,1)) msg += "Memory"; 59 | if (getBinaryData(stc,15,1)) msg += "Armed"; 60 | if (getBinaryData(stc,16,1)) msg += "Ready"; 61 | } 62 | 63 | if (cmd == 0xa5) 64 | { 65 | int year3 = getBinaryData(stc,9,4); 66 | int year4 = getBinaryData(stc,13,4); 67 | int month = getBinaryData(stc,19,4); 68 | int day = getBinaryData(stc,23,5); 69 | int hour = getBinaryData(stc,28,5); 70 | int minute = getBinaryData(stc,33,6); 71 | msg += "Date: 20" + String(year3) + String(year4) + "-" + String(month) + "-" + String(day) + " " + String(hour) + ":" + String(minute); 72 | } 73 | if (cmd == 0x27) 74 | { 75 | msg += "Zones: "; 76 | int zones = getBinaryData(stc,8+1+8+8+8+8,8); 77 | if (zones & 1) msg += "1"; 78 | if (zones & 2) msg += "2"; 79 | if (zones & 4) msg += "3"; 80 | if (zones & 8) msg += "4"; 81 | if (zones & 16) msg += "5"; 82 | if (zones & 32) msg += "6"; 83 | if (zones & 64) msg += "7"; 84 | if (zones & 128) msg += "8"; 85 | } 86 | 87 | Serial.print(formatDisplay(stc)); 88 | Serial.print("-> "); 89 | Serial.print(cmd,HEX); 90 | Serial.print(":"); 91 | Serial.println(msg); 92 | } 93 | void clkCalled() 94 | { 95 | if (st.length() > 200) return; // Do not overflow the arduino's little ram 96 | if (digitalRead(DTA)) st += "1"; else st += "0"; 97 | } 98 | unsigned long waitCLKchange(int currentState) 99 | { 100 | unsigned long c = 0; 101 | while (digitalRead(CLK) == currentState) 102 | { 103 | delayMicroseconds(10); 104 | c += 10; 105 | if (c > 10000) break; 106 | } 107 | return c; 108 | } 109 | String formatDisplay(String &st) 110 | { 111 | String res; 112 | res = st.substring(0,8) + " " + st.substring(8,9) + " "; 113 | int grps = (st.length() - 9) / 8; 114 | for(int i=0;i> (0 * 4)); 150 | crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); 151 | tbl_idx = crc ^ (data >> (1 * 4)); 152 | crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4); 153 | return crc; 154 | } 155 | 156 | unsigned long crc_string(char *s) 157 | { 158 | unsigned long crc = ~0L; 159 | while (*s) 160 | crc = crc_update(crc, *s++); 161 | crc = ~crc; 162 | return crc; 163 | } 164 | --------------------------------------------------------------------------------