Com: Implement tag programming
Define the protocol allowing the software to track tags and program them, in the README. The data structure is shared between a programming command and an information message, so create a new struct for it and an array to keep track of it and expose to the rest of the program. The goal is to keep the protocol core and message construction entirely inside Com, so this intermediary buffer allows exposing it outside while hiding the protocol details. This means that we are starting to receive data from the software, so implement generic functions to check and receive data as well as receiveMessage() which parses the message and handles them.
This commit is contained in:
parent
324cf50653
commit
b5b5268b79
3 changed files with 148 additions and 1 deletions
59
src/Com.cpp
59
src/Com.cpp
|
@ -3,9 +3,12 @@
|
|||
//
|
||||
|
||||
#include "Com.h"
|
||||
#include "RFID.h"
|
||||
|
||||
namespace Com {
|
||||
byte buffer[64];
|
||||
TagInfoRecord records[RFID::maxTags] = {};
|
||||
|
||||
/**
|
||||
* Will flush buffer through either HID or Serial depending on build
|
||||
* options.
|
||||
|
@ -19,7 +22,7 @@ namespace Com {
|
|||
Serial.write((const char*)buffer);
|
||||
#endif
|
||||
/* Clear the buffer. */
|
||||
memset(buffer, 0, 64);
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
}
|
||||
|
||||
void sendFigUpdate(int8_t event) {
|
||||
|
@ -36,4 +39,58 @@ namespace Com {
|
|||
va_end(args);
|
||||
flushBuffer();
|
||||
}
|
||||
|
||||
void sendTagInfo(uint8_t tagCount) {
|
||||
buffer[0] = '!';
|
||||
buffer[1] = tagCount;
|
||||
memcpy(buffer + 2, records, sizeof(TagInfoRecord)*tagCount);
|
||||
flushBuffer();
|
||||
}
|
||||
|
||||
TagInfoRecord* getTagRecords() {
|
||||
return records;
|
||||
}
|
||||
|
||||
inline bool dataAvailable() {
|
||||
#ifdef USB_RAWHID
|
||||
return usb_rawhid_available();
|
||||
#else
|
||||
/* Serial is used for debug, do not care about message length. */
|
||||
return Serial.available();
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void receiveData() {
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
#ifdef USB_RAWHID
|
||||
/* We know we have data, don't need to set a timeout. */
|
||||
usb_rawhid_recv(buffer, 0);
|
||||
#else
|
||||
/* Could lead to incomplete messages, but should be OK on Teensy. */
|
||||
Serial.readBytes(buffer, Serial.available());
|
||||
#endif
|
||||
}
|
||||
|
||||
ReceivedMessage receiveMessage() {
|
||||
if (!dataAvailable())
|
||||
return NO_MESSAGE;
|
||||
receiveData();
|
||||
|
||||
ReceivedMessage messageType;
|
||||
switch (buffer[0]) {
|
||||
case '?':
|
||||
messageType = TAG_INFO_REQUEST;
|
||||
break;
|
||||
case '=':
|
||||
messageType = TAG_PROGRAMMING;
|
||||
/* Make the record available to the rest of the code. */
|
||||
memset(records, 0, sizeof(records));
|
||||
memcpy(records, buffer + 1, sizeof(TagInfoRecord));
|
||||
break;
|
||||
default:
|
||||
messageType = INVALID_MESSAGE;
|
||||
Serial.printf("Invalid header received : %c\n", buffer[0]);
|
||||
}
|
||||
return messageType;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue