Skip to content

Commit aa1cba8

Browse files
machine: Add wait_opposite to time_pulse_us func.
If the pin is initially equal to *pulse_level* then first waits until the pin input becomes different from *pulse_level*. Then the function waits until the pin input becomes equal to *pulse_level*, then the function counts the duration that the pin is equal to *pulse_level*. Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com> Co-Authored-By: Robert Hammelrath <12476868+robert-hh@users.noreply.github.com>
1 parent 594670e commit aa1cba8

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

docs/library/machine.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,20 @@ Miscellaneous functions
182182
varies by hardware (so use substring of a full value if you expect a short
183183
ID). In some MicroPython ports, ID corresponds to the network MAC address.
184184

185-
.. function:: time_pulse_us(pin, pulse_level, timeout_us=1000000, /)
185+
.. function:: time_pulse_us(pin, pulse_level, timeout_us=1000000, wait_opposite=false, /)
186186

187187
Time a pulse on the given *pin*, and return the duration of the pulse in
188188
microseconds. The *pulse_level* argument should be 0 to time a low pulse
189189
or 1 to time a high pulse.
190190

191+
If `wait_opposite` is true, if the pin is initially equal to *pulse_level* then first
192+
waits until the pin input becomes different from *pulse_level* (***).
191193
If the current input value of the pin is different to *pulse_level*,
192194
the function first (*) waits until the pin input becomes equal to *pulse_level*,
193195
then (**) times the duration that the pin is equal to *pulse_level*.
194196
If the pin is already equal to *pulse_level* then timing starts straight away.
195197

198+
The function returns -3 if there was timeout waiting for condition marked (***) above.
196199
The function will return -2 if there was timeout waiting for condition marked
197200
(*) above, and -1 if there was timeout during the main measurement, marked (**)
198201
above. The timeout is the same for both cases and given by *timeout_us* (which

drivers/dht/dht.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ static mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
7171
}
7272
}
7373

74+
#define WAIT_OPPOSITE false
7475
// time pulse, should be 80us
75-
ticks = machine_time_pulse_us(pin, 1, 150);
76+
ticks = machine_time_pulse_us(pin, 1, 150, WAIT_OPPOSITE);
7677
if ((mp_int_t)ticks < 0) {
7778
goto timeout;
7879
}
7980

8081
// time 40 pulses for data (either 26us or 70us)
8182
uint8_t *buf = bufinfo.buf;
8283
for (int i = 0; i < 40; ++i) {
83-
ticks = machine_time_pulse_us(pin, 1, 100);
84+
ticks = machine_time_pulse_us(pin, 1, 100, WAIT_OPPOSITE);
8485
if ((mp_int_t)ticks < 0) {
8586
goto timeout;
8687
}

extmod/machine_pulse.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030

3131
#if MICROPY_PY_MACHINE_PULSE
3232

33-
MP_WEAK mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
33+
MP_WEAK mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us, bool wait_opposite) {
3434
mp_uint_t start = mp_hal_ticks_us();
35+
while (wait_opposite && (mp_hal_pin_read(pin) == pulse_level)) {
36+
if ((uint64_t)(mp_hal_ticks_us() - start) >= (uint64_t)timeout_us) {
37+
return (mp_uint_t)-3;
38+
}
39+
}
40+
start = mp_hal_ticks_us();
3541
while (mp_hal_pin_read(pin) != pulse_level) {
3642
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
3743
return (mp_uint_t)-2;
@@ -56,10 +62,14 @@ static mp_obj_t machine_time_pulse_us_(size_t n_args, const mp_obj_t *args) {
5662
if (n_args > 2) {
5763
timeout_us = mp_obj_get_int(args[2]);
5864
}
59-
mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us);
60-
// May return -1 or -2 in case of timeout
65+
bool wait_opposite = false;
66+
if (n_args > 3) {
67+
wait_opposite = mp_obj_is_true(args[3]);
68+
}
69+
mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us, wait_opposite);
70+
// May return -1 or -2 or -3 in case of timeout
6171
return mp_obj_new_int(us);
6272
}
63-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_);
73+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 4, machine_time_pulse_us_);
6474

6575
#endif

extmod/modmachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ uintptr_t MICROPY_MACHINE_MEM_GET_WRITE_ADDR(mp_obj_t addr_o, uint align);
244244

245245
NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args);
246246
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len);
247-
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us);
247+
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us, bool wait_opposite);
248248

249249
MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj);
250250
MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy