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);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -3,9 +3,11 @@
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
#include "Protocol.h"
|
||||
#include "MotionController.h" // Needed for pointers
|
||||
#include "DisplayManager.h" // Needed for pointers
|
||||
#include "MotionController.h"
|
||||
#include "DisplayManager.h"
|
||||
|
||||
class BotNetwork {
|
||||
public:
|
||||
@@ -13,7 +15,8 @@ public:
|
||||
bool begin(uint16_t port); // Returns true if WiFi, false if AP
|
||||
int readPacket(uint8_t* buffer, size_t maxSize);
|
||||
|
||||
void parsePacket(uint8_t *buffer, size_t size, MotionController* motion, DisplayManager* display);
|
||||
void parsePacket(uint8_t *buffer, size_t size, QueueHandle_t motionQueue,
|
||||
QueueHandle_t configQueue, QueueHandle_t displayQueue);
|
||||
|
||||
private:
|
||||
const char* _ssid;
|
||||
|
||||
+53
-7
@@ -1,13 +1,36 @@
|
||||
#include "DisplayManager.h"
|
||||
#include "img/image_cards_hearts_bits.h"
|
||||
#include "img/image_paint_3_bits.h"
|
||||
#include "img/tenor.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
DisplayManager::DisplayManager(int8_t cs, int8_t dc, int8_t rst)
|
||||
: _tft(cs, dc, rst) {}
|
||||
|
||||
static void drawAnimationFrame(Adafruit_ST7789& tft, int16_t x, uint8_t frame) {
|
||||
uint16_t pixels[240];
|
||||
const uint8_t* bitmap = tenor_frames[frame];
|
||||
|
||||
tft.startWrite();
|
||||
tft.setAddrWindow(x, 0, 240, 240);
|
||||
for (int16_t y = 0; y < 240; y++) {
|
||||
for (int16_t byteIndex = 0; byteIndex < 30; byteIndex++) {
|
||||
uint8_t bits = pgm_read_byte(&bitmap[y * 30 + byteIndex]);
|
||||
for (uint8_t bit = 0; bit < 8; bit++) {
|
||||
pixels[byteIndex * 8 + bit] = (bits & (0x80 >> bit)) ? ST77XX_WHITE : ST77XX_BLACK;
|
||||
}
|
||||
}
|
||||
tft.writePixels(pixels, 240);
|
||||
}
|
||||
tft.endWrite();
|
||||
}
|
||||
|
||||
void DisplayManager::begin() {
|
||||
_tft.init(240, 320);
|
||||
_tft.setRotation(1);
|
||||
clear();
|
||||
drawFaceScreen();
|
||||
}
|
||||
|
||||
void DisplayManager::clear() {
|
||||
@@ -33,20 +56,35 @@ void DisplayManager::toggleState() {
|
||||
}
|
||||
|
||||
void DisplayManager::update() {
|
||||
// Put any periodic animations here (e.g., blinking face frames)
|
||||
if (_currentState != STATE_FACE) return;
|
||||
|
||||
unsigned long now = millis();
|
||||
if (_showingText) {
|
||||
if (now - _textShownAt < 2000) return;
|
||||
drawFaceScreen();
|
||||
}
|
||||
|
||||
if (now - _lastScreenUpdate < 71) return;
|
||||
_lastScreenUpdate = now;
|
||||
|
||||
int16_t x = (_tft.width() - 240) / 2;
|
||||
drawAnimationFrame(_tft, x, _animationFrame);
|
||||
_tft.drawBitmap(x + 105, 14, image_cards_hearts_bits, 30, 32, 0xE8EC);
|
||||
_animationFrame = (_animationFrame + 1) % 8;
|
||||
}
|
||||
|
||||
// --- DESIGNED VIA LOPAKA.APP ---
|
||||
void DisplayManager::drawFaceScreen() {
|
||||
_showingText = false;
|
||||
_animationFrame = 0;
|
||||
clear();
|
||||
// Example layout elements generated/inspired by Lopaka:
|
||||
_tft.setTextColor(ST77XX_CYAN);
|
||||
_tft.setTextSize(3);
|
||||
_tft.setCursor(90, 100);
|
||||
_tft.print("^ _ ^"); // Simple default face
|
||||
int16_t x = (_tft.width() - 240) / 2;
|
||||
_tft.drawBitmap(x, 0, image_paint_3_bits, 240, 240, ST77XX_WHITE);
|
||||
_tft.drawBitmap(x + 105, 14, image_cards_hearts_bits, 30, 32, 0xE8EC);
|
||||
}
|
||||
|
||||
void DisplayManager::drawMenuScreen() {
|
||||
_showingText = false;
|
||||
clear();
|
||||
|
||||
// Example Menu/Stats UI layout adapted from Lopaka export
|
||||
@@ -69,8 +107,16 @@ void DisplayManager::drawMenuScreen() {
|
||||
}
|
||||
|
||||
void DisplayManager::drawCenteredText(const char* text, uint16_t color) {
|
||||
if (_currentState != STATE_FACE) return; // Only override face text if we are on face screen
|
||||
if (_currentState != STATE_FACE || text == nullptr) return;
|
||||
if (strcmp(text, _lastText) == 0) return;
|
||||
|
||||
size_t textLength = strlen(text);
|
||||
if (textLength >= sizeof(_lastText)) textLength = sizeof(_lastText) - 1;
|
||||
memcpy(_lastText, text, textLength);
|
||||
_lastText[textLength] = '\0';
|
||||
|
||||
_showingText = true;
|
||||
_textShownAt = millis();
|
||||
clear();
|
||||
_tft.setTextSize(2);
|
||||
_tft.setTextColor(color);
|
||||
|
||||
@@ -9,6 +9,11 @@ enum DisplayState {
|
||||
STATE_MENU
|
||||
};
|
||||
|
||||
struct DisplayMessage {
|
||||
char text[64];
|
||||
uint16_t color;
|
||||
};
|
||||
|
||||
class DisplayManager {
|
||||
public:
|
||||
DisplayManager(int8_t cs, int8_t dc, int8_t rst);
|
||||
@@ -29,6 +34,10 @@ private:
|
||||
Adafruit_ST7789 _tft;
|
||||
DisplayState _currentState = STATE_FACE;
|
||||
unsigned long _lastScreenUpdate = 0;
|
||||
unsigned long _textShownAt = 0;
|
||||
bool _showingText = false;
|
||||
uint8_t _animationFrame = 0;
|
||||
char _lastText[64] = {};
|
||||
};
|
||||
|
||||
#endif
|
||||
+69
-10
@@ -1,4 +1,5 @@
|
||||
#include <Wire.h>
|
||||
#include <string.h>
|
||||
#include "BotNetwork.h"
|
||||
#include "MotionController.h"
|
||||
#include "DisplayManager.h"
|
||||
@@ -8,20 +9,79 @@ BotNetwork botNetwork("PfLANze", "isteinbisschenengandiehals", "JackBot-AP", "ro
|
||||
MotionController motion;
|
||||
DisplayManager display(5, 16, 17); // TFT_CS, TFT_DC, TFT_RST
|
||||
|
||||
static QueueHandle_t motionQueue = nullptr;
|
||||
static QueueHandle_t configQueue = nullptr;
|
||||
static QueueHandle_t displayQueue = nullptr;
|
||||
|
||||
static void servoTask(void*) {
|
||||
TickType_t lastWakeTime = xTaskGetTickCount();
|
||||
const TickType_t updatePeriod = pdMS_TO_TICKS(5);
|
||||
MotionFrame frame;
|
||||
MotionConfig config;
|
||||
|
||||
for (;;) {
|
||||
if (xQueueReceive(configQueue, &config, 0) == pdTRUE) {
|
||||
motion.setConfigs(config.minPulse, config.maxPulse, config.intervalMs);
|
||||
}
|
||||
if (xQueueReceive(motionQueue, &frame, 0) == pdTRUE) {
|
||||
motion.setTarget(frame);
|
||||
}
|
||||
motion.update();
|
||||
vTaskDelayUntil(&lastWakeTime, updatePeriod);
|
||||
}
|
||||
}
|
||||
|
||||
static void displayTask(void*) {
|
||||
display.begin();
|
||||
DisplayMessage message;
|
||||
|
||||
for (;;) {
|
||||
if (xQueueReceive(displayQueue, &message, pdMS_TO_TICKS(10)) == pdTRUE) {
|
||||
display.drawCenteredText(message.text, message.color);
|
||||
}
|
||||
display.update();
|
||||
}
|
||||
}
|
||||
|
||||
static void queueDisplayText(const char* text, uint16_t color) {
|
||||
if (!displayQueue) return;
|
||||
|
||||
DisplayMessage message;
|
||||
size_t length = strlen(text);
|
||||
if (length >= sizeof(message.text)) length = sizeof(message.text) - 1;
|
||||
memcpy(message.text, text, length);
|
||||
message.text[length] = '\0';
|
||||
message.color = color;
|
||||
xQueueOverwrite(displayQueue, &message);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Wire.begin();
|
||||
Wire.setClock(400000);
|
||||
|
||||
display.begin();
|
||||
display.drawCenteredText("Connecting...", ST77XX_YELLOW);
|
||||
if (botNetwork.begin(3323)) {
|
||||
display.drawCenteredText("WiFi: Connected", ST77XX_CYAN);
|
||||
} else {
|
||||
display.drawCenteredText("AP: JackBot", ST77XX_MAGENTA);
|
||||
motion.begin();
|
||||
|
||||
motionQueue = xQueueCreate(1, sizeof(MotionFrame));
|
||||
configQueue = xQueueCreate(1, sizeof(MotionConfig));
|
||||
displayQueue = xQueueCreate(1, sizeof(DisplayMessage));
|
||||
if (!motionQueue || !configQueue || !displayQueue) {
|
||||
Serial.println("Failed to create task queues");
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
motion.begin();
|
||||
if (xTaskCreate(servoTask, "Servo", 4096, nullptr, 3, nullptr) != pdPASS ||
|
||||
xTaskCreate(displayTask, "Display", 4096, nullptr, 1, nullptr) != pdPASS) {
|
||||
Serial.println("Failed to create application tasks");
|
||||
while (true) delay(1000);
|
||||
}
|
||||
|
||||
queueDisplayText("Connecting...", ST77XX_YELLOW);
|
||||
if (botNetwork.begin(3323)) {
|
||||
queueDisplayText("WiFi: Connected", ST77XX_CYAN);
|
||||
} else {
|
||||
queueDisplayText("AP: JackBot", ST77XX_MAGENTA);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -29,9 +89,8 @@ void loop() {
|
||||
int size = botNetwork.readPacket(buffer, 512);
|
||||
|
||||
if (size > 0) {
|
||||
botNetwork.parsePacket(buffer, size, &motion, &display);
|
||||
botNetwork.parsePacket(buffer, size, motionQueue, configQueue, displayQueue);
|
||||
}
|
||||
|
||||
motion.update();
|
||||
display.update();
|
||||
delay(1);
|
||||
}
|
||||
+23
-35
@@ -1,4 +1,5 @@
|
||||
#include "MotionController.h"
|
||||
#include <math.h>
|
||||
|
||||
MotionController::MotionController() : board0(0x40), board1(0x41) {}
|
||||
|
||||
@@ -12,60 +13,47 @@ void MotionController::begin() {
|
||||
targetLeg[i][j] = 90.0;
|
||||
}
|
||||
}
|
||||
writeServos(currentLegF);
|
||||
}
|
||||
|
||||
void MotionController::update() {
|
||||
if (millis() - lastUpdate < 5) return; // 200Hz internal update
|
||||
lastUpdate = millis();
|
||||
|
||||
// Check if we arrived
|
||||
bool atTarget = true;
|
||||
bool moved = false;
|
||||
for(int i=0; i<6; i++) {
|
||||
for(int j=0; j<3; j++) {
|
||||
if (abs(currentLegF[i][j] - targetLeg[i][j]) > 0.5) atTarget = false;
|
||||
float remaining = targetLeg[i][j] - currentLegF[i][j];
|
||||
float step = stepSize[i][j];
|
||||
if (fabsf(remaining) > fabsf(step)) {
|
||||
currentLegF[i][j] += step;
|
||||
} else {
|
||||
currentLegF[i][j] = targetLeg[i][j];
|
||||
}
|
||||
if (currentLegF[i][j] != targetLeg[i][j] || remaining != 0.0f) {
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (atTarget && head != tail) {
|
||||
// Pop from buffer and calculate steps
|
||||
int next[6][3];
|
||||
memcpy(next, buffer[tail].leg, sizeof(next));
|
||||
tail = (tail + 1) % 10;
|
||||
if (moved) {
|
||||
writeServos(currentLegF);
|
||||
}
|
||||
}
|
||||
|
||||
void MotionController::setTarget(const MotionFrame& frame) {
|
||||
int numSteps = (_pythonInterval + 4) / 5;
|
||||
if (numSteps < 1) numSteps = 1;
|
||||
|
||||
int numSteps = _pythonInterval / 5;
|
||||
for(int i=0; i<6; i++) {
|
||||
for(int j=0; j<3; j++) {
|
||||
targetLeg[i][j] = (float)next[i][j];
|
||||
targetLeg[i][j] = (float)frame.leg[i][j];
|
||||
stepSize[i][j] = (targetLeg[i][j] - currentLegF[i][j]) / (float)numSteps;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move current position toward target
|
||||
for(int i=0; i<6; i++) {
|
||||
for(int j=0; j<3; j++) {
|
||||
if (abs(currentLegF[i][j] - targetLeg[i][j]) > abs(stepSize[i][j])) {
|
||||
currentLegF[i][j] += stepSize[i][j];
|
||||
} else {
|
||||
currentLegF[i][j] = targetLeg[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
writeServos(currentLegF);
|
||||
}
|
||||
|
||||
bool MotionController::pushFrame(int leg[6][3]) {
|
||||
int next = (head + 1) % 10;
|
||||
if (next == tail) return false;
|
||||
memcpy(buffer[head].leg, leg, sizeof(buffer[head].leg));
|
||||
head = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MotionController::setConfigs(int minP, int maxP, int interval) {
|
||||
_servoMin = minP;
|
||||
_servoMax = maxP;
|
||||
_pythonInterval = interval;
|
||||
_pythonInterval = interval < 5 ? 5 : interval;
|
||||
}
|
||||
|
||||
void MotionController::writeServos(float leg[6][3]) {
|
||||
|
||||
+3
-8
@@ -5,13 +5,14 @@
|
||||
#include "Protocol.h"
|
||||
|
||||
struct MotionFrame { int leg[6][3]; };
|
||||
struct MotionConfig { int minPulse; int maxPulse; int intervalMs; };
|
||||
|
||||
class MotionController {
|
||||
public:
|
||||
MotionController();
|
||||
void begin();
|
||||
void update(); // The 5ms interpolation "heartbeat"
|
||||
bool pushFrame(int leg[6][3]);
|
||||
void update();
|
||||
void setTarget(const MotionFrame& frame);
|
||||
void setConfigs(int minP, int maxP, int interval);
|
||||
|
||||
private:
|
||||
@@ -26,16 +27,10 @@ private:
|
||||
float currentLegF[6][3];
|
||||
float stepSize[6][3];
|
||||
|
||||
// Circular Buffer
|
||||
MotionFrame buffer[10];
|
||||
int head = 0;
|
||||
int tail = 0;
|
||||
|
||||
// Configs
|
||||
int _servoMin = 125;
|
||||
int _servoMax = 625;
|
||||
int _pythonInterval = 50;
|
||||
unsigned long lastUpdate = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
|
||||
#ifndef IMAGE_CARDS_HEARTS_BITS_H
|
||||
#define IMAGE_CARDS_HEARTS_BITS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
static const unsigned char PROGMEM image_cards_hearts_bits[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0xc0,0x0f,0xc0,0x0f,0xc0,0x0f,0xc0,0x3f,0xf0,0x3f,0xf0,0x3f,0xf0,0x3f,0xf0,0xff,0xfc,0xff,0xfc,0xff,0xfc,0xff,0xfc,0xff,0xff,0xff,0xfc,0xff,0xff,0xff,0xfc,0xff,0xff,0xff,0xfc,0xff,0xff,0xff,0xfc,0xff,0xff,0xff,0xfc,0xff,0xff,0xff,0xfc,0x3f,0xff,0xff,0xf0,0x3f,0xff,0xff,0xf0,0x0f,0xff,0xff,0xc0,0x0f,0xff,0xff,0xc0,0x03,0xff,0xff,0x00,0x03,0xff,0xff,0x00,0x00,0xff,0xfc,0x00,0x00,0xff,0xfc,0x00,0x00,0x3f,0xf0,0x00,0x00,0x3f,0xf0,0x00,0x00,0x0f,0xc0,0x00,0x00,0x0f,0xc0,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
|
||||
#endif // IMAGE_CARDS_HEARTS_BITS_H
|
||||
File diff suppressed because one or more lines are too long
+22
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user