Skip to content

Commit

Permalink
version -> 0.2.0
Browse files Browse the repository at this point in the history
- Variables: now _must_ be part of a VariableGroup
- VariableGroup is now passed as a reference instead of as a pointer, to make it more
  difficult to leave out.
- Work to support HA garage/cover, and MQTT-based commands
  • Loading branch information
chl33 committed Aug 9, 2024
1 parent 24fc177 commit caaedeb
Show file tree
Hide file tree
Showing 42 changed files with 129 additions and 112 deletions.
2 changes: 1 addition & 1 deletion examples/ha-app/ha-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Blink : public og3::Module {
: og3::Module("blink", &app->module_system()),
m_app(app),
m_vg("blink"),
m_high("led_on", false, "LED is on", &m_vg),
m_high("led_on", false, "LED is on", m_vg),
// Blink every 10 second, starting at first millisecond.
m_blink_timing(
1, 10 * og3::kMsecInSec, [this]() { blink(); }, &app->tasks()) {
Expand Down
2 changes: 1 addition & 1 deletion include/og3/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Adc : public Module {
static constexpr unsigned kMaxCounts = kResolution - 1;

Adc(const char* name_, uint8_t pin_, ModuleSystem* module_system_, const char* description,
unsigned var_flags, VariableGroup* vg);
unsigned var_flags, VariableGroup& vg);

Adc(const Adc&) = delete;

Expand Down
2 changes: 1 addition & 1 deletion include/og3/config_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConfigInterface : public Module {
static const char* kName; // module name

explicit ConfigInterface(ModuleSystem* module_system) OG3_NONNULL();
bool read_config(VariableGroup* var_group, const char* filename = nullptr) OG3_NONNULL((1));
bool read_config(VariableGroup& var_group, const char* filename = nullptr) OG3_NONNULL((1));
bool write_config(const VariableGroup& var_group, const char* filename = nullptr)
OG3_NONNULL((1));

Expand Down
2 changes: 1 addition & 1 deletion include/og3/din.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace og3 {
class DIn : public Module {
public:
DIn(const char* name_, ModuleSystem* module_system_, uint8_t pin_, const char* description,
VariableGroup* vg, bool publish = true, bool invert = false);
VariableGroup& vg, bool publish = true, bool invert = false);

// Read the dio pin and return isHigh().
bool read();
Expand Down
2 changes: 1 addition & 1 deletion include/og3/dout.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace og3 {
class DOut : public Module {
public:
DOut(const char* name_, bool initial_val, ModuleSystem* module_system_, uint8_t pin_,
const char* description, bool publish, VariableGroup* vg);
const char* description, bool publish, VariableGroup& vg);

void set(bool is_high);

Expand Down
5 changes: 4 additions & 1 deletion include/og3/ha_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ extern const char* kSignalStrength;
extern const char* kVoltage;
} // namespace sensor
namespace binary_sensor {
extern const char* kGarage;
extern const char* kLight;
extern const char* kMoisture;
extern const char* kMotion;
extern const char* kPower;
extern const char* kLight;
extern const char* kPresence;
} // namespace binary_sensor
} // namespace ha::device_class

Expand Down Expand Up @@ -86,6 +88,7 @@ class HADiscovery : public Module {
const char* subject_topic = nullptr;
const char* device_name = nullptr;
const char* icon = nullptr;
const char* command = nullptr;
ValueTemplateFn value_template_fn;
};
bool addEntry(JsonDocument* json, const Entry& entry);
Expand Down
2 changes: 1 addition & 1 deletion include/og3/kernel_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class KernelFilter {
size_t size; // The number of samples used for the smoothing kernel.
};

KernelFilter(const Options& options, ModuleSystem* module_system_, VariableGroup* vg);
KernelFilter(const Options& options, ModuleSystem* module_system_, VariableGroup& vg);

float value() const { return m_value.value(); }
float kernelValue(float dt) const { return exp(m_exp_scalar * dt * dt); }
Expand Down
4 changes: 2 additions & 2 deletions include/og3/mapped_analog_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class MappedAnalogSensor {
unsigned valid_in_max; // If the input reads more than this, the reading is invalid.
};

MappedAnalogSensor(const Options& options, ModuleSystem* module_system_, VariableGroup* cfgvg,
VariableGroup* vg);
MappedAnalogSensor(const Options& options, ModuleSystem* module_system_, VariableGroup& cfgvg,
VariableGroup& vg);

float map(int inval) const;
float read();
Expand Down
2 changes: 1 addition & 1 deletion include/og3/motion_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace og3 {
class MotionDetector : public DIn {
public:
MotionDetector(const char* name, ModuleSystem* module_system, uint8_t pin,
const char* description, VariableGroup* vg, bool publish = true,
const char* description, VariableGroup& vg, bool publish = true,
bool ha_discovery = true);

private:
Expand Down
2 changes: 1 addition & 1 deletion include/og3/mqtt_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MqttManager : public Module {

void mqttSend(const char topic[], const char content[]);
bool mqttSend(const VariableGroup& variables, unsigned flags = VariableBase::kNoPublish);

// arguments: (const char* topic, const char* payload, size_t len) {
using MqttMsgCallbackFn = std::function<void(const char*, const char*, size_t)>;
void subscribe(const String& topic, const MqttMsgCallbackFn& fn);

Expand Down
2 changes: 1 addition & 1 deletion include/og3/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PID {
float command_max = -1.0e10; // The maximum allowed output value.
};

PID(const Gains& gains, VariableGroup* vg, VariableGroup* cfg_vg, VariableGroup* cmd_vg);
PID(const Gains& gains, VariableGroup& vg, VariableGroup& cfg_vg, VariableGroup& cmd_vg);

void initialize() {
m_last_msec = millis();
Expand Down
2 changes: 1 addition & 1 deletion include/og3/pir.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace og3 {
class Pir : public Module {
public:
Pir(const char* module_name, const char* motion_name, ModuleSystem* module_system, Tasks* tasks,
uint8_t pin, const char* description, VariableGroup* vg, bool publish, bool ha_discovery);
uint8_t pin, const char* description, VariableGroup& vg, bool publish, bool ha_discovery);

void read();

Expand Down
2 changes: 1 addition & 1 deletion include/og3/relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Relay : public DOut {
};

Relay(const char* name_, Tasks* tasks, uint8_t pin_, const char* description, bool publish,
VariableGroup* vg, OnLevel on_level = OnLevel::kHigh);
VariableGroup& vg, OnLevel on_level = OnLevel::kHigh);

bool turnOn(int on_msec = 0, const std::function<void()>& off_fn = nullptr);
void turnOffIn(int msec, const std::function<void()>& fn = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion include/og3/sonar.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ constexpr float kSecPerUsec = 1e-6;
// This was used with a HC-SR04 sensor.
class Sonar : public Module {
public:
Sonar(const char* name, int trigPin, int echoPin, ModuleSystem* module_system, VariableGroup* vg,
Sonar(const char* name, int trigPin, int echoPin, ModuleSystem* module_system, VariableGroup& vg,
bool ha_discovery = true);

void setTemp(float tempC);
Expand Down
2 changes: 1 addition & 1 deletion include/og3/temp_humidity.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace og3 {
class TempHumidity : public Module {
public:
TempHumidity(const char* temp_name, const char* humidity_name, ModuleSystem* module_system_,
const char* description, VariableGroup* vg, bool publish = true,
const char* description, VariableGroup& vg, bool publish = true,
bool ha_discovery = true);

bool ok() const { return m_ok; }
Expand Down
35 changes: 22 additions & 13 deletions include/og3/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class VariableBase {
public:
VariableBase(const VariableBase&) = delete;
VariableBase(const char* name_, const char* units_, const char* description_, unsigned flags_,
VariableGroup* group);
VariableGroup& group);

virtual String string() const = 0;
virtual bool fromString(const String&) = 0;
Expand All @@ -75,7 +75,7 @@ class VariableBase {
const char* human_str() const {
return m_description && m_description[0] ? description() : name();
}
const VariableGroup* group() const { return m_group; }
const VariableGroup& group() const { return m_group; }

unsigned flags() const { return m_flags; }
bool settable() const { return testFlag(Flags::kSettable); }
Expand All @@ -93,14 +93,14 @@ class VariableBase {
const char* m_units;
const char* m_description;
const unsigned m_flags;
VariableGroup* m_group;
const VariableGroup& m_group;
bool m_failed = false;
};

class FloatVariableBase : public VariableBase {
public:
FloatVariableBase(const char* name_, const char* units_, const char* description_,
unsigned flags_, unsigned decimals_, VariableGroup* group)
unsigned flags_, unsigned decimals_, VariableGroup& group)
: VariableBase(name_, units_, description_, flags_, group), m_decimals(decimals_) {}

unsigned decimals() const { return m_decimals; }
Expand All @@ -113,7 +113,7 @@ template <typename T>
class Variable : public VariableBase {
public:
Variable(const char* name_, const T& value, const char* units_, const char* description_,
unsigned flags_, VariableGroup* group)
unsigned flags_, VariableGroup& group)
: VariableBase(name_, units_, description_, flags_, group), m_value(value) {}
String string() const override;
bool fromString(const String&) override;
Expand All @@ -137,7 +137,7 @@ class FloatingPointVariable : public FloatVariableBase {
public:
FloatingPointVariable(const char* name_, const T& value, const char* units_,
const char* description_, unsigned flags_, unsigned decimals_,
VariableGroup* group)
VariableGroup& group)
: FloatVariableBase(name_, units_, description_, flags_, decimals_, group), m_value(value) {}
String string() const override;
bool fromString(const String&) override;
Expand All @@ -159,15 +159,15 @@ class FloatingPointVariable : public FloatVariableBase {
class EnumVariableBase : public VariableBase {
public:
EnumVariableBase(const char* name_, const char* units_, const char* description_, unsigned flags_,
VariableGroup* group)
VariableGroup& group)
: VariableBase(name_, units_, description_, flags_, group) {}
};

class EnumStrVariableBase : public EnumVariableBase {
public:
EnumStrVariableBase(const char* name_, int value, const char* units_, const char* description_,
int min_value, int max_value, const char* value_names[], unsigned flags_,
VariableGroup* group);
VariableGroup& group);

String string() const override;
bool fromString(const String& value) override;
Expand All @@ -186,7 +186,7 @@ template <typename T>
class EnumVariable : public EnumVariableBase {
public:
EnumVariable(const char* name_, const T& value, const char* units_, const char* description_,
unsigned flags_, VariableGroup* group)
unsigned flags_, VariableGroup& group)
: EnumVariableBase(name_, units_, description_, flags_, group), m_value(value) {}
String string() const override { return String(static_cast<int>(value())); }
bool fromString(const String& value) override {
Expand Down Expand Up @@ -229,7 +229,7 @@ class EnumStrVariable : public EnumStrVariableBase {
public:
EnumStrVariable(const char* name_, const T& value, const char* units_, const char* description_,
T min_value, T max_value, const char* value_names[], unsigned flags_,
VariableGroup* group)
VariableGroup& group)
: EnumStrVariableBase(name_, static_cast<int>(value), units_, description_,
static_cast<int>(min_value), static_cast<int>(max_value), value_names,
flags_, group) {}
Expand Down Expand Up @@ -365,7 +365,7 @@ using DoubleVariable = FloatingPointVariable<double>;
class BoolVariable : public Variable<bool> {
public:
BoolVariable(const char* name_, const bool value, const char* description_, unsigned flags_,
VariableGroup* group)
VariableGroup& group)
: Variable<bool>(name_, value, "", description_, flags_, group) {}
String string() const final { return value() ? "true" : "false"; }
void toJson(JsonDocument* doc);
Expand All @@ -376,12 +376,21 @@ class BoolVariable : public Variable<bool> {
class BinarySensorVariable : public Variable<bool> {
public:
BinarySensorVariable(const char* name_, const bool value, const char* description_,
VariableGroup* group, bool publish = true)
VariableGroup& group, bool publish = true)
: Variable<bool>(name_, value, "", description_, publish ? 0 : kNoPublish, group) {}
String string() const final { return value() ? "ON" : "OFF"; }
String string() const override { return value() ? "ON" : "OFF"; }
void toJson(JsonDocument* doc);
bool fromJson(const JsonVariant& json);
BinarySensorVariable& operator=(bool value);
};

class BinaryCoverSensorVariable : public BinarySensorVariable {
public:
BinaryCoverSensorVariable(const char* name_, const bool value, const char* description_,
VariableGroup& group, bool publish = true)
: BinarySensorVariable(name_, value, description_, group, publish) {}
String string() const final { return value() ? "open" : "closed"; }
BinaryCoverSensorVariable& operator=(bool value);
};

} // namespace og3
4 changes: 2 additions & 2 deletions include/og3/web_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class VariableBase;
class VariableGroup;

#ifndef NATIVE
bool read(const AsyncWebServerRequest& request, VariableBase* var);
bool read(const AsyncWebServerRequest& request, VariableGroup* var_group);
bool read(const AsyncWebServerRequest& request, VariableBase& var);
bool read(const AsyncWebServerRequest& request, const VariableGroup& var_group);

// A module for managing a AsyncWebServer.
class WebServer : public Module {
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "og3",
"version": "0.1.6",
"version": "0.2.0",
"description": "A library for esp projects",
"keywords": "esp32, esp8266, modules, tasks, mqtt",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion src/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace og3 {

Adc::Adc(const char* name_, uint8_t pin_, ModuleSystem* module_system_, const char* description,
unsigned var_flags, VariableGroup* vg)
unsigned var_flags, VariableGroup& vg)
: Module(name_, module_system_),
m_pin(pin_),
m_counts(name_, false, "counts", description, var_flags, vg) {
Expand Down
4 changes: 2 additions & 2 deletions src/app_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ AppStatus::AppStatus(Tasks* tasks)
: Module(kName, tasks->module_system()),
m_tasks(tasks),
m_vg(kName),
m_mem_available("mem_avail", 0.0f, units::kKilobytes, "memory available", 0, 1, &m_vg),
m_uptime_msec("uptime", 0, units::kMilliseconds, "uptime", 0, &m_vg) {
m_mem_available("mem_avail", 0.0f, units::kKilobytes, "memory available", 0, 1, m_vg),
m_uptime_msec("uptime", 0, units::kMilliseconds, "uptime", 0, m_vg) {
add_link_fn([this](const NameToModule& name_to_module) -> bool {
m_mqtt_manager = MqttManager::get(name_to_module);
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/config_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ConfigInterface::ConfigInterface(ModuleSystem* module_system)

Logger* ConfigInterface::log() { return module_system()->log(); }

bool ConfigInterface::read_config(VariableGroup* var_group, const char* filename) {
bool ConfigInterface::read_config(VariableGroup& var_group, const char* filename) {
// Make sure flash is setup.
if (!m_fs || !m_fs->setup()) {
return false;
Expand All @@ -100,7 +100,7 @@ bool ConfigInterface::read_config(VariableGroup* var_group, const char* filename
if (filename) {
snprintf(fname, sizeof(fname), "%s%s", kFSRoot, filename);
} else {
snprintf(fname, sizeof(fname), "%s%s.json", kFSRoot, var_group->name());
snprintf(fname, sizeof(fname), "%s%s.json", kFSRoot, var_group.name());
}

if (!LittleFS.exists(fname)) {
Expand All @@ -115,7 +115,7 @@ bool ConfigInterface::read_config(VariableGroup* var_group, const char* filename
log()->debugf("Reading config file '%s'.", fname);
JsonDocument doc;
deserializeJson(doc, config_file);
for (auto* var : var_group->variables()) {
for (auto* var : var_group.variables()) {
if (!var->config()) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/din.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace og3 {

DIn::DIn(const char* name_, ModuleSystem* module_system_, uint8_t pin_, const char* description,
VariableGroup* vg, bool publish, bool invert)
VariableGroup& vg, bool publish, bool invert)
: Module(name_, module_system_),
m_pin(pin_),
m_invert(invert),
Expand Down
2 changes: 1 addition & 1 deletion src/dout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace og3 {
const char* strHigh(bool high) { return high ? "high" : "low"; }

DOut::DOut(const char* name_, bool initial_val, ModuleSystem* module_system_, uint8_t pin_,
const char* description, bool publish, VariableGroup* vg)
const char* description, bool publish, VariableGroup& vg)
: Module(name_, module_system_),
m_pin(pin_),
m_is_high(name_, false, description, vg, publish) {
Expand Down
2 changes: 1 addition & 1 deletion src/ha_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void HAApp::setup() { WebApp::setup(); }

void HAApp::handleMqttConfigRequest(AsyncWebServerRequest* request) {
#ifndef NATIVE
::og3::read(*request, &mqtt_manager().mutableVariables());
::og3::read(*request, mqtt_manager().mutableVariables());
m_web_page.clear();
html::writeFormTableInto(&m_web_page, mqtt_manager().variables());
m_web_page += F(HTML_BUTTON("/", "Back"));
Expand Down
Loading

0 comments on commit caaedeb

Please sign in to comment.