wiegand.cpp
Go to the documentation of this file.
1 #include <pigpio.h>
2 
3 #include "wiegand.hpp"
4 
5 Wiegand::Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout)
6 {
7  /*
8  Instantiate with the gpio for 0 (green wire), the gpio for 1
9  (white wire), the callback function, and the bit timeout in
10  milliseconds which indicates the end of a code.
11 
12  The callback is passed the code length in bits and the value.
13  */
14 
15  mygpio_0 = gpio_0;
16  mygpio_1 = gpio_1;
17 
19 
20  mytimeout = timeout;
21 
22  in_code = 0;
23 
24  gpioSetMode(gpio_0, PI_INPUT);
25  gpioSetMode(gpio_1, PI_INPUT);
26 
29 
30  gpioSetAlertFuncEx(gpio_0, _cbEx, this);
31  gpioSetAlertFuncEx(gpio_1, _cbEx, this);
32 }
33 
34 void Wiegand::_cb(int gpio, int level, uint32_t tick)
35 {
36  /*
37  Accumulate bits until both gpios 0 and 1 timeout.
38  */
39 
40  if (level == 0) /* a falling edge indicates a new bit */
41  {
42  if (!in_code)
43  {
44  bits = 1;
45  num = 0;
46 
47  in_code = 1;
48  code_timeout = 0;
49 
52  }
53  else
54  {
55  bits++;
56  num <<= 1;
57  }
58 
59  if (gpio == mygpio_0)
60  {
61  code_timeout &= 2; /* clear gpio 0 timeout */
62  }
63  else
64  {
65  code_timeout &= 1; /* clear gpio 1 timeout */
66  num |= 1;
67  }
68  }
69  else if (level == PI_TIMEOUT)
70  {
71  if (in_code)
72  {
73  if (gpio == mygpio_0)
74  {
75  code_timeout |= 1; /* timeout gpio 0 */
76  }
77  else
78  {
79  code_timeout |= 2; /* timeout gpio 1 */
80  }
81 
82  if (code_timeout == 3) /* both gpios timed out */
83  {
86 
87  in_code = 0;
88 
89  (mycallback)(bits, num);
90  }
91  }
92  }
93 }
94 
95 void Wiegand::_cbEx(int gpio, int level, uint32_t tick, void *user)
96 {
97  /*
98  Need a static callback to link with C.
99  */
100 
101  Wiegand *mySelf = (Wiegand *) user;
102 
103  mySelf->_cb(gpio, level, tick); /* Call the instance callback. */
104 }
105 
106 
107 void Wiegand::cancel(void)
108 {
109  /*
110  Cancel the Wiegand decoder.
111  */
112 
113  gpioSetAlertFuncEx(mygpio_0, 0, this);
114  gpioSetAlertFuncEx(mygpio_1, 0, this);
115 }
116 
int mytimeout
Definition: wiegand.hpp:10
void cancel(void)
Definition: wiegand.cpp:107
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
void callback(uint32_t hash)
int mygpio_1
Definition: wiegand.hpp:10
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
#define PI_PUD_UP
Definition: pigpio.h:594
#define PI_INPUT
Definition: pigpio.h:581
WiegandCB_t mycallback
Definition: wiegand.hpp:12
uint32_t code_timeout
Definition: wiegand.hpp:16
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
uint32_t num
Definition: wiegand.hpp:14
Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout=5)
Definition: wiegand.cpp:5
int mygpio_0
Definition: wiegand.hpp:10
void(* WiegandCB_t)(int, uint32_t)
Definition: wiegand.hpp:6
int bits
Definition: wiegand.hpp:10
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition: pigpio.c:8661
int in_code
Definition: wiegand.hpp:10
static void _cbEx(int gpio, int level, uint32_t tick, void *user)
Definition: wiegand.cpp:95
void _cb(int gpio, int level, uint32_t tick)
Definition: wiegand.cpp:34
#define PI_TIMEOUT
Definition: pigpio.h:577


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57