SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
frequency.cpp
Go to the documentation of this file.
1#include "frequency.h"
2
3namespace sensesp {
4
5// Frequency
6
7Frequency::Frequency(float multiplier, const String& config_path)
8 : Transform<int, float>(config_path),
9 multiplier_{multiplier},
10 last_update_(millis()) {
11 load();
12}
13
14void Frequency::set(const int& input) {
15 uint64_t const cur_millis = millis();
16 uint64_t const elapsed_millis = cur_millis - last_update_;
17 last_update_ = cur_millis;
18 this->emit(multiplier_ * input / (elapsed_millis / 1000.));
19}
20
21bool Frequency::to_json(JsonObject& root) {
22 root["multiplier"] = multiplier_;
23 return true;
24}
25
26bool Frequency::from_json(const JsonObject& config) {
27 String const expected[] = {"multiplier"};
28 for (auto str : expected) {
29 if (!config[str].is<JsonVariant>()) {
30 return false;
31 }
32 }
33 multiplier_ = config["multiplier"];
34 return true;
35}
36
37const String ConfigSchema(const Frequency& obj) {
38 return R"###({"type":"object","properties":{"multiplier":{"title":"Multiplier","type":"number"}}})###";
39}
40
41} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Transforms its input into frequency (Hz: cycles per second)
Definition frequency.h:22
virtual bool from_json(const JsonObject &config) override
Definition frequency.cpp:26
virtual void set(const int &input) override
Definition frequency.cpp:14
virtual bool to_json(JsonObject &root) override
Definition frequency.cpp:21
Frequency(float multiplier=1, const String &config_path="")
Definition frequency.cpp:7
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
void emit(const float &new_value)
const String ConfigSchema(const SmartSwitchController &obj)