From eb6bf12e265f0735bb311442f09eabfaa44b2ed1 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 2 Oct 2024 11:33:15 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP,=20analogio=20-=20Read=20vol?= =?UTF-8?q?tage,=20precalculate=20scale=20factor=20for=20optimization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/analogIO/controller.cpp | 8 ++++---- src/components/analogIO/controller.h | 2 -- src/components/analogIO/hardware.cpp | 11 +++++++++++ src/components/analogIO/hardware.h | 5 +++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/components/analogIO/controller.cpp b/src/components/analogIO/controller.cpp index bf4bc668..3a4c3110 100644 --- a/src/components/analogIO/controller.cpp +++ b/src/components/analogIO/controller.cpp @@ -18,17 +18,16 @@ AnalogIOController::AnalogIOController() { _analogio_hardware = new AnalogIOHardware(); _analogio_model = new AnalogIOModel(); _analogio_hardware->SetResolution(16); // Default to 16-bit resolution - SetRefVoltage(0.0); // Default to 0.0V + SetRefVoltage(3.3); // Default to 3.3V } AnalogIOController::~AnalogIOController() {} void AnalogIOController::SetRefVoltage(float voltage) { - _ref_voltage = voltage; + // To set the reference voltage, we call into the hardware + _analogio_hardware->SetReferenceVoltage(voltage); } -float AnalogIOController::GetRefVoltage(void) { return _ref_voltage; } - void AnalogIOController::SetTotalAnalogPins(uint8_t total_pins) { _total_analogio_pins = total_pins; } @@ -109,6 +108,7 @@ void AnalogIOController::update() { } else if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) { // TODO: Read and store the pin's raw value + uint16_t value = _analogio_hardware->GetPinValue(pin.name); } else { WS_DEBUG_PRINT("ERROR: Invalid read mode for analog pin: "); WS_DEBUG_PRINTLN(pin.name); diff --git a/src/components/analogIO/controller.h b/src/components/analogIO/controller.h index 656a40c2..526c2e77 100644 --- a/src/components/analogIO/controller.h +++ b/src/components/analogIO/controller.h @@ -52,7 +52,6 @@ public: void SetTotalAnalogPins(uint8_t total_pins); void SetRefVoltage(float voltage); - float GetRefVoltage(void); bool IsPinTimerExpired(analogioPin *pin, ulong cur_time); private: @@ -60,7 +59,6 @@ private: AnalogIOHardware *_analogio_hardware; ///< AnalogIO hardware std::vector _analogio_pins; ///< Vector of analogio pins uint8_t _total_analogio_pins; ///< Total number of analogio pins - float _ref_voltage; ///< The device's reference voltage }; extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance #endif // WS_ANALOGIO_CONTROLLER_H \ No newline at end of file diff --git a/src/components/analogIO/hardware.cpp b/src/components/analogIO/hardware.cpp index 82b7ceb2..7be76137 100644 --- a/src/components/analogIO/hardware.cpp +++ b/src/components/analogIO/hardware.cpp @@ -27,6 +27,11 @@ void AnalogIOHardware::DeinitPin(uint8_t pin) { pinMode(pin, INPUT); // set to a hi-z floating pin } +void AnalogIOHardware::SetReferenceVoltage(float voltage) { + _ref_voltage = voltage; + _voltage_scale_factor = _ref_voltage / 65536; +} + void AnalogIOHardware::SetNativeADCResolution() { _is_adc_resolution_scaled = false; #if defined(ARDUINO_ARCH_SAMD) @@ -75,4 +80,10 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) { } } return value; +} + +float Wippersnapper_AnalogIO::GetPinVoltage(uint8_t pin) { + uint16_t raw_value = GetPinValue(pin); + float voltage = raw_value * _voltage_scale_factor; + return voltage } \ No newline at end of file diff --git a/src/components/analogIO/hardware.h b/src/components/analogIO/hardware.h index 4c82ddc3..d1a91165 100644 --- a/src/components/analogIO/hardware.h +++ b/src/components/analogIO/hardware.h @@ -28,16 +28,21 @@ public: void SetNativeADCResolution(); void SetResolution(uint8_t resolution); uint8_t GetResolution(void); + void SetReferenceVoltage(float voltage); void CalculateScaleFactor(); + // Arduino/Wiring API void InitPin(uint8_t pin); void DeinitPin(uint8_t pin); uint16_t GetPinValue(uint8_t pin); + float GetPinVoltage(uint8_t pin); private: uint8_t _native_adc_resolution; uint8_t _adc_resolution; int _scale_factor; bool _is_adc_resolution_scaled; + float _ref_voltage; + float _voltage_scale_factor; }; #endif // WS_ANALOGIO_HARDWARE_H \ No newline at end of file