wiegand.cpp
Go to the documentation of this file.
00001 #include <pigpio.h>
00002 
00003 #include "wiegand.hpp"
00004 
00005 Wiegand::Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout)
00006 {
00007    /*
00008       Instantiate with the gpio for 0 (green wire), the gpio for 1
00009       (white wire), the callback function, and the bit timeout in
00010       milliseconds which indicates the end of a code.
00011 
00012       The callback is passed the code length in bits and the value.
00013    */
00014 
00015    mygpio_0 = gpio_0;
00016    mygpio_1 = gpio_1;
00017 
00018    mycallback = callback;
00019 
00020    mytimeout = timeout;
00021 
00022    in_code = 0;
00023 
00024    gpioSetMode(gpio_0, PI_INPUT);
00025    gpioSetMode(gpio_1, PI_INPUT);
00026 
00027    gpioSetPullUpDown(gpio_0, PI_PUD_UP);
00028    gpioSetPullUpDown(gpio_1, PI_PUD_UP);
00029 
00030    gpioSetAlertFuncEx(gpio_0, _cbEx, this);
00031    gpioSetAlertFuncEx(gpio_1, _cbEx, this);
00032 }
00033 
00034 void Wiegand::_cb(int gpio, int level, uint32_t tick)
00035 {
00036    /*
00037       Accumulate bits until both gpios 0 and 1 timeout.
00038    */
00039 
00040    if (level == 0) /* a falling edge indicates a new bit */
00041    {
00042       if (!in_code)
00043       {
00044          bits = 1;
00045          num = 0;
00046 
00047          in_code = 1;
00048          code_timeout = 0;
00049 
00050          gpioSetWatchdog(mygpio_0, mytimeout);
00051          gpioSetWatchdog(mygpio_1, mytimeout);
00052       }
00053       else
00054       {
00055          bits++;
00056          num <<= 1;
00057       }
00058 
00059       if (gpio == mygpio_0)
00060       {
00061          code_timeout &= 2; /* clear gpio 0 timeout */
00062       }
00063       else
00064       {
00065          code_timeout &= 1; /* clear gpio 1 timeout */
00066          num |= 1;
00067       }
00068    }
00069    else if (level == PI_TIMEOUT)
00070    {
00071       if (in_code)
00072       {
00073          if (gpio == mygpio_0)
00074          {
00075             code_timeout |= 1; /* timeout gpio 0 */
00076          }
00077          else
00078          {
00079             code_timeout |= 2; /* timeout gpio 1 */
00080          }
00081 
00082          if (code_timeout == 3) /* both gpios timed out */
00083          {
00084             gpioSetWatchdog(mygpio_0, 0);
00085             gpioSetWatchdog(mygpio_1, 0);
00086 
00087             in_code = 0;
00088 
00089             (mycallback)(bits, num);
00090          }
00091       }
00092    }
00093 }
00094 
00095 void Wiegand::_cbEx(int gpio, int level, uint32_t tick, void *user)
00096 {
00097    /*
00098       Need a static callback to link with C.
00099    */
00100 
00101    Wiegand *mySelf = (Wiegand *) user;
00102 
00103    mySelf->_cb(gpio, level, tick); /* Call the instance callback. */
00104 }
00105 
00106 
00107 void Wiegand::cancel(void)
00108 {
00109    /*
00110       Cancel the Wiegand decoder.
00111    */
00112 
00113    gpioSetAlertFuncEx(mygpio_0, 0, this);
00114    gpioSetAlertFuncEx(mygpio_1, 0, this);
00115 }
00116 


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