Skip to content

Commit bd32d2c

Browse files
committed
Refactor macro to an inline function
1 parent 6e5a8e2 commit bd32d2c

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

cores/esp8266/core_esp8266_wiring_pulse.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ extern "C" {
2626

2727
extern uint32_t xthal_get_ccount();
2828

29-
#define WAIT_FOR_PIN_STATE(state) \
30-
while (digitalRead(pin) != (state)) { \
31-
if (xthal_get_ccount() - start_cycle_count > timeout_cycles) { \
32-
return 0; \
33-
} \
34-
optimistic_yield(1000); \
29+
namespace {
30+
inline __attribute__((always_inline)) bool waitForPinState(
31+
const uint8_t pin, const uint8_t state,
32+
const uint32_t timeout_cycles, const uint32_t start_cycle_count)
33+
{
34+
while (digitalRead(pin) != state) {
35+
if (xthal_get_ccount() - start_cycle_count > timeout_cycles) {
36+
return false;
37+
}
38+
optimistic_yield(1000);
39+
}
40+
return true;
3541
}
42+
}
3643

3744
// max timeout is 27 seconds at 160MHz clock and 54 seconds at 80MHz clock
3845
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
@@ -43,10 +50,16 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
4350
}
4451
const uint32_t timeout_cycles = microsecondsToClockCycles(timeout);
4552
const uint32_t start_cycle_count = xthal_get_ccount();
46-
WAIT_FOR_PIN_STATE(!state);
47-
WAIT_FOR_PIN_STATE(state);
53+
if (!waitForPinState(pin, !state, timeout_cycles, start_cycle_count)) {
54+
return 0;
55+
}
56+
if (!waitForPinState(pin, state, timeout_cycles, start_cycle_count)) {
57+
return 0;
58+
}
4859
const uint32_t pulse_start_cycle_count = xthal_get_ccount();
49-
WAIT_FOR_PIN_STATE(!state);
60+
if (!waitForPinState(pin, !state, timeout_cycles, start_cycle_count)) {
61+
return 0;
62+
}
5063
return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count);
5164
}
5265

0 commit comments

Comments
 (0)