ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
dmx-interface.c
Go to the documentation of this file.
1#define LOG_TAG "MAIN"
2
3#include <stdio.h>
4
5#include "button_actions.h"
6#include "config.h"
7#include "esp_err.h"
8#include "esp_system.h"
9#include "freertos/FreeRTOS.h"
10#include "freertos/task.h"
11#include "led.h"
12#include "logger.h"
13#include "nvs_flash.h"
14#include "storage.h"
15#include "system.h"
16#include "web_server.h"
17#include "wifi.h"
18
25void app_main(void) {
26 LOGI("DMX Interface starting...");
27
28 ESP_ERROR_CHECK(led_init());
30
31 ESP_ERROR_CHECK(system_init());
32
33 // Basic system init needed for the button callback to work safely
34 esp_err_t err = nvs_flash_init();
35 if (err == ESP_ERR_NVS_NO_FREE_PAGES ||
36 err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
37 ESP_ERROR_CHECK(nvs_flash_erase());
38 err = nvs_flash_init();
39 }
40 ESP_ERROR_CHECK(err);
41
42 ESP_ERROR_CHECK(config_init());
43
44 err = button_init();
45 if (err != ESP_OK) {
46 LOGE("Failed to initialize button: %s", esp_err_to_name(err));
47 }
48
49
50 // Power-up Check:
51 // If button is held, block boot and show RESET led.
52 if (button_is_pressed()) {
53 LOGW("Button detected as PRESSED on power-up! Waiting 3s for factory "
54 "reset...");
56
57 int hold_time_ms = 0;
58 while (button_is_pressed()) {
59 vTaskDelay(pdMS_TO_TICKS(100));
60 hold_time_ms += 100;
61
62 if (hold_time_ms % 500 == 0) {
63 LOGI("Button still held... (%dms)", hold_time_ms);
64 }
65
66 if (hold_time_ms >= 3000) {
67 LOGW("3000ms reached! Factory reset triggered.");
68
69 ESP_ERROR_CHECK(config_reset_defaults());
70
71 LOGI("Configuration reset to factory defaults in RAM.");
72
74
75 vTaskDelay(pdMS_TO_TICKS(2000));
76
77 ESP_ERROR_CHECK(config_save());
78
79 LOGW("Factory defaults applied. PLEASE RELEASE BUTTON TO REBOOT.");
80
81 while (button_is_pressed()) {
82 vTaskDelay(pdMS_TO_TICKS(100));
83 }
84
85 LOGI("Button released. Rebooting now...");
86 esp_restart();
87 }
88 }
89
90 LOGI("Button released after %dms, continuing normal boot.", hold_time_ms);
92 } else {
93 LOGI("Button not pressed at startup.");
94 }
95
96 err = wifi_start_ap("DMX", "ChaosDMX", 1, 4);
97 if (err != ESP_OK) {
98 LOGE("Failed to start WiFi AP: %s", esp_err_to_name(err));
99 return;
100 }
101
102 httpd_handle_t server = webserver_start(NULL);
103 if (server == NULL) {
104 LOGE("Failed to start web server!");
105 return;
106 }
107
108 LOGI("Web server started successfully");
109 LOGI("Open http://192.168.4.1 in your browser");
110
112
113 vTaskDelay(pdMS_TO_TICKS(5000));
114
117
119
120 while (1) {
121 vTaskDelay(pdMS_TO_TICKS(1000));
122 }
123}
Hardware button abstraction layer for short and long press detection.
bool button_is_pressed(void)
Checks if the button is currently physically pressed.
esp_err_t button_init(void)
Initializes the hardware button configuration and event callbacks.
Thread-safe configuration component utilizing NVS for ESP32.
esp_err_t config_reset_defaults(void)
Resets all configuration settings back to factory defaults.
Definition config.c:194
uint8_t config_get_led_brightness(void)
Gets the current status LED brightness level.
Definition config.c:290
esp_err_t config_init(void)
Initializes the configuration component.
Definition config.c:136
esp_err_t config_save(void)
Flushes all staged RAM changes permanently to the non-volatile storage.
Definition config.c:204
void app_main(void)
Main entry point for the DMX Interface application.
LED control component with flexible blinking and breathing modes.
void led_set_brightness(uint8_t brightness)
Dynamically updates the LED maximum brightness.
Definition led.c:188
@ LED_MODE_NORMAL
Definition led.h:28
@ LED_MODE_OFF
Definition led.h:25
@ LED_MODE_BOOT_BREATHING
Definition led.h:26
@ LED_MODE_RESET
Definition led.h:27
void led_set_mode(led_mode_t mode)
Sets the current operational mode of the LED.
Definition led.c:141
esp_err_t led_init(void)
Initializes the LEDC peripheral for the status LED.
Definition led.c:117
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGW(...)
Log a message at Warning level.
Definition logger.h:40
#define LOGI(...)
Log a message at Info level.
Definition logger.h:42
#define LOGE(...)
Log a message at Error level.
Definition logger.h:38
void storage_print_info(void)
Print storage information to the log.
Definition storage.c:80
esp_err_t system_init(void)
Initializes the system monitoring component. Sets up the internal temperature sensor and tracks initi...
Definition system.c:27
void system_print_info(void)
Prints a complete, formatted overview of all system statistics to the console. Internally uses ESP_LO...
Definition system.c:162
Simple HTTP web server component for ESP32 with async FreeRTOS support.
httpd_handle_t webserver_start(const webserver_config_t *config)
Initialize and start the HTTP web server.
Definition web_server.c:166
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel, uint8_t max_connections)
Start WiFi Access Point (AP) mode.
Definition wifi.c:29