Commit 2ddac9e8 authored by Jakob Moser's avatar Jakob Moser
Browse files

Merge branch 'backend/nfc/mfrc522-library' into 'master'

Add (likely broken) mfrc522 library to handle NFC

See merge request moser/poolpay!9
parents 78b59620 a26923bc
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
from collections.abc import Callable, Sequence

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

from poolpay.card.CardReader import CardReader


def split_to_bytes(value: int) -> Sequence[int]:
    """
    Take an int and split it into a big endian representation of individual bytes.

    >>> split = split_to_bytes(0xcafebabe)
    >>> [hex(i) for i in split]
    [0xca, 0xfe, 0xba, 0xbe]
    """
    split = []

    while value:
        # Get the byte at the smallest position
        smallest_byte = value & 0xFF
        split.append(smallest_byte)

        # Shift the value one byte to the right
        value = value >> 8

    # We need to reverse the list to achieve big endianness
    return split[::-1]


class Mfrc522CardReader(CardReader):
    def __init__(self) -> None:
        self._reader = SimpleMFRC522()

    def read_id(self) -> Sequence[int]:
        card_id, content = self._reader.read()

        return split_to_bytes(card_id)

    def on_card_presented(self, handle_card: Callable[[CardReader], None]) -> None:
        raise NotImplementedError()

    def close(self) -> None:
        # TODO Maybe integrate this better
        GPIO.cleanup()
+1 −0
Original line number Diff line number Diff line
babel~=2.17.0
mfrc522~=0.0.7
SQLAlchemy~=2.0.38