Bugfixes
[rtl-433.git] / src / devices / elro_db286a.c
1 /* Doorbell implementation for Elro DB286A devices
2  * 
3  * Note that each device seems to have two codes, which alternate
4  * for every other button press.
5  * 
6  * Example code: 37f62a6c80
7  * 
8  * Copyright (C) 2016 Fabian Zaremba <fabian@youremail.eu>
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14  
15 #include "rtl_433.h"
16 #include "pulse_demod.h"
17 #include "data.h"
18 #include "util.h"
19
20 //33 pulses per data pattern
21 #define DB286A_PULSECOUNT               33
22 //5*8 = 40 bits, 7 trailing zero bits are encoded, too
23 #define DB286A_CODEBYTES                5
24 //Hex character count for code:
25 //(DB286A_CODEBYTES*8)/4 (8 bits per byte, 4 bits per hex character)
26 #define DB286A_CODECHARS                DB286A_CODEBYTES*2
27 //Minimum data pattern repetitions (14 is maximum)
28 #define DB286A_MINPATTERN               5
29
30 static int doorbell_db286a_callback(bitbuffer_t *bitbuffer) {
31         
32         char time_str[LOCAL_TIME_BUFLEN];
33         data_t *data;
34         bitrow_t *bb = bitbuffer->bb;
35         uint8_t *b = bb[1];
36         unsigned bits = bitbuffer->bits_per_row[1];
37
38         char id_string[DB286A_CODECHARS+1];
39         char *idsp = id_string;
40         
41         unsigned i;
42         
43         if (bits != DB286A_PULSECOUNT) {
44                 return 0;
45         }
46         
47         if (count_repeats(bitbuffer, 1) < DB286A_MINPATTERN) {
48                 return 0;
49         }
50         
51         //Get hex string representation of code pattern
52         for (i = 0; i <= DB286A_CODEBYTES; i++) {
53             idsp += sprintf(idsp, "%02x", b[i]);        
54         }
55         id_string[DB286A_CODECHARS] = '\0';
56         
57         local_time_str(0, time_str);
58         
59         data = data_make(
60                                 "time",                 "",             DATA_STRING, time_str,
61                                 "model",                "",             DATA_STRING, "Elro DB286A",
62                                 "code",                 "Code", DATA_STRING, id_string,
63                                 NULL);
64         
65         data_acquired_handler(data);
66                         
67         return 1;
68                 
69 }
70
71 static char *output_fields[] = {
72     "time",
73     "model",
74     "code",
75     NULL
76 };
77
78 r_device elro_db286a = {
79         .name                   = "Elro DB286A Doorbell",
80         .modulation     = OOK_PULSE_PWM_RAW,
81         .short_limit    = 800,
82         .long_limit     = 1500*4,
83         .reset_limit    = 2000*4,
84         .json_callback  = &doorbell_db286a_callback,
85         .disabled               = 0,
86     .fields         = output_fields
87 };