From 6c5a6703668f9e8a46e323ce2bdcd817dc992ee0 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sat, 21 Oct 2023 16:00:08 +0100 Subject: [PATCH] IDs: Introduce ID handling functions and data IDs will be needed by multiple subsystems, so introduce the needed variables and handling functions. --- include/IDs.h | 37 +++++++++++++++++++++++++++++++++++++ include/RFID.h | 1 + src/IDs.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/RFID.cpp | 2 +- src/main.cpp | 2 +- 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 include/IDs.h create mode 100644 src/IDs.cpp diff --git a/include/IDs.h b/include/IDs.h new file mode 100644 index 0000000..783e2d0 --- /dev/null +++ b/include/IDs.h @@ -0,0 +1,37 @@ +// +// Created by Teo-CD on 19/10/23. +// + +#ifndef JIN_BARBAPAPA_IDS_H +#define JIN_BARBAPAPA_IDS_H + +#include + +/*** + * Convenience functions and variables to handle IDs and data that they link to. + */ + +constexpr uint8_t idMaskGone = 0b10000000; +constexpr uint8_t idMaskChar = 0b01000000; + +bool isIdGone(int8_t ID); +bool isIdCharacter(int8_t ID); +/*** + * Get the ID without the control bits : direction and character/promotion bit. + * @param ID to check, with all bits + * @return The ID with only index bits. + */ +uint8_t getRawId(int8_t ID); +/*** + * Checks if the ID is known to the firmware, either as a character or promotion. + * @param ID to check, with all bits + * @return True if valid as a character or promotion, false otherwise. + */ +bool isValidId(int8_t ID); + +static constexpr uint8_t dirStrSize = 10; +extern const char promoDirs[][dirStrSize]; +extern const char charDirs[][dirStrSize]; + + +#endif //JIN_BARBAPAPA_IDS_H diff --git a/include/RFID.h b/include/RFID.h index 48fa1c6..88d4c65 100644 --- a/include/RFID.h +++ b/include/RFID.h @@ -15,6 +15,7 @@ #include #include +#include "IDs.h" #include "Pinout.h" /*** diff --git a/src/IDs.cpp b/src/IDs.cpp new file mode 100644 index 0000000..8a3ae9a --- /dev/null +++ b/src/IDs.cpp @@ -0,0 +1,45 @@ +// +// Created by Teo-CD on 19/10/23. +// + +#include "IDs.h" + +/* IDs are greater than one, offset the array to make it easier to use. */ +constexpr char promoDirs[][dirStrSize] = { + "INVALIDE", + "RANDONNEE", + "RESTO", + "AUTOBUS", + "MAYO", + "MADO", + "COOP", + "OURS", + "BAGAR", + "INCAPABLE", + "MYSTERE" +}; +constexpr char charDirs[][dirStrSize] = { + "INVALIDE", + "GUILLAUME", + "MICHEL" +}; + +bool isIdGone(int8_t ID) { + return ID & idMaskGone; +} + +bool isIdCharacter(int8_t ID) { + return ID & idMaskChar; +} + +uint8_t getRawId(int8_t ID) { + return ID & ~(idMaskGone | idMaskChar); +} + +bool isValidId(int8_t ID) { + uint8_t rawId = getRawId(ID); + if (isIdCharacter(ID)) + return rawId < sizeof(charDirs) / dirStrSize; + else + return rawId < sizeof(promoDirs) / dirStrSize; +} diff --git a/src/RFID.cpp b/src/RFID.cpp index 0ba5e1b..22c69fc 100644 --- a/src/RFID.cpp +++ b/src/RFID.cpp @@ -39,7 +39,7 @@ int8_t RFID::checkTags() { removeActiveTag(node, previousNode); // Return the ID with the sign bit set to signal it has gone. // If other tags are gone we can check on the next loop. - return node->tag_ID | 1<<7; + return node->tag_ID | idMaskGone; } } } diff --git a/src/main.cpp b/src/main.cpp index c19b04c..14c990f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,8 +2,8 @@ #include "Pinout.h" -#include "RFID.h" #include "Com.h" +#include "RFID.h" __attribute__((noreturn)) int main() { pinMode(pin_DBG_LED_1, OUTPUT);