organizing the communication code a bit better

This commit is contained in:
2025-10-30 00:32:28 +00:00
parent 6efdc2771d
commit 89da044474
5 changed files with 74 additions and 34 deletions
+14
View File
@@ -0,0 +1,14 @@
#ifndef __COMMANDS__
#define __COMMANDS__
#include <stdint.h>
typedef enum : uint8_t {
CmdRequestID = 0,
CmdAckID,
CmdCreateScreen,
CmdCreateWindow
} Command;
#endif
@@ -0,0 +1,23 @@
#include "identificationPacket.h"
#include <Arduino.h>
#include <cstdint>
#include <cstring>
void initIdentificationPacket(IdentificationPacket* packet, const char* name,
uint8_t id) {
memset(packet, 0, sizeof(*packet));
packet->StartMarker = 0x02; // Start of text
packet->EndMarker = 0x03; // End of text
packet->DeviceID = id;
packet->PktType = identificationPacket;
// snprintf(packet->deviceName, sizeof(packet->deviceName), "%s", name);
strncpy(packet->deviceName, name, strlen(name));
packet->deviceName[strlen(name)] = '\0';
}
void sendIdentificationPacket(IdentificationPacket* packet) {
Serial.write((uint8_t*)packet, sizeof(*packet));
Serial.flush();
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef __IDENTIFICATION_PACKET__
#define __IDENTIFICATION_PACKET__
#include "packets.h"
#include <cstdint>
#include <stdint.h>
// eventually try this out with __attrubute__((packed))
struct __attribute__((packed)) IdentificationPacket {
char StartMarker;
uint8_t DeviceID;
PacketType PktType;
char deviceName[32];
char EndMarker;
};
void initIdentificationPacket(IdentificationPacket* packet, const char* name,
uint8_t id);
void sendIdentificationPacket(IdentificationPacket* packet);
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef __PACKETS__
#define __PACKETS__
#include <stdint.h>
typedef uint8_t PacketType;
const uint8_t identificationPacket = 0;
#endif
+7 -34
View File
@@ -7,6 +7,9 @@ TODO:
to have more ways to run analytics to have more ways to run analytics
*/ */
#include "communication/commands.h"
#include "communication/packets.h"
#include "communication/identificationPacket.h"
#include "Arduino_GFX.h" #include "Arduino_GFX.h"
#include "HardwareSerial.h" #include "HardwareSerial.h"
#include "UIDecorations.h" #include "UIDecorations.h"
@@ -79,40 +82,7 @@ void setup() {
uint64_t lastDataRead = 0; uint64_t lastDataRead = 0;
typedef uint8_t PacketType;
const uint8_t IdentificationPacket = 0;
// eventually try this out with __attrubute__((packed))
struct __attribute__((packed)) FlarePacket {
char StartMarker;
uint8_t DeviceID;
PacketType PktType;
char deviceName[32];
char EndMarker;
};
void initFlarePacket(FlarePacket* packet) {
packet->StartMarker = 0x02; // Start of text
packet->EndMarker = 0x03; // End of text
}
bool gotAck = false; bool gotAck = false;
void sendHello() {
FlarePacket flare;
initFlarePacket(&flare);
flare.DeviceID = ESLABS_DEVICE_ID;
flare.PktType = IdentificationPacket;
strncpy(flare.deviceName, ESLABS_DEVICE_NAME, strlen(ESLABS_DEVICE_NAME));
Serial.write((uint8_t*)&flare, sizeof(flare));
Serial.flush();
}
typedef uint8_t Command;
const Command CmdRequestID = 0;
const Command CmdAckID = 1;
uint64_t lastSent = 0; uint64_t lastSent = 0;
void loop(void) { void loop(void) {
@@ -128,7 +98,10 @@ void loop(void) {
if (cmd == CmdRequestID) { if (cmd == CmdRequestID) {
Serial2.println("Got identification request!"); Serial2.println("Got identification request!");
gearText.Update("Connecting"); gearText.Update("Connecting");
sendHello();
IdentificationPacket papers;
initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID);
sendIdentificationPacket(&papers);
} else if (cmd == CmdAckID) { } else if (cmd == CmdAckID) {
Serial2.println("Got ack!"); Serial2.println("Got ack!");
gearText.Update("Connected"); gearText.Update("Connected");