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 
PI_INPUT
#define PI_INPUT
Definition: pigpio.h:581
Hasher::_callback
void _callback(int gpio, int level, uint32_t tick)
Definition: ir_hasher.cpp:17
Hasher::t3
uint32_t t3
Definition: ir_hasher.hpp:30
Hasher::edges
int edges
Definition: ir_hasher.hpp:29
ir_hasher.hpp
Hasher::mytimeout
int mytimeout
Definition: ir_hasher.hpp:25
Hasher::mygpio
int mygpio
Definition: ir_hasher.hpp:25
Hasher::mycallback
HasherCB_t mycallback
Definition: ir_hasher.hpp:26
callback
void callback(uint32_t hash)
Definition: test_ir_hasher.c:23
Hasher::t4
uint32_t t4
Definition: ir_hasher.hpp:30
gpioSetWatchdog
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
gpioSetMode
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
Hasher::t2
uint32_t t2
Definition: ir_hasher.hpp:30
pigpio.h
PI_TIMEOUT
#define PI_TIMEOUT
Definition: pigpio.h:577
Hasher::Hasher
Hasher(int gpio, HasherCB_t callback, int timeout=5)
Definition: ir_hasher.cpp:75
gpioSetAlertFuncEx
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
Hasher
Definition: ir_hasher.hpp:8
Hasher::in_code
int in_code
Definition: ir_hasher.hpp:27
Hasher::_callbackExt
static void _callbackExt(int gpio, int level, uint32_t tick, void *user)
Definition: ir_hasher.cpp:64
Hasher::hash_val
uint32_t hash_val
Definition: ir_hasher.hpp:28
Hasher::_hash
void _hash(int old_val, int new_val)
Definition: ir_hasher.cpp:5
HasherCB_t
void(* HasherCB_t)(uint32_t)
Definition: ir_hasher.hpp:6
Hasher::t1
uint32_t t1
Definition: ir_hasher.hpp:30


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Fri Aug 2 2024 09:40:56