8000 machine: Add wait_opposite to time_pulse_us func. · micropython/micropython@28906bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 28906bc

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 func 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 406bccc commit 28906bc

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

docs/library/machine.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,22 @@ Miscellaneous functions
181181
varies by hardware (so use substring of a full value if you expect a short
182182
ID). In some MicroPython ports, ID corresponds to the network MAC address.
183183

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

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

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

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

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);

ports/esp8266/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
329329
);
330330

331331
// Custom version of this function that feeds system WDT if necessary
332-
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
332+
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int 2364 pulse_level, mp_uint_t timeout_us, bool wait_opposite) {
333333
int nchanges = 2;
334334
uint32_t start = system_get_time(); // in microseconds
335335
for (;;) {

0 commit comments

Comments
 (0)
0