Skip to content

Commit 624ba3d

Browse files
committed
🚧 WIP, digitalio - refactoring
1 parent 70406f3 commit 624ba3d

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/components/digitalIO/controller.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
116116
releaseStatusLED();
117117

118118
// Deinit the pin if it's already in use
119-
if (GetDigitalOutputPinsIdx(pin_name) != -1)
119+
if (GetPinIdx(pin_name) != -1)
120120
_dio_hardware->deinit(pin_name);
121121

122122
// Attempt to configure the pin
@@ -144,7 +144,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
144144
@return The index of the digital output pin.
145145
*/
146146
/***********************************************************************/
147-
int DigitalIOController::GetDigitalOutputPinsIdx(uint8_t pin_name) {
147+
int DigitalIOController::GetPinIdx(uint8_t pin_name) {
148148
for (int i = 0; i < _digital_io_pins.size(); i++) {
149149
if (_digital_io_pins[i].pin_name == pin_name) {
150150
return i;
@@ -169,7 +169,7 @@ bool DigitalIOController::WriteDigitalIOPin(pb_istream_t *stream) {
169169
}
170170

171171
// Get the digital pin
172-
int pin_idx = GetDigitalOutputPinsIdx(
172+
int pin_idx = GetPinIdx(
173173
atoi(_dio_model->GetDigitalIOWriteMsg()->pin_name + 1));
174174
// Check if the pin was found and is a valid digital output pin
175175
if (pin_idx == -1) {
@@ -221,6 +221,20 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
221221
return cur_time - pin->prv_pin_time > pin->pin_period;
222222
}
223223

224+
/***********************************************************************/
225+
/*!
226+
@brief Print a pin's ID and value
227+
@param pin
228+
The specified pin.
229+
*/
230+
/***********************************************************************/
231+
void DigitalIOController::PrintPinValue(DigitalIOPin *pin) {
232+
WS_DEBUG_PRINT("DIO Pin D");
233+
WS_DEBUG_PRINT(pin->pin_name);
234+
WS_DEBUG_PRINT(" | value: ");
235+
WS_DEBUG_PRINTLN(pin->prv_pin_value);
236+
}
237+
224238
/***********************************************************************/
225239
/*!
226240
@brief Check if a pin's timer has expired
@@ -231,18 +245,15 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
231245
/***********************************************************************/
232246
bool DigitalIOController::CheckTimerPin(DigitalIOPin *pin) {
233247
ulong cur_time = millis();
234-
248+
// Bail out if the pin's timer has not expired
235249
if (!IsPinTimerExpired(pin, cur_time))
236250
return false;
237251

238252
// Fill in the pin's current time and value
239253
pin->prv_pin_time = cur_time;
240254
pin->pin_value = _dio_hardware->GetValue(pin->pin_name);
241255

242-
WS_DEBUG_PRINT("DIO Pin D");
243-
WS_DEBUG_PRINT(pin->pin_name);
244-
WS_DEBUG_PRINT(" | value: ");
245-
WS_DEBUG_PRINTLN(pin->prv_pin_value);
256+
PrintPinValue(pin);
246257
return true;
247258
}
248259

@@ -260,14 +271,10 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
260271
// Bail out if the pin value hasn't changed
261272
if (pin->pin_value == pin->prv_pin_value)
262273
return false;
263-
264274
// Update the pin's previous value to the current value
265275
pin->prv_pin_value = pin->pin_value;
266276

267-
WS_DEBUG_PRINT("DIO Pin D");
268-
WS_DEBUG_PRINT(pin->pin_name);
269-
WS_DEBUG_PRINT(" value changed to: ");
270-
WS_DEBUG_PRINTLN(pin->pin_value);
277+
PrintPinValue(pin);
271278

272279
return true;
273280
}
@@ -309,7 +316,8 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
309316

310317
/***********************************************************************/
311318
/*!
312-
@brief Updates the digital_io_pins array.
319+
@brief Iterates through the digital pins and updates their values
320+
(if necessary) and publishes the event to the broker.
313321
*/
314322
/***********************************************************************/
315323
void DigitalIOController::Update() {
@@ -325,7 +333,6 @@ void DigitalIOController::Update() {
325333
wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_OUTPUT)
326334
continue;
327335

328-
// TODO: Use Event sample mode first, its more common!!!
329336
if (pin.sample_mode ==
330337
wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_EVENT) {
331338
// Check if the pin value has changed

src/components/digitalIO/controller.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@ class DigitalIOController {
3636
public:
3737
DigitalIOController();
3838
~DigitalIOController();
39+
// Decoder-related Functions
3940
bool AddDigitalIOPin(pb_istream_t *stream);
4041
bool WriteDigitalIOPin(pb_istream_t *stream);
41-
42+
// Encoder-related Functions
4243
void Update();
4344
bool EncodePublishPinEvent(uint8_t pin_name, bool pin_value);
4445

45-
bool CheckTimerPin(DigitalIOPin *pin);
46-
bool IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time);
47-
bool CheckEventPin(DigitalIOPin *pin);
48-
49-
void SetMaxDigitalPins(uint8_t max_digital_pins);
50-
bool IsStatusLEDPin(uint8_t pin_name);
51-
int GetDigitalOutputPinsIdx(uint8_t pin_name);
52-
void
53-
CreateDigitalIOPin(uint8_t name,
46+
void CreateDigitalIOPin(uint8_t name,
5447
wippersnapper_digitalio_DigitalIODirection direction,
5548
wippersnapper_digitalio_DigitalIOSampleMode sample_mode,
5649
bool value, long period);
50+
bool CheckEventPin(DigitalIOPin *pin);
51+
bool CheckTimerPin(DigitalIOPin *pin);
52+
bool IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time);
53+
bool IsStatusLEDPin(uint8_t pin_name);
5754

55+
void PrintPinValue(DigitalIOPin *pin);
56+
void SetMaxDigitalPins(uint8_t max_digital_pins);
57+
int GetPinIdx(uint8_t pin_name);
5858
private:
5959
std::vector<DigitalIOPin> _digital_io_pins;
6060
uint8_t _max_digital_pins;

0 commit comments

Comments
 (0)