2023年7月6日 星期四

Arduino IDE 2 + ESP32 multiTask sample -- LED Section

Porpose:

The ESP32 comes with 2 Xtensa 32-bit LX6 microprocessors: core 0 and core 1. So, it is dual core. When we run code on Arduino IDE, by default, it runs on core 1. In this post we’ll show you how to run code on the ESP32 second core by creating tasks. You can run pieces of code simultaneously on both cores, and make your ESP32 multitasking.

Introduction:

The ESP32 comes with 2 Xtensa 32-bit LX6 microprocessors, so it’s dual core:

Core 0

Core 1

Some features to remember:

xTaskCreate() to create a task

xTaskCreatePinnedToCore() to create a task on a particular core

vTaskDelayUntil() to activate a task periodically

vTaskDelay() blocks a task for a certain number of clock ticks (uses pdMS_TO_TICKS to convert a duration into a number of ticks)

vTaskPrioritySet() changes the priority of a task

vTaskDelete() to delete a task

main.cpp

#include "main.h"

//-------------------------------------------------
void setup()
{
  Serial.begin(9600);

  Serial.println(F("init"));

  #ifdef ENABLE_LEDTASK
  initledTask();
  #endif

}

void loop()
{
  vTaskDelay(200);
  Serial.print(F("Main at core:"));
  Serial.println(xPortGetCoreID());

  while(1)
  {
    vTaskDelay(200);
  }

}

main.h

#ifndef __MAIN_H
#define __MAIN_H

#include <Arduino.h>

#include "configuration.h"
#include "public_structure.h"

// led Task stuff
#define ENABLE_LEDTASK
void initledTask(void);

//-----------------------------------------
#endif

ledtask.cpp

#include "main.h"
#include "ledTask.h"
#include "public_structure.h"

#ifdef ENABLE_LEDTASK
/** Forward dedclaration of the task handling LED */
TaskHandle_t hled;  //TaskHandler  -----   main extern

void ledTask(void *pvParameters);

int Led::addTwoInts(int a, int b)
{
  return a + b;
}

Led::Led(byte pin)
{
  this->pin = pin;
  init();
}

void Led::init()
{
  pinMode(pin, OUTPUT);
  off();
}

void Led::on()
{
  digitalWrite(pin, HIGH);
}

void Led::off()
{
  digitalWrite(pin, LOW);
}

Led led(LED_PIN);  //---need to using

void initledTask(void)
{
  // Create the task for the led flash
  xTaskCreatePinnedToCore(
    ledTask, "LED Task" // A name just for humans
    ,
    1024 // This stack size can be checked & adjusted by reading the Stack Highwater
    ,
    NULL, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,
    &hled //handle
    ,
    0);

  // Check the results
  if (hled == NULL)
  {
    Serial.println("Create Led task failed");
  }
  else
  {
    Serial.println("Led task up and running");
  }
}
//------------------------------------------------------------------------------
// Task blinking LED
void ledTask(void *pvParameters)
{
  Serial.print(F("LED Task at core:"));
  Serial.println(xPortGetCoreID());

  while(1)
  {
    led.on();
    //vTaskDelay((150L * configTICK_RATE_HZ) / 1000L);
    vTaskDelay(200);
    led.off();
    //vTaskDelay((150L * configTICK_RATE_HZ) / 1000L);
    vTaskDelay(200);
  }
}

#endif

ledtask.h

#ifndef __LEDTASK_H
#define __LEDTASK_H
#include <Arduino.h>

class Led
{
  private:
    byte pin;
    int a, b;
   
  public:
    // Setup pin LED and call init()
    Led(byte pin);

    // Setup the pin led as OUTPUT
    // and power off the LED - default state
    void init();
   
    // Power on the LED
    void on();

    // Power off the LED
    void off();

    int addTwoInts(int a, int b);
};

#endif

public_structure.h

#ifndef __MESSAGE_H
#define __MESSAGE_H

#include <Arduino.h>
#include "configuration.h"

//----------------------------------------------------------
typedef struct _vMessage_Type
{
  uint8_t messageId;
  //char *messageString;   //不行用會當機
  char messageString[128];
  char FWString[60];
} vMessage_Type;

extern vMessage_Type *message_Ptr;
extern vMessage_Type message;

typedef struct _vFlag
{
  uint8_t BTFlag = 0;
  uint8_t DC_Flag = 0;
  uint8_t CANFlag = 0;
  uint8_t I2C_Flag = 0;
  uint8_t JSONFlag = 0;
  uint8_t Radar_L_Flag = 0;
  uint8_t Radar_R_Flag = 0;
  uint8_t sensor_Flag = 0;
  uint8_t sensor1_Flag = 0;
  uint8_t initial_Flag = 0;
  uint8_t Tone_Flag = -1;
  uint8_t IR_RECV_Flag=0;
  uint8_t IR_SEND_Flag=0;
  uint8_t FunctionFlag = 1;
  uint8_t SendFlag = 0;
} vFlag;

extern vFlag *flag_Ptr;
extern vFlag flag;

//------------------------------------------------------------------------------
#endif

YouTube:





沒有留言:

張貼留言