first re-commit.
This commit is contained in:
4
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/README
Normal file
4
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/README
Normal file
@@ -0,0 +1,4 @@
|
||||
Class to hash a code from an IR receiver (reading an IR remote control).
|
||||
|
||||
Follow the instructions in the test file to build and run.
|
||||
|
91
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/ir_hasher.cpp
Normal file
91
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/ir_hasher.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "ir_hasher.hpp"
|
||||
|
||||
void Hasher::_hash(int old_val, int new_val)
|
||||
{
|
||||
int val;
|
||||
|
||||
if (new_val < (old_val * 0.60)) val = 13;
|
||||
else if (old_val < (new_val * 0.60)) val = 23;
|
||||
else val = 2;
|
||||
|
||||
hash_val ^= val;
|
||||
hash_val *= 16777619; /* FNV_PRIME_32 */
|
||||
}
|
||||
|
||||
void Hasher::_callback(int gpio, int level, uint32_t tick)
|
||||
{
|
||||
if (level != PI_TIMEOUT)
|
||||
{
|
||||
if (in_code == 0)
|
||||
{
|
||||
in_code = 1;
|
||||
|
||||
gpioSetWatchdog(mygpio, mytimeout);
|
||||
|
||||
hash_val = 2166136261U; /* FNV_BASIS_32 */
|
||||
|
||||
edges = 1;
|
||||
|
||||
t1 = 0;
|
||||
t2 = 0;
|
||||
t3 = 0;
|
||||
t4 = tick;
|
||||
}
|
||||
else
|
||||
{
|
||||
edges++;
|
||||
|
||||
t1 = t2;
|
||||
t2 = t3;
|
||||
t3 = t4;
|
||||
t4 = tick;
|
||||
|
||||
if (edges > 3) _hash(t2-t1, t4-t3);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (in_code)
|
||||
{
|
||||
in_code = 0;
|
||||
|
||||
gpioSetWatchdog(mygpio, 0);
|
||||
|
||||
if (edges > 12) /* Anything less is probably noise. */
|
||||
{
|
||||
(mycallback)(hash_val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Hasher::_callbackExt(int gpio, int level, uint32_t tick, void *user)
|
||||
{
|
||||
/*
|
||||
Need a static callback to link with C.
|
||||
*/
|
||||
|
||||
Hasher *mySelf = (Hasher *) user;
|
||||
|
||||
mySelf->_callback(gpio, level, tick); /* Call the instance callback. */
|
||||
}
|
||||
|
||||
Hasher::Hasher(int gpio, HasherCB_t callback, int timeout)
|
||||
{
|
||||
/*
|
||||
Initialises an IR remote hasher on a gpio. A gap of timeout
|
||||
milliseconds indicates the end of the remote key press.
|
||||
*/
|
||||
mygpio = gpio;
|
||||
mycallback = callback;
|
||||
mytimeout = timeout;
|
||||
|
||||
in_code = 0;
|
||||
|
||||
gpioSetMode(gpio, PI_INPUT);
|
||||
|
||||
gpioSetAlertFuncEx(gpio, _callbackExt, (void *)this);
|
||||
}
|
||||
|
44
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/ir_hasher.hpp
Normal file
44
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/ir_hasher.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef IR_RX_HASHER_HPP
|
||||
#define IR_RX_HASHER_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*HasherCB_t)(uint32_t);
|
||||
|
||||
class Hasher
|
||||
{
|
||||
|
||||
/*
|
||||
This class forms a hash over the IR pulses generated by an
|
||||
IR remote.
|
||||
|
||||
The remote key press is not converted into a code in the manner of
|
||||
the lirc module. No attempt is made to decode the type of protocol
|
||||
used by the remote. The hash is likely to be unique for different
|
||||
keys and different remotes but this is not guaranteed.
|
||||
|
||||
This hashing process works for some remotes/protocols but not for
|
||||
others. The only way to find out if it works for one or more of
|
||||
your remotes is to try it and see.
|
||||
*/
|
||||
|
||||
int mygpio, mytimeout;
|
||||
HasherCB_t mycallback;
|
||||
int in_code;
|
||||
uint32_t hash_val;
|
||||
int edges;
|
||||
uint32_t t1, t2, t3, t4;
|
||||
|
||||
void _hash(int old_val, int new_val);
|
||||
void _callback(int gpio, int level, uint32_t tick);
|
||||
|
||||
/* Need a static callback to link with C. */
|
||||
static void _callbackExt(int gpio, int level, uint32_t tick, void *user);
|
||||
|
||||
public:
|
||||
|
||||
Hasher(int gpio, HasherCB_t callback, int timeout=5);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
44
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/test_ir_hasher.cpp
Normal file
44
pigpio-master/EXAMPLES/CPP/IR_RECEIVER/test_ir_hasher.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pigpio.h>
|
||||
|
||||
#include "ir_hasher.hpp"
|
||||
|
||||
/*
|
||||
|
||||
REQUIRES
|
||||
|
||||
An IR receiver output pin connected to a Pi gpio.
|
||||
|
||||
TO BUILD
|
||||
|
||||
g++ -o ir_hash_cpp test_ir_hasher.cpp ir_hasher.cpp -lpigpio -lrt -lpthread
|
||||
|
||||
TO RUN
|
||||
|
||||
sudo ./ir_hash_cpp
|
||||
|
||||
*/
|
||||
|
||||
void callback(uint32_t hash)
|
||||
{
|
||||
std::cout << "hash=" << hash << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (gpioInitialise() >= 0)
|
||||
{
|
||||
/* Can't instantiate a Hasher before pigpio is initialised. */
|
||||
|
||||
/*
|
||||
This assumes the output pin of an IR receiver is
|
||||
connected to gpio 7.
|
||||
*/
|
||||
|
||||
Hasher ir(7, callback);
|
||||
|
||||
sleep(300);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user