ir_hasher.cpp
Go to the documentation of this file.
1 #include <pigpio.h>
2 
3 #include "ir_hasher.hpp"
4 
5 void Hasher::_hash(int old_val, int new_val)
6 {
7  int val;
8 
9  if (new_val < (old_val * 0.60)) val = 13;
10  else if (old_val < (new_val * 0.60)) val = 23;
11  else val = 2;
12 
13  hash_val ^= val;
14  hash_val *= 16777619; /* FNV_PRIME_32 */
15 }
16 
17 void Hasher::_callback(int gpio, int level, uint32_t tick)
18 {
19  if (level != PI_TIMEOUT)
20  {
21  if (in_code == 0)
22  {
23  in_code = 1;
24 
26 
27  hash_val = 2166136261U; /* FNV_BASIS_32 */
28 
29  edges = 1;
30 
31  t1 = 0;
32  t2 = 0;
33  t3 = 0;
34  t4 = tick;
35  }
36  else
37  {
38  edges++;
39 
40  t1 = t2;
41  t2 = t3;
42  t3 = t4;
43  t4 = tick;
44 
45  if (edges > 3) _hash(t2-t1, t4-t3);
46  }
47  }
48  else
49  {
50  if (in_code)
51  {
52  in_code = 0;
53 
55 
56  if (edges > 12) /* Anything less is probably noise. */
57  {
59  }
60  }
61  }
62 }
63 
64 void Hasher::_callbackExt(int gpio, int level, uint32_t tick, void *user)
65 {
66  /*
67  Need a static callback to link with C.
68  */
69 
70  Hasher *mySelf = (Hasher *) user;
71 
72  mySelf->_callback(gpio, level, tick); /* Call the instance callback. */
73 }
74 
75 Hasher::Hasher(int gpio, HasherCB_t callback, int timeout)
76 {
77  /*
78  Initialises an IR remote hasher on a gpio. A gap of timeout
79  milliseconds indicates the end of the remote key press.
80  */
81  mygpio = gpio;
83  mytimeout = timeout;
84 
85  in_code = 0;
86 
87  gpioSetMode(gpio, PI_INPUT);
88 
89  gpioSetAlertFuncEx(gpio, _callbackExt, (void *)this);
90 }
91 
uint32_t t2
Definition: ir_hasher.hpp:30
uint32_t t4
Definition: ir_hasher.hpp:30
uint32_t hash_val
Definition: ir_hasher.hpp:28
HasherCB_t mycallback
Definition: ir_hasher.hpp:26
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
Hasher(int gpio, HasherCB_t callback, int timeout=5)
Definition: ir_hasher.cpp:75
void callback(uint32_t hash)
int in_code
Definition: ir_hasher.hpp:27
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
static void _callbackExt(int gpio, int level, uint32_t tick, void *user)
Definition: ir_hasher.cpp:64
void _hash(int old_val, int new_val)
Definition: ir_hasher.cpp:5
uint32_t t1
Definition: ir_hasher.hpp:30
#define PI_INPUT
Definition: pigpio.h:581
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
void _callback(int gpio, int level, uint32_t tick)
Definition: ir_hasher.cpp:17
uint32_t t3
Definition: ir_hasher.hpp:30
int edges
Definition: ir_hasher.hpp:29
int mytimeout
Definition: ir_hasher.hpp:25
int mygpio
Definition: ir_hasher.hpp:25
void(* HasherCB_t)(uint32_t)
Definition: ir_hasher.hpp:6
#define PI_TIMEOUT
Definition: pigpio.h:577


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