1414 */
1515#include " controller.h"
1616
17+ /* **********************************************************************/
18+ /* !
19+ @brief DigitalIOController constructor
20+ */
21+ /* **********************************************************************/
1722DigitalIOController::DigitalIOController () {
1823 _dio_model = new DigitalIOModel ();
1924 _dio_hardware = new DigitalIOHardware ();
@@ -22,11 +27,23 @@ DigitalIOController::DigitalIOController() {
2227 SetMaxDigitalPins (0 );
2328}
2429
30+ /* **********************************************************************/
31+ /* !
32+ @brief DigitalIOController destructor
33+ */
34+ /* **********************************************************************/
2535DigitalIOController::~DigitalIOController () {
2636 delete _dio_model;
2737 delete _dio_hardware;
2838}
2939
40+ /* **********************************************************************/
41+ /* !
42+ @brief Set the maximum number of digital pins
43+ @param max_digital_pins
44+ The maximum number of digital pins
45+ */
46+ /* **********************************************************************/
3047void DigitalIOController::SetMaxDigitalPins (uint8_t max_digital_pins) {
3148 _max_digital_pins = max_digital_pins;
3249}
@@ -39,6 +56,21 @@ bool DigitalIOController::IsStatusLEDPin(uint8_t pin_name) {
3956 return false ;
4057}
4158
59+ /* **********************************************************************/
60+ /* !
61+ @brief Create a new digital pin and add it to the controller's vector
62+ @param name
63+ The pin's name.
64+ @param direction
65+ The pin's direction.
66+ @param sample_mode
67+ The pin's sample mode.
68+ @param value
69+ The pin's value.
70+ @param period
71+ The pin's period.
72+ */
73+ /* **********************************************************************/
4274void DigitalIOController::CreateDigitalIOPin (
4375 uint8_t name, wippersnapper_digitalio_DigitalIODirection direction,
4476 wippersnapper_digitalio_DigitalIOSampleMode sample_mode, bool value,
@@ -54,6 +86,14 @@ void DigitalIOController::CreateDigitalIOPin(
5486 _digital_io_pins.push_back (new_pin);
5587}
5688
89+ /* **********************************************************************/
90+ /* !
91+ @brief Add a new digital pin to the controller
92+ @param stream
93+ The nanopb input stream.
94+ @return True if the digital pin was successfully added.
95+ */
96+ /* **********************************************************************/
5797bool DigitalIOController::AddDigitalIOPin (pb_istream_t *stream) {
5898 // Early-out if we have reached the maximum number of digital pins
5999 if (_digital_io_pins.size () >= _max_digital_pins) {
@@ -96,6 +136,14 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
96136 return true ;
97137}
98138
139+ /* **********************************************************************/
140+ /* !
141+ @brief Get the index of a digital output pin
142+ @param pin_name
143+ The pin's name.
144+ @return The index of the digital output pin.
145+ */
146+ /* **********************************************************************/
99147int DigitalIOController::GetDigitalOutputPinsIdx (uint8_t pin_name) {
100148 for (int i = 0 ; i < _digital_io_pins.size (); i++) {
101149 if (_digital_io_pins[i].pin_name == pin_name) {
@@ -105,6 +153,14 @@ int DigitalIOController::GetDigitalOutputPinsIdx(uint8_t pin_name) {
105153 return -1 ; // Pin not found
106154}
107155
156+ /* **********************************************************************/
157+ /* !
158+ @brief Write a digital pin
159+ @param stream
160+ The nanopb input stream.
161+ @return True if the digital pin was successfully written.
162+ */
163+ /* **********************************************************************/
108164bool DigitalIOController::WriteDigitalIOPin (pb_istream_t *stream) {
109165 // Attempt to decode the DigitalIOWrite message
110166 if (!_dio_model->DecodeDigitalIOWrite (stream)) {
@@ -151,10 +207,28 @@ bool DigitalIOController::WriteDigitalIOPin(pb_istream_t *stream) {
151207 return true ;
152208}
153209
210+ /* **********************************************************************/
211+ /* !
212+ @brief Check if a pin's timer has expired
213+ @param pin
214+ The pin to check.
215+ @param cur_time
216+ The current time.
217+ @return True if the pin's timer has expired.
218+ */
219+ /* **********************************************************************/
154220bool DigitalIOController::IsPinTimerExpired (DigitalIOPin *pin, ulong cur_time) {
155221 return cur_time - pin->prv_pin_time > pin->pin_period ;
156222}
157223
224+ /* **********************************************************************/
225+ /* !
226+ @brief Check if a pin's timer has expired
227+ @param pin
228+ The pin to check.
229+ @return True if the pin's timer has expired.
230+ */
231+ /* **********************************************************************/
158232bool DigitalIOController::CheckTimerPin (DigitalIOPin *pin) {
159233 ulong cur_time = millis ();
160234
@@ -172,6 +246,14 @@ bool DigitalIOController::CheckTimerPin(DigitalIOPin *pin) {
172246 return true ;
173247}
174248
249+ /* **********************************************************************/
250+ /* !
251+ @brief Check if a pin's value has changed
252+ @param pin
253+ The pin to check.
254+ @return True if the pin's value has changed.
255+ */
256+ /* **********************************************************************/
175257bool DigitalIOController::CheckEventPin (DigitalIOPin *pin) {
176258 // Get the pin's current value
177259 pin->pin_value = _dio_hardware->GetValue (pin->pin_name );
@@ -190,6 +272,16 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
190272 return true ;
191273}
192274
275+ /* **********************************************************************/
276+ /* !
277+ @brief Encode and publish a pin event
278+ @param pin_name
279+ The pin's name.
280+ @param pin_value
281+ The pin's value.
282+ @return True if the pin event was successfully encoded and published.
283+ */
284+ /* **********************************************************************/
193285bool DigitalIOController::EncodePublishPinEvent (uint8_t pin_name,
194286 bool pin_value) {
195287 // Prefix pin_name with "D" to match the expected pin name format
@@ -215,6 +307,11 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
215307 return true ;
216308}
217309
310+ /* **********************************************************************/
311+ /* !
312+ @brief Updates the digital_io_pins array.
313+ */
314+ /* **********************************************************************/
218315void DigitalIOController::Update () {
219316 // Bail out if we have no digital pins to poll
220317 if (_digital_io_pins.empty ())
0 commit comments