Arduino is an open-source platform for creating and controlling interactive electronic objects. It consists of a hardware board, a software IDE, and a community of users and developers. Arduino supports various programming languages, libraries, and sensors. Arduino is ideal for beginners, hobbyists, and professionals who want to explore electronics and coding.
Arduino-ESP32 supports FreeRTOS, an open-source real-time operating system (RTOS) for microcontrollers and small microprocessors. It provides a kernel and libraries for connectivity, security, and over-the-air updates.
The ArduProf library provides a thin layer framework that makes it easier for developers to code inter-thread communication by an event driven method. By using the ArduProf framework, developers de-coupling different features into separate threads. All threads (including Arduino thread) are communicated by message format, as below:
typedef struct _Message
{
int16_t event;
int16_t iParam;
uint16_t uParam;
uint32_t lParam;
} Message;
Software setup for ESP32, ESP32-S3, ESP32C3- Install [Arduino IDE 2.2+ for Arduino] (https://www.arduino.cc/en/Main/Software)
- Install [Arduino-ESP32] (https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html)
- Install [Arduino DebugLog] (https://www.arduino.cc/reference/en/libraries/debuglog/)
- Install [ArduinoJson] (https://github.com/bblanchon/ArduinoJson)
- Install [ArduProf] (https://www.arduino.cc/reference/en/libraries/arduprof/)
- launch the Arduino IDE
- create a new project of example "basic" by clicking Menu -> "File" -> "Examples" -> "ArduProf" -> "basic"
- select ESP32C3 Dev Module by clicking Menu -> “Tools” -> “Select Board” -> "ESP32C3 Dev Module"
- config ESP32C3 Dev Module settings as below
- build the code by clicking Menu -> “Sketch” -> “Compile/Verify”If everything goes smoothly, you should see the following screen.
- select the ESP32C3 virtual port by clicking Menu -> “Tools” -> “Port” ->...
- build the code by clicking Menu -> “Sketch” -> “Upload”If everything goes smoothly, you should see the following screen.
Serial monitor log output
The first step to use ArduProf framework is including the "ArduProf.h" header file. Then defines "queueMain" and "threadApp" and starts them. Posting an event "EventNull" from "queueMain" to "threadApp" is simply as "queueMain.postEvent(context.threadApp, EventNull)" Sample code is listed in file "basic.ino"
#include <ArduProf.h>
...
// define variable "queueMain" for Arduino thred to interact with ArduProf framework.
static QueueMain queueMain;
// define variable "threadApp" for application thread. (Define other thread as you need)
static ThreadApp threadApp;
// define variable "context" and initialize pointers to "queueMain" and "threadApp"
static AppContext context = {
.queueMain = &queueMain,
.threadApp = &threadApp,
};
void setup()
{
...
// initialize queueMain
queueMain.start(&context);
// start threadApp
threadApp.start(&context);
}
void loop()
{
...
// delay 1s
delay(1000);
// post an event "EventNull" to threadApp
queueMain.postEvent(context.threadApp, EventNull);
}
Example 2: handling event "EventNull" in ThreadApp.There are 3 steps to handle an event. They are:
- declare event handler
- define event handler
- setup event handler (map event to handler)
Sample code of declare event handler in "ThreadApp.h"
class ThreadApp : public ThreadBase
{
...
///////////////////////////////////////////////////////////////////////
// declare event handler
///////////////////////////////////////////////////////////////////////
__EVENT_FUNC_DECLARATION(EventNull) // void handlerEventNull(const Message &msg);
}
Sample code of define event handler in "ThreadApp.cpp"
// define EventNull handler
__EVENT_FUNC_DEFINITION(QueueMain, EventNull, msg) // void QueueMain::handlerEventNull(const Message &msg)
{
LOG_TRACE("EventNull(", msg.event, "), iParam=", msg.iParam, ", uParam=", msg.uParam, ", lParam=", msg.lParam);
}
Sample code of setup event handler (map event to handler) in "ThreadApp.cpp"
ThreadApp::ThreadApp() : ...
{
...
// setup event handlers
handlerMap = {
__EVENT_MAP(ThreadApp, EventNull), // {EventNull, &ThreadApp::handlerEventNull},
};
}
,
Please refer to https://www.arduino.cc/reference/en/libraries/arduprof/ for more information.
Comments
Please log in or sign up to comment.