All functions I want to use are public and works fine with not library voids (wee 4. line).
i tried 3 ways to add my library void but for each I got an error.
The errors are:
.pio\build\esp32dev\src\main.cpp.o:(.literal._Z5setupv+0x4): undefined reference to `TaskExample::defineMyTask()'
.pio\build\esp32dev\src\main.cpp.o:(.literal.startup._GLOBAL__sub_I__ZN4TaskC2EmlPFvvEP9SchedulerbPFbvES1_+0x4): undefined reference to `TaskExample::TaskExample()'
.pio\build\esp32dev\src\main.cpp.o: In function `setup()':
C:\Users\swagner5073\Documents\PlatformIO\Projects\Bonogps/src/main.cpp:8: undefined reference to `TaskExample::defineMyTask()'
.pio\build\esp32dev\src\main.cpp.o: In function `_GLOBAL__sub_I__ZN4TaskC2EmlPFvvEP9SchedulerbPFbvES1_':
C:\Users\swagner5073\Documents\PlatformIO\Projects\Bonogps/src/main.cpp:5: undefined reference to `TaskExample::TaskExample()'
@r1snake,
From the error message, it looks like you're compiling for an ESP32. If so, why are you bothering with the TaskScheduler library? ESP32 runs FreeRTOS. Just use the task control features provided by built-in OS.
#include "certs.h" // include the connection infor for WiFi and MQTT
#include "sdkconfig.h" // used for log printing
#include "esp_system.h"
#include "freertos/FreeRTOS.h" //freeRTOS items to be used
#include "freertos/task.h"
#include <driver/adc.h>
#include <SimpleKalmanFilter.h>
#include <TaskExecute.h>
TaskExecute::TaskExecute() {}
void TaskExecute::defineMyTask()
{
xTaskCreate(
taskOne, /* Task function. */
"TaskOne", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
xTaskCreate(
taskTwo, /* Task function. */
"TaskTwo", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}
void TaskExecute::taskToExecute()
{
}
TaskHandle_t t1 = NULL;
void TaskExecute::taskOne(void *parameter)
{
for (int i = 0; i < 10; i++)
{
Serial.println("Hello from task 1");
delay(1000);
}
Serial.println("Ending task 1");
vTaskDelete(NULL);
}
void TaskExecute::taskTwo(void *parameter)
{
for (int i = 0; i < 10; i++)
{
Serial.println("Hello from task 2");
delay(1000);
}
Serial.println("Ending task 2");
vTaskDelete(NULL);
}
And I got following error:
lib\TaskExecute\TaskExecute.cpp: In member function 'void TaskExecute::defineMyTask()':
lib\TaskExecute\TaskExecute.cpp:13:13: error: invalid use of non-static member function
NULL); /* Task handle. /
^
lib\TaskExecute\TaskExecute.cpp:21:13: error: invalid use of non-static member function
NULL); / Task handle. */
^
*** [.pio\build\az-delivery-devkit-v4\lib6a4\TaskExecute\TaskExecute.cpp.o] Error 1
You can't use a pointer to a member function in place of a pointer to a regular function. They have different signatures. Read about it here: Standard C++
If you must have the task code be part of the class, try this:
When using member functions it's much easier to just use the C++ std::thread API, which supports executing member functions directly, without casting back and forth to void pointers:
The default stack size is rather small, if you need a bigger stack, call esp_pthread_set_cfg before starting the threads.
When using threads, you have to watch out for data races. In general, never write to data that is shared between threads without using some kind of synchronization primitive such as an std::mutex, std::condition_variable, std::atomic, FreeRTOS queues, etc.
Yes, they're just a layer on top of the pthreads and FreeRTOS threads. IIRC, the std::thread class only contains a handle to the underlying pthread. (sizeof(std::thread) == 4)