Skip to content

Commit

Permalink
added v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed May 16, 2024
1 parent d9f7ab4 commit d2b65a9
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pypi-release.yml
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
40 changes: 39 additions & 1 deletion README.md
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).
21 changes: 21 additions & 0 deletions pyproject.toml
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"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pigpio
15 changes: 15 additions & 0 deletions src/example.py
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
83 changes: 83 additions & 0 deletions src/pywiegandpi/__init__.py
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()

0 comments on commit d2b65a9

Please sign in to comment.