Threading/Queues and Display Gif added
This commit is contained in:
+32
-28
@@ -35,54 +35,55 @@ int BotNetwork::readPacket(uint8_t* buffer, size_t maxSize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void BotNetwork::parsePacket(uint8_t *buffer, size_t size, MotionController* motion, DisplayManager* display) {
|
||||
static void enqueueDisplayText(QueueHandle_t displayQueue, const uint8_t* data, size_t length) {
|
||||
if (!displayQueue) return;
|
||||
|
||||
DisplayMessage message;
|
||||
size_t textLength = length < sizeof(message.text) - 1 ? length : sizeof(message.text) - 1;
|
||||
memcpy(message.text, data, textLength);
|
||||
message.text[textLength] = '\0';
|
||||
message.color = 0x07E0;
|
||||
xQueueOverwrite(displayQueue, &message);
|
||||
}
|
||||
|
||||
void BotNetwork::parsePacket(uint8_t *buffer, size_t size, QueueHandle_t motionQueue,
|
||||
QueueHandle_t configQueue, QueueHandle_t displayQueue) {
|
||||
if (size < sizeof(PacketHeader)) return;
|
||||
PacketHeader *header = (PacketHeader *)buffer;
|
||||
if (header->version != PACKET_VERSION) return;
|
||||
if (header->payload_len > size - sizeof(PacketHeader)) return;
|
||||
|
||||
uint8_t *payload = buffer + sizeof(PacketHeader);
|
||||
size_t pos = 0;
|
||||
int tempLeg[6][3];
|
||||
|
||||
// Temporary trackers for config values so we can update them cleanly
|
||||
static int currentServoMin = 125;
|
||||
static int currentServoMax = 625;
|
||||
static int currentPythonInterval = 50;
|
||||
|
||||
while (pos < header->payload_len) {
|
||||
if (header->payload_len - pos < 3) return;
|
||||
uint8_t f_type = payload[pos++];
|
||||
uint16_t f_len = payload[pos] | (payload[pos+1] << 8);
|
||||
pos += 2;
|
||||
if (f_len > header->payload_len - pos) return;
|
||||
uint8_t *data = payload + pos;
|
||||
|
||||
// --- BRANCH 1: MOTION & DISPLAY COMMANDS ---
|
||||
if (header->packet_id == PKT_COMMAND) {
|
||||
if (f_type == FT_ARRAY_F32 && f_len == 18) {
|
||||
for (int i=0; i<6; i++) for (int j=0; j<3; j++) tempLeg[i][j] = data[i*3+j];
|
||||
motion->pushFrame(tempLeg);
|
||||
if (f_type == FT_ARRAY_F32 && f_len == 18 && motionQueue) {
|
||||
MotionFrame frame;
|
||||
for (int i=0; i<6; i++) {
|
||||
for (int j=0; j<3; j++) frame.leg[i][j] = data[i*3+j];
|
||||
}
|
||||
xQueueOverwrite(motionQueue, &frame);
|
||||
} else if (f_type == FT_STRING) {
|
||||
char textBuffer[64];
|
||||
size_t len = min((size_t)f_len, (size_t)63);
|
||||
memcpy(textBuffer, data, len);
|
||||
textBuffer[len] = '\0';
|
||||
display->drawCenteredText(textBuffer, 0x07E0); // Green text
|
||||
enqueueDisplayText(displayQueue, data, f_len);
|
||||
}
|
||||
}
|
||||
|
||||
// --- BRANCH 2: DISPLAY PACKET (If your Python sends separate PKT_DISPLAY) ---
|
||||
else if (header->packet_id == PKT_DISPLAY) {
|
||||
} else if (header->packet_id == PKT_DISPLAY) {
|
||||
if (f_type == FT_STRING) {
|
||||
char textBuffer[64];
|
||||
size_t len = min((size_t)f_len, (size_t)63);
|
||||
memcpy(textBuffer, data, len);
|
||||
textBuffer[len] = '\0';
|
||||
display->drawCenteredText(textBuffer, 0x07E0);
|
||||
enqueueDisplayText(displayQueue, data, f_len);
|
||||
}
|
||||
}
|
||||
|
||||
// --- BRANCH 3: CONFIGS (FIXED) ---
|
||||
else if (header->packet_id == PKT_CONFIG) {
|
||||
if (f_type == FT_CONFIG_VAL) {
|
||||
} else if (header->packet_id == PKT_CONFIG) {
|
||||
if (f_type == FT_CONFIG_VAL && f_len >= 3) {
|
||||
uint8_t id = data[0];
|
||||
int16_t val = data[1] | (data[2] << 8);
|
||||
|
||||
@@ -90,8 +91,11 @@ void BotNetwork::parsePacket(uint8_t *buffer, size_t size, MotionController* mot
|
||||
if (id == 2) currentServoMax = val;
|
||||
if (id == 3) currentPythonInterval = val;
|
||||
|
||||
// Push the updated configs down into the MotionController
|
||||
motion->setConfigs(currentServoMin, currentServoMax, currentPythonInterval);
|
||||
MotionConfig config;
|
||||
config.minPulse = currentServoMin;
|
||||
config.maxPulse = currentServoMax;
|
||||
config.intervalMs = currentPythonInterval;
|
||||
if (configQueue) xQueueOverwrite(configQueue, &config);
|
||||
Serial.printf("Config Sync: ID %d -> %d\n", id, val);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user