MeshTalos-Client/main/main.cpp
2025-12-31 15:55:12 +08:00

119 lines
No EOL
2.4 KiB
C++

#include "stdlib.h"
#include "esp_log.h"
#include "Arduino.h"
#include "nvs_flash.h"
#include <mutex>
#include "mpack.h"
#include "ESP32epdx.h"
#include "EPD.h"
#include "mesh_netif.h"
#include "tcphelper.h"
#include "main.h"
std::mutex epd_mtx;
/*
SPI: SCK = 6 ; MISO = 2 ; MOSI = 7 ; SS = 16
EPD: RES = 22 ; DC = 23 ; CS = 1 ; BUSY = 0
*/
uint8_t *image_buf = nullptr;
extern "C" int command_update_image(uint64_t index, char *buf, size_t len) {
if (!epd_mtx.try_lock()) {
return -3; // locked
}
if (
index < 0 || index >= CONFIG_IMAGE_BUF_SIZE ||
len < 0 || len > CONFIG_IMAGE_BUF_SIZE ||
(index+len) < 0 || (index+len) > CONFIG_IMAGE_BUF_SIZE
)
{
epd_mtx.unlock();
return -2;
}
memcpy(image_buf + index, buf, len);
epd_mtx.unlock();
return 0;
}
extern "C" int command_push_image() {
if (epd_mtx.try_lock()) {
EPD_HW_Init_Fast();
EPD_Display((unsigned char *)(image_buf));
EPD_DeepSleep();
epd_mtx.unlock();
return 0;
} else {
return -3;
}
}
extern "C" int tcp_client_callback(request_t *req) {
printf("cmd -> %s\n", req->command);
printf("index -> %llu\n", req->index);
printf("data_len -> %u\n", req->data_len);
// execute command
if (strcmp(req->command, "ping") == 0)
{
return 0;
}
else if (strcmp(req->command, "update_image") == 0)
{
return command_update_image(req->index, req->data, req->data_len);
}
else if (strcmp(req->command, "push_image") == 0)
{
return command_push_image();
}
else
{
return -2;
}
}
extern "C" void app_main() {
initArduino();
#if CONFIG_TCP_ROOT == 0
// init image_buf
image_buf = (uint8_t *)malloc(CONFIG_IMAGE_BUF_SIZE);
if (!image_buf) {
return;
}
memset(image_buf, 0, CONFIG_IMAGE_BUF_SIZE);
#endif
// initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Initialize EPD
EPD_Pin_Init(6, 2, 7, 16,
22, 23, 1, 0);
mesh_main();
// tcp client
tcp_initialize();
// Arduino-like setup()
// Arduino-like loop()
while (true)
{
vTaskDelay(500);
}
// WARNING: if program reaches end of function app_main() the MCU will restart.
}