From bfc8cd89bb85d68f4ec7460ff6749742d3fbd6c0 Mon Sep 17 00:00:00 2001 From: EnricoGuccii Date: Sat, 30 May 2026 00:36:57 +0200 Subject: state machine --- src/main.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 891c11d..3ad9107 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,41 +1,112 @@ #include -// #include "esp32-hal.h" #include "motors.h" #include "rgb.h" -#include "buzzer.h" #include "oled.h" #include "animations.h" RGB rgb; -BUZZER buzzer; MOTORS motors; OLED oled; +TaskHandle_t TaskOled; +TaskHandle_t TaskRgb; +TaskHandle_t TaskMotors; + +enum State { + NEUTRAL, + HAPPY, + SAD, + ANGRY +}; + +volatile State currentState = NEUTRAL; + +void oledTask(void * pvParameters) { + for(;;) { + switch(currentState) { + case HAPPY: + oled.showAnimation(animacja1[0], animacja1FrameCount, animacja1Height, animacja1Width, 160); + break; + case SAD: + oled.showAnimation(animacja2[0], animacja2FrameCount, animacja2Height, animacja2Width, 160); + break; + case ANGRY: + oled.showMessage("angry"); + break; + default: + oled.showMessage("neutral"); + break; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + +void rgbTask(void * pvParameters) { + for(;;) { + switch(currentState) { + case HAPPY: + rgb.setColorRGB(255, 250, 0, 100); + rgb.blink(3, 300); + break; + case SAD: + rgb.breathe(3, 40); + break; + case ANGRY: + rgb.setColorRGB(255, 0, 0, 100); + rgb.blink(3, 300); + break; + default: + rgb.setColorRGB(0, 0, 0, true); + break; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + + +void motorsTask(void * pvParameters) { + for(;;) { + switch(currentState) { + case HAPPY: + motors.leftTurn(100,1000); + motors.rightTurn(100,1000); + break; + case SAD: + break; + case ANGRY: + motors.forward(100,1000); + motors.backward(100,1000); + break; + default: + break; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} void setup() { - motors.init(); - rgb.init(); - - rgb.setColorRGB(0, 0, 0, true); oled.init(); - delay(2000); + rgb.init(); + motors.init(); + + xTaskCreate(oledTask, "OLED Task", 4096, NULL, 1, NULL); + xTaskCreate(rgbTask, "RGB Task", 2048, NULL, 1, NULL); + xTaskCreate(motorsTask, "RGB Task", 2048, NULL, 1, NULL); + + currentState = NEUTRAL; } void loop() { - oled.showAnimation(animacja1[0], animacja1FrameCount, animacja1Width, - animacja1Height, 160); - oled.showAnimation(animacja1[0], animacja1FrameCount, animacja1Width + 20, - animacja1Height, 50); // fajny efekt - // oled.showAnimation(animacja2[0], animacja2FrameCount, animacja2Width, - // animacja2Height, 160); - // oled.showAnimation(animacja2[0], animacja2FrameCount, animacja2Width, - // animacja2Height, 80); - rgb.rainbowCycle(2000, 7); - - motors.rightForward(100,4000); - motors.leftForward(100,4000); + currentState = HAPPY; + delay(5000); + currentState = NEUTRAL; + delay(5000); + currentState = ANGRY; + delay(5000); + currentState = SAD; + delay(5000); } -- cgit v1.2.3