1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <typeinfo>
using AT = double;
using PT = unsigned long long;
template<typename R = AT>
class iAllSensors {
public:
virtual R GetData(void) const = 0;
};
template<typename R = AT>
class AllSensors : public iAllSensors<R> {
protected:
uint16_t m_SensorPort {};
public:
virtual R GetData() const = 0;
};
template <size_t arraysize, typename R = AT>
class AnalogSensor : public AllSensors<R> {
protected:
R m_DataArray[arraysize] {};
uint32_t m_NumberOfSamples {};
R DoMath() const ;
public:
AnalogSensor(uint8_t SensorPort) {
this->m_SensorPort = SensorPort;
}
R GetData(void) const override;
};
template <size_t arraysize, typename R = AT>
class CurrentLoopSensor : public AnalogSensor<arraysize, R> {
protected:
R m_4maValue {};
R m_20maValue {};
public:
CurrentLoopSensor(uint8_t SensorPort, R a4maValue, R a20maValue) :
AnalogSensor<arraysize, R>(SensorPort), m_4maValue(a4maValue), m_20maValue(a20maValue) {}
R GetData(void) const override;
};
template <size_t arraysize, typename R = PT>
class PulseSensor : public AnalogSensor<arraysize, R> {
protected:
R m_Pulses {};
void AddData();
public:
PulseSensor(uint8_t SensorPort) : AnalogSensor<arraysize, R>(SensorPort) {}
R GetData(void) const override;
};
template <size_t arraysize, typename R>
R AnalogSensor<arraysize, R>::DoMath() const {
if (m_NumberOfSamples % 2 != 0)
return m_DataArray[m_NumberOfSamples / 2];
return m_DataArray[(m_NumberOfSamples - 1) / 2] + m_DataArray[m_NumberOfSamples / 2] / 2.0;
}
template <size_t arraysize, typename R>
R AnalogSensor<arraysize, R>::GetData() const {
return DoMath();
}
template <size_t arraysize, typename R>
void PulseSensor<arraysize, R>::AddData() {
++m_Pulses;
}
template <size_t arraysize, typename R>
R PulseSensor<arraysize, R>::GetData() const {
return m_Pulses;
}
int main() {
AnalogSensor<2> as{1};
PulseSensor<3> ps{2};
auto ar { as.GetData() };
auto ap { ps.GetData() };
std::cout << "type of as.GetData() is " << typeid(ar).name() << '\n';
std::cout << "type of ps.getData() is " << typeid(ap).name() << '\n';
}
| |