summaryrefslogtreecommitdiff
path: root/drivers/dht/dht.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-05-31 13:58:48 +0100
committerDamien George <damien.p.george@gmail.com>2016-05-31 13:58:48 +0100
commit4940bee62a636f7e2653a4d4eeac5071448addc0 (patch)
treed7b315c3ec0fe1e52796bd548080218980c90652 /drivers/dht/dht.c
parent298c2ae2c79606bd50522c5cb297855c070a823e (diff)
extmod: Add machine time_pulse_us function (at C and Python level).
The C implementation is taken from the DHT driver.
Diffstat (limited to 'drivers/dht/dht.c')
-rw-r--r--drivers/dht/dht.c21
1 files changed, 3 insertions, 18 deletions
diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c
index 04c590e0c..95b074451 100644
--- a/drivers/dht/dht.c
+++ b/drivers/dht/dht.c
@@ -29,24 +29,9 @@
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
+#include "extmod/machine_pulse.h"
#include "drivers/dht/dht.h"
-STATIC mp_uint_t time_pulse_us(mp_hal_pin_obj_t pin, int pulse_value, mp_uint_t timeout) {
- mp_uint_t start = mp_hal_ticks_us();
- while (mp_hal_pin_read(pin) != pulse_value) {
- if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) {
- return (mp_uint_t)-1;
- }
- }
- start = mp_hal_ticks_us();
- while (mp_hal_pin_read(pin) == pulse_value) {
- if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout) {
- return (mp_uint_t)-1;
- }
- }
- return mp_hal_ticks_us() - start;
-}
-
STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in);
mp_hal_pin_open_drain(pin);
@@ -79,7 +64,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
}
// time pulse, should be 80us
- ticks = time_pulse_us(pin, 1, 150);
+ ticks = machine_time_pulse_us(pin, 1, 150);
if (ticks == (mp_uint_t)-1) {
goto timeout;
}
@@ -87,7 +72,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
// time 40 pulses for data (either 26us or 70us)
uint8_t *buf = bufinfo.buf;
for (int i = 0; i < 40; ++i) {
- ticks = time_pulse_us(pin, 1, 100);
+ ticks = machine_time_pulse_us(pin, 1, 100);
if (ticks == (mp_uint_t)-1) {
goto timeout;
}