Skip to content

Commit

Permalink
wip snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
s-t-a-n committed May 12, 2024
1 parent e0fa425 commit f6ba9ce
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 217 deletions.
74 changes: 55 additions & 19 deletions include/kaskas/KasKas.hpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,92 @@
#pragma once

#include "kaskas/components/clock.hpp"
#include "kaskas/components/relay.hpp"
#include "kaskas/component.hpp"
#include "kaskas/events.hpp"
#include "kaskas/io/clock.hpp"
#include "kaskas/io/relay.hpp"
#include "kaskas/subsystems/fluidsystem.hpp"
#include "kaskas/subsystems/growlights.hpp"
#include "kaskas/subsystems/ui.hpp"
#include "kaskas/subsystems/ventilation.hpp"

#include <spine/core/exception.hpp>
#include <spine/core/time.hpp>
#include <spine/eventsystem/eventsystem.hpp>
#include <spine/platform/hal.hpp>
#include <spine/structure/pointer.hpp>
#include <spine/structure/vector.hpp>

using Event = spn::core::Event;
using EventSystem = spn::core::EventSystem;
#include <AH/STL/vector>
namespace kaskas {

using spn::core::Event;
using spn::core::EventSystem;
using spn::structure::Vector;

class KasKas {
public:
struct Config {
EventSystem::Config esc_cfg;
Growlights::Config growlights_cfg;
Fluidsystem::Config fluidsystem_cfg;
UI::Config ui_cfg;
uint16_t component_cap = 1;
};

public:
explicit KasKas(Config& cfg)
: _cfg(cfg), //
_evsys({cfg.esc_cfg}), //
_growlights({&_evsys, cfg.growlights_cfg}), //
_fluidsystem(&_evsys, cfg.fluidsystem_cfg), //
_ui(&_evsys, cfg.ui_cfg) {}
_components(std::vector<std::unique_ptr<Component>>()) {}
KasKas(const KasKas& other) = delete;
KasKas(KasKas&& other) = delete;
KasKas& operator=(const KasKas&) = delete;
KasKas& operator=(const KasKas&&) = delete;

~KasKas() = default;

int setup() {
_growlights.initialize();
_fluidsystem.initialize();
_ui.initialize();
int initialize() {
// set the global exception handler;
spn::core::set_machine_exception_handler(new KasKasExceptionHandler{*this});

// initialize all components
for (const auto& component : _components) {
component->initialize();
}
_evsys.trigger(_evsys.event(Events::WakeUp, time_s(0), Event::Data()));
return 0;
}

void hotload_component(std::unique_ptr<Component> component) {
//
component->attach_event_system(&_evsys);
_components.emplace_back(std::move(component));
}

EventSystem& evsys() { return _evsys; };

int loop() {
_evsys.loop();
return 0;
}

private:
Config _cfg;
EventSystem _evsys;
class KasKasExceptionHandler final : public spn::core::ExceptionHandler {
public:
KasKasExceptionHandler(KasKas& kaskas) : _kk(kaskas) {}

void handle_exception(const Exception& exception) override {
//
DBGF("KasKasExceptionHandler: Handling exception: %s", exception.error_type());
for (auto& sf : _kk._components) {
sf->safe_shutdown(Component::State::CRITICAL);
}
}

private:
const KasKas& _kk;
};

private:
Growlights _growlights;
Fluidsystem _fluidsystem;
UI _ui;
Config _cfg;
EventSystem _evsys;
std::vector<std::unique_ptr<Component>> _components;
};
} // namespace kaskas
25 changes: 25 additions & 0 deletions include/kaskas/component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <spine/eventsystem/eventsystem.hpp>

namespace kaskas {

using spn::core::EventHandler;
using spn::core::EventSystem;

class Component : public EventHandler {
public:
enum class State { SAFE, CRITICAL };

public:
explicit Component() : Component(nullptr) {}
explicit Component(EventSystem* event_system) : EventHandler(event_system) {}

// virtual Component& operator=(Component&& other) {}
virtual void initialize() { assert(!"Virtual base function called"); }
virtual void safe_shutdown(State state = State::SAFE) { assert(!"Virtual base function called"); }

private:
};

} // namespace kaskas
2 changes: 2 additions & 0 deletions include/kaskas/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace kaskas {
enum class Events {
WakeUp, //
OutOfWater, //
VentilationStart, //
VentilationStop, //
WaterLevelCheck, //
WaterInjectCheck, //
WaterInjectStart, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <DS3231-RTC.h>
using DateTime = DS3231::DateTime;

namespace kaskas::components::clock {
namespace kaskas::io {

template<typename ClockImp>
class Clock {
Expand All @@ -27,4 +27,4 @@ class Clock {
private:
};

} // namespace kaskas::components::clock
} // namespace kaskas::io
2 changes: 2 additions & 0 deletions include/kaskas/io/implementations/DS18B20_Temp_Probe.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

// #include <AH/Arduino-Wrapper.h>
#include "kaskas/components/clock.hpp"
#include "kaskas/io/clock.hpp"

#include <DS3231-RTC.h>
#include <spine/core/debugging.hpp>
#include <spine/platform/hal.hpp>

#include <AH/STL/cstdint>

namespace kaskas::components::clock {
namespace kaskas::io::clock {

// threadsafe, not so much
class DS3231Clock final : Clock<DS3231Clock> {
Expand Down Expand Up @@ -60,6 +60,6 @@ class DS3231Clock final : Clock<DS3231Clock> {
}
};

} // namespace kaskas::components::clock
} // namespace kaskas::io::clock

using Clock = kaskas::components::clock::DS3231Clock;
using Clock = kaskas::io::clock::DS3231Clock;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "kaskas/components/relay.hpp"
#include "kaskas/io/relay.hpp"

#include <spine/core/exception.hpp>
#include <spine/core/timers.hpp>
Expand All @@ -10,6 +10,7 @@

#include <AH/STL/cstdint>

namespace kaskas::io {
using spn::core::Exception;
using spn::core::time::Timer;

Expand Down Expand Up @@ -68,11 +69,11 @@ struct Pump {
_ml = 0;
reset_interrupt_counter();
attach_interrupt();
_pump.set_state(DigitalState::ON);
_pump.set_state(LogicalState::ON);
}

void stop_injection() {
_pump.set_state(DigitalState::OFF);
_pump.set_state(LogicalState::OFF);
detach_interrupt();
_lifetime_ml += _ml;
_flowrate.reset_to(0);
Expand Down Expand Up @@ -116,4 +117,5 @@ struct Pump {
Timer _pump_timer; // tracks time since start of injection
Timer _last_reading; // tracks time since last interrupt reading
Timer _last_injection; // tracks time since last injection
};
};
} // namespace kaskas::io
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Relay {
explicit Relay(const Config&& cfg) : _cfg(cfg), _pin(std::move(_cfg.pin_cfg)) {}
~Relay() { delete _backoff_timer; }

void set_state(DigitalState state) {
void set_state(LogicalState state) {
// hard protect against flipping relay back on within backoff threshold

if (_backoff_timer && _cfg.backoff_time > time_ms(0) && _backoff_timer->timeSinceLast() < _cfg.backoff_time
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions include/kaskas/io/temperature_probe.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once

44 changes: 0 additions & 44 deletions include/kaskas/subsystems/clock.hpp

This file was deleted.

68 changes: 38 additions & 30 deletions include/kaskas/subsystems/fluidsystem.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "kaskas/components/clock.hpp"
#include "kaskas/components/pump.hpp"
#include "kaskas/components/relay.hpp"
#include "kaskas/components/sensor.hpp"
#include "kaskas/component.hpp"
#include "kaskas/events.hpp"
#include "kaskas/io/clock.hpp"
#include "kaskas/io/pump.hpp"
#include "kaskas/io/relay.hpp"
#include "kaskas/io/sensor.hpp"

#include <spine/core/exception.hpp>
#include <spine/core/timers.hpp>
Expand All @@ -14,6 +15,8 @@

#include <AH/STL/cstdint>

using kaskas::Component;
using kaskas::io::Pump;
using spn::core::Exception;
using spn::core::time::Timer;
using spn::eventsystem::Event;
Expand All @@ -23,7 +26,7 @@ using spn::filter::EWMA;
using Events = kaskas::Events;
using EventSystem = spn::core::EventSystem;

class Fluidsystem : public EventHandler {
class Fluidsystem final : public Component {
public:
using GroundMoistureSensorFilter = EWMA<double>;
using GroundMoistureSensor = Sensor<AnalogueInput, GroundMoistureSensorFilter>;
Expand All @@ -38,14 +41,41 @@ class Fluidsystem : public EventHandler {
};

public:
explicit Fluidsystem(Config& cfg) : Fluidsystem(nullptr, cfg) {}
Fluidsystem(EventSystem* evsys, Config& cfg)
: EventHandler(evsys), //
: Component(evsys), //
_cfg(cfg), //
_ground_moisture_sensor(std::move(cfg.ground_moisture_sensor_cfg)), //
_pump(std::move(cfg.pump_cfg)){};

void handle_event(Event* event) override {
switch (static_cast<Events>(event->id())) {
void initialize() override {
_pump.initialize();

assert(evsys());
evsys()->attach(Events::OutOfWater, this);
evsys()->attach(Events::WaterLevelCheck, this);
evsys()->attach(Events::WaterInjectCheck, this);
evsys()->attach(Events::WaterInjectStart, this);
evsys()->attach(Events::WaterInjectFollowUp, this);
evsys()->attach(Events::WaterInjectStop, this);

evsys()->schedule(evsys()->event(Events::WaterLevelCheck, time_s(10), Event::Data()));
evsys()->schedule(evsys()->event(Events::WaterInjectCheck, _cfg.inject_check_interval, Event::Data()));

{
char msg[512];
snprintf(msg, sizeof(msg), "Fluidsystem: scheduling WaterInjectCheck event in %lli hours (%lli minutes).",
time_h(_cfg.inject_check_interval).raw(), time_m(_cfg.inject_check_interval).raw());
DBG(msg);
}
// evsys()->schedule(evsys()->event(Events::WaterInjectStart, time_s(30), Event::Data()));
// evsys()->schedule(evsys()->event(Events::WaterInjectCheck, time_s(30), Event::Data()));
}

void safe_shutdown(State state) override { _pump.stop_injection(); }

void handle_event(const Event& event) override {
switch (static_cast<Events>(event.id())) {
case Events::OutOfWater: {
//
DBG("Fluidsystem: OutOfWater");
Expand Down Expand Up @@ -160,28 +190,6 @@ class Fluidsystem : public EventHandler {
default: assert(!"event not handled"); break;
}
}
void initialize() {
_pump.initialize();

evsys()->attach(Events::OutOfWater, this);
evsys()->attach(Events::WaterLevelCheck, this);
evsys()->attach(Events::WaterInjectCheck, this);
evsys()->attach(Events::WaterInjectStart, this);
evsys()->attach(Events::WaterInjectFollowUp, this);
evsys()->attach(Events::WaterInjectStop, this);

evsys()->schedule(evsys()->event(Events::WaterLevelCheck, time_s(10), Event::Data()));
evsys()->schedule(evsys()->event(Events::WaterInjectCheck, _cfg.inject_check_interval, Event::Data()));

{
char msg[512];
snprintf(msg, sizeof(msg), "Fluidsystem: scheduling WaterInjectCheck event in %lli hours (%lli minutes).",
time_h(_cfg.inject_check_interval).raw(), time_m(_cfg.inject_check_interval).raw());
DBG(msg);
}
// evsys()->schedule(evsys()->event(Events::WaterInjectStart, time_s(30), Event::Data()));
// evsys()->schedule(evsys()->event(Events::WaterInjectCheck, time_s(30), Event::Data()));
}

private:
const Config _cfg;
Expand Down
Loading

0 comments on commit f6ba9ce

Please sign in to comment.