mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Configuration
This commit is contained in:
3
components/config/CMakeLists.txt
Normal file
3
components/config/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "ConfigManager.cpp"
|
||||
PRIV_REQUIRES constants nvs_flash
|
||||
INCLUDE_DIRS "include")
|
||||
59
components/config/ConfigManager.cpp
Normal file
59
components/config/ConfigManager.cpp
Normal 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);
|
||||
}
|
||||
21
components/config/include/ConfigManager.h
Normal file
21
components/config/include/ConfigManager.h
Normal 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
|
||||
20
components/constants/include/constants/config.h
Normal file
20
components/constants/include/constants/config.h
Normal 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
|
||||
@@ -13,4 +13,6 @@
|
||||
|
||||
#define SLEEP_AFTER_FAIL_MS 5000
|
||||
|
||||
#define TCP_PORT 3001
|
||||
|
||||
#endif //TCP_H
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
idf_component_register(SRCS "logger.cpp"
|
||||
PRIV_REQUIRES
|
||||
INCLUDE_DIRS "include")
|
||||
@@ -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
|
||||
@@ -1,3 +0,0 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-26.
|
||||
//
|
||||
@@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer"
|
||||
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants
|
||||
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 config
|
||||
INCLUDE_DIRS "include")
|
||||
|
||||
@@ -4,13 +4,21 @@
|
||||
|
||||
#include "mdns.h"
|
||||
|
||||
#include "ConfigManager.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() {
|
||||
mdns_init();
|
||||
mdns_hostname_set("botchain-0000");
|
||||
mdns_instance_name_set("BotChain Robot Module 0000");
|
||||
mdns_hostname_set(std::format("botchain-{}", ConfigManager::get_module_id()).c_str());
|
||||
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_instance_name_set("_robotcontrol", "_udp", "Robot Control UDP Server");
|
||||
mdns_service_add(nullptr, "_robotcontrol", "_tcp", TCP_PORT, nullptr, 0);
|
||||
mdns_service_instance_name_set("_robotcontrol", "_tcp", "Robot Control TCP Server");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
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 "")
|
||||
|
||||
@@ -4,24 +4,22 @@
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_flash.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "WifiManager.h"
|
||||
#include "mDNSDiscoveryService.h"
|
||||
#include "TCPServer.h"
|
||||
#include "ConfigManager.h"
|
||||
#include "constants/tcp.h"
|
||||
|
||||
extern "C" [[noreturn]] void app_main(void) {
|
||||
nvs_flash_init();
|
||||
ConfigManager::init_config();
|
||||
|
||||
const auto manager = std::make_unique<WifiManager>();
|
||||
manager->connect();
|
||||
|
||||
const auto discovery = std::make_unique<mDNSDiscoveryService>();
|
||||
|
||||
vTaskDelay(20000 / portTICK_PERIOD_MS);
|
||||
|
||||
const auto tcp_server = std::make_unique<TCPServer>(3001);
|
||||
const auto tcp_server = std::make_unique<TCPServer>(TCP_PORT);
|
||||
|
||||
printf("Hello world!\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user