Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / telemetry / telemetry_holders.h
blob70b2b6a9cc161fa7f7966607387d145f2cba8023
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _TELEMETRY_HOLDERS_H_
22 #define _TELEMETRY_HOLDERS_H_
24 #include <inttypes.h>
25 #include "../timers.h"
27 #define TELEMETRY_AVERAGE_COUNT 3 // we actually average one more reading!
28 #define RAW_FRSKY_MINMAX(v) v.values[TELEMETRY_AVERAGE_COUNT-1]
30 class TelemetryValue {
31 public:
32 uint8_t value()
34 return _value;
37 void set(uint8_t value)
39 _value = value;
42 void reset()
44 set(0);
47 protected:
48 uint8_t _value;
51 template <class T>
52 class TelemetryFilterDecorator: public T {
53 public:
54 void set(uint8_t value)
56 if (value == 0 || this->_value == 0) {
57 memset(values, value, TELEMETRY_AVERAGE_COUNT);
58 this->_value = value;
60 else {
61 // calculate the average from values[] and value
62 // also shift readings in values [] array
63 unsigned int sum = values[0];
64 for (int i=0; i<TELEMETRY_AVERAGE_COUNT-1; i++) {
65 uint8_t tmp = values[i+1];
66 values[i] = tmp;
67 sum += tmp;
69 values[TELEMETRY_AVERAGE_COUNT-1] = value;
70 sum += value;
71 this->_value = sum / (TELEMETRY_AVERAGE_COUNT+1);
75 void reset()
77 memclear(this, sizeof(*this));
80 protected:
81 uint8_t values[TELEMETRY_AVERAGE_COUNT];
84 template <class T>
85 class TelemetryExpiringDecorator: public T {
86 public:
87 void set(uint8_t value)
89 T::set(value);
90 expirationTime = get_tmr10ms() + 1000/*10s*/;
93 void reset()
95 memclear(this, sizeof(*this));
98 bool isFresh()
100 return get_tmr10ms() < expirationTime;
103 protected:
104 tmr10ms_t expirationTime;
107 template <class T>
108 class TelemetryMinDecorator: public T {
109 public:
110 void set(uint8_t value)
112 T::set(value);
113 if (!_min || value < _min) {
114 _min = value;
118 uint8_t min()
120 return _min;
123 void reset()
125 memclear(this, sizeof(*this));
128 protected:
129 uint8_t _min;
132 #endif // _TELEMETRY_HOLDERS_H_