Files
nanopsx/firmware/src/main.c
T

390 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// File: main.c
#include <string.h>
#include <driver/i2c_types.h>
#include <driver/i2c_master.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "wifi_mgr.h"
#include "sen5x_app.h"
#include "storage.h"
#include <stdio.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "nvs_flash.h"
#include "esp_system.h" // esp_restart
#include "esp_lvgl_port.h"
#include "bsp_display.h"
#include "bsp_touch.h"
#include "bsp_i2c.h"
#include "bsp_spi.h"
#include "bsp_wifi.h"
#include "bsp_sdcard.h"
#include "bsp_battery.h"
#include "bsp_qmi8658.h"
#include "iot_button.h"
#include "button_gpio.h"
#include "demos/lv_demos.h"
#include "ui.h"
#include "i2cdev.h"
#include "sys_status.h"
#include "images.h"
#include "screens.h" // <-- Zugriff auf objects.* und tick_screen()
// Entfernt: extern lv_obj_t *ui_wifi/influx/mdns;
// ---------- Status-Icons updaten: nutzt die in screens.c erzeugten Objekte ----------
static void ui_update_status_icons(void)
{
static bool first = true;
static bool pw=false, pi=false, pm=false;
bool w = sys_status_get_wifi_ok();
bool i = sys_status_get_influx_ok();
bool m = sys_status_get_mdns_ok();
// WiFi
if (first || w != pw) {
if (objects.wifi_ok && objects.wifi_off) {
if (w) {
lv_obj_clear_flag(objects.wifi_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag (objects.wifi_off, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag (objects.wifi_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(objects.wifi_off, LV_OBJ_FLAG_HIDDEN);
}
}
pw = w;
}
// Influx
if (first || i != pi) {
if (objects.influx_ok && objects.influx_off) {
if (i) {
lv_obj_clear_flag(objects.influx_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag (objects.influx_off, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag (objects.influx_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(objects.influx_off, LV_OBJ_FLAG_HIDDEN);
}
}
pi = i;
}
// mDNS
if (first || m != pm) {
if (objects.mdns_ok && objects.mdns_off) {
if (m) {
lv_obj_clear_flag(objects.mdns_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag (objects.mdns_off, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag (objects.mdns_ok, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(objects.mdns_off, LV_OBJ_FLAG_HIDDEN);
}
}
pm = m;
}
first = false;
}
#define EXAMPLE_DISPLAY_ROTATION 270
#if EXAMPLE_DISPLAY_ROTATION == 90 || EXAMPLE_DISPLAY_ROTATION == 270
#define EXAMPLE_LCD_H_RES (320)
#define EXAMPLE_LCD_V_RES (172)
#else
#define EXAMPLE_LCD_H_RES (172)
#define EXAMPLE_LCD_V_RES (320)
#endif
#define EXAMPLE_LCD_DRAW_BUFF_HEIGHT (50)
#define EXAMPLE_LCD_DRAW_BUFF_DOUBLE (1)
static char *TAG = "app";
/* LCD IO and panel */
static esp_lcd_panel_io_handle_t io_handle = NULL;
static esp_lcd_panel_handle_t panel_handle = NULL;
static esp_lcd_touch_handle_t touch_handle = NULL;
/* LVGL display and touch */
static lv_display_t *lvgl_disp = NULL;
static lv_indev_t *lvgl_touch_indev = NULL;
/* --- Button Handle global halten, falls benötigt --- */
static button_handle_t g_btn = NULL;
/* Vorwärtsdeklarationen */
void lv_fs_fatfs_init(void);
static esp_err_t app_lvgl_init(void);
static void button_init(void);
static void touch_test(void);
static void ui_tick_cb(lv_timer_t *t);
/* ==================== LVGL Setup (unverändert) ==================== */
static esp_err_t app_lvgl_init(void)
{
/* Initialize LVGL */
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = 4,
.task_stack = 1024 * 10,
.task_affinity = -1,
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
ESP_RETURN_ON_ERROR(lvgl_port_init(&lvgl_cfg), TAG, "LVGL port initialization failed");
/* Add LCD screen */
ESP_LOGD(TAG, "Add LCD screen");
lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_DRAW_BUFF_HEIGHT,
.double_buffer = EXAMPLE_LCD_DRAW_BUFF_DOUBLE,
.hres = EXAMPLE_LCD_H_RES,
.vres = EXAMPLE_LCD_V_RES,
.monochrome = false,
.rotation = {
.swap_xy = false,
.mirror_x = false,
.mirror_y = false,
},
.flags = {
.buff_dma = true,
#if LVGL_VERSION_MAJOR >= 9
.swap_bytes = true,
#endif
}};
#if EXAMPLE_DISPLAY_ROTATION == 90
disp_cfg.rotation.swap_xy = true;
disp_cfg.rotation.mirror_x = true;
disp_cfg.rotation.mirror_y = false;
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, 0, 34));
#elif EXAMPLE_DISPLAY_ROTATION == 180
disp_cfg.rotation.swap_xy = false;
disp_cfg.rotation.mirror_x = true;
disp_cfg.rotation.mirror_y = true;
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, 34, 0));
#elif EXAMPLE_DISPLAY_ROTATION == 270
disp_cfg.rotation.swap_xy = true;
disp_cfg.rotation.mirror_x = false;
disp_cfg.rotation.mirror_y = true;
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, 0, 34));
#else
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, 34, 0));
#endif
lvgl_disp = lvgl_port_add_disp(&disp_cfg);
/* Add touch input (for selected screen) */
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = lvgl_disp,
.handle = touch_handle,
};
lvgl_touch_indev = lvgl_port_add_touch(&touch_cfg);
return ESP_OK;
}
static void ui_tick_cb(lv_timer_t *t) {
(void)t;
if (lvgl_port_lock(0)) {
ui_update_status_icons();
tick_screen(0);
lvgl_port_unlock();
}
}
/* ==================== Touch-Test (unverändert) ==================== */
static bool touch_test_done = false;
static void button_event_cb(void *arg, void *data)
{
button_event_t event = iot_button_get_event((button_handle_t)arg);
ESP_LOGI(TAG, "%s", iot_button_get_event_str(event));
touch_test_done = true;
}
static void touch_test(void)
{
uint16_t touchpad_x[1] = {0};
uint16_t touchpad_y[1] = {0};
uint8_t touchpad_cnt = 0;
uint16_t color_arr[16] = {0};
lv_obj_t *lable = NULL;
for (int i = 0; i < 16; i++) color_arr[i] = 0xf800;
if (lvgl_port_lock(0)) {
lable = lv_label_create(lv_scr_act());
lv_label_set_text(lable, "Touch testing mode \nExit with BOOT button");
lv_obj_center(lable);
lvgl_port_unlock();
}
vTaskDelay(pdMS_TO_TICKS(500));
if (lvgl_port_lock(0)) {
while (!touch_test_done) {
esp_lcd_touch_read_data(touch_handle);
bool touchpad_pressed = esp_lcd_touch_get_coordinates(touch_handle, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
if (touchpad_pressed && touchpad_cnt > 0) {
if (touchpad_x[0] < 2) touchpad_x[0] = 2;
else if (touchpad_x[0] > EXAMPLE_LCD_H_RES - 2 - 1) touchpad_x[0] = EXAMPLE_LCD_H_RES - 2 - 1;
if (touchpad_y[0] < 2) touchpad_y[0] = 2;
else if (touchpad_y[0] > EXAMPLE_LCD_V_RES - 2 - 1) touchpad_y[0] = EXAMPLE_LCD_V_RES - 2 - 1;
esp_lcd_panel_draw_bitmap(panel_handle, touchpad_x[0] - 2, touchpad_y[0] - 2, touchpad_x[0] + 2, touchpad_y[0] + 2, color_arr);
}
vTaskDelay(pdMS_TO_TICKS(10));
}
lv_obj_del(lable);
lvgl_port_unlock();
}
}
/* ==================== Factory-Reset per Long-Press ==================== */
static void longpress_feedback_show(const char *msg)
{
if (!lvgl_port_lock(0)) return;
// einfache Overlay-Message
lv_obj_t *box = lv_obj_create(lv_scr_act());
lv_obj_set_size(box, EXAMPLE_LCD_H_RES - 20, 60);
lv_obj_center(box);
lv_obj_t *label = lv_label_create(box);
lv_label_set_text(label, msg);
lv_obj_center(label);
lvgl_port_unlock();
}
static void reset_button_cb(void *arg, void *data)
{
button_event_t ev = iot_button_get_event((button_handle_t)arg);
if (ev == BUTTON_LONG_PRESS_START) {
ESP_LOGW(TAG, "Factory-Reset: LONG PRESS detected – hold to confirm...");
longpress_feedback_show("Factory Reset...\nRelease to confirm");
} else if (ev == BUTTON_LONG_PRESS_UP) {
ESP_LOGW(TAG, "Factory-Reset: executing (erase NVS + reboot)!");
// NVS löschen
if (storage_factory_reset_all() == ESP_OK) {
// kleine visuelle Rückmeldung
longpress_feedback_show("Erase NVS\nReboot...");
vTaskDelay(pdMS_TO_TICKS(300));
}
// harter Neustart
esp_restart();
}
}
static void button_init(void)
{
button_config_t btn_cfg = {};
// Optional: Long-Press-Dauer setzen (ms). Standard ist häufig ~1500 ms.
// Einige Versionen des iot_button haben field "long_press_time".
btn_cfg.long_press_time = 5000; // 5 Sekunden halten zum Reset
button_gpio_config_t btn_gpio_cfg = {};
btn_gpio_cfg.gpio_num = GPIO_NUM_9; // dein BOOT/USER Button
btn_gpio_cfg.active_level = 0;
ESP_ERROR_CHECK(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &g_btn));
// Kurzer Klick für Touch-Test (wie bisher)
iot_button_register_cb(g_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, NULL);
// Long-Press Feedback (Start)
iot_button_register_cb(g_btn, BUTTON_LONG_PRESS_START, NULL, reset_button_cb, NULL);
// Beim Loslassen nach langem Drücken -> wirklich resetten
iot_button_register_cb(g_btn, BUTTON_LONG_PRESS_UP, NULL, reset_button_cb, NULL);
}
/* ==================== app_main (unverändert, plus Button-Reset) ==================== */
void app_main(void)
{
/*
esp_log_level_set("storage", ESP_LOG_NONE);
*/
esp_log_level_set("*", ESP_LOG_NONE);
sys_status_init();
sys_status_set_wifi_ok(false);
sys_status_set_influx_ok(false);
sys_status_set_mdns_ok(false);
// 0) NVS zuerst
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);
// --- Storage/WiFi wie gehabt ---
ESP_ERROR_CHECK(storage_init());
storage_debug_dump();
wifi_mgr_init();
wifi_mgr_start();
ESP_ERROR_CHECK(i2cdev_init());
// 1) I2C-Bus initialisieren (bsp_i2c.h)
i2c_master_bus_handle_t i2c_bus_handle = bsp_i2c_init();
// 2) Diesen Bus von i2cdev "adoptieren", damit SEN5x denselben Bus nutzt
ESP_ERROR_CHECK(i2cdev_adopt_bus(0, i2c_bus_handle, 18, 19)); // Port 0, SDA=18, SCL=19
// 3) Restliche Peripherie
bsp_spi_init();
bsp_battery_init();
// bsp_qmi8658_init(i2c_bus_handle);
bsp_display_init(&io_handle, &panel_handle, EXAMPLE_LCD_H_RES * EXAMPLE_LCD_DRAW_BUFF_HEIGHT);
bsp_touch_init(&touch_handle, i2c_bus_handle, EXAMPLE_LCD_H_RES, EXAMPLE_LCD_V_RES, EXAMPLE_DISPLAY_ROTATION);
bsp_sdcard_init();
ESP_ERROR_CHECK(app_lvgl_init());
lv_fs_fatfs_init();
bsp_display_brightness_init();
bsp_display_set_brightness(100);
// >>> Button inkl. Long-Press-Reset
button_init();
// touch_test(); // optional
if (lvgl_port_lock(0)) {
ui_init(); // erzeugt auch die Objekte in screens.c
lvgl_port_unlock();
}
lv_timer_t *ui_timer = lv_timer_create(ui_tick_cb, 1000, NULL);
(void)ui_timer;
// 4) SEN5x
const sen5x_i2c_cfg_t cfg = {
.addr = 0x69,
.port = 0,
.sda = 18,
.scl = 19,
.sda_pullup = true,
.scl_pullup = true,
.freq = 400000
};
ESP_ERROR_CHECK(sen5x_app_init(&cfg));
// 5) Task starten
xTaskCreate(sen5x_app_task, "sen5x_task", 4096, NULL, 5, NULL);
for (;;) vTaskDelay(pdMS_TO_TICKS(1000));
}