Skip to content

Commit

Permalink
dumpster fire
Browse files Browse the repository at this point in the history
  • Loading branch information
s-t-a-n committed Jun 13, 2024
1 parent 7534857 commit 5d46f28
Show file tree
Hide file tree
Showing 24 changed files with 1,756 additions and 413 deletions.
12 changes: 8 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ build_flags =
-std=c++17
-std=gnu++17
-I include
-O3
-Wno-register
-Wno-format
-Wno-sign-compare
-Wno-parentheses
-Wno-unused-variable
-Wno-unused-but-set-variable
-u_printf_float
-u_scanf_float
-DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
-DFAKEIT_ASSERT_ON_UNEXPECTED_METHOD_INVOCATION
-DMAGIC_ENUM_NO_ASSERT
build_unflags =
-std=gnu++11
Expand All @@ -35,12 +40,11 @@ debug_build_flags =
-g3
-ggdb3
-O0
-u_printf_float
-u_scanf_float
-DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
; -u_printf_float
; -u_scanf_float
; -DPIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
-D_GLIBCXX_DEBUG
-D_GLIBCXX_ASSERTIONS
-DFAKEIT_ASSERT_ON_UNEXPECTED_METHOD_INVOCATION
check_tool = clangtidy
lib_ldf_mode = deep
check_flags =
Expand Down
4 changes: 4 additions & 0 deletions src/kaskas/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum class Events {
WakeUp, //
UIButtonCheck,
UIWatchDog,
UIPromptFollowUp,
SensorFollowUp,
OutOfWater,
VentilationFollowUp,
Expand All @@ -30,6 +31,9 @@ enum class Events {
VioletSpectrumTurnOff,
BroadSpectrumTurnOn,
BroadSpectrumTurnOff,
MetricsStartDatadump,
MetricsFollowUp,
MetricsStopDatadump,
Size
};
}; // namespace kaskas
5 changes: 4 additions & 1 deletion src/kaskas/io/peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class Peripheral {

bool needs_update() { return is_updateable() && _timer->expired(); }
bool is_updateable() const { return _timer != std::nullopt; }
time_ms update_interval() const { return _timer->sampling_interval(); }
time_ms update_interval() const {
assert(is_updateable());
return is_updateable() ? _timer->sampling_interval() : time_ms(0);
}

private:
std::optional<IntervalTimer> _timer;
Expand Down
9 changes: 9 additions & 0 deletions src/kaskas/io/provider.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#pragma once

#include "kaskas/data_providers.hpp"
#include "kaskas/prompt/rpc.hpp"

namespace kaskas::io {

/// Encapsulates a single provider of data
class Provider {
public:
virtual std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name,
const std::string_view& root) {
assert(!"Virtual base function called");
}

protected:
private:
};

Expand Down
17 changes: 17 additions & 0 deletions src/kaskas/io/providers/analogue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "kaskas/io/provider.hpp"

#include <magic_enum/magic_enum.hpp>
#include <spine/core/assert.hpp>

#include <functional>
Expand All @@ -15,6 +16,18 @@ class AnalogueSensor : public Provider {

double value() const { return _value_f(); }

std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name,
const std::string_view& root) override {
using namespace prompt;
auto model = std::make_unique<RPCRecipe>(
RPCRecipe(std::string(recipe_name), //
{
RPCModel(std::string(root),
[this](const OptStringView&) { return RPCResult(std::to_string(value())); }),
}));
return std::move(model);
}

private:
const std::function<double()> _value_f;
};
Expand All @@ -35,6 +48,10 @@ class AnalogueActuator : public Provider {
_map.fade_to_f(setpoint, increment, increment_interval);
}

std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name, const std::string_view& root) {
return {};
}

private:
FunctionMap _map;
};
Expand Down
4 changes: 4 additions & 0 deletions src/kaskas/io/providers/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Clock : public Provider {
UnixTime epoch() const { return _map.epoch_f(); }
bool is_ready() const { return _map.isready_f(); }

std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name, const std::string_view& root) {
return {};
}

private:
const FunctionMap _map;
};
Expand Down
18 changes: 18 additions & 0 deletions src/kaskas/io/providers/digital.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "kaskas/io/provider.hpp"

#include <magic_enum/magic_enum.hpp>

#include <functional>

namespace kaskas::io {
Expand All @@ -13,6 +15,18 @@ class DigitalSensor : public Provider {

LogicalState value() const { return _value_f(); }

std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name,
const std::string_view& root) override {
using namespace prompt;
auto model = std::make_unique<RPCRecipe>(
RPCRecipe(std::string(recipe_name), //
{
RPCModel(std::string(root),
[this](const OptStringView&) { return RPCResult(std::to_string(value())); }),
}));
return std::move(model);
}

private:
const std::function<LogicalState()> _value_f;
};
Expand All @@ -30,6 +44,10 @@ class DigitalActuator : public Provider {
double state() const { return _map.state_f(); }
void set_state(LogicalState state) { _map.set_state_f(state); }

std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name, const std::string_view& root) {
return {};
}

private:
FunctionMap _map;
};
Expand Down
4 changes: 4 additions & 0 deletions src/kaskas/io/providers/non_volatile_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace kaskas::io {

class NonVolatileMemory : public Provider {
public:
std::unique_ptr<prompt::RPCRecipe> rpc_recipe(const std::string_view& recipe_name, const std::string_view& root) {
return {};
}

private:
};

Expand Down
13 changes: 11 additions & 2 deletions src/kaskas/io/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "kaskas/io/providers/analogue.hpp"
#include "kaskas/io/providers/clock.hpp"
#include "kaskas/io/providers/digital.hpp"
#include "kaskas/prompt/cookbook.hpp"

#include <spine/core/standard.hpp>
#include <spine/structure/array.hpp>
Expand All @@ -28,6 +29,7 @@ class HardwareStack {
using Idx = uint16_t;

struct Config {
std::string_view alias;
Idx max_providers = 16;
Idx max_peripherals = 8;
};
Expand Down Expand Up @@ -66,14 +68,17 @@ class HardwareStack {
time_ms interval = time_ms(INT32_MAX);
for (auto& p : _peripherals) {
if (p && p->is_updateable() && p->update_interval() < interval) {
interval = p->update_interval();
interval = std::max(p->update_interval(), time_ms(1));
assert(interval > time_ms(0));
}
}
assert(interval != time_ms(INT32_MAX));
return interval;
}

const std::string_view& alias() const { return _cfg.alias; }
prompt::RPCCookbook& cookbook() { return _rpc_cookbook; }

public:
const AnalogueSensor& analog_sensor(Idx sensor_idx) {
assert(_providers[sensor_idx]);
Expand Down Expand Up @@ -106,16 +111,20 @@ class HardwareStack {
Array<std::unique_ptr<Peripheral>> _peripherals;
Array<std::shared_ptr<Provider>> _providers;

prompt::RPCCookbook _rpc_cookbook;

friend HardwareStackFactory;
};

class HardwareStackFactory {
public:
HardwareStackFactory(const HardwareStack::Config&& cfg) : _stack(std::make_shared<HardwareStack>(std::move(cfg))) {}

void hotload_provider(uint8_t provider_id, std::shared_ptr<Provider> provider) {
void hotload_provider(Providers::ProvidersEnum provider_id, std::shared_ptr<Provider> provider) {
assert(provider_id < _stack->_cfg.max_providers);
assert(_stack->_providers[provider_id] == nullptr);
stack()->cookbook().add_recipe(
std::move(provider->rpc_recipe(stack()->alias(), magic_enum::enum_name(provider_id))));
_stack->_providers[provider_id] = std::move(provider);
}
void hotload_peripheral(uint8_t peripheral_id, std::unique_ptr<Peripheral> peripheral) {
Expand Down
25 changes: 15 additions & 10 deletions src/kaskas/kaskas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "kaskas/io/peripherals/relay.hpp"
#include "kaskas/io/providers/clock.hpp"
#include "kaskas/io/stack.hpp"
#include "kaskas/prompt/cookbook.hpp"
#include "kaskas/prompt/prompt.hpp"
#include "kaskas/subsystems/climatecontrol.hpp"
#include "kaskas/subsystems/fluidsystem.hpp"
Expand Down Expand Up @@ -55,8 +56,8 @@ class KasKas {
// auto dl = std::make_shared<MockDatalink>(
// MockDatalink::Config{.message_length = prompt_cfg.message_length, .pool_size =
// prompt_cfg.pool_size});
_prompt = std::make_unique<Prompt>(std::move(*_cfg.prompt_cfg));
_prompt.value()->hotload_datalink(std::move(dl));
_prompt = std::make_shared<Prompt>(std::move(*_cfg.prompt_cfg));
_prompt->hotload_datalink(std::move(dl));
}
}
KasKas(const KasKas& other) = delete;
Expand All @@ -76,7 +77,7 @@ class KasKas {
}

if (_cfg.prompt_cfg) {
_prompt.value()->initialize();
_prompt->initialize();
DBGF("Initialized prompt");
}

Expand All @@ -88,24 +89,28 @@ class KasKas {
component->attach_event_system(&_evsys);

if (_cfg.prompt_cfg) {
if (auto model = component->rpc_recipe()) {
assert(model != nullptr);
if (auto recipe = component->rpc_recipe()) {
assert(recipe != nullptr);
DBGF("Hotloading component rpc recipes!");
_prompt.value()->hotload_rpc_recipe(std::move(model));
hotload_rpc_recipe(std::move(recipe));
}
}
_components.emplace_back(std::move(component));
}

void hotload_rpc_recipe(std::unique_ptr<prompt::RPCRecipe> recipe) {
_prompt->hotload_rpc_recipe(std::move(recipe));
}

EventSystem& evsys() { return _evsys; };
std::shared_ptr<Prompt> prompt() { return _prompt; }

int loop() {
_hws->update_all();
// _hws->update_all();
_evsys.loop();
if (_cfg.prompt_cfg) {
assert(_prompt);
assert(_prompt.value());
_prompt.value()->update();
// _prompt->update();
}
return 0;
}
Expand All @@ -132,6 +137,6 @@ class KasKas {
EventSystem _evsys;
std::shared_ptr<io::HardwareStack> _hws;
std::vector<std::unique_ptr<Component>> _components;
std::optional<std::unique_ptr<Prompt>> _prompt = nullptr;
std::shared_ptr<Prompt> _prompt;
};
} // namespace kaskas
13 changes: 7 additions & 6 deletions src/kaskas/prompt/charbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ namespace kaskas::prompt {

class CharBuffer {
private:
std::unique_ptr<char> _buffer;
std::unique_ptr<char[]> _buffer;

public:
char* raw;
char* const raw;
const size_t capacity;
size_t length = 0;

void reset(bool zero = true) {
length = 0;
memset((unsigned char*)raw, '\0', capacity);
if (zero)
memset((unsigned char*)raw, '\0', capacity);
}

explicit CharBuffer(size_t buffer_length)
: _buffer(std::make_unique<char>(buffer_length)), raw(_buffer.get()), capacity(buffer_length) {}
: _buffer(std::make_unique<char[]>(buffer_length)), raw(_buffer.get()), capacity(buffer_length) {}

~CharBuffer() {
DBGF("Charbuffer scuddling off our mortal coil");
delete _buffer.get();
DBGF("Destroying Charbuffer");
reset();
_buffer.reset();
}
};
Expand Down
Loading

0 comments on commit 5d46f28

Please sign in to comment.