追蹤者

2019年7月16日 星期二

ESP32 雙核程式的寫法

ESP32 雙核程式的寫法
https://randomnerdtutorials.com/esp32-dual-core-arduino-ide/

RAW CODES
https://raw.githubusercontent.com/RuiSantosdotme/ESP32-Course/master/code/Dual_Core/Dual_Core_Blinking_LEDs/Dual_Core_Blinking_LEDs.ino


How the Code Works

TaskHandle_t Task1;
TaskHandle_t Task2;

In the setup()
Serial.begin(115200);

Then, create Task1 using the xTaskCreatePinnedToCore() function:
xTaskCreatePinnedToCore(
             Task1code, /* Task function. */
             "Task1",   /* name of task. */
             10000,     /* Stack size of task */
             NULL,      /* parameter of the task */
             1,         /* priority of the task */
             &Task1,    /* Task handle to keep track of created task */
             0);        /* pin task to core 0 */
Task1 will be implemented with the Task1code() function. So, we need to create that function later on the code. We give the task priority 1, and pinned it to core 0.
 Create Task2 
xTaskCreatePinnedToCore(
             Task2code,  /* Task function. */
             "Task2",    /* name of task. */
             10000,      /* Stack size of task */
             NULL,       /* parameter of the task */
             1,          /* priority of the task */
             &Task2,     /* Task handle to keep track of created task */
             1);         /* pin task to core 0 */
After creating the tasks, we need to create the functions 
void Task1code( void * pvParameters ){
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;){
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
  }
}
The function to Task1 is called Task1code() (you can call it whatever you want). For debugging purposes, we first print the core in which the task is running:
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
Then, we have an infinite loop similar to the loop() on the Arduino sketch. In that loop, we blink LED1 every one second.
The same thing happens for Task2, but we blink the LED with a different delay time.
void Task2code( void * pvParameters ){
  Serial.print("Task2 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;){
    digitalWrite(led2, HIGH);
    delay(700);
    digitalWrite(led2, LOW);
    delay(700);
  }
}
 the loop() function is empty:
void loop() { }
Note: as mentioned previously, the Arduino loop() runs on core 1. So, instead of creating a task to run on core 1, you can simply write your code inside the loop().


Oled 96x64 color SPI
Adafruit-SSD1331-OLED-Driver-Library-for-Arduino
Where to buy
https://m.ruten.com.tw
Wiring and program
rgb-oled-0-95-96x64-pixels-16-bit-color-oled-spi-interface

TIME slice multi_tasking