Move the SimpleAnalyzer to a separate file

This commit is contained in:
acfloria
2018-04-11 15:15:28 +02:00
committed by Beat Küng
parent ae807fc189
commit fb1bb7a769
5 changed files with 311 additions and 169 deletions
-54
View File
@@ -43,8 +43,6 @@
#include "mavlink_stream.h"
#include <limits>
class StreamListItem
{
@@ -63,56 +61,4 @@ public:
const char *get_stream_name(const uint16_t msg_id);
MavlinkStream *create_mavlink_stream(const char *stream_name, Mavlink *mavlink);
class SimpleAnalyzer
{
public:
enum Mode {
AVERAGE = 0,
MIN,
MAX,
};
SimpleAnalyzer(Mode mode, float window = 60.0f);
virtual ~SimpleAnalyzer();
void reset();
void add_value(float val, float update_rate);
bool valid() const;
float get() const;
float get_scaled(float scalingfactor) const;
template <typename T>
void get_scaled(T &ret, float scalingfactor) const
{
float avg = get_scaled(scalingfactor);
int_round(avg);
check_limits(avg, std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
ret = static_cast<T>(avg);
}
private:
unsigned int _n = 0;
float _window = 60.0f;
Mode _mode = AVERAGE;
float _result = 0.0f;
void check_limits(float &x, float min, float max) const;
void int_round(float &x) const;
};
template<typename Tin, typename Tout>
void convert_limit_safe(Tin in, Tout *out)
{
if (in > std::numeric_limits<Tout>::max()) {
*out = std::numeric_limits<Tout>::max();
} else if (in < std::numeric_limits<Tout>::min()) {
*out = std::numeric_limits<Tout>::min();
} else {
*out = static_cast<Tout>(in);
}
}
#endif /* MAVLINK_MESSAGES_H_ */