66 */
77
88#pragma once
9- #include < cstdint>
109#include < type_traits>
1110
12- #include " HALAL/Services/ADC/NewADC .hpp"
11+ #include " Sensors/Common/ADCSensor .hpp"
1312
1413template <class Type >
1514 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
16- class LinearSensor {
15+ class LinearSensor : protected ST_LIB ::Sensors::ADCValueSensor<Type> {
1716public:
1817 LinearSensor () = default ;
1918 LinearSensor (
@@ -34,19 +33,21 @@ class LinearSensor {
3433 void read ();
3534
3635 void set_offset (Type new_offset);
37- Type get_offset ();
36+ [[nodiscard]] Type get_offset () const ;
3837
3938 void set_gain (Type new_gain);
40- Type get_gain ();
39+ [[nodiscard]] Type get_gain () const ;
4140
42- Type* get_value_pointer () const ;
41+ [[nodiscard]] Type* get_value_pointer () const ;
4342
4443protected:
45- ST_LIB ::ADCDomain::Instance* adc = nullptr ;
46- Type slope;
47- Type offset;
48- Type* value = nullptr ;
49- float vref = 3 .3f ;
44+ using Base = ST_LIB ::Sensors::ADCValueSensor<Type>;
45+
46+ [[nodiscard]] Type compute_value_from_voltage (float voltage) const ;
47+
48+ Type slope{};
49+ Type offset{};
50+ float vref = ST_LIB ::Sensors::kDefaultADCReferenceVoltage ;
5051};
5152
5253template <class Type >
@@ -58,7 +59,7 @@ LinearSensor<Type>::LinearSensor(
5859 Type* value,
5960 float vref
6061)
61- : adc(& adc), slope(slope), offset(offset), value(value ), vref(vref) {}
62+ : Base( adc, value ), slope(slope), offset(offset), vref(vref) {}
6263
6364template <class Type >
6465 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
@@ -69,29 +70,34 @@ LinearSensor<Type>::LinearSensor(
6970 Type& value,
7071 float vref
7172)
72- : LinearSensor::LinearSensor (adc, slope, offset, &value, vref) {}
73+ : LinearSensor(adc, slope, offset, &value, vref) {}
7374
7475template <class Type >
7576 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
7677void LinearSensor<Type>::read() {
77- if (adc == nullptr || value == nullptr ) {
78+ if (! this -> is_configured () ) {
7879 return ;
7980 }
80- const float raw = adc->get_raw ();
81- const float val = adc->get_value_from_raw (raw, vref);
8281
83- *value = slope * (Type)val + offset;
82+ *this ->value = compute_value_from_voltage (this ->read_voltage (vref));
83+ }
84+
85+ template <class Type >
86+ requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
87+ Type LinearSensor<Type>::compute_value_from_voltage(float voltage) const {
88+ const Type sensor_voltage = static_cast <Type>(voltage);
89+ return slope * sensor_voltage + offset;
8490}
8591
8692template <class Type >
8793 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
88- Type LinearSensor<Type>::get_offset() {
94+ Type LinearSensor<Type>::get_offset() const {
8995 return offset;
9096}
9197
9298template <class Type >
9399 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
94- Type LinearSensor<Type>::get_gain() {
100+ Type LinearSensor<Type>::get_gain() const {
95101 return slope;
96102}
97103
@@ -110,5 +116,5 @@ void LinearSensor<Type>::set_gain(Type new_gain) {
110116template <class Type >
111117 requires std::is_integral_v<Type> || std::is_floating_point_v<Type>
112118Type* LinearSensor<Type>::get_value_pointer() const {
113- return value;
119+ return this -> value ;
114120}
0 commit comments