ir_hasher.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 
3 #include <pigpio.h>
4 
5 #include "ir_hasher.h"
6 
7 /*
8 This code forms a hash over the IR pulses generated by an
9 IR remote.
10 
11 The remote key press is not converted into a code in the manner of
12 the lirc module. No attempt is made to decode the type of protocol
13 used by the remote. The hash is likely to be unique for different
14 keys and different remotes but this is not guaranteed.
15 
16 This hashing process works for some remotes/protocols but not for
17 others. The only way to find out if it works for one or more of
18 your remotes is to try it and see.
19 */
20 
22 {
23  int gpio;
25  int timeout;
26  int in_code;
27  uint32_t hash_val;
28  int edges;
29  int t1;
30  int t2;
31  int t3;
32  int t4;
33 };
34 
35 
36 static uint32_t _hash(uint32_t hv, int old_val, int new_val)
37 {
38  int val;
39 
40  if (new_val < (old_val * 0.60)) val = 13;
41  else if (old_val < (new_val * 0.60)) val = 23;
42  else val = 2;
43 
44  hv ^= val;
45  hv *= 16777619; /* FNV_PRIME_32 */
46 
47  return hv;
48 }
49 
50 static void _cb(int gpio, int level, uint32_t tick, void *user)
51 {
52  Pi_Hasher_t * hasher;
53 
54  hasher = user;
55 
56  if (level != PI_TIMEOUT)
57  {
58  if (hasher->in_code == 0)
59  {
60  hasher->in_code = 1;
61 
62  gpioSetWatchdog(gpio, hasher->timeout);
63 
64  hasher->hash_val = 2166136261U; /* FNV_BASIS_32 */
65 
66  hasher->edges = 1;
67 
68  hasher->t1 = 0;
69  hasher->t2 = 0;
70  hasher->t3 = 0;
71  hasher->t4 = tick;
72  }
73  else
74  {
75  hasher->edges++;
76 
77  hasher->t1 = hasher->t2;
78  hasher->t2 = hasher->t3;
79  hasher->t3 = hasher->t4;
80  hasher->t4 = tick;
81 
82  if (hasher->edges > 3)
83  {
84  hasher->hash_val =
85  _hash(hasher->hash_val,
86  (hasher->t2)-(hasher->t1),
87  (hasher->t4)-(hasher->t3));
88  }
89  }
90  }
91  else
92  {
93  if (hasher->in_code)
94  {
95  hasher->in_code = 0;
96 
97  gpioSetWatchdog(gpio, 0);
98 
99  if (hasher->edges > 12) /* Anything less is probably noise. */
100  {
101  (hasher->callback)(hasher->hash_val);
102  }
103  }
104  }
105 }
106 
108 {
109  Pi_Hasher_t *hasher;
110 
111  hasher = malloc(sizeof(Pi_Hasher_t));
112 
113  hasher->gpio = gpio;
114  hasher->callback = callback;
115  hasher->timeout = 5;
116 
117  hasher->in_code = 0;
118 
119  gpioSetMode(gpio, PI_INPUT);
120  gpioSetAlertFuncEx(gpio, _cb, hasher);
121 
122  return hasher;
123 }
124 
126 {
127  if (hasher)
128  {
129  gpioSetAlertFunc(hasher->gpio, 0);
130 
131  free(hasher);
132 
133  hasher = NULL;
134  }
135 }
136 
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
Definition: pigpio.c:11303
void Pi_Hasher_cancel(Pi_Hasher_t *hasher)
Definition: ir_hasher.c:125
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
#define PI_INPUT
Definition: pigpio.h:581
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
Pi_Hasher_CB_t callback
Definition: ir_hasher.c:24
static uint32_t _hash(uint32_t hv, int old_val, int new_val)
Definition: ir_hasher.c:36
static void _cb(int gpio, int level, uint32_t tick, void *user)
Definition: ir_hasher.c:50
uint32_t hash_val
Definition: ir_hasher.c:27
void(* Pi_Hasher_CB_t)(uint32_t)
Definition: ir_hasher.h:6
#define PI_TIMEOUT
Definition: pigpio.h:577
Pi_Hasher_t * Pi_Hasher(int gpio, Pi_Hasher_CB_t callback, int timeout)
Definition: ir_hasher.c:107


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57