- Updated KVStorageHandler interface to use std::string instead of char* for key-value operations. - Enhanced NVSStorageHandler to utilize ESP_LOG for error and info messages instead of printf. - Refactored WifiHandler to manage WiFi credentials using JSON format for better structure and storage. - Replaced raw pointers with std::unique_ptr in WifiHandler and NetworkHandler for automatic memory management. - Removed unused TouchHandler and EInkTouchHandler classes to clean up the codebase. - Adjusted CMakeLists.txt to remove unnecessary include directories. - Updated lv_conf.h to enable FreeRTOS and gesture recognition features.
181 lines
5.6 KiB
C++
181 lines
5.6 KiB
C++
#include <stdio.h>
|
|
#include <inttypes.h>
|
|
#include <stdexcept>
|
|
#include "sdkconfig.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_chip_info.h"
|
|
#include "esp_flash.h"
|
|
#include "esp_system.h"
|
|
#include "esp_log.h"
|
|
|
|
//
|
|
#include "common/constants.h"
|
|
#include "common/queue_defs.h"
|
|
#include "io/nvs_handler.h"
|
|
#include "info/info.h"
|
|
#include "display/display.h"
|
|
#include <tick/lv_tick.h>
|
|
#include "network.h"
|
|
|
|
// nvs storage namespaces, 15 characters max
|
|
#define DEFAULT_STORAGE_NAMESPACE "storage"
|
|
#define WIFI_CREDENTIALS_STORAGE_NAMESPACE "wifi_cred"
|
|
#define TAG "Main"
|
|
|
|
extern "C" void app_main(void);
|
|
|
|
void init_queues(
|
|
QueueHandle_t& touch_queue,
|
|
EventGroupHandle_t& system_event_group,
|
|
EventGroupHandle_t& system_lifecycle_event_group
|
|
);
|
|
|
|
|
|
void app_main(void) {
|
|
display_chip_info();
|
|
|
|
QueueHandle_t touch_event_queue = NULL;
|
|
EventGroupHandle_t system_event_group = NULL, system_lifecycle_event_group = NULL;
|
|
|
|
init_queues(touch_event_queue, system_event_group, system_lifecycle_event_group);
|
|
if (touch_event_queue == NULL || system_event_group == NULL || system_lifecycle_event_group == NULL) {
|
|
ESP_LOGE("Main", "Failed to create one or more queues/event groups");
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
return esp_restart();
|
|
}
|
|
ESP_LOGI(TAG, "Queues initialized.\n");
|
|
SemaphoreHandle_t lvgl_mutex = xSemaphoreCreateMutex();
|
|
if (lvgl_mutex == NULL) {
|
|
ESP_LOGE("Main", "Failed to create LVGL mutex");
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
return esp_restart();
|
|
}
|
|
//
|
|
KVStorageHandler* kv_storage_handler = new NVSStorageHandler(
|
|
DEFAULT_STORAGE_NAMESPACE
|
|
);
|
|
|
|
auto wifi_handler = std::make_unique<WifiHandler>(
|
|
std::unique_ptr<KVStorageHandler>(new NVSStorageHandler(WIFI_CREDENTIALS_STORAGE_NAMESPACE))
|
|
);
|
|
NetworkHandler* network_handler = new NetworkHandler(std::move(wifi_handler));
|
|
// DisplayHandler* display_handler = new EInkDisplayHandler(touch_event_queue, lvgl_mutex);
|
|
//
|
|
kv_storage_handler->init(system_event_group);
|
|
network_handler->init(system_event_group);
|
|
|
|
|
|
// display_handler->init(system_event_group);
|
|
//
|
|
// LVGL tick timer
|
|
auto lvgl_tick_timer_callback = [](TimerHandle_t xTimer) {
|
|
lv_tick_inc(5);
|
|
};
|
|
TickType_t lvgl_tick_period = pdMS_TO_TICKS(5);
|
|
if (lvgl_tick_period == 0) {
|
|
lvgl_tick_period = 1; // ensure at least 1 tick to avoid FreeRTOS assert
|
|
}
|
|
TimerHandle_t lvgl_tick_timer = xTimerCreate(
|
|
"lvgl_tick_timer",
|
|
lvgl_tick_period,
|
|
pdTRUE,
|
|
NULL,
|
|
lvgl_tick_timer_callback
|
|
);
|
|
if (lvgl_tick_timer == NULL) {
|
|
ESP_LOGE("Main", "Failed to create LVGL tick timer");
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
return esp_restart();
|
|
}
|
|
xTimerStart(lvgl_tick_timer, 0);
|
|
|
|
//
|
|
ESP_LOGI(TAG, "Waiting for system to be ready...\n");
|
|
xEventGroupWaitBits(
|
|
system_event_group,
|
|
// DISPLAY_READY_BIT | TOUCH_CALIBRATED_BIT |
|
|
STORAGE_READY_BIT | NETWORK_READY_BIT,
|
|
// do not clear on exit, require explicit reset
|
|
pdFALSE,
|
|
pdTRUE,
|
|
portMAX_DELAY
|
|
);
|
|
ESP_LOGI(TAG, "System is ready. Starting main application...\n");
|
|
// starting event loops
|
|
// display_handler->start_event_loop();
|
|
// wait for shutdown signal
|
|
ESP_LOGI(TAG, "Waiting for shutdown signal...\n");
|
|
EventBits_t bits = xEventGroupWaitBits(
|
|
system_lifecycle_event_group,
|
|
SYSTEM_SHUTDOWN_BIT | SYSTEM_RESTART_BIT,
|
|
// do not clear on exit, require explicit reset
|
|
pdFALSE,
|
|
pdFALSE,
|
|
portMAX_DELAY
|
|
);
|
|
ESP_LOGI(TAG, "Shutdown signal received. Cleaning up...\n");
|
|
|
|
// cleanup
|
|
// shutdown_display_handlerFunc shutdown_display_handler = display_handler->get_shutdown_display_handler();
|
|
// restart_display_handlerFunc restart_display_handler = display_handler->get_restart_display_handler();
|
|
// delete display_handler;
|
|
vSemaphoreDelete(lvgl_mutex);
|
|
vEventGroupDelete(system_event_group);
|
|
vQueueDelete(touch_event_queue);
|
|
ESP_LOGI(TAG, "Cleanup complete.\n");
|
|
|
|
// handle shutdown or restart
|
|
if (bits & SYSTEM_SHUTDOWN_BIT) {
|
|
// if (shutdown_display_handler != nullptr) {
|
|
// ESP_LOGI(TAG, "Calling display shutdown handler...\n");
|
|
// shutdown_display_handler();
|
|
// } else {
|
|
// ESP_LOGI(TAG, "No display shutdown handler to call.\n");
|
|
// }
|
|
ESP_LOGI(TAG, "System is shutting down.\n");
|
|
fflush(stdout);
|
|
// wait for start bit to be set again if future restart is desired, else expect manual power cycle
|
|
EventBits_t bits = xEventGroupWaitBits(
|
|
system_lifecycle_event_group,
|
|
SYSTEM_START_BIT,
|
|
pdFALSE,
|
|
pdFALSE,
|
|
portMAX_DELAY
|
|
);
|
|
if (bits & SYSTEM_START_BIT) {
|
|
ESP_LOGI(TAG, "SYSTEM_START_BIT received, restarting system.\n");
|
|
} else {
|
|
ESP_LOGW(TAG, "No restart signal received, waiting for manual power cycle.\n");
|
|
while (true) {
|
|
vTaskDelay(portMAX_DELAY);
|
|
}
|
|
}
|
|
} else if (bits & SYSTEM_RESTART_BIT) {
|
|
// if (restart_display_handler != nullptr) {
|
|
// ESP_LOGI(TAG, "Calling display restart handler...\n");
|
|
// restart_display_handler();
|
|
// } else {
|
|
// ESP_LOGI(TAG, "No display restart handler to call.\n");
|
|
// }
|
|
ESP_LOGI(TAG, "System is restarting.\n");
|
|
fflush(stdout);
|
|
} else {
|
|
ESP_LOGW(TAG, "Unknown shutdown signal received. Restarting by default.\n");
|
|
fflush(stdout);
|
|
}
|
|
|
|
return esp_restart();
|
|
}
|
|
|
|
void init_queues(
|
|
QueueHandle_t& touch_queue,
|
|
EventGroupHandle_t& system_event_group,
|
|
EventGroupHandle_t& system_lifecycle_event_group
|
|
) {
|
|
// Implementation of queue initialization
|
|
touch_queue = xQueueCreate(10, sizeof(touch_event_t));
|
|
system_event_group = xEventGroupCreate();
|
|
system_lifecycle_event_group = xEventGroupCreate();
|
|
}
|