From 8d80dd26042c1f4880df1526a0777efaa345fb43 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 24 Sep 2023 12:31:01 +0100 Subject: [PATCH 1/2] RFID: Fix tag return when gone Previously, the return value when a tag was gone was the negative ID. However, this isn't the most effective to process later as a negative number uses two's complement, changing all lower bits in addition to the sign bit. Change this to just set the sign bit instead. This still produces a negative number, which is what we want to check to konw if the tag is new or went away, but maintains the lower bits so the ID can be retrieved with a simple bit-wise and. --- src/RFID.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RFID.cpp b/src/RFID.cpp index da42d4e..0ba5e1b 100644 --- a/src/RFID.cpp +++ b/src/RFID.cpp @@ -37,9 +37,9 @@ int8_t RFID::checkTags() { // Serial.printf("UID 0x%lX is gone\n", *(uint32_t *)node->uid.uidByte); // If we don't, assume the tag is gone and remove it from the list. removeActiveTag(node, previousNode); - // Return the negative ID to signal it has gone. + // 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; + return node->tag_ID | 1<<7; } } } From b330ddab377f9c5778b9f23cc1c91195f0aa18b5 Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 24 Sep 2023 18:00:42 +0100 Subject: [PATCH 2/2] Com: Implement proper communications Implement a set of functions to create messages following the protocols defined in the README for communication. Handle HID, which is used to communicate with the interactive application. Update README with details on the message structures. --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- include/Com.h | 20 +++++++++++++++++ platformio.ini | 2 ++ src/Com.cpp | 39 +++++++++++++++++++++++++++++++++ src/main.cpp | 11 +++++----- 5 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 include/Com.h create mode 100644 src/Com.cpp diff --git a/README.md b/README.md index 87ae17e..a8c26d5 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,64 @@ mise à jour et suivi des dépendances. La structure du code assume une carte Teensy. En particulier, il est conçu pour la [Teensy 4.1](https://www.pjrc.com/store/teensy41.html). +# Interfaçage avec l'application Unity + +Afin de permettre la détection automatique de l'objet, la communication se fait +par Human Interface Device (HID). +La communication HID est possible grâce aux bibliothèques de Teensyduino qui +fournissent des fonctionnalités de base. +Ces fonctionnalités ne sont pas idéales et gâchent pas mal de bande passante, +en effet par défaut on ne peut qu'envoyer des rapports HIDs de 64 octets, malgré +le besoin de n'en consommer que quelques-uns dans le cas général. +Faute de vouloir modifier les bibliothèques Teensyduino, nous nous contenterons +de ce gâchis, la Teensy étant absurdement rapide de toute façon. + +## Protocole + +Le protocole est constitué de messages avec un octet d'en-tête, puis d'un message +de longueur définie par l'en-tête, potentiellement variable jusqu'au maximum de +64 octets (avec en-tête). + +### Changement de figurine + +**En-tête** : `@` +**Longueur** : 1 octet +**Description** : Message envoyé lors de la dépose ou du retrait d'une figurine. +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. + +#### Schéma +``` +Octet 1 +┌─┬─┬───────────┐ +│D│C│ Figure ID │ +└─┴─┴───────────┘ + 7 0 - Bits +``` +`D`: bit 7, 1 si enlevé, 0 sinon +`C`: bit 6, 1 si personnage, 0 si année + +### Message informatif + +**En-tête** : `#` +**Longueur** : 1+[1-62] octets +**Description** : Message générique à titre informatif, utile pour la communication +d'informations lors du développement ou de débug. +Le premier octet est fixe et donne la longueur du reste du message, le message +est du texte encodé en ASCII. + +#### Schéma +``` + ┌───┬── ── ── ──┐ + │Len│ Message │ + └───┴── ── ── ──┘ + 0 1 Len - Octets +``` + # License -Les éléments dans ce dépôts sont Open Source, sous license +Les éléments dans ce dépôt sont Open Source, sous license [Apache License V2.0](https://apache.org/licenses/LICENSE-2.0). diff --git a/include/Com.h b/include/Com.h new file mode 100644 index 0000000..b4f376f --- /dev/null +++ b/include/Com.h @@ -0,0 +1,20 @@ +// +// Created by Teo-CD on 24/09/23. +// + +#ifndef JIN_BARBAPAPA_COM_H +#define JIN_BARBAPAPA_COM_H + +#include +#include + +/** + * Handle communication with a connected device and provide convenience + * functions for the fixed protocols. + */ +namespace Com { + void sendFigUpdate(int8_t event); + void sendComment(const char* message, ...); +} + +#endif //JIN_BARBAPAPA_COM_H diff --git a/platformio.ini b/platformio.ini index 33cfd2d..f348053 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,3 +17,5 @@ lib_deps = adafruit/Adafruit PCD8544 Nokia 5110 LCD library adafruit/Adafruit GFX Library adafruit/Adafruit BusIO +; Should be uncommented in production in order to use HID. +;build_flags = -D USB_RAWHID diff --git a/src/Com.cpp b/src/Com.cpp new file mode 100644 index 0000000..bd33269 --- /dev/null +++ b/src/Com.cpp @@ -0,0 +1,39 @@ +// +// Created by Teo-CD on 24/09/23. +// + +#include "Com.h" + +namespace Com { + byte buffer[64]; + /** + * Will flush buffer through either HID or Serial depending on build + * options. + * HID is the expected production usage as the interactive software will + * use it for auto-detection. + */ + inline void flushBuffer() { +#ifdef USB_RAWHID + usb_rawhid_send(buffer, 0); +#else + Serial.write((const char*)buffer); +#endif + /* Clear the buffer. */ + memset(buffer, 0, 64); + } + + void sendFigUpdate(int8_t event) { + buffer[0] = '@'; + buffer[1] = event; + flushBuffer(); + } + + void sendComment(const char *message, ...) { + va_list args; + va_start(args, message); + buffer[0] = '#'; + buffer[1] = vsnprintf((char *)(buffer + 2), 62, message, args); + va_end(args); + flushBuffer(); + } +} diff --git a/src/main.cpp b/src/main.cpp index 677112f..ddebea6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "Pinout.h" #include "RFID.h" +#include "Com.h" __attribute__((noreturn)) int main() { pinMode(pin_DBG_LED_1, OUTPUT); @@ -36,7 +37,7 @@ __attribute__((noreturn)) int main() { */ Serial.begin(115200); - Serial.println("# System is powered up, running set-up."); + Com::sendComment("# System is powered up, running set-up."); /* TODO: Setups once module structure is up. */ RFID rfid; @@ -45,11 +46,11 @@ __attribute__((noreturn)) int main() { /* Main loop */ while (true) { - int8_t tagID; + int8_t tagEvent; - tagID = rfid.checkTags(); - if (tagID) { - Serial.printf("Check tag result : %d\n", tagID); + tagEvent = rfid.checkTags(); + if (tagEvent) { + Com::sendFigUpdate(tagEvent); } delay(100);