Verified Commit 0f413113 authored by Jakob Moser's avatar Jakob Moser
Browse files

Distinguish between presenting a scene and placing a card

parent c83c7369
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ def launch_application(password: str) -> None:
            db.open(paths.db_file)
            director.start()
            card_reader.on_card_removed(lambda reader: director.card_removed())
            card_reader.on_card_presented(
                lambda reader: director.card_presented(reader.read_id())
            card_reader.on_card_placed(
                lambda reader: director.card_placed(reader.read_id())
            )
        finally:
            db.close()
+6 −6
Original line number Diff line number Diff line
@@ -10,17 +10,17 @@ class CardReader(AbstractContextManager, ABC):
    @abstractmethod
    def read_id(self) -> int | None:
        """
        Read the UID of the MIFARE Ultralight card that is presented.
        Read the UID of the MIFARE Ultralight card that is placed.

        If the presented card is not a MIFARE Ultralight card, there is more than one card presented
        at once, or there is no card presented at all, this method's behavior is undefined.
        If the placed card is not a MIFARE Ultralight card, there is more than one card placed
        at once, or there is no card placed at all, this method's behavior is undefined.
        """
        ...

    @abstractmethod
    def on_card_presented(self, handle_card: Callable[[CardReader], None]) -> None:
    def on_card_placed(self, handle_card: Callable[[CardReader], None]) -> None:
        """
        Register the function handle_card so that it is called whenever a MIFARE Ultralight card is presented.
        Register the function handle_card so that it is called whenever a MIFARE Ultralight card is placed.

        If this is called multiple times, the CardReader may decide whether it only remembers and calls the
        last registered function, or all of them.
@@ -30,7 +30,7 @@ class CardReader(AbstractContextManager, ABC):
    @abstractmethod
    def on_card_removed(self, handle_removal: Callable[[CardReader], None]) -> None:
        """
        Register the function handle_removal so that it is called whenever a MIFARE Ultralight card was presented and removed again.
        Register the function handle_removal so that it is called whenever a MIFARE Ultralight card has previously been placed and is now removed.

        If this is called multiple times, the CardReader may decide whether it only remembers and calls the
        last registered function, or all of them.
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ class Mfrc522CardReader(CardReader):

        return card_id

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

    def close(self) -> None:
+6 −6
Original line number Diff line number Diff line
@@ -30,21 +30,21 @@ class Pirc522CardReader(CardReader):
        # the bytes the appropriate amount of positions, and adding this all up.
        return sum(byte << 8 * index for index, byte in enumerate(reversed(uid)))

    def on_card_presented(self, handle_card: Callable[[CardReader], None]) -> None:
    def on_card_placed(self, handle_card: Callable[[CardReader], None]) -> None:
        # If the interrupt pin (pin_irq) is correctly initialized, self._reader.wait_for_tag() might work
        # TODO Run this in a background thread

        card_recently_presented = False
        card_recently_placed = False

        while True:
            uid = self.read_id()

            if uid is None:
                if card_recently_presented and self._handle_removal is not None:
                if card_recently_placed and self._handle_removal is not None:
                    self._handle_removal(self)
                card_recently_presented = False
            elif not card_recently_presented:
                card_recently_presented = True
                card_recently_placed = False
            elif not card_recently_placed:
                card_recently_placed = True
                handle_card(self)

            sleep(0.1)
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ def action_id(reader: CardReader) -> None:
        print("Card ID (base 16)", hex(card_id))

    try:
        reader.on_card_presented(lambda _: _read_and_print_id())
        reader.on_card_placed(lambda _: _read_and_print_id())
    except NotImplementedError:
        _read_and_print_id()

Loading