From 2fd19397463f2bdc8c50a06ab7c8581f7bc96f0b Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Thu, 7 Mar 2024 21:49:14 +0000 Subject: [PATCH 1/3] Com: Append new line when flusing serial Our HID implementation sends fixed-length reports, that will be parsed by the receiving software. Serial outputs are more geared towards debugging, thus splitting the messages sent is much more usable. --- src/Com.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Com.cpp b/src/Com.cpp index 8c45eaa..86e238c 100644 --- a/src/Com.cpp +++ b/src/Com.cpp @@ -20,6 +20,7 @@ namespace Com { usb_rawhid_send(buffer, 0); #else Serial.write((const char*)buffer); + Serial.write('\n'); #endif /* Clear the buffer. */ memset(buffer, 0, sizeof(buffer)); From 3ee09f284a3475379bae445708eaff2f8ced6cb2 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Thu, 7 Mar 2024 23:05:14 +0000 Subject: [PATCH 2/3] RFID: Change the checkTag fail return Previously, checkTag() returned 0 when it failed or when there was no changes. This is fine if we are only expecting already programmed tags, but as tags arrive new with blocks zeroed out, which conflicts with the 0 return value. Introduce a new constant to RFID for the return error, -1, and use it instead of 0 everywhere. Update comments and README to highlight this new reserved ID. --- README.md | 3 ++- include/RFID.h | 5 ++++- src/RFID.cpp | 8 ++++---- src/main.cpp | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8833522..ba8adc2 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,8 @@ Il contient l'ID de la figurine sur les bits 0-5 de l'octet, si la figurine est un personnage (1) ou une année (0) sur le bit 6 et si la figurine a été posée (0) ou retirée (1) sur le bit 7. Ainsi, si on interprète l'octet comme un nombre signé, si le nombre est négatif -on sait que la figurine a été retirée. +on sait que la figurine a été retirée. +**_Dû à un détail d'implémentation, l'identifiant 127 est invalide et réservé._** #### Schéma ``` diff --git a/include/RFID.h b/include/RFID.h index 66a42f8..1c8bd8b 100644 --- a/include/RFID.h +++ b/include/RFID.h @@ -32,6 +32,8 @@ struct uidNode { class RFID { public: static constexpr int maxTags = 2; + /* Error value returned when checking for new or gone tags. */ + static constexpr int8_t checkFail = -1; /*** * Use the main SPI port and NFC chip select by default, but allow choosing @@ -48,7 +50,8 @@ public: * at a time, another call would be needed to get eventual concurrent events. * This updates the respective lists and returns the ID with the direction * of the change. - * @return 0 if no change, ID if new tag detected, ID & sign bit if the tag left. + * @return ID if new tag detected, ID & sign bit if the tag left + * @return RFID::checkFail if no change or error. */ int8_t checkTags(); diff --git a/src/RFID.cpp b/src/RFID.cpp index 2b15851..62a4a2f 100644 --- a/src/RFID.cpp +++ b/src/RFID.cpp @@ -52,21 +52,21 @@ int8_t RFID::checkTags() { if ( currentActiveTags >= maxTags || !mfrc.PICC_IsNewCardPresent() || !mfrc.PICC_ReadCardSerial()) { - return 0; + return checkFail; } // Assume we have a new tag. uidNode* newTag = addActiveTag(mfrc.uid); // Tag ID is put in a specific block, which can be read after from comData. if ( !newTag ) { - return 0; + return checkFail; } // If the read fails, we cannot use this tag anyway so remove it from the list. if (!readBlock(newTag->uid, tagIdBlock) ) { removeActiveTag(newTag, nullptr); - return 0; + return checkFail; } - // Don' t check the ID, otherwise we cannot program the tag. + // Don't check the ID, otherwise we cannot program the tag. newTag->tag_ID = comData[0]; return newTag->tag_ID; } diff --git a/src/main.cpp b/src/main.cpp index b281973..dcaf1c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,7 +67,7 @@ __attribute__((noreturn)) int main() { speaker.checkPlayingAndDisable(); tagEvent = rfid.checkTags(); - if (tagEvent) { + if (tagEvent != RFID::checkFail) { Com::sendFigUpdate(tagEvent); /* Currently, we only play effects when placing a new figurine. */ if (!isIdGone(tagEvent)) { From 776a4e252f2cb680fb91db7000e989114e20fb87 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Thu, 7 Mar 2024 23:22:24 +0000 Subject: [PATCH 3/3] main: Use both RFID readers As our RFID tags cannot co-exist on the same reader as originally plan, handle another reader on SPI1. Checking tags is not time sensitive and can be made in successive loops, which minimizes the changes needed from the original code. --- README.md | 1 + src/main.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba8adc2..aa125af 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ se synchroniser avec le firmware et potentiellement le mettre à jour. - [ ] Contrôle des modules - [x] RFID (base) - [x] RFID (avancé, écriture des tags) + - [x] RFID (deux lecteurs) - [x] LCD (base) - [x] LCD (animations) - [ ] LCD (UI) diff --git a/src/main.cpp b/src/main.cpp index dcaf1c7..1389e37 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,11 +16,13 @@ __attribute__((noreturn)) int main() { pinMode(pin_Audio_AMP_GAIN_EN, OUTPUT); pinMode(pin_NFC1_RST, OUTPUT); + pinMode(pin_NFC2_RST, OUTPUT); pinMode(pin_LCD_RST, OUTPUT); /* Preemptively disable what we can before the regulator is ON. */ digitalWrite(pin_Audio_Amp_enable, LOW); digitalWrite(pin_NFC1_RST, LOW); + digitalWrite(pin_NFC2_RST, LOW); digitalWrite(pin_LCD_RST, LOW); pinMode(pin_BAT_SNS, INPUT); @@ -45,6 +47,11 @@ __attribute__((noreturn)) int main() { digitalWrite(pin_NFC1_RST, HIGH); rfid.init(); + SPI1.setMISO(pin_SPI1_CIPO); + RFID rfid2(pin_NFC2_CS, SPI1); + digitalWrite(pin_NFC2_RST, HIGH); + rfid2.init(); + if (!SD.begin(BUILTIN_SDCARD)) Com::sendComment("Could not use the SD Card."); @@ -66,7 +73,14 @@ __attribute__((noreturn)) int main() { lcd.checkAndDisplay(); speaker.checkPlayingAndDisable(); + /* + * In the unlikely case where two changes happen in the same cycle, we + * can check the second RFID reader on the next one. + */ tagEvent = rfid.checkTags(); + if (tagEvent == RFID::checkFail) { + tagEvent = rfid2.checkTags(); + } if (tagEvent != RFID::checkFail) { Com::sendFigUpdate(tagEvent); /* Currently, we only play effects when placing a new figurine. */ @@ -80,9 +94,11 @@ __attribute__((noreturn)) int main() { Com::ReceivedMessage message = Com::receiveMessage(); if (message == Com::TAG_INFO_REQUEST) { uint8_t tagCount = rfid.gatherTagInfo(Com::getTagRecords()); + tagCount += rfid2.gatherTagInfo(Com::getTagRecords() + tagCount); Com::sendTagInfo(tagCount); } else if (message == Com::TAG_PROGRAMMING) { - if (!rfid.programTag(Com::getTagRecords())) { + if (!rfid.programTag(Com::getTagRecords()) && + !rfid2.programTag(Com::getTagRecords())) { Com::sendComment("Tag programming failed."); } }