ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
config.h File Reference

Thread-safe configuration component utilizing NVS for ESP32. More...

#include "esp_err.h"
#include "esp_wifi.h"
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for config.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define APP_CONFIG_DMX_PORT_COUNT   2
 Total number of physical DMX ports supported by the hardware.
#define APP_CONFIG_INVALID_UNIVERSE   0xFFFF
 Error/Invalid indicator for DMX universe.
Compile-time Factory Defaults
#define APP_CONFIG_DEFAULT_CONNECTION   APP_CONFIG_CONN_WIFI_AP
#define APP_CONFIG_DEFAULT_IP_METHOD   APP_CONFIG_IP_DHCP
#define APP_CONFIG_DEFAULT_LED_BRIGHTNESS   128
#define APP_CONFIG_DEFAULT_STA_SSID   ""
#define APP_CONFIG_DEFAULT_STA_PASSWORD   ""
#define APP_CONFIG_DEFAULT_AP_PASSWORD   "ChaosDMX"
#define APP_CONFIG_DEFAULT_AP_SSID_PREFIX   "ChaosDMX"
#define APP_CONFIG_DEFAULT_DMX_DIR   APP_CONFIG_DIR_OUTPUT
#define APP_CONFIG_DEFAULT_START_UNIVERSE   1
#define APP_CONFIG_DEFAULT_SINGLE_CLICK_ACT   APP_BUTTON_ACTION_TOGGLE_LED
#define APP_CONFIG_DEFAULT_DOUBLE_CLICK_ACT   APP_BUTTON_ACTION_NONE
#define APP_CONFIG_DEFAULT_MULTI_CLICK_ACT   APP_BUTTON_ACTION_REBOOT

Enumerations

enum  app_button_event_t {
  APP_BUTTON_EVENT_SINGLE_CLICK , APP_BUTTON_EVENT_DOUBLE_CLICK , APP_BUTTON_EVENT_MULTIPLE_CLICK , APP_BUTTON_EVENT_LONG_HOLD ,
  APP_BUTTON_EVENT_MAX
}
 Supported button event types that can be configured in the system. More...
enum  config_button_action_t { APP_BUTTON_ACTION_NONE = 0 , APP_BUTTON_ACTION_TOGGLE_LED , APP_BUTTON_ACTION_REBOOT , APP_BUTTON_ACTION_MAX }
 Actions that can be dynamically assigned to button events. More...
enum  config_ip_method_t { APP_CONFIG_IP_STATIC = 0 , APP_CONFIG_IP_DHCP }
 IP assignment method configurations. More...
enum  config_connection_t { APP_CONFIG_CONN_WIFI_AP = 0 , APP_CONFIG_CONN_WIFI_STA , APP_CONFIG_CONN_ETHERNET }
 Network connection medium types. More...
enum  config_direction_t { APP_CONFIG_DIR_OUTPUT = 0 , APP_CONFIG_DIR_INPUT }
 Data direction for dmx-port. More...

Functions

esp_err_t config_init (void)
 Initializes the configuration component.
esp_err_t config_reset_defaults (void)
 Resets all configuration settings back to factory defaults.
esp_err_t config_save (void)
 Flushes all staged RAM changes permanently to the non-volatile storage.
config_connection_t config_get_connection (void)
 Gets the current network connection type.
bool config_set_connection (config_connection_t conn)
 Sets the network connection type in RAM.
config_ip_method_t config_get_ip_method (void)
 Gets the current IP allocation method.
bool config_set_ip_method (config_ip_method_t method)
 Sets the IP assignment method in RAM.
uint8_t config_get_led_brightness (void)
 Gets the current status LED brightness level.
bool config_set_led_brightness (uint8_t brightness)
 Sets the status LED brightness level in RAM.
config_button_action_t config_get_button_action (app_button_event_t event)
 Retrieves the action assigned to a specific button event.
bool config_set_button_action (app_button_event_t event, config_button_action_t action)
 Assigns a new action to a specific button event in RAM.
uint16_t config_get_dmx_universe (uint8_t port_index)
 Gets the configured DMX universe for a specific port.
bool config_set_dmx_universe (uint8_t port_index, uint16_t universe)
 Sets the DMX universe for a specific port in RAM.
config_direction_t config_get_dmx_direction (uint8_t port_index)
 Gets the data direction for a specific port.
bool config_set_dmx_direction (uint8_t port_index, config_direction_t direction)
 Sets the data direction for a specific port in RAM.
void config_get_wifi_sta_config (wifi_config_t *dest)
 Gets the Wi-Fi Station mode configuration.
bool config_set_wifi_sta_config (const wifi_config_t *src)
 Sets the Wi-Fi Station mode configuration in RAM.
void config_get_wifi_ap_config (wifi_config_t *dest)
 Gets the Wi-Fi Access Point mode configuration.
bool config_set_wifi_ap_config (const wifi_config_t *src)
 Sets the Wi-Fi Access Point mode configuration in RAM.

Detailed Description

Thread-safe configuration component utilizing NVS for ESP32.

This header defines the public application configuration interface, including Wi-Fi credentials, DMX port settings, and system-wide parameters like LED brightness and button actions.

Definition in file config.h.

Macro Definition Documentation

◆ APP_CONFIG_DEFAULT_AP_PASSWORD

#define APP_CONFIG_DEFAULT_AP_PASSWORD   "ChaosDMX"

Factory default AP password

Definition at line 95 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_AP_SSID_PREFIX

#define APP_CONFIG_DEFAULT_AP_SSID_PREFIX   "ChaosDMX"

Prefix for runtime generated AP SSID

Definition at line 97 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_CONNECTION

#define APP_CONFIG_DEFAULT_CONNECTION   APP_CONFIG_CONN_WIFI_AP

Default connection mode

Definition at line 83 of file config.h.

Referenced by config_get_connection(), and load_factory_defaults().

◆ APP_CONFIG_DEFAULT_DMX_DIR

#define APP_CONFIG_DEFAULT_DMX_DIR   APP_CONFIG_DIR_OUTPUT

Fallback direction for all DMX ports

Definition at line 100 of file config.h.

Referenced by config_get_dmx_direction(), and load_factory_defaults().

◆ APP_CONFIG_DEFAULT_DOUBLE_CLICK_ACT

#define APP_CONFIG_DEFAULT_DOUBLE_CLICK_ACT   APP_BUTTON_ACTION_NONE

Default action for double click

Definition at line 107 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_IP_METHOD

#define APP_CONFIG_DEFAULT_IP_METHOD   APP_CONFIG_IP_DHCP

Default IP assignment

Definition at line 85 of file config.h.

Referenced by config_get_ip_method(), and load_factory_defaults().

◆ APP_CONFIG_DEFAULT_LED_BRIGHTNESS

#define APP_CONFIG_DEFAULT_LED_BRIGHTNESS   128

Default status LED brightness (0-255)

Definition at line 87 of file config.h.

Referenced by config_get_led_brightness(), and load_factory_defaults().

◆ APP_CONFIG_DEFAULT_MULTI_CLICK_ACT

#define APP_CONFIG_DEFAULT_MULTI_CLICK_ACT   APP_BUTTON_ACTION_REBOOT

Default action for multi click

Definition at line 109 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_SINGLE_CLICK_ACT

#define APP_CONFIG_DEFAULT_SINGLE_CLICK_ACT   APP_BUTTON_ACTION_TOGGLE_LED

Default action for single click

Definition at line 105 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_STA_PASSWORD

#define APP_CONFIG_DEFAULT_STA_PASSWORD   ""

Default STA Password (empty) \

Definition at line 91 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_STA_SSID

#define APP_CONFIG_DEFAULT_STA_SSID   ""

Default STA SSID (empty)

Definition at line 90 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DEFAULT_START_UNIVERSE

#define APP_CONFIG_DEFAULT_START_UNIVERSE   1

First port starts at universe X, increments per port

Definition at line 102 of file config.h.

Referenced by load_factory_defaults().

◆ APP_CONFIG_DMX_PORT_COUNT

#define APP_CONFIG_DMX_PORT_COUNT   2

Total number of physical DMX ports supported by the hardware.

Definition at line 24 of file config.h.

Referenced by config_get_dmx_direction(), config_get_dmx_universe(), config_set_dmx_direction(), config_set_dmx_universe(), and load_factory_defaults().

◆ APP_CONFIG_INVALID_UNIVERSE

#define APP_CONFIG_INVALID_UNIVERSE   0xFFFF

Error/Invalid indicator for DMX universe.

Definition at line 29 of file config.h.

Referenced by config_get_dmx_universe(), and config_set_dmx_universe().

Enumeration Type Documentation

◆ app_button_event_t

Supported button event types that can be configured in the system.

Enumerator
APP_BUTTON_EVENT_SINGLE_CLICK 

Triggered on a single short press

APP_BUTTON_EVENT_DOUBLE_CLICK 

Triggered on a rapid double press

APP_BUTTON_EVENT_MULTIPLE_CLICK 

Triggered on multiple rapid presses

APP_BUTTON_EVENT_LONG_HOLD 

Triggered once long-press duration is reached

APP_BUTTON_EVENT_MAX 

Definition at line 34 of file config.h.

◆ config_button_action_t

Actions that can be dynamically assigned to button events.

Enumerator
APP_BUTTON_ACTION_NONE 

Do nothing

APP_BUTTON_ACTION_TOGGLE_LED 

Toggle status LEDs or change brightness

APP_BUTTON_ACTION_REBOOT 

Restart the ESP

APP_BUTTON_ACTION_MAX 

Definition at line 46 of file config.h.

◆ config_connection_t

Network connection medium types.

Enumerator
APP_CONFIG_CONN_WIFI_AP 

Act as a Wi-Fi Access Point

APP_CONFIG_CONN_WIFI_STA 

Connect to an existing Wi-Fi network (Station)

APP_CONFIG_CONN_ETHERNET 

Wired Ethernet connection

Definition at line 64 of file config.h.

◆ config_direction_t

Data direction for dmx-port.

Enumerator
APP_CONFIG_DIR_OUTPUT 

Port acts as an output

APP_CONFIG_DIR_INPUT 

Port acts as an input

Definition at line 74 of file config.h.

◆ config_ip_method_t

IP assignment method configurations.

Enumerator
APP_CONFIG_IP_STATIC 

Use a static IP address config

APP_CONFIG_IP_DHCP 

Obtain IP address via DHCP

Definition at line 56 of file config.h.

Function Documentation

◆ config_get_button_action()

config_button_action_t config_get_button_action ( app_button_event_t event)

Retrieves the action assigned to a specific button event.

Parameters
[in]eventThe button event to query.
Returns
The assigned config_button_action_t.
Parameters
[in]eventThe button event to query.
Returns
The config_button_action_t assigned to the event, or APP_BUTTON_ACTION_NONE if uninitialized or invalid.

Definition at line 447 of file config.c.

References APP_BUTTON_ACTION_NONE, APP_BUTTON_EVENT_DOUBLE_CLICK, APP_BUTTON_EVENT_MULTIPLE_CLICK, APP_BUTTON_EVENT_SINGLE_CLICK, LOCK, s_config, s_is_initialized, and UNLOCK.

Referenced by iot_button_double_click_cb(), iot_button_multiple_click_cb(), and iot_button_single_click_cb().

Here is the caller graph for this function:

◆ config_get_connection()

config_connection_t config_get_connection ( void )

Gets the current network connection type.

Returns
Current connection mode as config_connection_t.

Definition at line 243 of file config.c.

References APP_CONFIG_DEFAULT_CONNECTION, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_dmx_direction()

config_direction_t config_get_dmx_direction ( uint8_t port_index)

Gets the data direction for a specific port.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
Returns
Configuration direction (Defaults to OUTPUT if index invalid).

Definition at line 342 of file config.c.

References APP_CONFIG_DEFAULT_DMX_DIR, APP_CONFIG_DMX_PORT_COUNT, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_dmx_universe()

uint16_t config_get_dmx_universe ( uint8_t port_index)

Gets the configured DMX universe for a specific port.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
Returns
Universe number (0-32768), or APP_CONFIG_INVALID_UNIVERSE if port_index is invalid.

Definition at line 318 of file config.c.

References APP_CONFIG_DMX_PORT_COUNT, APP_CONFIG_INVALID_UNIVERSE, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_ip_method()

config_ip_method_t config_get_ip_method ( void )

Gets the current IP allocation method.

Returns
Current IP method as config_ip_method_t.

Definition at line 267 of file config.c.

References APP_CONFIG_DEFAULT_IP_METHOD, LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_led_brightness()

uint8_t config_get_led_brightness ( void )

Gets the current status LED brightness level.

Returns
Brightness value ranging from 0 to 255.

Definition at line 290 of file config.c.

References APP_CONFIG_DEFAULT_LED_BRIGHTNESS, LOCK, s_config, s_is_initialized, and UNLOCK.

Referenced by app_main(), and execute_button_action().

Here is the caller graph for this function:

◆ config_get_wifi_ap_config()

void config_get_wifi_ap_config ( wifi_config_t * dest)

Gets the Wi-Fi Access Point mode configuration.

Parameters
[out]destPointer to a wifi_config_t struct to receive the data.

Definition at line 404 of file config.c.

References LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_get_wifi_sta_config()

void config_get_wifi_sta_config ( wifi_config_t * dest)

Gets the Wi-Fi Station mode configuration.

Parameters
[out]destPointer to a wifi_config_t struct to receive the data.

Definition at line 367 of file config.c.

References LOCK, s_config, s_is_initialized, and UNLOCK.

◆ config_init()

esp_err_t config_init ( void )

Initializes the configuration component.

Sets up internal mutexes and loads the persisted configuration blob from NVS.

Note
The NVS flash subsystem must be initialized (via nvs_flash_init()) before calling this function.
Returns
  • ESP_OK on success.
  • ESP_ERR_NO_MEM if mutex creation failed.
  • Other NVS-related error codes if loading fails.

Definition at line 136 of file config.c.

References APP_CONFIG_MAGIC, APP_CONFIG_VERSION, load_factory_defaults(), LOGE, LOGI, LOGW, NVS_BLOB_KEY, NVS_NAMESPACE, s_config, s_config_mutex, s_is_dirty, and s_is_initialized.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_reset_defaults()

esp_err_t config_reset_defaults ( void )

Resets all configuration settings back to factory defaults.

Note
This stages the defaults in RAM. A call to config_save() is required to permanently write them to flash.
Returns
  • ESP_OK on success
  • ESP_FAIL if memory or initialization state is invalid

Definition at line 194 of file config.c.

References load_factory_defaults(), LOCK, LOGI, s_is_initialized, and UNLOCK.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_save()

esp_err_t config_save ( void )

Flushes all staged RAM changes permanently to the non-volatile storage.

Note
To prevent flash wear, this function returns early with ESP_OK if no settings were mutated since the last call.
Returns
  • ESP_OK on success or if no write was required
  • ESP_ERR_NVS_NOT_INITIALIZED if NVS system is down
  • Other flash driver error codes on write failures

Definition at line 204 of file config.c.

References LOCK, LOGE, LOGI, NVS_BLOB_KEY, NVS_NAMESPACE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

Referenced by app_main(), and execute_button_action().

Here is the caller graph for this function:

◆ config_set_button_action()

bool config_set_button_action ( app_button_event_t event,
config_button_action_t action )

Assigns a new action to a specific button event in RAM.

Parameters
[in]eventThe button event to modify.
[in]actionThe config_button_action_t to assign.
Returns
true if updated, false if uninitialized, unchanged, or invalid.
Parameters
[in]eventThe button event to modify.
[in]actionThe config_button_action_t to assign.
Returns
true if the configuration was updated, false if uninitialized, unchanged, or arguments were invalid.

Definition at line 477 of file config.c.

References APP_BUTTON_ACTION_MAX, APP_BUTTON_EVENT_DOUBLE_CLICK, APP_BUTTON_EVENT_MULTIPLE_CLICK, APP_BUTTON_EVENT_SINGLE_CLICK, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_connection()

bool config_set_connection ( config_connection_t conn)

Sets the network connection type in RAM.

Parameters
[in]connNew connection mode to apply.
Returns
true if the value was modified, false if it was identical or invalid.

Definition at line 252 of file config.c.

References APP_CONFIG_CONN_ETHERNET, LOCK, LOGI, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_dmx_direction()

bool config_set_dmx_direction ( uint8_t port_index,
config_direction_t direction )

Sets the data direction for a specific port in RAM.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
[in]directionNew direction (Input/Output).
Returns
true if updated, false if index invalid or unchanged.

Definition at line 351 of file config.c.

References APP_CONFIG_DIR_INPUT, APP_CONFIG_DMX_PORT_COUNT, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_dmx_universe()

bool config_set_dmx_universe ( uint8_t port_index,
uint16_t universe )

Sets the DMX universe for a specific port in RAM.

Parameters
[in]port_indexIndex of the port (0 to APP_CONFIG_DMX_PORT_COUNT - 1).
[in]universeDMX Universe number (typically 0 - 32768).
Returns
true if updated, false if index invalid or unchanged.

Definition at line 327 of file config.c.

References APP_CONFIG_DMX_PORT_COUNT, APP_CONFIG_INVALID_UNIVERSE, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_ip_method()

bool config_set_ip_method ( config_ip_method_t method)

Sets the IP assignment method in RAM.

Parameters
[in]methodNew IP allocation method to apply.
Returns
true if the value was modified, false if identical or invalid.

Definition at line 276 of file config.c.

References APP_CONFIG_IP_DHCP, LOCK, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_led_brightness()

bool config_set_led_brightness ( uint8_t brightness)

Sets the status LED brightness level in RAM.

Parameters
[in]brightnessDesired brightness (0 - 255).
Returns
true if value was updated, false if invalid (out of bounds) or unchanged.

Definition at line 299 of file config.c.

References led_set_brightness(), LOCK, LOGI, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

Referenced by execute_button_action().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_set_wifi_ap_config()

bool config_set_wifi_ap_config ( const wifi_config_t * src)

Sets the Wi-Fi Access Point mode configuration in RAM.

Parameters
[in]srcPointer to a wifi_config_t struct containing the new config.
Returns
true if updated, false if invalid or unchanged.

Definition at line 415 of file config.c.

References LOCK, LOGE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.

◆ config_set_wifi_sta_config()

bool config_set_wifi_sta_config ( const wifi_config_t * src)

Sets the Wi-Fi Station mode configuration in RAM.

Parameters
[in]srcPointer to a wifi_config_t struct containing the new config.
Returns
true if updated, false if invalid or unchanged.

Definition at line 379 of file config.c.

References LOCK, LOGE, s_config, s_is_dirty, s_is_initialized, and UNLOCK.