Skip to content

Commit 067c220

Browse files
committed
🎨 WIP, analogio - Doxygen analogio classes
1 parent 6a82e77 commit 067c220

File tree

6 files changed

+254
-17
lines changed

6 files changed

+254
-17
lines changed

src/components/analogIO/controller.cpp

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,60 @@
1414
*/
1515
#include "controller.h"
1616

17+
/***********************************************************************/
18+
/*!
19+
@brief AnalogIO controller constructor
20+
*/
21+
/***********************************************************************/
1722
AnalogIOController::AnalogIOController() {
1823
_analogio_hardware = new AnalogIOHardware();
1924
_analogio_model = new AnalogIOModel();
2025
_analogio_hardware->SetResolution(16); // Default to 16-bit resolution
2126
SetRefVoltage(3.3); // Default to 3.3V
2227
}
2328

24-
AnalogIOController::~AnalogIOController() {}
29+
/***********************************************************************/
30+
/*!
31+
@brief AnalogIO controller destructor
32+
*/
33+
/***********************************************************************/
34+
AnalogIOController::~AnalogIOController() {
35+
delete _analogio_hardware;
36+
delete _analogio_model;
37+
}
2538

39+
/***********************************************************************/
40+
/*!
41+
@brief Set the reference voltage for the analog pins
42+
@param voltage
43+
The reference voltage.
44+
*/
45+
/***********************************************************************/
2646
void AnalogIOController::SetRefVoltage(float voltage) {
2747
// To set the reference voltage, we call into the hardware
2848
_analogio_hardware->SetReferenceVoltage(voltage);
2949
}
3050

51+
/***********************************************************************/
52+
/*!
53+
@brief Set the total number of analog pins present on the hardware.
54+
@param total_pins
55+
The hardware's total number of analog pins.
56+
*/
57+
/***********************************************************************/
3158
void AnalogIOController::SetTotalAnalogPins(uint8_t total_pins) {
3259
_total_analogio_pins = total_pins;
3360
}
3461

62+
/***********************************************************************/
63+
/*!
64+
@brief Handles an AnalogIOAdd message from the broker and adds a
65+
new analog pin to the controller.
66+
@param stream
67+
The nanopb input stream.
68+
@return True if the pin was successfully added, False otherwise.
69+
*/
70+
/***********************************************************************/
3571
bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
3672
// Attempt to decode the incoming message into an AnalogIOAdd object
3773
if (!_analogio_model->DecodeAnalogIOAdd(stream)) {
@@ -60,6 +96,15 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
6096
return true;
6197
}
6298

99+
/***************************************************************************/
100+
/*!
101+
@brief Handles an AnalogIORemove message from the broker and removes
102+
the requested analog pin from the controller.
103+
@param stream
104+
The nanopb input stream.
105+
@return True if the pin was successfully removed, False otherwise.
106+
*/
107+
/***************************************************************************/
63108
bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
64109
// Attempt to decode the incoming message into an AnalogIORemove object
65110
if (!_analogio_model->DecodeAnalogIORemove(stream)) {
@@ -84,10 +129,32 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
84129
return true;
85130
}
86131

132+
/***************************************************************************/
133+
/*!
134+
@brief Checks if a pin's periodic timer has expired.
135+
@param pin
136+
The requested pin to check.
137+
@param cur_time
138+
The current time (called from millis()).
139+
@return True if the pin's period has expired, False otherwise.
140+
*/
141+
/***************************************************************************/
87142
bool AnalogIOController::IsPinTimerExpired(analogioPin *pin, ulong cur_time) {
88143
return cur_time - pin->prv_period > pin->period;
89144
}
90145

146+
/***************************************************************************/
147+
/*!
148+
@brief Encodes and publishes an AnalogIOEvent message to the broker.
149+
@param pin
150+
The pin to encode and publish.
151+
@param value
152+
The pin's value.
153+
@param read_type
154+
The type of read to perform on the pin.
155+
@return True if the message was successfully encoded and published.
156+
*/
157+
/***************************************************************************/
91158
bool AnalogIOController::EncodePublishPinEvent(
92159
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
93160
char c_pin_name[12];
@@ -121,16 +188,43 @@ bool AnalogIOController::EncodePublishPinEvent(
121188
return true;
122189
}
123190

191+
/***************************************************************************/
192+
/*!
193+
@brief Encodes and publishes an AnalogIOEvent message to the broker.
194+
@param pin
195+
The requested pin.
196+
@param value
197+
The pin's value.
198+
@return True if the message was successfully encoded and published,
199+
False othewise.
200+
*/
201+
/***************************************************************************/
124202
bool AnalogIOController::EncodePublishPinValue(uint8_t pin, uint16_t value) {
125203
return EncodePublishPinEvent(pin, (float)value,
126204
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
127205
}
128206

207+
/***************************************************************************/
208+
/*!
209+
@brief Encodes and publishes an AnalogIOEvent message to the broker.
210+
@param pin
211+
The requested pin.
212+
@param value
213+
The pin's value, as a voltage.
214+
@return True if the message was successfully encoded and published,
215+
False othewise.
216+
*/
217+
/***************************************************************************/
129218
bool AnalogIOController::EncodePublishPinVoltage(uint8_t pin, float value) {
130219
return EncodePublishPinEvent(
131220
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
132221
}
133222

223+
/***************************************************************************/
224+
/*!
225+
@brief Update/polling loop for the AnalogIO controller.
226+
*/
227+
/***************************************************************************/
134228
void AnalogIOController::update() {
135229
// Bail-out if the vector is empty
136230
if (_analogio_pins.empty())
@@ -159,7 +253,7 @@ void AnalogIOController::update() {
159253
// Encode and publish the voltage value to the broker
160254
EncodePublishPinVoltage(pin.name, pin_value);
161255
} else {
162-
WS_DEBUG_PRINTLN("ERROR: Invalid read mode for analog pin!");
256+
WS_DEBUG_PRINTLN("ERROR: Invalid read mode for analog pin!");
163257
}
164258
}
165259
}

src/components/analogIO/controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AnalogIOHardware; ///< Forward declaration
2424

2525
/**
2626
* @struct analogioPin
27-
* @brief Structure repesenting an analogio pin on the device.
27+
* @brief Represents a device's analogio pin.
2828
*/
2929
struct analogioPin {
3030
uint8_t name; ///< The pin's name.

src/components/analogIO/hardware.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,61 @@
1414
*/
1515
#include "hardware.h"
1616

17+
/***********************************************************************/
18+
/*!
19+
@brief AnalogIO hardware constructor
20+
*/
21+
/***********************************************************************/
1722
AnalogIOHardware::AnalogIOHardware() {
1823
SetNativeADCResolution(); // Configure the device's native ADC resolution
1924
_scale_factor = 0;
2025
}
2126

27+
/***********************************************************************/
28+
/*!
29+
@brief AnalogIO hardware destructor
30+
*/
31+
/***********************************************************************/
2232
AnalogIOHardware::~AnalogIOHardware() {}
2333

34+
/***********************************************************************/
35+
/*!
36+
@brief Initializes an analog input pin.
37+
@param pin
38+
The requested pin.
39+
*/
40+
/***********************************************************************/
2441
void AnalogIOHardware::InitPin(uint8_t pin) { pinMode(pin, INPUT); }
2542

43+
/***********************************************************************/
44+
/*!
45+
@brief Deinitializes an analog input pin and frees it for
46+
other uses.
47+
@param pin
48+
The requested pin.
49+
*/
50+
/***********************************************************************/
2651
void AnalogIOHardware::DeinitPin(uint8_t pin) {
2752
pinMode(pin, INPUT); // set to a hi-z floating pin
2853
}
2954

55+
/*************************************************************************/
56+
/*!
57+
@brief Sets the hardware's reference voltage for reading analog pins
58+
@param voltage
59+
The reference voltage.
60+
*/
61+
/*************************************************************************/
3062
void AnalogIOHardware::SetReferenceVoltage(float voltage) {
3163
_ref_voltage = voltage;
3264
_voltage_scale_factor = _ref_voltage / 65536;
3365
}
3466

67+
/*************************************************************************/
68+
/*!
69+
@brief Configures the hardware's native ADC resolution.
70+
*/
71+
/*************************************************************************/
3572
void AnalogIOHardware::SetNativeADCResolution() {
3673
_is_adc_resolution_scaled = false;
3774
#if defined(ARDUINO_ARCH_SAMD)
@@ -49,12 +86,25 @@ void AnalogIOHardware::SetNativeADCResolution() {
4986
#endif
5087
}
5188

89+
/*************************************************************************/
90+
/*!
91+
@brief Sets the hardware ADC's resolution.
92+
@param resolution
93+
The requested resolution, in bits.
94+
*/
95+
/*************************************************************************/
5296
void AnalogIOHardware::SetResolution(uint8_t resolution) {
5397
_adc_resolution = resolution;
5498
// Calculate (or re-calculate) the scale factor when we set the resolution
5599
CalculateScaleFactor();
56100
}
57101

102+
/*************************************************************************/
103+
/*!
104+
@brief Calculates a factor used by the hardware for scaling
105+
the ADC's resolution.
106+
*/
107+
/*************************************************************************/
58108
void AnalogIOHardware::CalculateScaleFactor() {
59109
if (!_is_adc_resolution_scaled)
60110
return;
@@ -66,6 +116,14 @@ void AnalogIOHardware::CalculateScaleFactor() {
66116
}
67117
}
68118

119+
/*************************************************************************/
120+
/*!
121+
@brief Gets the "raw" value of an analog pin from the ADC.
122+
@param pin
123+
The requested pin.
124+
@return The pin's value.
125+
*/
126+
/*************************************************************************/
69127
uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
70128
// Get the pin's value using Arduino API
71129
uint16_t value = analogRead(pin);
@@ -80,6 +138,14 @@ uint16_t AnalogIOHardware::GetPinValue(uint8_t pin) {
80138
return value;
81139
}
82140

141+
/*************************************************************************/
142+
/*!
143+
@brief Calculates the voltage of an analog pin.
144+
@param raw_value
145+
The pin's raw value.
146+
@return The pin's voltage, in Volts.
147+
*/
148+
/*************************************************************************/
83149
float AnalogIOHardware::CalculatePinVoltage(uint16_t raw_value) {
84150
float voltage = raw_value * _voltage_scale_factor;
85151
return voltage;

src/components/analogIO/hardware.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ class AnalogIOHardware {
2929
void SetResolution(uint8_t resolution);
3030
void SetReferenceVoltage(float voltage);
3131
void CalculateScaleFactor();
32-
3332
// Arduino/Wiring API
3433
void InitPin(uint8_t pin);
3534
void DeinitPin(uint8_t pin);
3635
uint16_t GetPinValue(uint8_t pin);
3736
float CalculatePinVoltage(uint16_t raw_voltage);
3837

3938
private:
40-
uint8_t _native_adc_resolution;
41-
uint8_t _adc_resolution;
42-
int _scale_factor;
43-
bool _is_adc_resolution_scaled;
44-
float _ref_voltage;
45-
float _voltage_scale_factor;
39+
uint8_t _native_adc_resolution; ///< Hardware's native ADC resolution.
40+
uint8_t _adc_resolution; ///< Requested ADC resolution.
41+
int _scale_factor; ///< Factor used for scaling the ADC's resolution.
42+
bool _is_adc_resolution_scaled; ///< True if the ADC's resolution is scaled,
43+
///< False otherwise.
44+
float _ref_voltage; ///< Reference voltage for reading analog pins.
45+
float _voltage_scale_factor; ///< Scaling factor used for calculating the
46+
///< pin's voltage.
4647
};
4748
#endif // WS_ANALOGIO_HARDWARE_H

0 commit comments

Comments
 (0)