-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: PyPi Release | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- "main" | ||
|
||
jobs: | ||
pypi-publish: | ||
name: Upload release to PyPI | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: pypi | ||
url: https://pypi.org/p/pywiegandpi | ||
permissions: | ||
id-token: write | ||
steps: | ||
- name: Install dependencies | ||
run: pip install build | ||
- name: Build package | ||
run: python -m build | ||
- name: Publish package distributions to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,40 @@ | ||
# pywiegandpi | ||
Read Wiegand data from Raspberry Pi GPIO pins. | ||
Read Wiegand data from Raspberry Pi GPIO pins with a simple callback structure and automatic decoding. | ||
|
||
## Getting Started | ||
Install pigpio if it's not already installed: | ||
```bash | ||
sudo apt install pigpio | ||
``` | ||
Enable the pigpio daemon: | ||
```bash | ||
sudo systemctl enable pigpiod | ||
``` | ||
|
||
Install the required python packages by running the following command: | ||
```bash | ||
pip3 install -r requirements.txt | ||
``` | ||
|
||
Use it like so: | ||
```python | ||
from pywiegandpi import WiegandDecoder | ||
|
||
data_0_pin = 6 | ||
data_1_pin = 5 | ||
|
||
|
||
def callback(value): | ||
print("Got Wiegand data: {}".format(value)) | ||
|
||
|
||
wiegand_reader = WiegandDecoder(data_0_pin, data_1_pin, callback) | ||
|
||
while True: | ||
# do something else | ||
pass | ||
|
||
``` | ||
|
||
## Acknowledgements | ||
This library is based on the original example from [joan2937](https://github.com/joan2937/pigpio/tree/master/EXAMPLES/Python/WIEGAND_CODE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[project] | ||
name = "pywiegandpi" | ||
version = "1.0.0" | ||
dependencies = [ | ||
"pigpio >= 1.78", | ||
] | ||
authors = [ | ||
{ name="Jaimyn Mayer", email="hello@jaimyn.dev" }, | ||
] | ||
description = "Automatically decode Wiegand data from Raspberry Pi GPIO pins." | ||
readme = "README.md" | ||
requires-python = ">=3.9" | ||
classifiers = [ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: POSIX :: Linux", | ||
] | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/membermatters/pywiegandpi" | ||
Issues = "https://github.com/membermatters/pywiegandpi/issues" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pigpio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pywiegandpi import WiegandDecoder | ||
|
||
data_0_pin = 6 | ||
data_1_pin = 5 | ||
|
||
|
||
def callback(value): | ||
print("Got Wiegand data: {}".format(value)) | ||
|
||
|
||
wiegand_reader = WiegandDecoder(data_0_pin, data_1_pin, callback) | ||
|
||
while True: | ||
# do something else | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import pigpio | ||
|
||
__version__ = "1.0.0" | ||
|
||
class WiegandDecoder: | ||
def __init__(self, gpio_0, gpio_1, callback, bit_timeout=5, wiegand_32bit_mode=False, raw_mode=False): | ||
""" | ||
The callback is passed the code length in bits and the card uid/value. | ||
""" | ||
|
||
self.pi = pigpio.pi() | ||
self.gpio_0 = gpio_0 | ||
self.gpio_1 = gpio_1 | ||
self.raw_mode = raw_mode | ||
self.wiegand_32bit_mode = wiegand_32bit_mode | ||
|
||
self.callback = callback | ||
self.bit_timeout = bit_timeout | ||
self.receiving_bits = False | ||
|
||
self.pi.set_mode(gpio_0, pigpio.INPUT) | ||
self.pi.set_mode(gpio_1, pigpio.INPUT) | ||
self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP) | ||
self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP) | ||
|
||
self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._pin_change_callback) | ||
self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._pin_change_callback) | ||
|
||
def _pin_change_callback(self, gpio, trigger, tick): | ||
""" | ||
Accumulate bits until both GPIO 0 and 1 timeout. | ||
""" | ||
|
||
# if it was a falling or rising edge then accumulate the bit (this cb is also called on timeout) | ||
if trigger in [pigpio.FALLING_EDGE, pigpio.RISING_EDGE]: | ||
if not self.receiving_bits: | ||
self.bits = 1 | ||
self.num = 0 | ||
|
||
self.receiving_bits = True | ||
self.receiving_bits_timeout = 0 | ||
self.pi.set_watchdog(self.gpio_0, self.bit_timeout) | ||
self.pi.set_watchdog(self.gpio_1, self.bit_timeout) | ||
else: | ||
self.bits += 1 | ||
self.num = self.num << 1 | ||
|
||
if gpio == self.gpio_0: | ||
self.receiving_bits_timeout = self.receiving_bits_timeout & 2 # clear gpio 0 timeout | ||
else: | ||
self.receiving_bits_timeout = self.receiving_bits_timeout & 1 # clear gpio 1 timeout | ||
self.num = self.num | 1 | ||
|
||
else: | ||
# if we aren't receiving bits, just continue | ||
if self.receiving_bits: | ||
if gpio == self.gpio_0: | ||
self.receiving_bits_timeout = self.receiving_bits_timeout | 1 # timeout gpio 0 | ||
else: | ||
self.receiving_bits_timeout = self.receiving_bits_timeout | 2 # timeout gpio 1 | ||
|
||
if self.receiving_bits_timeout == 3: # both GPIO timed out | ||
self.pi.set_watchdog(self.gpio_0, 0) | ||
self.pi.set_watchdog(self.gpio_1, 0) | ||
self.receiving_bits = False | ||
|
||
if self.raw_mode: | ||
self.callback(self.bits, self.num) | ||
else: | ||
# get only the bits we're interested in | ||
card_mask_26bit = 0b0_00000000_11111111_11111111_11111111_0 | ||
card_mask_34bit = 0b0_11111111_11111111_11111111_11111111_0 | ||
card_mask = card_mask_34bit if self.wiegand_32bit_mode else card_mask_26bit | ||
card_uid = self.num & card_mask >> 1 # strip off the remaining parity bit | ||
self.callback(self.bits, card_uid) | ||
|
||
def cancel(self): | ||
""" | ||
Cancel the Wiegand decoder. | ||
""" | ||
|
||
self.cb_0.cancel() | ||
self.cb_1.cancel() |