ESP32: ESP32_xTaskParameter.ino

File ESP32_xTaskParameter.ino, 1.6 KB (added by krit, 3 years ago)
Line 
1#include <Arduino.h>
2
3/** Structure to initialize Pixel task */
4typedef struct
5{
6  int dio_pin;
7  int delay_ms;
8} LED_PIN;
9
10static const char *pcTextForTask1 = "Task 1 is running.";
11static const char *pcTextForTask2 = "Task 2 is running.";
12
13/*
14void vTask(void * pvParameters){
15  char *pcTaskName;
16  pcTaskName = (char*)pvParameters;
17  while(true){
18    Serial.println(pcTaskName);
19    vTaskDelay(1000 / portTICK_PERIOD_MS);
20  }
21}
22*/
23
24void vBlink1Task(void *pvParameters)
25{
26  LED_PIN *config_pin = (LED_PIN *)pvParameters;
27  int pin = config_pin->dio_pin;
28  int delay_t = config_pin->delay_ms;
29  pinMode(pin, OUTPUT);
30  while (true)
31  {
32    digitalWrite(pin, !digitalRead(pin));
33    vTaskDelay(delay_t / portTICK_PERIOD_MS);
34    Serial.println("LED blink");
35  }
36  vTaskDelete(NULL);
37  // we need to free malloc
38  free((LED_PIN *)config_pin);
39}
40void setup()
41{
42
43  Serial.begin(115200);
44  vTaskDelay(5000 / portTICK_PERIOD_MS);
45  Serial.println("\r\n -------- FreeRTOS ESP32_xTaskParameter ----------");
46
47  // We can declare following structure as following
48  //LED_PIN pin_struct;
49  //LED_PIN *config = &pin_struct;
50  // or We can declare following
51  LED_PIN *config = (LED_PIN *)malloc(sizeof(LED_PIN));
52  if (config == NULL)
53  {
54    Serial.println("Cannot allocate memory area to structue");
55  }
56  // end of declare
57
58  Serial.begin(115200);
59  config->dio_pin = 12;
60  config->delay_ms = 3000;
61  //xTaskCreate(vTask, "vTask1", 1024, (void*)pcTextForTask1, tskIDLE_PRIORITY, NULL);
62  xTaskCreate(vBlink1Task, "vBlink1Task", 1024, (void *)config, tskIDLE_PRIORITY + 1, NULL);
63}
64
65void loop() {}