1
0
Fork 0

Compare commits

...

2 commits

Author SHA1 Message Date
b330ddab37 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.
2023-09-24 18:00:42 +01:00
8d80dd2604 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.
2023-09-24 12:31:01 +01:00
6 changed files with 127 additions and 8 deletions

View file

@ -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 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). [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 # 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). [Apache License V2.0](https://apache.org/licenses/LICENSE-2.0).

20
include/Com.h Normal file
View file

@ -0,0 +1,20 @@
//
// Created by Teo-CD on 24/09/23.
//
#ifndef JIN_BARBAPAPA_COM_H
#define JIN_BARBAPAPA_COM_H
#include <Arduino.h>
#include <cstring>
/**
* 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

View file

@ -17,3 +17,5 @@ lib_deps =
adafruit/Adafruit PCD8544 Nokia 5110 LCD library adafruit/Adafruit PCD8544 Nokia 5110 LCD library
adafruit/Adafruit GFX Library adafruit/Adafruit GFX Library
adafruit/Adafruit BusIO adafruit/Adafruit BusIO
; Should be uncommented in production in order to use HID.
;build_flags = -D USB_RAWHID

39
src/Com.cpp Normal file
View file

@ -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();
}
}

View file

@ -37,9 +37,9 @@ int8_t RFID::checkTags() {
// Serial.printf("UID 0x%lX is gone\n", *(uint32_t *)node->uid.uidByte); // 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. // If we don't, assume the tag is gone and remove it from the list.
removeActiveTag(node, previousNode); 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. // If other tags are gone we can check on the next loop.
return -node->tag_ID; return node->tag_ID | 1<<7;
} }
} }
} }

View file

@ -3,6 +3,7 @@
#include "Pinout.h" #include "Pinout.h"
#include "RFID.h" #include "RFID.h"
#include "Com.h"
__attribute__((noreturn)) int main() { __attribute__((noreturn)) int main() {
pinMode(pin_DBG_LED_1, OUTPUT); pinMode(pin_DBG_LED_1, OUTPUT);
@ -36,7 +37,7 @@ __attribute__((noreturn)) int main() {
*/ */
Serial.begin(115200); 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. */ /* TODO: Setups once module structure is up. */
RFID rfid; RFID rfid;
@ -45,11 +46,11 @@ __attribute__((noreturn)) int main() {
/* Main loop */ /* Main loop */
while (true) { while (true) {
int8_t tagID; int8_t tagEvent;
tagID = rfid.checkTags(); tagEvent = rfid.checkTags();
if (tagID) { if (tagEvent) {
Serial.printf("Check tag result : %d\n", tagID); Com::sendFigUpdate(tagEvent);
} }
delay(100); delay(100);