Skip to content

Commit

Permalink
settings: continue refactoring internal constants
Browse files Browse the repository at this point in the history
Settling on naming 'options' for enumerations (...possibly, everything
else in the future, would that make sense to store for 'setting' object)

Update terminal commands that were reporting status to also report a
full list of 'indexed' settings for the specific entity
Also updates the WebUI outputs which are (hopefuly) are already handled
as-is through the .js processing pipeline and the .html properties
receiving certain special string values

More namespacing and save ~2KiB of RAM by reducing the amount of loaded keys strings
However, ROM side of things may suffer b/c of template specializations for the
generic conversion functions when there are many different types involved.
  • Loading branch information
mcspr committed Dec 29, 2021
1 parent 7727d7f commit e213b58
Show file tree
Hide file tree
Showing 24 changed files with 2,517 additions and 1,363 deletions.
492 changes: 325 additions & 167 deletions code/espurna/button.cpp

Large diffs are not rendered by default.

88 changes: 65 additions & 23 deletions code/espurna/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,53 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <WiFiUdp.h>
#endif

namespace espurna {
namespace debug {
namespace settings {
namespace internal {
namespace options {
namespace {

using ::settings::options::Enumeration;

alignas(4) static constexpr char Disabled[] PROGMEM = "off";
alignas(4) static constexpr char Enabled[] PROGMEM = "on";
alignas(4) static constexpr char SkipBoot[] PROGMEM = "skip-boot";

static constexpr const EnumOption<DebugLogMode> DebugLogModeOptions[] PROGMEM {
static constexpr Enumeration<DebugLogMode> DebugLogModeOptions[] PROGMEM {
{DebugLogMode::Disabled, Disabled},
{DebugLogMode::Enabled, Enabled},
{DebugLogMode::SkipBoot, SkipBoot},
};

} // namespace
} // namespace options

namespace keys {
namespace {

alignas(4) static constexpr char SdkDebug[] PROGMEM = "dbgSDK";
alignas(4) static constexpr char Mode[] PROGMEM = "dbgLogMode";
alignas(4) static constexpr char Buffer[] PROGMEM = "dbgLogBuf";
alignas(4) static constexpr char BufferSize[] PROGMEM = "dbgLogBufSize";

alignas(4) static constexpr char HeartbeatMode[] PROGMEM = "dbgHbMode";
alignas(4) static constexpr char HeartbeatInterval[] PROGMEM = "dbgHbIntvl";

} // namespace
} // namespace keys
} // namespace settings
} // namespace debug
} // namespace espurna

namespace settings {
namespace internal {
namespace {

using espurna::debug::settings::options::DebugLogModeOptions;

} // namespace

String serialize(DebugLogMode value) {
String serialize(::DebugLogMode value) {
return serialize(DebugLogModeOptions, value);
}

Expand All @@ -57,6 +87,7 @@ DebugLogMode convert(const String& value) {
} // namespace internal
} // namespace settings

namespace espurna {
namespace debug {
namespace {

Expand All @@ -75,7 +106,10 @@ struct Timestamp {
bool _value { false };
};

} // namespace

namespace build {
namespace {

constexpr Timestamp AddTimestamp { 1 == DEBUG_ADD_TIMESTAMP };

Expand Down Expand Up @@ -103,42 +137,49 @@ constexpr size_t bufferSize() {
return DEBUG_LOG_BUFFER_SIZE;
}

} // namespace
} // namespace build

namespace settings {
namespace {

bool sdkDebug() {
return getSetting("dbgSDK", build::sdkDebug());
return getSetting(keys::SdkDebug, build::sdkDebug());
}

DebugLogMode mode() {
return getSetting("dbgLogMode", build::mode());
return getSetting(keys::Mode, build::mode());
}

bool buffer() {
return getSetting("dbgLogBuf", build::buffer());
return getSetting(keys::Buffer, build::buffer());
}

size_t bufferSize() {
return getSetting("dbgLogBufSize", build::bufferSize());
return getSetting(keys::BufferSize, build::bufferSize());
}

espurna::heartbeat::Mode heartbeatMode() {
return getSetting("dbgHbMode", espurna::heartbeat::currentMode());
return getSetting(keys::HeartbeatMode, espurna::heartbeat::currentMode());
}

espurna::duration::Seconds heartbeatInterval() {
return getSetting("dbgHbIntvl", espurna::heartbeat::currentInterval());
return getSetting(keys::HeartbeatInterval, espurna::heartbeat::currentInterval());
}

} // namespace
} // namespace settings

namespace internal {
namespace {

bool enabled { false };

} // namespace
} // namespace internal

namespace {

bool enabled() {
return internal::enabled;
}
Expand Down Expand Up @@ -610,35 +651,36 @@ void onBoot() {

} // namespace
} // namespace debug
} // namespace espurna

void debugSendBytes(const uint8_t* bytes, size_t size) {
debug::buffer::sendBytes(bytes, size);
espurna::debug::buffer::sendBytes(bytes, size);
}

#if DEBUG_LOG_BUFFER_SUPPORT
bool debugLogBuffer() {
return debug::buffer::enabled();
return espurna::debug::buffer::enabled();
}
#endif

void debugSend(const char* format, ...) {
if (debug::enabled()) {
if (espurna::debug::enabled()) {
va_list args;
va_start(args, format);
debug::formatAndSend(format, args);
espurna::debug::formatAndSend(format, args);
va_end(args);
}
}

void debugConfigureBoot() {
debug::onBoot();
espurna::debug::onBoot();
}

#if WEB_SUPPORT
void debugWebSetup() {
wsRegister()
.onVisible(debug::web::onVisible)
.onAction(debug::web::onAction);
.onVisible(espurna::debug::web::onVisible)
.onAction(espurna::debug::web::onAction);
}
#endif

Expand All @@ -648,24 +690,24 @@ void debugSetup() {
#endif

#if DEBUG_UDP_SUPPORT
if (debug::syslog::build::enabled()) {
debug::syslog::configure();
espurnaRegisterReload(debug::syslog::configure);
if (espurna::debug::syslog::build::enabled()) {
espurna::debug::syslog::configure();
espurnaRegisterReload(espurna::debug::syslog::configure);
}
#endif

#if DEBUG_LOG_BUFFER_SUPPORT
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("DEBUG.BUFFER"), [](::terminal::CommandContext&& ctx) {
debug::buffer::disable();
if (!debug::buffer::size()) {
espurna::debug::buffer::disable();
if (!espurna::debug::buffer::size()) {
terminalError(ctx, F("buffer is empty\n"));
return;
}

ctx.output.printf_P(PSTR("buffer size: %u / %u bytes\n"),
debug::buffer::size(), debug::buffer::capacity());
debug::buffer::dump(ctx.output);
espurna::debug::buffer::size(), espurna::debug::buffer::capacity());
espurna::debug::buffer::dump(ctx.output);
terminalOK(ctx);
});
#endif
Expand Down
49 changes: 36 additions & 13 deletions code/espurna/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,48 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>

#include "ws.h"

namespace espurna {
namespace gpio {
namespace settings {
namespace internal {
namespace options {
namespace {

template <>
GpioType convert(const String& value) {
alignas(4) static constexpr char None[] PROGMEM = "none";
alignas(4) static constexpr char Hardware[] PROGMEM = "hardware";
using ::settings::options::Enumeration;

alignas(4) static constexpr char None[] PROGMEM = "none";
alignas(4) static constexpr char Hardware[] PROGMEM = "hardware";

[[gnu::unused]] alignas(4) static constexpr char Mcp23s08[] PROGMEM = "mcp23s08";
[[gnu::unused]] alignas(4) static constexpr char Mcp23s08[] PROGMEM = "mcp23s08";

constexpr static const EnumOption<GpioType> options[] PROGMEM {
{GpioType::Hardware, Hardware},
static constexpr Enumeration<GpioType> GpioTypeOptions[] PROGMEM {
{GpioType::Hardware, Hardware},
#if MCP23S08_SUPPORT
{GpioType::Mcp23s08, Mcp23s08},
{GpioType::Mcp23s08, Mcp23s08},
#endif
{GpioType::None, None},
};
{GpioType::None, None},
};

} // namespace
} // namespace options
} // namespace settings
} // namespace gpio
} // namespace espurna

namespace settings {
namespace internal {
namespace {

using espurna::gpio::settings::options::GpioTypeOptions;

} // namespace

template <>
GpioType convert(const String& value) {
return convert(GpioTypeOptions, value, GpioType::None);
}

return convert(options, value, GpioType::None);
String serialize(GpioType value) {
return serialize(GpioTypeOptions, value);
}

} // namespace internal
Expand Down Expand Up @@ -99,7 +122,7 @@ class GpioHardware : public GpioBase {
return false;
}

BasePinPtr pin(unsigned char index) {
BasePinPtr pin(unsigned char index) override {
return std::make_unique<GpioPin>(index);
}

Expand Down
8 changes: 8 additions & 0 deletions code/espurna/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ enum class GpioType : int {
Mcp23s08
};

namespace settings {
namespace internal {

String serialize(GpioType);

} // namespace internal
} // namespace settings

class GpioBase {
public:
virtual const char* id() const = 0;
Expand Down
17 changes: 15 additions & 2 deletions code/espurna/ifan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,35 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

// TODO: in case there are more FANs, move externally

namespace ifan {
namespace settings {
namespace internal {
namespace options {
namespace {

alignas(4) static constexpr char Off[] PROGMEM = "off";
alignas(4) static constexpr char Low[] PROGMEM = "low";
alignas(4) static constexpr char Medium[] PROGMEM = "medium";
alignas(4) static constexpr char High[] PROGMEM = "high";

static constexpr const std::array<EnumOption<FanSpeed>, 4> FanSpeedOptions PROGMEM {
using ::settings::options::Enumeration;
static constexpr std::array<Enumeration<FanSpeed>, 4> FanSpeedOptions PROGMEM {
{{FanSpeed::Off, Off},
{FanSpeed::Low, Low},
{FanSpeed::Medium, Medium},
{FanSpeed::High, High}}
};

} // namespace
} // namespace options
} // namespace settings
} // namespace ifan

namespace settings {
namespace internal {
namespace {

using ifan::settings::options::FanSpeedOptions;

} // namespace

template <>
Expand Down
Loading

0 comments on commit e213b58

Please sign in to comment.