Add uart templates, send and receive tasks with templates work

both boards compile and send/receive example data using new templates in
common uart code
common/uart_common.hpp
This commit is contained in:
jonny_jr9
2023-08-29 11:11:29 +02:00
parent 76e8bac113
commit 3722b0af74
15 changed files with 854 additions and 305 deletions

View File

@@ -12,127 +12,138 @@ extern "C"
#include "esp_spiffs.h"
//custom C files
//#include "wifi.h"
//custom C files
//#include "wifi.h"
}
//custom C++ files
//#include "config.hpp"
//#include "control.hpp"
//#include "button.hpp"
//#include "http.hpp"
#include "uart.hpp"
//=========================
//======= UART TEST =======
//=========================
//only run uart test code at the end
//disables other functionality
#define UART_TEST_ONLY
//tag for logging
static const char * TAG = "main";
#ifndef UART_TEST_ONLY
//custom C++ files
#include "config.hpp"
#include "control.hpp"
#include "button.hpp"
#include "http.hpp"
// //======================================
// //============ buzzer task =============
// //======================================
// //TODO: move the task creation to buzzer class (buzzer.cpp)
// //e.g. only have function buzzer.createTask() in app_main
// void task_buzzer( void * pvParameters ){
// ESP_LOGI("task_buzzer", "Start of buzzer task...");
// //run function that waits for a beep events to arrive in the queue
// //and processes them
// buzzer.processQueue();
// }
//
//
//
// //=======================================
// //============ control task =============
// //=======================================
// //task that controls the armchair modes and initiates commands generation and applies them to driver
// void task_control( void * pvParameters ){
// ESP_LOGI(TAG, "Initializing controlledArmchair and starting handle loop");
// //start handle loop (control object declared in config.hpp)
// control.startHandleLoop();
// }
//
//
//
// //======================================
// //============ button task =============
// //======================================
// //task that handles the button interface/commands
// void task_button( void * pvParameters ){
// ESP_LOGI(TAG, "Initializing command-button and starting handle loop");
// //create button instance
// buttonCommands commandButton(&buttonJoystick, &joystick, &control, &buzzer, &motorLeft, &motorRight);
// //start handle loop
// commandButton.startHandleLoop();
// }
//
//
//
// //=======================================
// //============== fan task ===============
// //=======================================
// //task that controlls fans for cooling the drivers
// void task_fans( void * pvParameters ){
// ESP_LOGI(TAG, "Initializing fans and starting fan handle loop");
// //create fan instances with config defined in config.cpp
// controlledFan fan(configCooling, &motorLeft, &motorRight);
// //repeatedly run fan handle function in a slow loop
// while(1){
// fan.handle();
// vTaskDelay(500 / portTICK_PERIOD_MS);
// }
// }
//
//
//
// //=================================
// //========== init spiffs ==========
// //=================================
// //initialize spi flash filesystem (used for webserver)
// void init_spiffs(){
// ESP_LOGI(TAG, "init spiffs");
// esp_vfs_spiffs_conf_t esp_vfs_spiffs_conf = {
// .base_path = "/spiffs",
// .partition_label = NULL,
// .max_files = 5,
// .format_if_mount_failed = true};
// esp_vfs_spiffs_register(&esp_vfs_spiffs_conf);
//
// size_t total = 0;
// size_t used = 0;
// esp_spiffs_info(NULL, &total, &used);
//
// ESP_LOGI(TAG, "SPIFFS: total %d, used %d", total, used);
// esp_vfs_spiffs_unregister(NULL);
// }
//
//
//
// //==================================
// //======== define loglevels ========
// //==================================
// void setLoglevels(void){
// //set loglevel for all tags:
// esp_log_level_set("*", ESP_LOG_WARN);
//
// //--- set loglevel for individual tags ---
// esp_log_level_set("main", ESP_LOG_INFO);
// esp_log_level_set("buzzer", ESP_LOG_ERROR);
// //esp_log_level_set("motordriver", ESP_LOG_INFO);
// //esp_log_level_set("motor-control", ESP_LOG_DEBUG);
// //esp_log_level_set("evaluatedJoystick", ESP_LOG_DEBUG);
// //esp_log_level_set("joystickCommands", ESP_LOG_DEBUG);
// esp_log_level_set("button", ESP_LOG_INFO);
// esp_log_level_set("control", ESP_LOG_INFO);
// esp_log_level_set("fan-control", ESP_LOG_INFO);
// esp_log_level_set("wifi", ESP_LOG_INFO);
// esp_log_level_set("http", ESP_LOG_INFO);
// esp_log_level_set("automatedArmchair", ESP_LOG_DEBUG);
// //esp_log_level_set("current-sensors", ESP_LOG_INFO);
// }
//======================================
//============ buzzer task =============
//======================================
//TODO: move the task creation to buzzer class (buzzer.cpp)
//e.g. only have function buzzer.createTask() in app_main
void task_buzzer( void * pvParameters ){
ESP_LOGI("task_buzzer", "Start of buzzer task...");
//run function that waits for a beep events to arrive in the queue
//and processes them
buzzer.processQueue();
}
//=======================================
//============ control task =============
//=======================================
//task that controls the armchair modes and initiates commands generation and applies them to driver
void task_control( void * pvParameters ){
ESP_LOGI(TAG, "Initializing controlledArmchair and starting handle loop");
//start handle loop (control object declared in config.hpp)
control.startHandleLoop();
}
//======================================
//============ button task =============
//======================================
//task that handles the button interface/commands
void task_button( void * pvParameters ){
ESP_LOGI(TAG, "Initializing command-button and starting handle loop");
//create button instance
buttonCommands commandButton(&buttonJoystick, &joystick, &control, &buzzer, &motorLeft, &motorRight);
//start handle loop
commandButton.startHandleLoop();
}
//=======================================
//============== fan task ===============
//=======================================
//task that controlls fans for cooling the drivers
void task_fans( void * pvParameters ){
ESP_LOGI(TAG, "Initializing fans and starting fan handle loop");
//create fan instances with config defined in config.cpp
controlledFan fan(configCooling, &motorLeft, &motorRight);
//repeatedly run fan handle function in a slow loop
while(1){
fan.handle();
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
//=================================
//========== init spiffs ==========
//=================================
//initialize spi flash filesystem (used for webserver)
void init_spiffs(){
ESP_LOGI(TAG, "init spiffs");
esp_vfs_spiffs_conf_t esp_vfs_spiffs_conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true};
esp_vfs_spiffs_register(&esp_vfs_spiffs_conf);
size_t total = 0;
size_t used = 0;
esp_spiffs_info(NULL, &total, &used);
ESP_LOGI(TAG, "SPIFFS: total %d, used %d", total, used);
esp_vfs_spiffs_unregister(NULL);
}
#endif
//==================================
//======== define loglevels ========
//==================================
void setLoglevels(void){
//set loglevel for all tags:
esp_log_level_set("*", ESP_LOG_WARN);
//--- set loglevel for individual tags ---
esp_log_level_set("main", ESP_LOG_INFO);
esp_log_level_set("buzzer", ESP_LOG_ERROR);
//esp_log_level_set("motordriver", ESP_LOG_INFO);
//esp_log_level_set("motor-control", ESP_LOG_DEBUG);
//esp_log_level_set("evaluatedJoystick", ESP_LOG_DEBUG);
//esp_log_level_set("joystickCommands", ESP_LOG_DEBUG);
esp_log_level_set("button", ESP_LOG_INFO);
esp_log_level_set("control", ESP_LOG_INFO);
esp_log_level_set("fan-control", ESP_LOG_INFO);
esp_log_level_set("wifi", ESP_LOG_INFO);
esp_log_level_set("http", ESP_LOG_INFO);
esp_log_level_set("automatedArmchair", ESP_LOG_DEBUG);
//esp_log_level_set("current-sensors", ESP_LOG_INFO);
}
@@ -143,81 +154,77 @@ static const char * TAG = "main";
//=========== app_main ============
//=================================
extern "C" void app_main(void) {
// //enable 5V volate regulator
// gpio_pad_select_gpio(GPIO_NUM_17);
// gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
// gpio_set_level(GPIO_NUM_17, 1);
//
// //---- define log levels ----
// setLoglevels();
//
// //------------------------------
// //--- create task for buzzer ---
// //------------------------------
// xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
//
// //-------------------------------
// //--- create task for control ---
// //-------------------------------
// //task that generates motor commands depending on the current mode and sends those to motorctl task
// xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL);
//
// //------------------------------
// //--- create task for button ---
// //------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL);
//
// //-----------------------------------
// //--- create task for fan control ---
// //-----------------------------------
// //task that evaluates and processes the button input and runs the configured commands
// xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL);
//
//
// //beep at startup
// buzzer.beep(3, 70, 50);
//
// //--- initialize nvs-flash and netif (needed for wifi) ---
// wifi_initNvs_initNetif();
//
// //--- initialize spiffs ---
// init_spiffs();
//
// //--- initialize and start wifi ---
// //FIXME: run wifi_init_client or wifi_init_ap as intended from control.cpp when switching state
// //currently commented out because of error "assert failed: xQueueSemaphoreTake queue.c:1549 (pxQueue->uxItemSize == 0)" when calling control->changeMode from button.cpp
// //when calling control.changeMode(http) from main.cpp it worked without error for some reason?
// ESP_LOGI(TAG,"starting wifi...");
// //wifi_init_client(); //connect to existing wifi
// wifi_init_ap(); //start access point
// ESP_LOGI(TAG,"done starting wifi");
//
//
// //--- testing http server ---
// // wifi_init_client(); //connect to existing wifi
// // vTaskDelay(2000 / portTICK_PERIOD_MS);
// // ESP_LOGI(TAG, "initializing http server");
// // http_init_server();
//
//
// //--- testing force http mode after startup ---
// //control.changeMode(controlMode_t::HTTP);
//
//
// //--- main loop ---
// //does nothing except for testing things
#ifndef UART_TEST_ONLY
//enable 5V volate regulator
gpio_pad_select_gpio(GPIO_NUM_17);
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_17, 1);
//---- define log levels ----
setLoglevels();
//------------------------------
//--- create task for buzzer ---
//------------------------------
xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
//-------------------------------
//--- create task for control ---
//-------------------------------
//task that generates motor commands depending on the current mode and sends those to motorctl task
xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL);
//------------------------------
//--- create task for button ---
//------------------------------
//task that evaluates and processes the button input and runs the configured commands
xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL);
//-----------------------------------
//--- create task for fan control ---
//-----------------------------------
//task that evaluates and processes the button input and runs the configured commands
xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL);
//TESTING UART
//xTaskCreate(uart_task_testing, "uart_task", 4096, NULL, 10, NULL);
//beep at startup
buzzer.beep(3, 70, 50);
//--- initialize nvs-flash and netif (needed for wifi) ---
wifi_initNvs_initNetif();
//--- initialize spiffs ---
init_spiffs();
//--- initialize and start wifi ---
//FIXME: run wifi_init_client or wifi_init_ap as intended from control.cpp when switching state
//currently commented out because of error "assert failed: xQueueSemaphoreTake queue.c:1549 (pxQueue->uxItemSize == 0)" when calling control->changeMode from button.cpp
//when calling control.changeMode(http) from main.cpp it worked without error for some reason?
ESP_LOGI(TAG,"starting wifi...");
//wifi_init_client(); //connect to existing wifi
wifi_init_ap(); //start access point
ESP_LOGI(TAG,"done starting wifi");
//--- testing http server ---
// wifi_init_client(); //connect to existing wifi
// vTaskDelay(2000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "initializing http server");
// http_init_server();
//--- testing force http mode after startup ---
//control.changeMode(controlMode_t::HTTP);
#endif
uart_init();
xTaskCreate(task_uartReceive, "task_uartReceive", 4096, NULL, 10, NULL);
xTaskCreate(task_uartSend, "task_uartSend", 5*4096, NULL, 10, NULL);
xTaskCreate(task_uartReceive, "task_uartReceive", 4096, NULL, 10, NULL);
xTaskCreate(task_uartSend, "task_uartSend", 4096, NULL, 10, NULL);
//--- main loop ---
//does nothing except for testing things
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
//---------------------------------
//-------- TESTING section --------
//---------------------------------

View File

@@ -21,43 +21,11 @@ static const char * TAG = "uart";
void uart_task_testing(void *arg){
//repeatedly send 8 bit count and log received 1 byte
uint8_t *data = (uint8_t *) malloc(1024);
uint8_t count = 0;
ESP_LOGW(TAG, "startloop...");
while (1) {
vTaskDelay(500 / portTICK_PERIOD_MS);
int len = uart_read_bytes(UART_NUM_1, data, (1024 - 1), 20 / portTICK_PERIOD_MS);
//uart_flush_input(UART_NUM_1);
//uart_flush(UART_NUM_1);
ESP_LOGW(TAG, "received len=%d data=%d", len, *data);
*data = 99; //set to 99 (indicates no new data received)
uart_write_bytes(UART_NUM_1, (const char *) &count, 1);
ESP_LOGW(TAG, "sent data %d", count);
count++;
}
}
//==============================
//====== task_uartReceive ======
//==============================
//TODO copy receive task from board_motorctl/uart.cpp
void task_uartReceive(void *arg){
static const char * TAG = "uart-receive";
//repeatedly send 8 bit count and log received 1 byte
char *data = (char *) malloc(1024);
char count = 0;
ESP_LOGW(TAG, "startloop...");
while (1) {
vTaskDelay(200 / portTICK_PERIOD_MS);
int len = uart_read_bytes(UART_NUM_1, data, (1024 - 1), 20 / portTICK_PERIOD_MS);
if (len>0) ESP_LOGW(TAG, "received len=%d data=%d", len, *data);
}
}
void task_uartReceiveQueue(void *arg){
static const char * TAG = "uart-receive";
while (1) {
vTaskDelay(200 / portTICK_PERIOD_MS);
}
@@ -65,38 +33,17 @@ void task_uartReceiveQueue(void *arg){
// //send incrementing count
// void task_uartSend(void *arg){
// static const char * TAG = "uart-send";
// //repeatedly send 8 bit count and log received 1 byte
// char *data = (char *) malloc(1024);
// char count = 0;
// ESP_LOGW(TAG, "startloop...");
// while (1) {
// vTaskDelay(200 / portTICK_PERIOD_MS);
// uart_write_bytes(UART_NUM_1, (const char *) &count, 1);
// ESP_LOGW(TAG, "sent data %d", (int)count);
// count++;
// }
// ESP_LOGE(TAG, "loop exit...");
// }
//send struct
//=============================
//======= task_uartSend =======
//=============================
//repeatedly send structs to uart
void task_uartSend(void *arg){
static const char * TAG = "uart-send";
uartDataStruct data = {123, 0, 1.1};
uint8_t serialData[sizeof(uartDataStruct)];
char count = 0;
uartData_test_t data = {123, 0, 1.1};
ESP_LOGW(TAG, "startloop...");
while (1) {
vTaskDelay(500 / portTICK_PERIOD_MS);
memcpy(serialData, &data, sizeof(uartDataStruct));
uart_write_bytes(UART_NUM_1, (const char *)serialData, sizeof(uartDataStruct));
ESP_LOGW(TAG, "sent data struct with len %d", sizeof(uartDataStruct));
ESP_LOGW(TAG, "sent DATA: timestamp=%d, id=%d, value=%.1f", data.timestamp, data.id, data.value);
uart_sendStruct<uartData_test_t>(data);
//change data values
data.timestamp = esp_log_timestamp();

View File

@@ -8,8 +8,3 @@ void task_uartSend(void *arg);
typedef struct {
uint32_t timestamp;
int id;
float value;
} uartDataStruct;