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


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