Bugfixes
[rtl-433.git] / src / devices / kerui.c
1 /* Kerui PIR sensor
2 *
3 *       Code derrived from akhan_100F14.c
4 *
5 * Such as
6 * http://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR0.TRC0.H0.Xkerui+pir.TRS0&_nkw=kerui+pir&_sacat=0
7 */
8
9 #include "rtl_433.h"
10 #include "pulse_demod.h"
11 #include "util.h"
12 #include "data.h"
13
14 static int kerui_callback(bitbuffer_t *bitbuffer) {
15
16         int isKerui = 0;
17         data_t *data;
18         char time_str[LOCAL_TIME_BUFLEN];
19         uint32_t ID;
20         uint32_t dataBits;
21                         
22         char *CMD;
23
24         bitrow_t *bb = bitbuffer->bb;
25
26         for (int i=0; i<bitbuffer->num_rows; i++) {
27                 uint8_t *b = bb[i];
28
29                 //invert bits, short pulse is 0, long pulse is 1
30                 b[0] = ~b[0];
31                 b[1] = ~b[1];
32                 b[2] = ~b[2];
33
34                 unsigned bits = bitbuffer->bits_per_row[i];
35
36                 if (bits == 25) {
37                         local_time_str(0, time_str);
38
39                         ID = (b[0] << 12) | (b[1] << 4) | (b[2] >> 4);
40                         dataBits = b[2] & 0x0F;
41
42                         switch (dataBits) {
43                                 case 0xa:
44                                         isKerui = 1;    
45                                         CMD = "0xa (PIR)";
46                                         break;
47                                 case 0xe:
48                                         isKerui = 1;    
49                                         CMD = "0xe (DOOR)";
50                                         break;
51                                 default:
52                                         isKerui = 0;
53                                         break;
54                         }
55
56                 }
57                 
58                 if (isKerui == 1) break;
59                 
60         }
61         
62         if (isKerui == 1) {
63                 data = data_make(       "time",         "",                             DATA_STRING,    time_str,
64                                         "device",       "",                             DATA_STRING,    "Kerui Sensor",
65                                         "id",                   "ID (20bit)",   DATA_FORMAT,    "0x%x",         DATA_INT, ID,
66                                         "data",         "Data (4bit)",  DATA_STRING,    CMD,
67                                         NULL);
68                 data_acquired_handler(data);
69                 return 1;
70         } else {
71                 return 0;
72         }
73 }
74
75 static char *output_fields[] = {
76     "time",
77     "device",
78     "id",
79     "data",
80     "other",
81     NULL
82 };
83
84 PWM_Precise_Parameters pwm_precise_parameters_kerui = {
85         .pulse_tolerance        = 20,
86         .pulse_sync_width       = 0,
87 };
88
89 r_device kerui = {
90         .name          = "Kerui Sensor",
91         .modulation    = OOK_PULSE_PWM_RAW,
92         .short_limit   = 340,
93         .long_limit    = 1200,
94         .reset_limit   = 1800,
95         .json_callback = &kerui_callback,
96         .disabled      = 0,
97         .demod_arg     = (uintptr_t)&pwm_precise_parameters_kerui,
98 };