ir_hasher.cpp
Go to the documentation of this file.
00001 #include <pigpio.h>
00002 
00003 #include "ir_hasher.hpp"
00004 
00005 void Hasher::_hash(int old_val, int new_val)
00006 {
00007    int val;
00008 
00009    if      (new_val < (old_val * 0.60)) val = 13;
00010    else if (old_val < (new_val * 0.60)) val = 23;
00011    else                                 val = 2;
00012 
00013    hash_val ^= val;
00014    hash_val *= 16777619; /* FNV_PRIME_32 */
00015 }
00016 
00017 void Hasher::_callback(int gpio, int level, uint32_t tick)
00018 {
00019    if (level != PI_TIMEOUT)
00020    {
00021       if (in_code == 0)
00022       {
00023          in_code = 1;
00024 
00025          gpioSetWatchdog(mygpio, mytimeout);
00026 
00027          hash_val = 2166136261U; /* FNV_BASIS_32 */
00028 
00029          edges = 1;
00030 
00031          t1 = 0;
00032          t2 = 0;
00033          t3 = 0;
00034          t4 = tick;
00035       }
00036       else
00037       {
00038          edges++;
00039 
00040          t1 = t2;
00041          t2 = t3;
00042          t3 = t4;
00043          t4 = tick;
00044 
00045          if (edges > 3) _hash(t2-t1, t4-t3);
00046       }
00047    }
00048    else
00049    {
00050       if (in_code)
00051       {
00052          in_code = 0;
00053 
00054          gpioSetWatchdog(mygpio, 0);
00055 
00056          if (edges > 12) /* Anything less is probably noise. */
00057          {
00058             (mycallback)(hash_val);
00059          }
00060       }
00061    }
00062 }
00063 
00064 void Hasher::_callbackExt(int gpio, int level, uint32_t tick, void *user)
00065 {
00066    /*
00067       Need a static callback to link with C.
00068    */
00069 
00070    Hasher *mySelf = (Hasher *) user;
00071 
00072    mySelf->_callback(gpio, level, tick); /* Call the instance callback. */
00073 }
00074 
00075 Hasher::Hasher(int gpio, HasherCB_t callback, int timeout)
00076 {
00077    /*
00078       Initialises an IR remote hasher on a gpio.  A gap of timeout
00079       milliseconds indicates the end of the remote key press.
00080    */
00081    mygpio     = gpio;
00082    mycallback = callback;
00083    mytimeout  = timeout;
00084 
00085    in_code = 0;
00086 
00087    gpioSetMode(gpio, PI_INPUT);
00088 
00089    gpioSetAlertFuncEx(gpio, _callbackExt, (void *)this);
00090 }
00091 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57