Skip to content

Commit 549874a

Browse files
committed
🐛 WIP, analogio - bring in
#635
1 parent 067c220 commit 549874a

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/components/analogIO/controller.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,15 @@ void AnalogIOController::update() {
240240
continue;
241241

242242
// Pins timer has expired, lets read the pin
243-
// Read the pin's raw value
244-
uint16_t value = _analogio_hardware->GetPinValue(pin.name);
245243
if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
246-
// Since we already read the raw value, encode and publish it to the
247-
// broker
244+
// Read the pin's raw value
245+
uint16_t value = _analogio_hardware->GetPinValue(pin.name);
246+
// Encode and publish it to the broker
248247
EncodePublishPinValue(pin.name, value);
249248
} else if (pin.read_mode ==
250249
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE) {
251250
// Convert the raw value into voltage
252-
float pin_value = _analogio_hardware->CalculatePinVoltage(value);
251+
float pin_value = _analogio_hardware->GetPinVoltage(pin.name);
253252
// Encode and publish the voltage value to the broker
254253
EncodePublishPinVoltage(pin.name, pin_value);
255254
} else {

src/components/analogIO/hardware.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ void AnalogIOHardware::SetNativeADCResolution() {
7575
analogReadResolution(16);
7676
_native_adc_resolution = 12;
7777
#elif defined(ARDUINO_ARCH_ESP32)
78-
_is_adc_resolution_scaled = true;
79-
_native_adc_resolution = 13;
78+
_is_adc_resolution_scaled = false; // Defined in esp32-arduino BSP
79+
#if defined(ESP32S3)
80+
_native_adc_resolution = 13; // The ESP32-S3 ADC uses 13-bit resolution
81+
#else
82+
_native_adc_resolution =
83+
12; // The ESP32, ESP32-S2, ESP32-C3 ADCs uses 12-bit resolution
84+
#endif
8085
#elif defined(ARDUINO_ARCH_RP2040)
8186
_is_adc_resolution_scaled = true;
8287
_native_adc_resolution = 10;
@@ -125,7 +130,6 @@ void AnalogIOHardware::CalculateScaleFactor() {
125130
*/
126131
/*************************************************************************/
127132
uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
128-
// Get the pin's value using Arduino API
129133
uint16_t value = analogRead(pin);
130134
// Scale the pins value
131135
if (_is_adc_resolution_scaled) {
@@ -140,13 +144,16 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
140144

141145
/*************************************************************************/
142146
/*!
143-
@brief Calculates the voltage of an analog pin.
144-
@param raw_value
145-
The pin's raw value.
146-
@return The pin's voltage, in Volts.
147+
@brief Gets the voltage of an analog pin.
148+
@param pin
149+
The requested pin.
150+
@return The pin's voltage.
147151
*/
148152
/*************************************************************************/
149-
float AnalogIOHardware::CalculatePinVoltage(uint16_t raw_value) {
150-
float voltage = raw_value * _voltage_scale_factor;
151-
return voltage;
153+
float AnalogIOHardware::GetPinVoltage(uint8_t pin) {
154+
#ifdef ARDUINO_ARCH_ESP32
155+
return analogReadMilliVolts(pin) / 1000.0;
156+
#else
157+
return getPinValue(pin) * _ref_voltage / 65536;
158+
#endif // ARDUINO_ARCH_ESP32
152159
}

src/components/analogIO/hardware.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AnalogIOHardware {
3333
void InitPin(uint8_t pin);
3434
void DeinitPin(uint8_t pin);
3535
uint16_t GetPinValue(uint8_t pin);
36-
float CalculatePinVoltage(uint16_t raw_voltage);
36+
float GetPinVoltage(uint8_t pin);
3737

3838
private:
3939
uint8_t _native_adc_resolution; ///< Hardware's native ADC resolution.

0 commit comments

Comments
 (0)