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 
PI_INPUT
#define PI_INPUT
Definition: pigpio.h:581
Wiegand::_cbEx
static void _cbEx(int gpio, int level, uint32_t tick, void *user)
Definition: wiegand.cpp:95
gpioSetPullUpDown
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition: pigpio.c:8661
Wiegand::mycallback
WiegandCB_t mycallback
Definition: wiegand.hpp:12
Wiegand::mytimeout
int mytimeout
Definition: wiegand.hpp:10
Wiegand::mygpio_1
int mygpio_1
Definition: wiegand.hpp:10
Wiegand::code_timeout
uint32_t code_timeout
Definition: wiegand.hpp:16
Wiegand::in_code
int in_code
Definition: wiegand.hpp:10
Wiegand
Definition: wiegand.hpp:8
callback
void callback(uint32_t hash)
Definition: test_ir_hasher.c:23
gpioSetWatchdog
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
gpioSetMode
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
pigpio.h
PI_TIMEOUT
#define PI_TIMEOUT
Definition: pigpio.h:577
Wiegand::mygpio_0
int mygpio_0
Definition: wiegand.hpp:10
gpioSetAlertFuncEx
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
Wiegand::bits
int bits
Definition: wiegand.hpp:10
PI_PUD_UP
#define PI_PUD_UP
Definition: pigpio.h:594
wiegand.hpp
Wiegand::cancel
void cancel(void)
Definition: wiegand.cpp:107
WiegandCB_t
void(* WiegandCB_t)(int, uint32_t)
Definition: wiegand.hpp:6
Wiegand::_cb
void _cb(int gpio, int level, uint32_t tick)
Definition: wiegand.cpp:34
Wiegand::Wiegand
Wiegand(int gpio_0, int gpio_1, WiegandCB_t callback, int timeout=5)
Definition: wiegand.cpp:5
Wiegand::num
uint32_t num
Definition: wiegand.hpp:14


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Fri Aug 2 2024 09:40:57