- 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(stderr, "NewKaku event:\n");
80         fprintf(stderr, "Model      = NewKaKu on/off/dimmer switch\n");
81         fprintf(stderr, "KakuId     = %d (H%.2X)\n", kakuid, kakuid);
82         fprintf(stderr, "Unit       = %d (H%.2X)\n", unit, unit);
83         fprintf(stderr, "Group Call = %s\n", (((bb[0][6] & (0x04)) == 0x04)&((bb[0][6] & (0x02)) == 0)) ? "Yes" : "No");
84         fprintf(stderr, "Command    = %s\n", (((bb[0][6] & (0x01)) == 0x01)&((bb[0][7] & (0x80)) == 0)) ? "On" : "Off");
85         if (((bb[0][6] & (0x01)) == 0x01)&((bb[0][7] & (0x80)) == 0x80)) {//11 indicates DIM command, 4 extra bits indicate DIM value
86             fprintf(stderr, "Dim        = Yes\n");
87             tmp = bb[0][8] << 1; // get packet, loose first bit
88             uint8_t dv = 0;
89             if ((bb[0][9]&(1 << 7)) != 0) {// if bit is set Add to current packet
90                 tmp++;
91                 for (bitcount = 0; bitcount < 8; bitcount += 2) {//process last bit outside
92                     if (((tmp << bitcount & (0x80)) == 0x80)&((tmp << bitcount & (0x40)) == 0)) {
93                         //add 1
94                         dv = dv << 1;
95                         dv++;
96                     } else
97                         if (((tmp << bitcount & (0x80)) == 0)&((tmp << bitcount & (0x40)) == 0x40)) {
98                         dv = dv << 1;
99                         //add 0
100                     } else {
101                         return 0; //00 and 11 indicates packet error. Do exit, no valid packet
102                     }
103                 }
104             }
105             fprintf(stderr, "Dim Value  = %d\n", dv);
106         } else {
107             fprintf(stderr, "Dim        = No\n");
108             fprintf(stderr, "Dim Value  = 0\n");
109         }
110         fprintf(stderr, "%02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
111                 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]);
112         if (debug_output)
113             debug_callback(bb, bits_per_row);
114         return 1;
115     }
116     return 0;
117 }
118
119 r_device newkaku = {
120     /* .id             = */ 17,
121     /* .name           = */ "KlikAanKlikUit Wireless Switch",
122     /* .modulation     = */ OOK_PWM_D,
123     /* .short_limit    = */ 200,
124     /* .long_limit     = */ 800,
125     /* .reset_limit    = */ 4000,
126     /* .json_callback  = */ &newkaku_callback,
127 };