Skip to content

Commit

Permalink
🐛 WIP, analogio - bring in
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Oct 3, 2024
1 parent 067c220 commit 549874a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
9 changes: 4 additions & 5 deletions src/components/analogIO/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 17 additions & 10 deletions src/components/analogIO/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion src/components/analogIO/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 549874a

Please sign in to comment.