Configuration

This commit is contained in:
2025-06-12 20:13:04 -04:00
parent 52194c19db
commit b39ed30ecc
12 changed files with 124 additions and 39 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "ConfigManager.cpp"
PRIV_REQUIRES constants nvs_flash
INCLUDE_DIRS "include")

View File

@@ -0,0 +1,59 @@
//
// Created by Johnathon Slightham on 2025-06-12.
//
#include "nvs_flash.h"
#include "constants/config.h"
#include "ConfigManager.h"
void ConfigManager::init_config() {
nvs_flash_init();
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
uint16_t id;
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u16(config_handle, "id", &id)) {
nvs_set_u16(config_handle, MODULE_ID_KEY, DEFAULT_MODULE_ID);
}
uint16_t type;
if (ESP_ERR_NVS_NOT_FOUND == nvs_get_u16(config_handle, "id", &type)) {
nvs_set_u16(config_handle, MODULE_TYPE_KEY, DEFAULT_MODULE_TYPE);
}
nvs_close(config_handle);
}
uint16_t ConfigManager::get_module_id() {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle);
uint16_t id;
nvs_get_u16(config_handle, MODULE_ID_KEY, &id);
nvs_close(config_handle);
return id;
}
ModuleType ConfigManager::get_module_type() {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle);
uint16_t type;
nvs_get_u16(config_handle, MODULE_TYPE_KEY, &type);
nvs_close(config_handle);
return static_cast<ModuleType>(type);
}
void ConfigManager::set_module_id(const uint16_t id) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
nvs_set_u16(config_handle, MODULE_ID_KEY, id);
nvs_close(config_handle);
}
void ConfigManager::set_module_type(const ModuleType type) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
nvs_set_u16(config_handle, MODULE_TYPE_KEY, type);
nvs_close(config_handle);
}

View File

@@ -0,0 +1,21 @@
//
// Created by Johnathon Slightham on 2025-06-12.
//
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include "constants/config.h"
class ConfigManager {
public:
static void init_config();
static uint16_t get_module_id();
static ModuleType get_module_type();
static void set_module_id(uint16_t id);
static void set_module_type(ModuleType type);
};
#endif //CONFIGMANAGER_H

View File

@@ -0,0 +1,20 @@
//
// Created by Johnathon Slightham on 2025-06-12.
//
#ifndef CONFIG_H
#define CONFIG_H
#define NVS_FLASH_NAMESPACE "app"
#define DEFAULT_MODULE_ID 0
#define DEFAULT_MODULE_TYPE 0
#define MODULE_ID_KEY "module_id"
#define MODULE_TYPE_KEY "module_type"
enum ModuleType { splitter }; // todo: maybe this should be flatbuffer
#endif //CONFIG_H

View File

@@ -13,4 +13,6 @@
#define SLEEP_AFTER_FAIL_MS 5000 #define SLEEP_AFTER_FAIL_MS 5000
#define TCP_PORT 3001
#endif //TCP_H #endif //TCP_H

View File

@@ -1,3 +0,0 @@
idf_component_register(SRCS "logger.cpp"
PRIV_REQUIRES
INCLUDE_DIRS "include")

View File

@@ -1,20 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-26.
//
#ifndef LOGGER_H
#define LOGGER_H
class Logger {
public:
static Logger &getInstance() {
static Logger instance;
return instance;
}
private:
virtual Logger();
}
#endif //LOGGER_H

View File

@@ -1,3 +0,0 @@
//
// Created by Johnathon Slightham on 2025-05-26.
//

View File

@@ -1,3 +1,3 @@
idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer" idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp"
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config
INCLUDE_DIRS "include") INCLUDE_DIRS "include")

View File

@@ -4,13 +4,21 @@
#include "mdns.h" #include "mdns.h"
#include "ConfigManager.h"
#include "mDNSDiscoveryService.h" #include "mDNSDiscoveryService.h"
#include "constants/tcp.h"
#include <string>
#include <format>
// todo: clean this up (strange to be a constructor) also need to add more details
mDNSDiscoveryService::mDNSDiscoveryService() { mDNSDiscoveryService::mDNSDiscoveryService() {
mdns_init(); mdns_init();
mdns_hostname_set("botchain-0000"); mdns_hostname_set(std::format("botchain-{}", ConfigManager::get_module_id()).c_str());
mdns_instance_name_set("BotChain Robot Module 0000"); mdns_instance_name_set(std::format("BotChain Robot Module {}", ConfigManager::get_module_id()).c_str());
mdns_service_add(NULL, "_robotcontrol", "_udp", 3000, NULL, 0); mdns_service_add(nullptr, "_robotcontrol", "_tcp", TCP_PORT, nullptr, 0);
mdns_service_instance_name_set("_robotcontrol", "_udp", "Robot Control UDP Server"); mdns_service_instance_name_set("_robotcontrol", "_tcp", "Robot Control TCP Server");
} }

View File

@@ -1,3 +1,3 @@
idf_component_register(SRCS "main.cpp" idf_component_register(SRCS "main.cpp"
PRIV_REQUIRES spi_flash nvs_flash esp_event rpc PRIV_REQUIRES spi_flash nvs_flash esp_event rpc constants config
INCLUDE_DIRS "") INCLUDE_DIRS "")

View File

@@ -4,24 +4,22 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_flash.h"
#include "nvs_flash.h"
#include "WifiManager.h" #include "WifiManager.h"
#include "mDNSDiscoveryService.h" #include "mDNSDiscoveryService.h"
#include "TCPServer.h" #include "TCPServer.h"
#include "ConfigManager.h"
#include "constants/tcp.h"
extern "C" [[noreturn]] void app_main(void) { extern "C" [[noreturn]] void app_main(void) {
nvs_flash_init(); ConfigManager::init_config();
const auto manager = std::make_unique<WifiManager>(); const auto manager = std::make_unique<WifiManager>();
manager->connect(); manager->connect();
const auto discovery = std::make_unique<mDNSDiscoveryService>(); const auto discovery = std::make_unique<mDNSDiscoveryService>();
vTaskDelay(20000 / portTICK_PERIOD_MS); const auto tcp_server = std::make_unique<TCPServer>(TCP_PORT);
const auto tcp_server = std::make_unique<TCPServer>(3001);
printf("Hello world!\n"); printf("Hello world!\n");