diff --git a/JackBotEsp32C3/JackBotEsp32C3.ino b/JackBotEsp32C3/JackBotEsp32C3.ino new file mode 100644 index 0000000..1121b2e --- /dev/null +++ b/JackBotEsp32C3/JackBotEsp32C3.ino @@ -0,0 +1,96 @@ +#include +#include +#include "BotNetwork.h" +#include "MotionController.h" +#include "DisplayManager.h" + +// Initialize Objects +BotNetwork botNetwork("PfLANze", "isteinbisschenengandiehals", "JackBot-AP", "robot-control"); +MotionController motion; +DisplayManager display(8, 20, 21); // 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); + + 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); + } + + 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() { + uint8_t buffer[512]; + int size = botNetwork.readPacket(buffer, 512); + + if (size > 0) { + botNetwork.parsePacket(buffer, size, motionQueue, configQueue, displayQueue); + } + + delay(1); +}