VGottselig / ESP32-RGB-Matrix-Display

Apache License 2.0
65 stars 13 forks source link

alternative with a coreTask (does not block wifi) #6

Closed polygontwist closed 6 years ago

polygontwist commented 6 years ago

i did not get the example to run together with wifi, OTA etc.. timerAttachInterrupt blocks wifi. My solution:

TaskHandle_t xHandleA; //for Stop Task
TaskHandle_t xHandleB; //for Stop Task

void setup() {
//other Stuff
//..
//http://esp-idf.readthedocs.io/en/latest/api-reference/system/freertos.html
//https://techtutorialsx.com/2017/05/09/esp32-running-code-on-a-specific-core/
//http://hit-karlsruhe.de/hit-info/info-ws17/Drehteller_Revisited/0403Softwaredoku.html
//start task on CPU 1
xTaskCreatePinnedToCore(renderTask,"renderTask", 10000,NULL,2 | portPRIVILEGE_BIT,&xHandleA, 1);
xTaskCreatePinnedToCore(drawTask,"drawTask", 10000,NULL,2 | portPRIVILEGE_BIT,&xHandleB, 0);
}

void drawTask( void * pvParameters ){
   while(true){
       String s="Hello";
        matrix.setBrightness(10);//0..10
        matrix.setTextWrap(false);
        matrix.setTextSize(1);
        matrix.black();
        matrix.setCursor(0, 0);
        matrix.setTextColor(matrix.AdafruitColor(0,0,255));
        matrix.println(s);
        matrix.setTextColor(matrix.AdafruitColor(255,0,0));
        matrix.setCursor(16, 10);
        matrix.println(s);

      delay(40);//25fps
}

void renderTask( void * pvParameters ){
   while(true){        //10x pro 1ms
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        matrix.update();
        delay(1);//1 ms 
       // yield(); //block wifi :-/
    }

}

void loop() {//other things
  handleSerial();
  if(wifiaktiv){
    ArduinoOTA.handle();
    server.handleClient();
    handleTime();
  }
}
...

stop Task when upload OTA:

void stoptasks(){
   vTaskDelete(xHandleB);
   vTaskDelete(xHandleA);
   Serial.println("Task stopped");
}
polygontwist commented 6 years ago

ok, the brightness control does not work...

VGottselig commented 6 years ago

Hello @polygontwist, thanks! Will try it out later

polygontwist commented 6 years ago

I tried different (timings) but didn't get it to run without flicker in the task. I'm trying interrupt again: If I stop the interrupt before sending data to the client it sometimes works. My scripts now run in two tasks (xTaskCreatePinnedToCore) at Core 0 (draw matrix; OTA+Wifi+Serialinput+getNTP). Sometimes I get "Guru Meditation Error: Core 1 panic'ed (Cache disabled but cached memory region accessed)". With timerAlarmWrite(displayUpdateTimer, 20, true); it is better - but not the display. I'll keep trying...

polygontwist commented 6 years ago

I've now worked out a solution that seems to be working. Before I process the HTTP request, I stop the timer, and then I create a new one.

bool timeriststopped=false;
void stoppTimer(){
   if(timeriststopped)return;
   timeriststopped=true;
   matrix.black(); //or disable display?
   matrix.update();  

   timerDetachInterrupt(displayUpdateTimer);
   timerStop(displayUpdateTimer);
   timerAlarmDisable(displayUpdateTimer);// 
   timerEnd(displayUpdateTimer);
   displayUpdateTimer=NULL;
}
void startTimer(){
  if(!timeriststopped)return;
  displayUpdateTimer = timerBegin(0, 80, true);//id,divider,countUp, true=edge type 
  timerAttachInterrupt(displayUpdateTimer, &onDisplayUpdate, true);//timer,function,
  timerAlarmWrite(displayUpdateTimer, 2, true);//2  *timer,interruptAt,autoreload
  timerAlarmEnable(displayUpdateTimer);
  timeriststopped=false;
}

edit: add checks

VGottselig commented 6 years ago

Great! I can not test it now

polygontwist commented 6 years ago

I was able to further encircle my problem "Core panic'ed/blocked wifi Task", the task gets stuck when I want to read a character via SPIFFS and the timer is running. If I stop the timer here, everything's okay.

i have upload my Projekt to: https://github.com/polygontwist/ESP32_32x32RGBMatrix/