123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- TaskHandle_t primaryTask;
- TaskHandle_t secondaryTask;
- TaskHandle_t animationTask;
- SemaphoreHandle_t animationMutex;
- void createSemaphore() {
- animationMutex = xSemaphoreCreateMutex();
- xSemaphoreGive(animationMutex);
- }
- void mutexLock() {
- xSemaphoreTake(animationMutex, portMAX_DELAY);
- }
- void mutexUnlock() {
- xSemaphoreGive(animationMutex);
- }
- void setAnimationCode(char filename) {
- Serial.println("Updating animation frame to " + String(filename));
- mutexLock();
- animation = filename;
- mutexUnlock();
- }
- char getAnimationCode() {
- char anicode = 'a';
- mutexLock();
- anicode = animation;
- mutexUnlock();
- return anicode;
- }
- bool getSwitchState() {
- int switchState = digitalRead(TOGGLE_SWITCH);
- return (switchState == 1);
- }
- bool initPrimaryLogicNetworkMode() {
- clearFrame();
- bool switchPushed = getSwitchState();
- if (switchPushed || ENABLE_WIFI_DEBUG) {
-
-
- if (ENABLE_WIFI_DEBUG) {
- setAnimationCode('x');
-
- WiFi.mode(WIFI_STA);
- WiFi.begin(DEBUG_SSID, DEBUG_PWD);
- Serial.println("\nConnecting");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(100);
- }
- Serial.println("\nConnected to the WiFi network");
- Serial.print("Local IP: ");
- Serial.println(WiFi.localIP());
- setAnimationCode('w');
- } else {
-
- setAnimationCode('w');
- WiFi.softAP(AP_SSID, NULL);
- Serial.print("Manual mode started. SSID=" + String(AP_SSID) + " listening on : ");
- Serial.println(WiFi.softAPIP());
-
- dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
- dnsServer.start(53, "*", WiFi.softAPIP());
- }
-
- registerAPIEndpoints();
- server.begin();
- return true;
- }
- return false;
- }
- void startCoreTasks() {
-
- xTaskCreatePinnedToCore(
- AnimationController,
- "animator",
- 8192,
- NULL,
- 2,
- &animationTask,
- 0);
-
- xTaskCreatePinnedToCore(
- PrimaryController,
- "primary",
- 24576,
- NULL,
- 1,
- &primaryTask,
- 1);
-
- xTaskCreate(
- SecondaryController,
- "secondary",
- 2048,
- NULL,
- 3,
- &secondaryTask
- );
-
- vTaskStartScheduler();
- }
- void SecondaryController(void* pvParameters) {
- Serial.println("Secondary logic process started on core " + String(xPortGetCoreID()));
- while (1) {
- if (movingActive) {
- if (movingDirection == 0) {
- forward(1);
- } else if (movingDirection == 1) {
- backward(1);
- } else if (movingDirection == 2) {
- rotateAntiClockwise(1);
- } else if (movingDirection == 3) {
- rotateClockwise(1);
- } else {
-
- delay(100);
- return;
- }
- } else {
-
- delay(100);
- }
- }
- }
- void PrimaryController(void* pvParameters) {
- Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
- if (!initPrimaryLogicNetworkMode()) {
-
- int seqCounter = 0;
- bool switchPushed = false;
- while (1) {
- switchPushed = getSwitchState();
- if (switchPushed) {
-
- executePushAnimationSequence(seqCounter);
- seqCounter++;
- }
- delay(1);
- }
- } else {
- while (1) {
- dnsServer.processNextRequest();
- delay(1);
- }
- }
- }
- void AnimationController(void* pvParameters) {
- Serial.println("Animation render started on core " + String(xPortGetCoreID()));
- while (1) {
- char anicode = getAnimationCode();
- handleAnimationRendering(anicode);
- delay(1);
- }
- }
|