- Merged with upstream version
[rtl-433.git] / src / devices / newkaku.c
1 #include "rtl_433.h"
2
3 static int newkaku_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS], int16_t bits_per_row[BITBUF_ROWS]) {
4     /* Two bits map to 2 states, 0 1 -> 0 and 1 1 -> 1 */
5     /* Status bit can be 1 1 -> 1 which indicates DIM value. 4 extra bits are present with value */
6     /*start pulse: 1T high, 10.44T low */
7     /*- 26 bit:  Address */
8     /*- 1  bit:  group bit*/
9     /*- 1  bit:  Status bit on/off/[dim]*/
10     /*- 4  bit:  unit*/
11     /*- [4 bit:  dim level. Present if [dim] is used, but might be present anyway...]*/
12     /*- stop pulse: 1T high, 40T low */
13     int i;
14     uint8_t tmp = 0;
15     uint8_t unit = 0;
16     uint8_t packet = 0;
17     uint8_t bitcount = 0;
18     uint32_t kakuid = 0;
19
20     if (bb[0][0] == 0xac) {//allways starts with ac
21         // first bit is from startbit sequence, not part of payload!
22         // check protocol if value is 10 or 01, else stop processing as it is no vallid KAKU packet!
23         //get id=24bits, remember 1st 1 bit = startbit, no payload!
24         for (packet = 0; packet < 6; packet++) {//get first part kakuid
25             tmp = bb[0][packet] << 1;
26             if ((bb[0][packet + 1]&(1 << 7)) != 0) {// if set add bit to current
27                 tmp++;
28             }
29
30             for (bitcount = 0; bitcount < 8; bitcount += 2) {//process bitstream, check protocol!
31
32                 if (((tmp << bitcount & (0x80)) == 0x80)&((tmp << bitcount & (0x40)) == 0)) {
33                     //add 1
34                     kakuid = kakuid << 1;
35                     kakuid++;
36                 } else
37                     if (((tmp << bitcount & (0x80)) == 0)&((tmp << bitcount & (0x40)) == 0x40)) {
38                     kakuid = kakuid << 1;
39                     //add 0
40                 } else {
41                     return 0; //00 and 11 indicates packet error. Do exit, no valid packet
42                 }
43             }
44         }
45         tmp = bb[0][6] << 1; //Get last part ID
46         for (bitcount = 0; bitcount < 4; bitcount += 2) {
47             if (((tmp << bitcount & (0x80)) == 0x80)&((tmp << bitcount & (0x40)) == 0)) {
48                 //add 1
49                 kakuid = kakuid << 1;
50                 kakuid++;
51             } else
52                 if (((tmp << bitcount & (0x80)) == 0)&((tmp << bitcount & (0x40)) == 0x40)) {
53                 //= add bit on kakuid
54                 kakuid = kakuid << 1;
55                 //add 0
56             } else {
57                 //fprintf(stderr, " Packet error, no newkaku!!\n", tmp << bitcount);
58                 return 0; //00 and 11 indicates packet error. no valid packet! do exit
59             }
60         }
61         //Get unit ID
62         tmp = bb[0][7] << 1;
63         if ((bb[0][8]&(1 << 7)) != 0) {// if set add bit to current
64             tmp++;
65         }
66         for (bitcount = 0; bitcount < 8; bitcount += 2) {//process bitstream, check protocol!
67             if (((tmp << bitcount & (0x80)) == 0x80)&((tmp << bitcount & (0x40)) == 0)) {
68                 //add 1
69                 unit = unit << 1;
70                 unit++;
71             } else
72                 if (((tmp << bitcount & (0x80)) == 0)&((tmp << bitcount & (0x40)) == 0x40)) {
73                 unit = unit << 1;
74                 //add 0
75             } else {
76                 return 0; //00 and 11 indicates packet error. Do exit, no valid packet
77             }
78         }
79         fprintf(stdout, "BUTTON:TYPE=NEWKAKU,");
80         fprintf(stdout, "ID=%d,", kakuid);
81         fprintf(stdout, "UNIT=%d,", unit);
82         fprintf(stdout, "GROUPCALL=%s,", (((bb[0][6] & (0x04)) == 0x04)&((bb[0][6] & (0x02)) == 0)) ? "YES" : "NO");
83         fprintf(stdout, "COMMAND=%s,", (((bb[0][6] & (0x01)) == 0x01)&((bb[0][7] & (0x80)) == 0)) ? "ON" : "OFF");
84         if (((bb[0][6] & (0x01)) == 0x01)&((bb[0][7] & (0x80)) == 0x80)) {//11 indicates DIM command, 4 extra bits indicate DIM value
85             fprintf(stdout, "DIM=YES,");
86             tmp = bb[0][8] << 1; // get packet, loose first bit
87             uint8_t dv = 0;
88             if ((bb[0][9]&(1 << 7)) != 0) {// if bit is set Add to current packet
89                 tmp++;
90                 for (bitcount = 0; bitcount < 8; bitcount += 2) {//process last bit outside
91                     if (((tmp << bitcount & (0x80)) == 0x80)&((tmp << bitcount & (0x40)) == 0)) {
92                         //add 1
93                         dv = dv << 1;
94                         dv++;
95                     } else
96                         if (((tmp << bitcount & (0x80)) == 0)&((tmp << bitcount & (0x40)) == 0x40)) {
97                         dv = dv << 1;
98                         //add 0
99                     } else {
100                         return 0; //00 and 11 indicates packet error. Do exit, no valid packet
101                     }
102                 }
103             }
104             fprintf(stdout, "DIMVALUE=%d\n", dv);
105         } else {
106             fprintf(stdout, "DIM=NO\n");
107         }
108         fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
109                 bb[0][0], bb[0][1], bb[0][2], bb[0][3], bb[0][4], bb[0][5], bb[0][6], bb[0][7], bb[0][8]);
110         if (debug_output)
111             debug_callback(bb, bits_per_row);
112         return 1;
113     }
114     return 0;
115 }
116
117 r_device newkaku = {
118     /* .id             = */ 17,
119     /* .name           = */ "KlikAanKlikUit Wireless Switch",
120     /* .modulation     = */ OOK_PWM_D,
121     /* .short_limit    = */ 200,
122     /* .long_limit     = */ 800,
123     /* .reset_limit    = */ 4000,
124     /* .json_callback  = */ &newkaku_callback,
125 };