Bugfixes
[rtl-433.git] / src / devices / ht680.c
1 /* HT680 Remote control
2  *
3  * Copyright (C) 2016 Igor Polovnikov
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "rtl_433.h"
10
11 static int ht680_callback(bitbuffer_t *bitbuffer) {
12         bitrow_t *bb = bitbuffer->bb;
13         data_t *data;
14         
15         for (uint8_t row = 0;row < bitbuffer->num_rows;row++){
16                 uint8_t *b = bb[row];
17                 if(bitbuffer->bits_per_row[row] == 40 && //Length of packet is 40
18                         (b[0] & 0x50) == 0x50 && //Sync mask 01010000
19                         (b[1] & 0x0A) == 0x0A && //Address always mask 00001010
20                         (b[3] & 0x82) == 0x82 && //Buttons(4,3) always mask 10000010
21                         (b[4] & 0x0A) == 0x0A){  //Buttons(2,1) always mask 00001010
22                         b[0] = b[0] & 0x0F; //Clear sync
23                                                 
24                         // Tristate coding
25                         char tristate[21];
26                         char *p = tristate;
27                         for(uint8_t byte = 0; byte < 5; byte++){
28                                 for(int8_t bit = 7; bit > 0; bit -= 2){
29                                         switch ((b[byte] >> (bit-1)) & 0x03){
30                                                 case 0x00:      *p++ = '0'; break;
31                                                 case 0x01:      *p++ = '?'; break; //Invalid code 01
32                                                 case 0x02:      *p++ = 'Z'; break; //Floating state Z is 10
33                                                 case 0x03:      *p++ = '1'; break;
34                                                 default: *p++ = '!'; break; //Unknown error
35                                         }
36                                 }
37                         }
38                         *p = '\0';
39                         
40                         data = data_make("model",       "",                             DATA_STRING,    "HT680 Remote control",
41                                                          "tristate","Tristate code",DATA_STRING,        tristate,
42                                                          "address",     "Address",      DATA_FORMAT,    "0x%06X", DATA_INT, (b[0]<<16)+(b[1]<<8)+b[2],
43                                                          "button1",     "Button 1",             DATA_STRING,    (((b[4]>>4) & 0x03) == 3) ? "PRESSED" : "",
44                                                          "button2",     "Button 2",             DATA_STRING,    (((b[4]>>6) & 0x03) == 3) ? "PRESSED" : "",
45                                                          "button3",     "Button 3",             DATA_STRING,    ((((b[3]&0x7D)>>2) & 0x03) == 3) ? "PRESSED" : "",
46                                                          "button4",     "Button 4",             DATA_STRING,    ((((b[3]&0x7D)>>4) & 0x03) == 3) ? "PRESSED" : "",
47                                                          NULL);
48                         data_acquired_handler(data);
49                         
50                         return 1;
51                 }
52         }
53         return 0;
54 }
55
56 static char *output_fields[] = {
57         "model",
58         "tristate",
59         "address",
60         "data",
61         "button1",
62         "button2",
63         "button3",
64         "button4",
65         NULL
66 };
67
68 r_device ht680 = {
69   .name          = "HT680 Remote control",
70   .modulation    = OOK_PULSE_PWM_RAW,
71   .short_limit   = 400,
72   .long_limit    = 1200,
73   .reset_limit   = 13000,
74   .json_callback = &ht680_callback,
75   .disabled      = 0,
76   .demod_arg     = 1
77 };