Skip to content

Commit

Permalink
Merge pull request #751 from SignalK/repeat_templates
Browse files Browse the repository at this point in the history
Fix Repeat template classes
  • Loading branch information
mairas authored Oct 2, 2024
2 parents 0a36e7d + 771cf35 commit b762582
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
39 changes: 20 additions & 19 deletions src/sensesp/transforms/repeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <elapsedMillis.h>

#include "sensesp_base_app.h"
#include "sensesp/types/nullable.h"
#include "transform.h"

Expand All @@ -23,29 +24,29 @@ namespace sensesp {
*
* @param config_path Path to configure this transform in the Config UI.
*/
template <typename T>
class Repeat : public SymmetricTransform<T> {
template <typename FROM, typename TO>
class Repeat : public Transform<FROM, TO> {
public:
Repeat(long interval) : SymmetricTransform<T>(), interval_{interval} {}
Repeat(unsigned long interval) : Transform<FROM, TO>(), interval_{interval} {}

void set(T input) override {
virtual void set(const FROM& input) override {
this->emit(input);
if (repeat_event_ != nullptr) {
// Delete the old repeat event
repeat_event_->remove();
repeat_event_->remove(SensESPBaseApp::get_event_loop());
}
repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat(
interval_, [this]() { this->notify(); });
}

protected:
long interval_;
unsigned long interval_;
reactesp::RepeatEvent* repeat_event_ = nullptr;
};

// For compatibility with the old RepeatReport class
template <typename T>
class RepeatReport : public Repeat<T> {};
class RepeatReport : public Repeat<T, T> {};

/**
* @brief Repeat transform that stops emitting if the value age exceeds
Expand All @@ -54,10 +55,10 @@ class RepeatReport : public Repeat<T> {};
* @tparam T
*/
template <typename T>
class RepeatStopping : public Repeat<T> {
class RepeatStopping : public Repeat<T, T> {
public:
RepeatStopping(long interval, long max_age)
: Repeat<T>(interval), max_age_{max_age} {
RepeatStopping(unsigned long interval, unsigned long max_age)
: Repeat<T, T>(interval), max_age_{max_age} {
age_ = max_age;

if (this->repeat_event_ != nullptr) {
Expand All @@ -81,7 +82,7 @@ class RepeatStopping : public Repeat<T> {

protected:
elapsedMillis age_;
long max_age_;
unsigned long max_age_;

protected:
void repeat_function() {
Expand All @@ -104,15 +105,15 @@ class RepeatStopping : public Repeat<T> {
* @tparam T
*/
template <typename T>
class RepeatExpiring : public Repeat<Nullable<T>> {
class RepeatExpiring : public Repeat<T, Nullable<T>> {
public:
RepeatExpiring(long interval, long max_age)
: Repeat<T>(interval), max_age_{max_age} {
RepeatExpiring(unsigned long interval, unsigned long max_age)
: Repeat<T, Nullable<T>>(interval), max_age_{max_age} {
age_ = max_age;

if (this->repeat_event_ != nullptr) {
// Delete the old repeat event
this->repeat_event_->remove();
this->repeat_event_->remove(SensESPBaseApp::get_event_loop());
}
this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat(
this->interval_, [this]() { this->repeat_function(); });
Expand All @@ -123,15 +124,15 @@ class RepeatExpiring : public Repeat<Nullable<T>> {
age_ = 0;
if (this->repeat_event_ != nullptr) {
// Delete the old repeat event
this->repeat_event_->remove();
this->repeat_event_->remove(SensESPBaseApp::get_event_loop());
}
this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat(
this->interval_, [this]() { this->repeat_function(); });
}

protected:
elapsedMillis age_;
long max_age_;
unsigned long max_age_;

protected:
void repeat_function() {
Expand All @@ -140,7 +141,7 @@ class RepeatExpiring : public Repeat<Nullable<T>> {
if (age_ < max_age_) {
this->notify();
} else {
this->emit(Nullable::invalid());
this->emit(this->get().invalid());
}
};
};
Expand All @@ -159,7 +160,7 @@ class RepeatExpiring : public Repeat<Nullable<T>> {
template <typename T>
class RepeatConstantRate : public RepeatExpiring<T> {
public:
RepeatConstantRate(long interval, long max_age)
RepeatConstantRate(unsigned long interval, unsigned long max_age)
: RepeatExpiring<T>(interval, max_age) {
if (this->repeat_event_ != nullptr) {
// Delete the old repeat event
Expand Down
37 changes: 31 additions & 6 deletions src/sensesp/types/nullable.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
#include "nullable.h"

#include <limits>
#include <cstdint>
#include <limits>

namespace sensesp {

template <typename T> T Nullable<T>::invalid_value_ = T{};
template <> int Nullable<int>::invalid_value_ = std::numeric_limits<int>::lowest();
template <> float Nullable<float>::invalid_value_ = std::numeric_limits<float>::lowest();
template <> double Nullable<double>::invalid_value_ = std::numeric_limits<double>::lowest();
template <> char Nullable<char>::invalid_value_ = 255;
// Invalid values below are set equal to NMEA 2000 "missing data" values

template <typename T>
T Nullable<T>::invalid_value_ = T{};
template <>
float Nullable<float>::invalid_value_ = -1e9;
template <>
double Nullable<double>::invalid_value_ = -1e9;
template <>
char Nullable<char>::invalid_value_ = 0xff;
template <>
uint8_t Nullable<uint8_t>::invalid_value_ = 0xff;
template <>
int8_t Nullable<int8_t>::invalid_value_ = 0x7f;
template <>
uint16_t Nullable<uint16_t>::invalid_value_ = 0xffff;
template <>
int16_t Nullable<int16_t>::invalid_value_ = 0x7fff;
template <>
uint32_t Nullable<uint32_t>::invalid_value_ = 0xffffffff;
template <>
int32_t Nullable<int32_t>::invalid_value_ = 0x7fffffff;
template <>
uint64_t Nullable<uint64_t>::invalid_value_ = 0xffffffffffffffffLL;
template <>
int64_t Nullable<int64_t>::invalid_value_ = 0x7fffffffffffffffLL;

template <>
bool Nullable<bool>::invalid_value_ = false;


} // namespace sensesp
1 change: 1 addition & 0 deletions src/sensesp/types/nullable.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Nullable {
typedef Nullable<int> NullableInt;
typedef Nullable<float> NullableFloat;
typedef Nullable<double> NullableDouble;
typedef Nullable<bool> NullableBool;

template <typename T>
void convertFromJson(JsonVariantConst src, Nullable<T> &dst) {
Expand Down

0 comments on commit b762582

Please sign in to comment.