wiegand.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 
3 #include <pigpio.h>
4 
5 #include "wiegand.h"
6 
7 struct _Pi_Wieg_s
8 {
9  int mygpio_0;
10  int mygpio_1;
11  int mytimeout;
12  int in_code;
13  int bits;
15  uint32_t num;
16  uint32_t code_timeout;
17 };
18 
19 void _cb(int gpio, int level, uint32_t tick, void *user)
20 {
21  /*
22  Accumulate bits until both gpios 0 and 1 timeout.
23  */
24 
25  Pi_Wieg_t *wieg;
26 
27  wieg = user;
28 
29  if (level == 0) /* a falling edge indicates a new bit */
30  {
31  if (!wieg->in_code)
32  {
33  wieg->bits = 1;
34  wieg->num = 0;
35 
36  wieg->in_code = 1;
37  wieg->code_timeout = 0;
38 
39  gpioSetWatchdog(wieg->mygpio_0, wieg->mytimeout);
40  gpioSetWatchdog(wieg->mygpio_1, wieg->mytimeout);
41  }
42  else
43  {
44  wieg->bits++;
45  wieg->num <<= 1;
46  }
47 
48  if (gpio == wieg->mygpio_0)
49  {
50  wieg->code_timeout &= 2; /* clear gpio 0 timeout */
51  }
52  else
53  {
54  wieg->code_timeout &= 1; /* clear gpio 1 timeout */
55  wieg->num |= 1;
56  }
57  }
58  else if (level == PI_TIMEOUT)
59  {
60  if (wieg->in_code)
61  {
62  if (gpio == wieg->mygpio_0)
63  {
64  wieg->code_timeout |= 1; /* timeout gpio 0 */
65  }
66  else
67  {
68  wieg->code_timeout |= 2; /* timeout gpio 1 */
69  }
70 
71  if (wieg->code_timeout == 3) /* both gpios timed out */
72  {
73  gpioSetWatchdog(wieg->mygpio_0, 0);
74  gpioSetWatchdog(wieg->mygpio_1, 0);
75 
76  wieg->in_code = 0;
77 
78  (wieg->mycallback)(wieg->bits, wieg->num);
79  }
80  }
81  }
82 }
83 
85  int gpio_0,
86  int gpio_1,
88  int timeout)
89 {
90  /*
91  Instantiate with the gpio for 0 (green wire), the gpio for 1
92  (white wire), the callback function, and the timeout in
93  milliseconds which indicates the end of a code.
94 
95  The callback is passed the code length in bits and the value.
96  */
97 
98  Pi_Wieg_t *wieg;
99 
100  wieg = malloc(sizeof(Pi_Wieg_t));
101 
102  wieg->mygpio_0 = gpio_0;
103  wieg->mygpio_1 = gpio_1;
104 
105  wieg->mycallback = callback;
106 
107  wieg->mytimeout = timeout;
108 
109  wieg->in_code = 0;
110 
111  gpioSetMode(gpio_0, PI_INPUT);
112  gpioSetMode(gpio_1, PI_INPUT);
113 
114  gpioSetPullUpDown(gpio_0, PI_PUD_UP);
115  gpioSetPullUpDown(gpio_1, PI_PUD_UP);
116 
117  gpioSetAlertFuncEx(gpio_0, _cb, wieg);
118  gpioSetAlertFuncEx(gpio_1, _cb, wieg);
119 
120  return wieg;
121 }
122 
124 {
125  /*
126  Cancel the Wiegand decoder.
127  */
128 
129  if (wieg)
130  {
131  gpioSetAlertFunc(wieg->mygpio_0, 0);
132  gpioSetAlertFunc(wieg->mygpio_1, 0);
133 
134  free(wieg);
135  }
136 }
137 
void(* Pi_Wieg_CB_t)(int, uint32_t)
Definition: wiegand.h:6
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
Definition: pigpio.c:11303
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
void callback(uint32_t hash)
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
Pi_Wieg_t * Pi_Wieg(int gpio_0, int gpio_1, Pi_Wieg_CB_t callback, int timeout)
Definition: wiegand.c:84
Pi_Wieg_CB_t mycallback
Definition: wiegand.c:14
#define PI_PUD_UP
Definition: pigpio.h:594
int in_code
Definition: wiegand.c:12
#define PI_INPUT
Definition: pigpio.h:581
int bits
Definition: wiegand.c:13
int mygpio_1
Definition: wiegand.c:10
void _cb(int gpio, int level, uint32_t tick, void *user)
Definition: wiegand.c:19
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
uint32_t code_timeout
Definition: wiegand.c:16
int mytimeout
Definition: wiegand.c:11
void Pi_Wieg_cancel(Pi_Wieg_t *wieg)
Definition: wiegand.c:123
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition: pigpio.c:8661
int mygpio_0
Definition: wiegand.c:9
uint32_t num
Definition: wiegand.c:15
#define PI_TIMEOUT
Definition: pigpio.h:577


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