From 549874a2f02ec03e1b51fb4b8a37b0bd0a930fcd Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 3 Oct 2024 12:06:08 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20WIP,=20analogio=20-=20bring=20in?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #635 --- src/components/analogIO/controller.cpp | 9 ++++----- src/components/analogIO/hardware.cpp | 27 ++++++++++++++++---------- src/components/analogIO/hardware.h | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/components/analogIO/controller.cpp b/src/components/analogIO/controller.cpp index 15785d171..d0d233150 100644 --- a/src/components/analogIO/controller.cpp +++ b/src/components/analogIO/controller.cpp @@ -240,16 +240,15 @@ void AnalogIOController::update() { continue; // Pins timer has expired, lets read the pin - // Read the pin's raw value - uint16_t value = _analogio_hardware->GetPinValue(pin.name); if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) { - // Since we already read the raw value, encode and publish it to the - // broker + // Read the pin's raw value + uint16_t value = _analogio_hardware->GetPinValue(pin.name); + // Encode and publish it to the broker EncodePublishPinValue(pin.name, value); } else if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE) { // Convert the raw value into voltage - float pin_value = _analogio_hardware->CalculatePinVoltage(value); + float pin_value = _analogio_hardware->GetPinVoltage(pin.name); // Encode and publish the voltage value to the broker EncodePublishPinVoltage(pin.name, pin_value); } else { diff --git a/src/components/analogIO/hardware.cpp b/src/components/analogIO/hardware.cpp index 76f783a6e..839566304 100644 --- a/src/components/analogIO/hardware.cpp +++ b/src/components/analogIO/hardware.cpp @@ -75,8 +75,13 @@ void AnalogIOHardware::SetNativeADCResolution() { analogReadResolution(16); _native_adc_resolution = 12; #elif defined(ARDUINO_ARCH_ESP32) - _is_adc_resolution_scaled = true; - _native_adc_resolution = 13; + _is_adc_resolution_scaled = false; // Defined in esp32-arduino BSP +#if defined(ESP32S3) + _native_adc_resolution = 13; // The ESP32-S3 ADC uses 13-bit resolution +#else + _native_adc_resolution = + 12; // The ESP32, ESP32-S2, ESP32-C3 ADCs uses 12-bit resolution +#endif #elif defined(ARDUINO_ARCH_RP2040) _is_adc_resolution_scaled = true; _native_adc_resolution = 10; @@ -125,7 +130,6 @@ void AnalogIOHardware::CalculateScaleFactor() { */ /*************************************************************************/ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) { - // Get the pin's value using Arduino API uint16_t value = analogRead(pin); // Scale the pins value if (_is_adc_resolution_scaled) { @@ -140,13 +144,16 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) { /*************************************************************************/ /*! - @brief Calculates the voltage of an analog pin. - @param raw_value - The pin's raw value. - @return The pin's voltage, in Volts. + @brief Gets the voltage of an analog pin. + @param pin + The requested pin. + @return The pin's voltage. */ /*************************************************************************/ -float AnalogIOHardware::CalculatePinVoltage(uint16_t raw_value) { - float voltage = raw_value * _voltage_scale_factor; - return voltage; +float AnalogIOHardware::GetPinVoltage(uint8_t pin) { +#ifdef ARDUINO_ARCH_ESP32 + return analogReadMilliVolts(pin) / 1000.0; +#else + return getPinValue(pin) * _ref_voltage / 65536; +#endif // ARDUINO_ARCH_ESP32 } \ No newline at end of file diff --git a/src/components/analogIO/hardware.h b/src/components/analogIO/hardware.h index 6cac98b18..6597b87e9 100644 --- a/src/components/analogIO/hardware.h +++ b/src/components/analogIO/hardware.h @@ -33,7 +33,7 @@ class AnalogIOHardware { void InitPin(uint8_t pin); void DeinitPin(uint8_t pin); uint16_t GetPinValue(uint8_t pin); - float CalculatePinVoltage(uint16_t raw_voltage); + float GetPinVoltage(uint8_t pin); private: uint8_t _native_adc_resolution; ///< Hardware's native ADC resolution.