Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
path: ${{ matrix.path }}
command: idf.py build && idf.py merge-bin -o merged.bin

# TODO: Overwrites NVS. Supply individual bin files and build web flasher.
- name: Package firmware
run: |
VERSION="${{ github.event.release.tag_name || env.TARGET_BRANCH }}"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ Firmware written for DIY devices in the Beacon ecosystem


# TODO
- [ ] Implement new LIGHT state!
- [x] Implement new LIGHT state!
- [ ] Better startup state.
- [ ] Initial long and short names
- [ ] Add some setup instructions to the displays?
- [ ] Section grouping
- [ ] Multi alert text indeces
- [ ] Make textAlert an array of tasks in the group, one per index of alert text.
Expand Down
21 changes: 17 additions & 4 deletions components/beacon_device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ idf_component_register(
"consumer/display/Hub75LvglDisplayConsumer.cpp"
${HELVATICA_FONT_SRCS}
"platform/Platform.cpp"
"httpServer/EspHttpServer.cpp"
"httpServer/HttpHandlers.cpp"
"http/EspHttpDaemon.cpp"
"http/BeaconHttpServer.cpp"
"http/api/StaticAssetApi.cpp"
"http/api/ConfigApi.cpp"
"http/api/HardwareConfigApi.cpp"
"config/NvsHardwareStore.cpp"
"hardware/HardwareConfig.cpp"
"networkConnection/StaWifiConnection.cpp"
"orchestrator/IOrchestrator.cpp"
"orchestrator/SateliteOrchestrator.cpp"
Expand All @@ -47,8 +52,16 @@ idf_component_register(
"consumer/display/Ili9341LvglDisplayConsumer.hpp"
"consumer/display/Hub75LvglDisplayConsumer.hpp"
"platform/Platform.hpp"
"httpServer/EspHttpServer.hpp"
"httpServer/HttpHandlers.hpp"
"http/IHttpDaemon.hpp"
"http/IHttpApi.hpp"
"http/EspHttpDaemon.hpp"
"http/BeaconHttpServer.hpp"
"http/api/StaticAssetApi.hpp"
"http/api/ConfigApi.hpp"
"http/api/HardwareConfigApi.hpp"
"config/IHardwareStore.hpp"
"config/NvsHardwareStore.hpp"
"hardware/HardwareConfig.hpp"
"networkConnection/INetworkConnection.hpp"
"networkConnection/IEthernetConnection.hpp"
"networkConnection/IWifiConnection.hpp"
Expand Down
11 changes: 11 additions & 0 deletions components/beacon_device/config/IHardwareStore.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cstdint>

class IHardwareStore {
public:
virtual ~IHardwareStore() = default;
virtual bool getInt(const char* key, int32_t& out) const = 0;
virtual bool setInt(const char* key, int32_t val) = 0;
virtual bool commit() = 0;
};
47 changes: 47 additions & 0 deletions components/beacon_device/config/NvsHardwareStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "config/NvsHardwareStore.hpp"
#include "esp_log.h"

static constexpr char TAG[] = "NvsHardwareStore";

NvsHardwareStore::~NvsHardwareStore()
{
if (_writeHandle) nvs_close(_writeHandle);
}

bool NvsHardwareStore::getInt(const char* key, int32_t& out) const
{
nvs_handle_t h;
if (nvs_open(NS, NVS_READONLY, &h) != ESP_OK) return false;
esp_err_t err = nvs_get_i32(h, key, &out);
nvs_close(h);
return err == ESP_OK;
}

bool NvsHardwareStore::setInt(const char* key, int32_t val)
{
if (!openWrite()) return false;
esp_err_t err = nvs_set_i32(_writeHandle, key, val);
if (err != ESP_OK) ESP_LOGW(TAG, "nvs_set_i32('%s') failed: %s", key, esp_err_to_name(err));
return err == ESP_OK;
}

bool NvsHardwareStore::commit()
{
if (!_writeHandle) return true;
esp_err_t err = nvs_commit(_writeHandle);
nvs_close(_writeHandle);
_writeHandle = 0;
if (err != ESP_OK) ESP_LOGW(TAG, "nvs_commit failed: %s", esp_err_to_name(err));
return err == ESP_OK;
}

bool NvsHardwareStore::openWrite()
{
if (_writeHandle) return true;
esp_err_t err = nvs_open(NS, NVS_READWRITE, &_writeHandle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "nvs_open for write failed: %s", esp_err_to_name(err));
_writeHandle = 0;
}
return err == ESP_OK;
}
18 changes: 18 additions & 0 deletions components/beacon_device/config/NvsHardwareStore.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "config/IHardwareStore.hpp"
#include "nvs.h"

class NvsHardwareStore : public IHardwareStore {
public:
~NvsHardwareStore();
bool getInt(const char* key, int32_t& out) const override;
bool setInt(const char* key, int32_t val) override;
bool commit() override;

private:
static constexpr char NS[] = "hw_cfg";
nvs_handle_t _writeHandle = 0;

bool openWrite();
};
190 changes: 190 additions & 0 deletions components/beacon_device/hardware/HardwareConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#include "hardware/HardwareConfig.hpp"
#include "esp_log.h"
#include <cassert>
#include <cstdlib>

static constexpr char TAG[] = "HardwareConfig";

// ── IntField ──────────────────────────────────────────────────────────────────

void IntField::addToJson(cJSON* arr) const
{
cJSON* f = cJSON_CreateObject();
cJSON_AddStringToObject(f, "key", _key);
cJSON_AddStringToObject(f, "label", _label);
cJSON_AddStringToObject(f, "type", "int");
cJSON_AddNumberToObject(f, "value", static_cast<double>(_raw));
cJSON_AddNumberToObject(f, "default", static_cast<double>(_default));
cJSON_AddNumberToObject(f, "min", static_cast<double>(_min));
cJSON_AddNumberToObject(f, "max", static_cast<double>(_max));
cJSON_AddItemToArray(arr, f);
}

// ── BoolField ─────────────────────────────────────────────────────────────────

void BoolField::addToJson(cJSON* arr) const
{
cJSON* f = cJSON_CreateObject();
cJSON_AddStringToObject(f, "key", _key);
cJSON_AddStringToObject(f, "label", _label);
cJSON_AddStringToObject(f, "type", "bool");
cJSON_AddBoolToObject (f, "value", _raw != 0);
cJSON_AddBoolToObject (f, "default", _default != 0);
cJSON_AddItemToArray(arr, f);
}

// ── SelectField ───────────────────────────────────────────────────────────────

bool SelectField::validate(int32_t v) const
{
for (size_t i = 0; i < _optionCount; i++) {
if (_options[i].value == v) return true;
}
return false;
}

void SelectField::addToJson(cJSON* arr) const
{
cJSON* f = cJSON_CreateObject();
cJSON_AddStringToObject(f, "key", _key);
cJSON_AddStringToObject(f, "label", _label);
cJSON_AddStringToObject(f, "type", "select");
cJSON_AddNumberToObject(f, "value", static_cast<double>(_raw));
cJSON_AddNumberToObject(f, "default", static_cast<double>(_default));
cJSON* opts = cJSON_AddArrayToObject(f, "options");
for (size_t i = 0; i < _optionCount; i++) {
cJSON* o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "label", _options[i].label);
cJSON_AddNumberToObject(o, "value", static_cast<double>(_options[i].value));
cJSON_AddItemToArray(opts, o);
}
cJSON_AddItemToArray(arr, f);
}

// ── GpioField ─────────────────────────────────────────────────────────────────

GpioField::GpioField(const char* key, const char* label, GpioFieldConfig cfg)
: HardwareFieldBase(key, label, static_cast<int32_t>(cfg.defaultValue))
{
if (cfg.allow && cfg.allowCount > 0) {
for (size_t i = 0; i < cfg.allowCount; i++) {
if (GPIO_IS_VALID_OUTPUT_GPIO(static_cast<int>(cfg.allow[i])))
_available.push_back(cfg.allow[i]);
}
} else {
for (int i = 0; i < GPIO_NUM_MAX; i++) {
if (!GPIO_IS_VALID_OUTPUT_GPIO(i)) continue;
gpio_num_t pin = static_cast<gpio_num_t>(i);
bool excluded = false;
for (size_t j = 0; j < cfg.excludeCount; j++) {
if (cfg.exclude[j] == pin) { excluded = true; break; }
}
if (!excluded) _available.push_back(pin);
}
}
}

bool GpioField::validate(int32_t v) const
{
gpio_num_t pin = static_cast<gpio_num_t>(v);
for (gpio_num_t a : _available) {
if (a == pin) return true;
}
return false;
}

void GpioField::addToJson(cJSON* arr) const
{
cJSON* f = cJSON_CreateObject();
cJSON_AddStringToObject(f, "key", _key);
cJSON_AddStringToObject(f, "label", _label);
cJSON_AddStringToObject(f, "type", "gpio");
cJSON_AddNumberToObject(f, "value", static_cast<double>(_raw));
cJSON_AddNumberToObject(f, "default", static_cast<double>(_default));
cJSON* avail = cJSON_AddArrayToObject(f, "available");
for (gpio_num_t pin : _available) {
cJSON_AddItemToArray(avail, cJSON_CreateNumber(static_cast<double>(pin)));
}
cJSON_AddItemToArray(arr, f);
}

// ── HardwareSection ───────────────────────────────────────────────────────────

IntField& HardwareSection::addInt(const char* key, const char* label, IntFieldConfig cfg)
{
assert(strlen(key) <= 15 && "NVS key must be <= 15 chars");
_intFields.emplace_back(std::make_unique<IntField>(key, label, cfg));
_allFields.push_back(_intFields.back().get());
return *_intFields.back();
}

BoolField& HardwareSection::addBool(const char* key, const char* label, BoolFieldConfig cfg)
{
assert(strlen(key) <= 15 && "NVS key must be <= 15 chars");
_boolFields.emplace_back(std::make_unique<BoolField>(key, label, cfg));
_allFields.push_back(_boolFields.back().get());
return *_boolFields.back();
}

SelectField& HardwareSection::addSelect(const char* key, const char* label, SelectFieldConfig cfg)
{
assert(strlen(key) <= 15 && "NVS key must be <= 15 chars");
_selectFields.emplace_back(std::make_unique<SelectField>(key, label, cfg));
_allFields.push_back(_selectFields.back().get());
return *_selectFields.back();
}

GpioField& HardwareSection::addGpio(const char* key, const char* label, GpioFieldConfig cfg)
{
assert(strlen(key) <= 15 && "NVS key must be <= 15 chars");
_gpioFields.emplace_back(std::make_unique<GpioField>(key, label, cfg));
_allFields.push_back(_gpioFields.back().get());
return *_gpioFields.back();
}

// ── HardwareConfig ────────────────────────────────────────────────────────────

HardwareConfig::HardwareConfig(IHardwareStore& store)
: _store(store) {}

HardwareSection& HardwareConfig::addSection(const char* title)
{
_sections.emplace_back(std::make_unique<HardwareSection>(title));
return *_sections.back();
}

void HardwareConfig::load()
{
for (auto& section : _sections) {
for (auto* field : section->allFields()) {
int32_t val;
if (_store.getInt(field->key(), val)) {
field->setRaw(val);
}
}
}
ESP_LOGI(TAG, "Hardware config loaded");
}

bool HardwareConfig::save()
{
for (auto& section : _sections) {
for (auto* field : section->allFields()) {
if (!_store.setInt(field->key(), field->rawValue())) {
ESP_LOGE(TAG, "Failed to set '%s'", field->key());
return false;
}
}
}
return _store.commit();
}

HardwareFieldBase* HardwareConfig::findField(const char* key)
{
for (auto& section : _sections) {
for (auto* field : section->allFields()) {
if (strcmp(field->key(), key) == 0) return field;
}
}
return nullptr;
}
Loading
Loading