Skip to content

Commit

Permalink
🎨 WIP, analogio - Doxygen analogio classes
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Oct 3, 2024
1 parent 6a82e77 commit 067c220
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 17 deletions.
98 changes: 96 additions & 2 deletions src/components/analogIO/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,60 @@
*/
#include "controller.h"

/***********************************************************************/
/*!
@brief AnalogIO controller constructor
*/
/***********************************************************************/
AnalogIOController::AnalogIOController() {
_analogio_hardware = new AnalogIOHardware();
_analogio_model = new AnalogIOModel();
_analogio_hardware->SetResolution(16); // Default to 16-bit resolution
SetRefVoltage(3.3); // Default to 3.3V
}

AnalogIOController::~AnalogIOController() {}
/***********************************************************************/
/*!
@brief AnalogIO controller destructor
*/
/***********************************************************************/
AnalogIOController::~AnalogIOController() {
delete _analogio_hardware;
delete _analogio_model;
}

/***********************************************************************/
/*!
@brief Set the reference voltage for the analog pins
@param voltage
The reference voltage.
*/
/***********************************************************************/
void AnalogIOController::SetRefVoltage(float voltage) {
// To set the reference voltage, we call into the hardware
_analogio_hardware->SetReferenceVoltage(voltage);
}

/***********************************************************************/
/*!
@brief Set the total number of analog pins present on the hardware.
@param total_pins
The hardware's total number of analog pins.
*/
/***********************************************************************/
void AnalogIOController::SetTotalAnalogPins(uint8_t total_pins) {
_total_analogio_pins = total_pins;
}

/***********************************************************************/
/*!
@brief Handles an AnalogIOAdd message from the broker and adds a
new analog pin to the controller.
@param stream
The nanopb input stream.
@return True if the pin was successfully added, False otherwise.
*/
/***********************************************************************/
bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
// Attempt to decode the incoming message into an AnalogIOAdd object
if (!_analogio_model->DecodeAnalogIOAdd(stream)) {
Expand Down Expand Up @@ -60,6 +96,15 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
return true;
}

/***************************************************************************/
/*!
@brief Handles an AnalogIORemove message from the broker and removes
the requested analog pin from the controller.
@param stream
The nanopb input stream.
@return True if the pin was successfully removed, False otherwise.
*/
/***************************************************************************/
bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
// Attempt to decode the incoming message into an AnalogIORemove object
if (!_analogio_model->DecodeAnalogIORemove(stream)) {
Expand All @@ -84,10 +129,32 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
return true;
}

/***************************************************************************/
/*!
@brief Checks if a pin's periodic timer has expired.
@param pin
The requested pin to check.
@param cur_time
The current time (called from millis()).
@return True if the pin's period has expired, False otherwise.
*/
/***************************************************************************/
bool AnalogIOController::IsPinTimerExpired(analogioPin *pin, ulong cur_time) {
return cur_time - pin->prv_period > pin->period;
}

/***************************************************************************/
/*!
@brief Encodes and publishes an AnalogIOEvent message to the broker.
@param pin
The pin to encode and publish.
@param value
The pin's value.
@param read_type
The type of read to perform on the pin.
@return True if the message was successfully encoded and published.
*/
/***************************************************************************/
bool AnalogIOController::EncodePublishPinEvent(
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
char c_pin_name[12];
Expand Down Expand Up @@ -121,16 +188,43 @@ bool AnalogIOController::EncodePublishPinEvent(
return true;
}

/***************************************************************************/
/*!
@brief Encodes and publishes an AnalogIOEvent message to the broker.
@param pin
The requested pin.
@param value
The pin's value.
@return True if the message was successfully encoded and published,
False othewise.
*/
/***************************************************************************/
bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
return EncodePublishPinEvent(pin, (float)value,
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
}

/***************************************************************************/
/*!
@brief Encodes and publishes an AnalogIOEvent message to the broker.
@param pin
The requested pin.
@param value
The pin's value, as a voltage.
@return True if the message was successfully encoded and published,
False othewise.
*/
/***************************************************************************/
bool AnalogIOController::EncodePublishPinVoltage(uint8_t pin, float value) {
return EncodePublishPinEvent(
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
}

/***************************************************************************/
/*!
@brief Update/polling loop for the AnalogIO controller.
*/
/***************************************************************************/
void AnalogIOController::update() {
// Bail-out if the vector is empty
if (_analogio_pins.empty())
Expand Down Expand Up @@ -159,7 +253,7 @@ void AnalogIOController::update() {
// Encode and publish the voltage value to the broker
EncodePublishPinVoltage(pin.name, pin_value);
} else {
WS_DEBUG_PRINTLN("ERROR: Invalid read mode for analog pin!");
WS_DEBUG_PRINTLN("ERROR: Invalid read mode for analog pin!");
}
}
}
2 changes: 1 addition & 1 deletion src/components/analogIO/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AnalogIOHardware; ///< Forward declaration

/**
* @struct analogioPin
* @brief Structure repesenting an analogio pin on the device.
* @brief Represents a device's analogio pin.
*/
struct analogioPin {
uint8_t name; ///< The pin's name.
Expand Down
66 changes: 66 additions & 0 deletions src/components/analogIO/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,61 @@
*/
#include "hardware.h"

/***********************************************************************/
/*!
@brief AnalogIO hardware constructor
*/
/***********************************************************************/
AnalogIOHardware::AnalogIOHardware() {
SetNativeADCResolution(); // Configure the device's native ADC resolution
_scale_factor = 0;
}

/***********************************************************************/
/*!
@brief AnalogIO hardware destructor
*/
/***********************************************************************/
AnalogIOHardware::~AnalogIOHardware() {}

/***********************************************************************/
/*!
@brief Initializes an analog input pin.
@param pin
The requested pin.
*/
/***********************************************************************/
void AnalogIOHardware::InitPin(uint8_t pin) { pinMode(pin, INPUT); }

/***********************************************************************/
/*!
@brief Deinitializes an analog input pin and frees it for
other uses.
@param pin
The requested pin.
*/
/***********************************************************************/
void AnalogIOHardware::DeinitPin(uint8_t pin) {
pinMode(pin, INPUT); // set to a hi-z floating pin
}

/*************************************************************************/
/*!
@brief Sets the hardware's reference voltage for reading analog pins
@param voltage
The reference voltage.
*/
/*************************************************************************/
void AnalogIOHardware::SetReferenceVoltage(float voltage) {
_ref_voltage = voltage;
_voltage_scale_factor = _ref_voltage / 65536;
}

/*************************************************************************/
/*!
@brief Configures the hardware's native ADC resolution.
*/
/*************************************************************************/
void AnalogIOHardware::SetNativeADCResolution() {
_is_adc_resolution_scaled = false;
#if defined(ARDUINO_ARCH_SAMD)
Expand All @@ -49,12 +86,25 @@ void AnalogIOHardware::SetNativeADCResolution() {
#endif
}

/*************************************************************************/
/*!
@brief Sets the hardware ADC's resolution.
@param resolution
The requested resolution, in bits.
*/
/*************************************************************************/
void AnalogIOHardware::SetResolution(uint8_t resolution) {
_adc_resolution = resolution;
// Calculate (or re-calculate) the scale factor when we set the resolution
CalculateScaleFactor();
}

/*************************************************************************/
/*!
@brief Calculates a factor used by the hardware for scaling
the ADC's resolution.
*/
/*************************************************************************/
void AnalogIOHardware::CalculateScaleFactor() {
if (!_is_adc_resolution_scaled)
return;
Expand All @@ -66,6 +116,14 @@ void AnalogIOHardware::CalculateScaleFactor() {
}
}

/*************************************************************************/
/*!
@brief Gets the "raw" value of an analog pin from the ADC.
@param pin
The requested pin.
@return The pin's value.
*/
/*************************************************************************/
uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
// Get the pin's value using Arduino API
uint16_t value = analogRead(pin);
Expand All @@ -80,6 +138,14 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
return value;
}

/*************************************************************************/
/*!
@brief Calculates the voltage of an analog pin.
@param raw_value
The pin's raw value.
@return The pin's voltage, in Volts.
*/
/*************************************************************************/
float AnalogIOHardware::CalculatePinVoltage(uint16_t raw_value) {
float voltage = raw_value * _voltage_scale_factor;
return voltage;
Expand Down
15 changes: 8 additions & 7 deletions src/components/analogIO/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ class AnalogIOHardware {
void SetResolution(uint8_t resolution);
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 CalculatePinVoltage(uint16_t raw_voltage);

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;
uint8_t _native_adc_resolution; ///< Hardware's native ADC resolution.
uint8_t _adc_resolution; ///< Requested ADC resolution.
int _scale_factor; ///< Factor used for scaling the ADC's resolution.
bool _is_adc_resolution_scaled; ///< True if the ADC's resolution is scaled,
///< False otherwise.
float _ref_voltage; ///< Reference voltage for reading analog pins.
float _voltage_scale_factor; ///< Scaling factor used for calculating the
///< pin's voltage.
};
#endif // WS_ANALOGIO_HARDWARE_H
Loading

0 comments on commit 067c220

Please sign in to comment.