Browse Source

sns: clean-up sensor init

- BaseFilter must have virtual dtor
- magnitude object is not trivial, we need to manage filter lifetime
- Ensure move also moves filter object
- Clean-up using magnitude object by value in functions
mcspr-patch-1
Maxim Prokhorov 4 years ago
parent
commit
efdcdef196
3 changed files with 27 additions and 12 deletions
  1. +11
    -7
      code/espurna/filters/BaseFilter.h
  2. +16
    -3
      code/espurna/sensor.cpp
  3. +0
    -2
      code/espurna/sensor.h

+ 11
- 7
code/espurna/filters/BaseFilter.h View File

@ -10,15 +10,19 @@
class BaseFilter { class BaseFilter {
public: public:
virtual void add(double value) = 0;
virtual unsigned char count() = 0;
virtual void reset() = 0;
virtual double result() = 0;
virtual void resize(unsigned char size) = 0;
unsigned char size() { return _size; };
virtual ~BaseFilter() {}
virtual void add(double value) = 0;
virtual unsigned char count() = 0;
virtual void reset() = 0;
virtual double result() = 0;
virtual void resize(unsigned char size) = 0;
unsigned char size() { return _size; };
protected: protected:
unsigned char _size;
unsigned char _size;
}; };


+ 16
- 3
code/espurna/sensor.cpp View File

@ -227,6 +227,19 @@ struct sensor_magnitude_t {
} }
sensor_magnitude_t() = default; sensor_magnitude_t() = default;
sensor_magnitude_t& operator=(const sensor_magnitude_t&) = default;
sensor_magnitude_t(const sensor_magnitude_t&) = default;
sensor_magnitude_t(sensor_magnitude_t&& other) {
*this = other;
other.filter = nullptr;
}
~sensor_magnitude_t() {
delete filter;
}
sensor_magnitude_t(unsigned char slot, unsigned char index_local, unsigned char type, sensor::Unit units, BaseSensor* sensor); sensor_magnitude_t(unsigned char slot, unsigned char index_local, unsigned char type, sensor::Unit units, BaseSensor* sensor);
BaseSensor * sensor { nullptr }; // Sensor object BaseSensor * sensor { nullptr }; // Sensor object
@ -2165,7 +2178,7 @@ void _sensorLoad() {
void _sensorReport(unsigned char index, double value) { void _sensorReport(unsigned char index, double value) {
const auto& magnitude = _magnitudes.at(index);
auto& magnitude = _magnitudes.at(index);
// XXX: ensure that the received 'value' will fit here // XXX: ensure that the received 'value' will fit here
// dtostrf 2nd arg only controls leading zeroes and the // dtostrf 2nd arg only controls leading zeroes and the
@ -2596,7 +2609,7 @@ String magnitudeDescription(unsigned char index) {
String magnitudeTopicIndex(unsigned char index) { String magnitudeTopicIndex(unsigned char index) {
char topic[32] = {0}; char topic[32] = {0};
if (index < _magnitudes.size()) { if (index < _magnitudes.size()) {
sensor_magnitude_t magnitude = _magnitudes[index];
auto& magnitude = _magnitudes[index];
if (SENSOR_USE_INDEX || (sensor_magnitude_t::counts(magnitude.type) > 1)) { if (SENSOR_USE_INDEX || (sensor_magnitude_t::counts(magnitude.type) > 1)) {
snprintf(topic, sizeof(topic), "%s/%u", magnitudeTopic(magnitude.type).c_str(), magnitude.index_global); snprintf(topic, sizeof(topic), "%s/%u", magnitudeTopic(magnitude.type).c_str(), magnitude.index_global);
} else { } else {
@ -2724,7 +2737,7 @@ void sensorLoop() {
// Get readings // Get readings
for (unsigned char i=0; i<_magnitudes.size(); i++) { for (unsigned char i=0; i<_magnitudes.size(); i++) {
sensor_magnitude_t magnitude = _magnitudes[i];
auto& magnitude = _magnitudes[i];
if (magnitude.sensor->status()) { if (magnitude.sensor->status()) {


+ 0
- 2
code/espurna/sensor.h View File

@ -128,8 +128,6 @@ struct Energy {
}; };
struct Value { struct Value {
constexpr static size_t BufferSize { 33u };
double get(); double get();
double last; double last;


Loading…
Cancel
Save