🚧 WIP, analogio - Read voltage, precalculate scale factor for optimization
This commit is contained in:
parent
fcd87d19ce
commit
eb6bf12e26
4 changed files with 20 additions and 6 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<analogioPin> _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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
Loading…
Reference in a new issue