Skip to content

Commit fb2f5df

Browse files
nliviucesantabot
authored andcommitted
Implement pulseInLong
Integrates #4 PUBLISHED_FROM=5455f47435e9370d1736938fefc3ef33b33b116c
1 parent f1e3e20 commit fb2f5df

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

include/Arduino.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ void detachInterrupt(uint8_t pin);
136136
void interrupts(void);
137137
void noInterrupts(void);
138138

139+
unsigned long pulseInLong(uint8_t pin, uint8_t state,
140+
unsigned long timeout = 1000000L);
141+
139142
void setup(void);
140143
void loop(void);
141144

@@ -175,8 +178,6 @@ uint16_t makeWord(byte h, byte l);
175178

176179
unsigned long pulseIn(uint8_t pin, uint8_t state,
177180
unsigned long timeout = 1000000L);
178-
unsigned long pulseInLong(uint8_t pin, uint8_t state,
179-
unsigned long timeout = 1000000L);
180181

181182
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
182183
void noTone(uint8_t _pin);

src/mgos_arduino.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,38 @@
2121
#define IRAM
2222
#endif
2323

24+
static inline uint64_t uptime() {
25+
return (uint64_t)(1000000 * mgos_uptime());
26+
}
27+
28+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) {
29+
uint64_t startMicros = uptime();
30+
31+
// wait for any previous pulse to end
32+
while (state == mgos_gpio_read(pin)) {
33+
if ((uptime() - startMicros) > timeout) {
34+
return 0;
35+
}
36+
}
37+
38+
// wait for the pulse to start
39+
while (state != mgos_gpio_read(pin)) {
40+
if ((uptime() - startMicros) > timeout) {
41+
return 0;
42+
}
43+
}
44+
45+
uint64_t start = uptime();
46+
47+
// wait for the pulse to stop
48+
while (state == mgos_gpio_read(pin)) {
49+
if ((uptime() - startMicros) > timeout) {
50+
return 0;
51+
}
52+
}
53+
return (uint32_t)(uptime() - start);
54+
}
55+
2456
IRAM void pinMode(uint8_t pin, uint8_t mode) {
2557
switch (mode) {
2658
case INPUT:

0 commit comments

Comments
 (0)