Bugfixes
[rtl-433.git] / src / devices / springfield.c
1 #include "rtl_433.h"
2 #include "data.h"
3 #include "util.h"
4
5 // Actually 37 bits for all but last transmission which is 36 bits
6 #define NUM_BITS        36
7
8 static int springfield_callback(bitbuffer_t *bitbuffer) {
9         int ret = 0;
10         char time_str[LOCAL_TIME_BUFLEN];
11         int row;
12         int cs;
13         int i;
14         int nibble[NUM_BITS/4+1];
15         int sid, battery, transmit, channel, temp;
16         float tempC;
17         int moisture, uk1;
18         int checksum;
19         data_t *data;
20         long tmpData;
21         long savData = 0;
22
23         local_time_str(0, time_str);
24
25         for(row = 0; row < bitbuffer->num_rows; row++) {
26                 if(bitbuffer->bits_per_row[row] == NUM_BITS || bitbuffer->bits_per_row[row] == NUM_BITS + 1) {
27                         cs = 0;
28                         tmpData = (bitbuffer->bb[row][0] << 24) + (bitbuffer->bb[row][1] << 16) + (bitbuffer->bb[row][2] << 8) + bitbuffer->bb[row][3];
29                         for(i = 0; i < (NUM_BITS/4); i++) {
30                                 if((i & 0x01) == 0x01)
31                                         nibble[i] = bitbuffer->bb[row][i >> 1] & 0x0f;
32                                 else
33                                         nibble[i] = bitbuffer->bb[row][i >> 1] >> 0x04;
34                                 if(i < 7) cs ^= nibble[i];
35                         }
36                         cs = (cs & 0xF);
37                         checksum = nibble[7];
38                         if(checksum == cs && tmpData != savData) {
39                                 savData = tmpData;
40                                 sid      = (nibble[0] << 4) + nibble[1];
41                                 battery  = (nibble[2] >> 3) & 0x01;
42                                 transmit = (nibble[2] >> 2) & 0x01;
43                                 channel  = (nibble[2] & 0x03) + 1;
44                                 temp     = ((nibble[3] << 8) + (nibble[4] << 4) + nibble[5]);
45                                 if(temp >= 0xf00) temp = temp - 0x1000;
46                                 tempC    = temp / 10.0;
47                                 moisture =  nibble[6];
48                                 uk1      =  nibble[8];  /* unknown. */
49
50                                 data = data_make(
51                                         "time",                 "",                             DATA_STRING,    time_str,
52                                         "model",                "",                             DATA_STRING,    "Springfield Temperature & Moisture",
53                                         "sid",                  "SID",                  DATA_INT,               sid,
54                                         "channel",              "Channel",              DATA_INT,               channel,
55                                         "battery",              "Battery",              DATA_STRING,    battery ? "LOW" : "OK",
56                                         "transmit",             "Transmit",             DATA_STRING,    transmit ? "MANUAL" : "AUTO",
57                                         "temperature_C","Temperature",  DATA_FORMAT,    "%.01f C",                              DATA_DOUBLE,    tempC,
58                                         "moisture",             "Moisture",             DATA_INT,               moisture,
59 //                                      "uk1",                  "uk1",                  DATA_INT,               uk1,
60                                         NULL);
61                                 data_acquired_handler(data);
62                                 ret++;
63                         }
64                 }
65         }
66         return ret;
67 }
68
69 static char *output_fields[] = {
70   "time",
71   "model",
72   "sid",
73   "channel",
74   "battery",
75   "transmit",
76   "temperature_C",
77   "moisture",
78   NULL
79 };
80
81 r_device springfield = {
82   .name           = "Springfield Temperature and Soil Moisture",
83   .modulation     = OOK_PULSE_PPM_RAW, 
84   .short_limit    = 500 * 4,
85   .long_limit     = 1000 * 4, 
86   .reset_limit    = 2300 * 4,
87   .json_callback  = &springfield_callback,
88   .disabled       = 0,
89   .demod_arg      = 0,
90   .fields         = output_fields
91 };