Skip to content

Commit eb6bf12

Browse files
committed
🚧 WIP, analogio - Read voltage, precalculate scale factor for optimization
1 parent fcd87d1 commit eb6bf12

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

‎src/components/analogIO/controller.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ AnalogIOController::AnalogIOController() {
1818
_analogio_hardware = new AnalogIOHardware();
1919
_analogio_model = new AnalogIOModel();
2020
_analogio_hardware->SetResolution(16); // Default to 16-bit resolution
21-
SetRefVoltage(0.0); // Default to 0.0V
21+
SetRefVoltage(3.3); // Default to 3.3V
2222
}
2323

2424
AnalogIOController::~AnalogIOController() {}
2525

2626
void AnalogIOController::SetRefVoltage(float voltage) {
27-
_ref_voltage = voltage;
27+
// To set the reference voltage, we call into the hardware
28+
_analogio_hardware->SetReferenceVoltage(voltage);
2829
}
2930

30-
float AnalogIOController::GetRefVoltage(void) { return _ref_voltage; }
31-
3231
void AnalogIOController::SetTotalAnalogPins(uint8_t total_pins) {
3332
_total_analogio_pins = total_pins;
3433
}
@@ -109,6 +108,7 @@ void AnalogIOController::update() {
109108
} else if (pin.read_mode ==
110109
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
111110
// TODO: Read and store the pin's raw value
111+
uint16_t value = _analogio_hardware->GetPinValue(pin.name);
112112
} else {
113113
WS_DEBUG_PRINT("ERROR: Invalid read mode for analog pin: ");
114114
WS_DEBUG_PRINTLN(pin.name);

‎src/components/analogIO/controller.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ class AnalogIOController {
5252

5353
void SetTotalAnalogPins(uint8_t total_pins);
5454
void SetRefVoltage(float voltage);
55-
float GetRefVoltage(void);
5655
bool IsPinTimerExpired(analogioPin *pin, ulong cur_time);
5756

5857
private:
5958
AnalogIOModel *_analogio_model; ///< AnalogIO model
6059
AnalogIOHardware *_analogio_hardware; ///< AnalogIO hardware
6160
std::vector<analogioPin> _analogio_pins; ///< Vector of analogio pins
6261
uint8_t _total_analogio_pins; ///< Total number of analogio pins
63-
float _ref_voltage; ///< The device's reference voltage
6462
};
6563
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
6664
#endif // WS_ANALOGIO_CONTROLLER_H

‎src/components/analogIO/hardware.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ void AnalogIOHardware::DeinitPin(uint8_t pin) {
2727
pinMode(pin, INPUT); // set to a hi-z floating pin
2828
}
2929

30+
void AnalogIOHardware::SetReferenceVoltage(float voltage) {
31+
_ref_voltage = voltage;
32+
_voltage_scale_factor = _ref_voltage / 65536;
33+
}
34+
3035
void AnalogIOHardware::SetNativeADCResolution() {
3136
_is_adc_resolution_scaled = false;
3237
#if defined(ARDUINO_ARCH_SAMD)
@@ -75,4 +80,10 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
7580
}
7681
}
7782
return value;
83+
}
84+
85+
float Wippersnapper_AnalogIO::GetPinVoltage(uint8_t pin) {
86+
uint16_t raw_value = GetPinValue(pin);
87+
float voltage = raw_value * _voltage_scale_factor;
88+
return voltage
7889
}

‎src/components/analogIO/hardware.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@ class AnalogIOHardware {
2828
void SetNativeADCResolution();
2929
void SetResolution(uint8_t resolution);
3030
uint8_t GetResolution(void);
31+
void SetReferenceVoltage(float voltage);
3132
void CalculateScaleFactor();
3233

34+
// Arduino/Wiring API
3335
void InitPin(uint8_t pin);
3436
void DeinitPin(uint8_t pin);
3537
uint16_t GetPinValue(uint8_t pin);
38+
float GetPinVoltage(uint8_t pin);
3639

3740
private:
3841
uint8_t _native_adc_resolution;
3942
uint8_t _adc_resolution;
4043
int _scale_factor;
4144
bool _is_adc_resolution_scaled;
45+
float _ref_voltage;
46+
float _voltage_scale_factor;
4247
};
4348
#endif // WS_ANALOGIO_HARDWARE_H

0 commit comments

Comments
 (0)