Go to the documentation of this file.00001
00002
00003 import pigpio
00004
00005 class decoder:
00006
00007 """
00008 A class to read Wiegand codes of an arbitrary length.
00009
00010 The code length and value are returned.
00011
00012 EXAMPLE
00013
00014 #!/usr/bin/env python
00015
00016 import time
00017
00018 import pigpio
00019
00020 import wiegand
00021
00022 def callback(bits, code):
00023 print("bits={} code={}".format(bits, code))
00024
00025 pi = pigpio.pi()
00026
00027 w = wiegand.decoder(pi, 14, 15, callback)
00028
00029 time.sleep(300)
00030
00031 w.cancel()
00032
00033 pi.stop()
00034 """
00035
00036 def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=5):
00037
00038 """
00039 Instantiate with the pi, gpio for 0 (green wire), the gpio for 1
00040 (white wire), the callback function, and the bit timeout in
00041 milliseconds which indicates the end of a code.
00042
00043 The callback is passed the code length in bits and the value.
00044 """
00045
00046 self.pi = pi
00047 self.gpio_0 = gpio_0
00048 self.gpio_1 = gpio_1
00049
00050 self.callback = callback
00051
00052 self.bit_timeout = bit_timeout
00053
00054 self.in_code = False
00055
00056 self.pi.set_mode(gpio_0, pigpio.INPUT)
00057 self.pi.set_mode(gpio_1, pigpio.INPUT)
00058
00059 self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP)
00060 self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP)
00061
00062 self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
00063 self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)
00064
00065 def _cb(self, gpio, level, tick):
00066
00067 """
00068 Accumulate bits until both gpios 0 and 1 timeout.
00069 """
00070
00071 if level < pigpio.TIMEOUT:
00072
00073 if self.in_code == False:
00074 self.bits = 1
00075 self.num = 0
00076
00077 self.in_code = True
00078 self.code_timeout = 0
00079 self.pi.set_watchdog(self.gpio_0, self.bit_timeout)
00080 self.pi.set_watchdog(self.gpio_1, self.bit_timeout)
00081 else:
00082 self.bits += 1
00083 self.num = self.num << 1
00084
00085 if gpio == self.gpio_0:
00086 self.code_timeout = self.code_timeout & 2
00087 else:
00088 self.code_timeout = self.code_timeout & 1
00089 self.num = self.num | 1
00090
00091 else:
00092
00093 if self.in_code:
00094
00095 if gpio == self.gpio_0:
00096 self.code_timeout = self.code_timeout | 1
00097 else:
00098 self.code_timeout = self.code_timeout | 2
00099
00100 if self.code_timeout == 3:
00101 self.pi.set_watchdog(self.gpio_0, 0)
00102 self.pi.set_watchdog(self.gpio_1, 0)
00103 self.in_code = False
00104 self.callback(self.bits, self.num)
00105
00106 def cancel(self):
00107
00108 """
00109 Cancel the Wiegand decoder.
00110 """
00111
00112 self.cb_0.cancel()
00113 self.cb_1.cancel()
00114
00115 if __name__ == "__main__":
00116
00117 import time
00118
00119 import pigpio
00120
00121 import wiegand
00122
00123 def callback(bits, value):
00124 print("bits={} value={}".format(bits, value))
00125
00126 pi = pigpio.pi()
00127
00128 w = wiegand.decoder(pi, 14, 15, callback)
00129
00130 time.sleep(300)
00131
00132 w.cancel()
00133
00134 pi.stop()
00135