Skip to content

Commit

Permalink
drivers: Add C-level function to read DHT11 and DHT22 devices.
Browse files Browse the repository at this point in the history
Uses mp_hal_pin API.
  • Loading branch information
dpgeorge committed May 26, 2016
1 parent 4b37e77 commit 48a7ef0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
104 changes: 104 additions & 0 deletions drivers/dht/dht.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <stdio.h>

#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.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);

mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE);

if (bufinfo.len < 5) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
}

// issue start command
mp_hal_pin_od_high(pin);
mp_hal_delay_ms(250);
mp_hal_pin_od_low(pin);
mp_hal_delay_ms(18);

mp_uint_t irq_state = mp_hal_quiet_timing_enter();

// release the line so the device can respond
mp_hal_pin_od_high(pin);
mp_hal_delay_us_fast(10);

// wait for device to respond
mp_uint_t ticks = mp_hal_ticks_us();
while (mp_hal_pin_read(pin) != 0) {
if ((mp_uint_t)(mp_hal_ticks_us() - ticks) > 100) {
goto timeout;
}
}

// time pulse, should be 80us
ticks = time_pulse_us(pin, 1, 150);
if (ticks == (mp_uint_t)-1) {
goto timeout;
}

// 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);
if (ticks == (mp_uint_t)-1) {
goto timeout;
}
buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48);
}

mp_hal_quiet_timing_exit(irq_state);
return mp_const_none;

timeout:
mp_hal_quiet_timing_exit(irq_state);
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(MP_ETIMEDOUT)));
}
MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto);
3 changes: 3 additions & 0 deletions drivers/dht/dht.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "py/obj.h"

MP_DECLARE_CONST_FUN_OBJ(dht_readinto_obj);

0 comments on commit 48a7ef0

Please sign in to comment.