- Merged with upstream version
[rtl-433.git] / src / devices / elv.c~
1 #include "rtl_433.h"
2
3 uint16_t AD_POP(uint8_t bb[BITBUF_COLS], uint8_t bits, uint8_t bit) {
4     uint16_t val = 0;
5     uint8_t i, byte_no, bit_no;
6     for (i=0;i<bits;i++) {
7         byte_no=   (bit+i)/8 ;
8         bit_no =7-((bit+i)%8);
9         if (bb[byte_no]&(1<<bit_no)) val = val | (1<<i);
10     }
11     return val;
12 }
13
14 static int em1000_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS],int16_t bits_per_row[BITBUF_ROWS]) {
15     // based on fs20.c
16     uint8_t dec[10];
17     uint8_t bytes=0;
18     uint8_t bit=18; // preamble
19     uint8_t bb_p[14];
20     char* types[] = {"S", "?", "GZ"};
21     uint8_t checksum_calculated = 0;
22     uint8_t i;
23         uint8_t stopbit;
24         uint8_t checksum_received;
25
26     // check and combine the 3 repetitions
27     for (i = 0; i < 14; i++) {
28         if(bb[0][i]==bb[1][i] || bb[0][i]==bb[2][i]) bb_p[i]=bb[0][i];
29         else if(bb[1][i]==bb[2][i])                  bb_p[i]=bb[1][i];
30         else return 0;
31     }
32
33     // read 9 bytes with stopbit ...
34     for (i = 0; i < 9; i++) {
35         dec[i] = AD_POP (bb_p, 8, bit); bit+=8;
36         stopbit=AD_POP (bb_p, 1, bit); bit+=1;
37         if (!stopbit) {
38 //            fprintf(stderr, "!stopbit: %i\n", i);
39             return 0;
40         }
41         checksum_calculated ^= dec[i];
42         bytes++;
43     }
44
45     // Read checksum
46     checksum_received = AD_POP (bb_p, 8, bit); bit+=8;
47     if (checksum_received != checksum_calculated) {
48 //        fprintf(stderr, "checksum_received != checksum_calculated: %d %d\n", checksum_received, checksum_calculated);
49         return 0;
50     }
51
52 //for (i = 0; i < bytes; i++) fprintf(stderr, "%02X ", dec[i]); fprintf(stderr, "\n");
53
54     // based on 15_CUL_EM.pm
55     fprintf(stdout, "SENSOR:TYPE=ELV_ENERGY,");
56     fprintf(stdout, "MODEL=EM1000-%s,",dec[0]>=1&&dec[0]<=3?types[dec[0]-1]:"?");
57     fprintf(stdout, "ID=%d,",dec[1]);
58     fprintf(stdout, "SEQNO=%d,",dec[2]);
59     fprintf(stdout, "TOTAL=%d,",dec[3]|dec[4]<<8);
60     fprintf(stdout, "CURRENT=%d,",dec[5]|dec[6]<<8);
61     fprintf(stdout, "PEAK=%d\n",dec[7]|dec[8]<<8);
62
63     return 1;
64 }
65
66 static int ws2000_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS],int16_t bits_per_row[BITBUF_ROWS]) {
67     // based on http://www.dc3yc.privat.t-online.de/protocol.htm
68     uint8_t dec[13];
69     uint8_t nibbles=0;
70     uint8_t bit=11; // preamble
71     char* types[]={"!AS3", "AS2000/ASH2000/S2000/S2001A/S2001IA/ASH2200/S300IA", "!S2000R", "!S2000W", "S2001I/S2001ID", "!S2500H", "!Pyrano", "!KS200/KS300"};
72     uint8_t check_calculated=0, sum_calculated=0;
73     uint8_t i;
74     uint8_t stopbit;
75         uint8_t sum_received;
76
77     dec[0] = AD_POP (bb[0], 4, bit); bit+=4;
78     stopbit= AD_POP (bb[0], 1, bit); bit+=1;
79     if (!stopbit) {
80 //fprintf(stderr, "!stopbit\n");
81         return 0;
82     }
83     check_calculated ^= dec[0];
84     sum_calculated   += dec[0];
85
86     // read nibbles with stopbit ...
87     for (i = 1; i <= (dec[0]==4?12:8); i++) {
88         dec[i] = AD_POP (bb[0], 4, bit); bit+=4;
89         stopbit= AD_POP (bb[0], 1, bit); bit+=1;
90         if (!stopbit) {
91 //fprintf(stderr, "!stopbit %i\n", i);
92             return 0;
93         }
94         check_calculated ^= dec[i];
95         sum_calculated   += dec[i];
96         nibbles++;
97     }
98
99     if (check_calculated) {
100 //fprintf(stderr, "check_calculated (%d) != 0\n", check_calculated);
101         return 0;
102     }
103
104     // Read sum
105     sum_received = AD_POP (bb[0], 4, bit); bit+=4;
106     sum_calculated+=5;
107     sum_calculated&=0xF;
108     if (sum_received != sum_calculated) {
109 //fprintf(stderr, "sum_received (%d) != sum_calculated (%d) ", sum_received, sum_calculated);
110         return 0;
111     }
112
113 //for (i = 0; i < nibbles; i++) fprintf(stderr, "%02X ", dec[i]); fprintf(stderr, "\n");
114
115     fprintf(stdout, "SENSOR:TYPE=ELV_WEATHER,");
116     fprintf(stdout, "TYPE=%s,", dec[0]<=7?types[dec[0]]:"?");
117     fprintf(stdout, "ID= %d,", dec[1]&7);
118     if(dec[0]==4) {
119         fprintf(stdout, "PRESSURE=%d,", 200+dec[10]*100+dec[9]*10+dec[8]);
120     }
121     fprintf(stdout, "TEMP=%s%d.%d,", dec[1]&8?"-":"", dec[4]*10+dec[3], dec[2]);
122     fprintf(stdout, "HUMIDITY=%d.%d\n", dec[7]*10+dec[6], dec[5]);
123
124     return 1;
125 }
126
127 r_device elv_em1000 = {
128     /* .id             = */ 7,
129     /* .name           = */ "ELV EM 1000",
130     /* .modulation     = */ OOK_PWM_D,
131     /* .short_limit    = */ 750/4,
132     /* .long_limit     = */ 7250/4,
133     /* .reset_limit    = */ 30000/4,
134     /* .json_callback  = */ &em1000_callback,
135 };
136
137 r_device elv_ws2000 = {
138     /* .id             = */ 8,
139     /* .name           = */ "ELV WS 2000",
140     /* .modulation     = */ OOK_PWM_D,
141     /* .short_limit    = */ (602+(1155-602)/2)/4,
142     /* .long_limit     = */ ((1755635-1655517)/2)/4, // no repetitions
143     /* .reset_limit    = */ ((1755635-1655517)*2)/4,
144     /* .json_callback  = */ &ws2000_callback,
145 };