wiegand.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import pigpio
4 
5 class decoder:
6 
7  """
8  A class to read Wiegand codes of an arbitrary length.
9 
10  The code length and value are returned.
11 
12  EXAMPLE
13 
14  #!/usr/bin/env python
15 
16  import time
17 
18  import pigpio
19 
20  import wiegand
21 
22  def callback(bits, code):
23  print("bits={} code={}".format(bits, code))
24 
25  pi = pigpio.pi()
26 
27  w = wiegand.decoder(pi, 14, 15, callback)
28 
29  time.sleep(300)
30 
31  w.cancel()
32 
33  pi.stop()
34  """
35 
36  def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=5):
37 
38  """
39  Instantiate with the pi, gpio for 0 (green wire), the gpio for 1
40  (white wire), the callback function, and the bit timeout in
41  milliseconds which indicates the end of a code.
42 
43  The callback is passed the code length in bits and the value.
44  """
45 
46  self.pi = pi
47  self.gpio_0 = gpio_0
48  self.gpio_1 = gpio_1
49 
50  self.callback = callback
51 
52  self.bit_timeout = bit_timeout
53 
54  self.in_code = False
55 
56  self.pi.set_mode(gpio_0, pigpio.INPUT)
57  self.pi.set_mode(gpio_1, pigpio.INPUT)
58 
59  self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP)
60  self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP)
61 
62  self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
63  self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)
64 
65  def _cb(self, gpio, level, tick):
66 
67  """
68  Accumulate bits until both gpios 0 and 1 timeout.
69  """
70 
71  if level < pigpio.TIMEOUT:
72 
73  if self.in_code == False:
74  self.bits = 1
75  self.num = 0
76 
77  self.in_code = True
78  self.code_timeout = 0
79  self.pi.set_watchdog(self.gpio_0, self.bit_timeout)
80  self.pi.set_watchdog(self.gpio_1, self.bit_timeout)
81  else:
82  self.bits += 1
83  self.num = self.num << 1
84 
85  if gpio == self.gpio_0:
86  self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
87  else:
88  self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
89  self.num = self.num | 1
90 
91  else:
92 
93  if self.in_code:
94 
95  if gpio == self.gpio_0:
96  self.code_timeout = self.code_timeout | 1 # timeout gpio 0
97  else:
98  self.code_timeout = self.code_timeout | 2 # timeout gpio 1
99 
100  if self.code_timeout == 3: # both gpios timed out
101  self.pi.set_watchdog(self.gpio_0, 0)
102  self.pi.set_watchdog(self.gpio_1, 0)
103  self.in_code = False
104  self.callback(self.bits, self.num)
105 
106  def cancel(self):
107 
108  """
109  Cancel the Wiegand decoder.
110  """
111 
112  self.cb_0.cancel()
113  self.cb_1.cancel()
114 
115 if __name__ == "__main__":
116 
117  import time
118 
119  import pigpio
120 
121  import wiegand
122 
123  def callback(bits, value):
124  print("bits={} value={}".format(bits, value))
125 
126  pi = pigpio.pi()
127 
128  w = wiegand.decoder(pi, 14, 15, callback)
129 
130  time.sleep(300)
131 
132  w.cancel()
133 
134  pi.stop()
135 
def _cb(self, gpio, level, tick)
Definition: wiegand.py:65
def callback(bits, value)
Definition: wiegand.py:123
def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=5)
Definition: wiegand.py:36
def cancel(self)
Definition: wiegand.py:106


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