Skip to content

Commit 3abd37c

Browse files
committed
integrate timer use into task
1 parent abebc08 commit 3abd37c

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This is a DHT11/22 library for ESP32 using the RMT peripheral, for use in the Arduino framework.
66
For ESP8266, please look into this repo: [DHT](https://github.com/bertmelis/DHT)
77

8-
The library is non blocking, doesn't use delay and is usable in async frameworks. The library is kept simple on purpose. You are responsible yourself to follow the sensor's constraints (like polling frequency) and logical programming errors. Supplementary functions like dew point calculation are not included.
8+
The library is non blocking and is usable in async frameworks. Time consuming methods are offloaded to a separate task. The library is kept simple on purpose. You are responsible yourself to follow the sensor's constraints (like polling frequency) and logical programming errors. Supplementary functions like dew point calculation are not included.
99

1010
## Installation
1111

src/esp32DHT.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,16 @@ DHT::DHT() :
3333
_channel(RMT_CHANNEL_0),
3434
_onData(nullptr),
3535
_onError(nullptr),
36-
_timer(nullptr),
3736
_task(nullptr) {}
3837

3938
DHT::~DHT() {
40-
if (_timer) { // if _timer is true, setup() has been called
41-
// so RMT driver is loaded and the aux task is
42-
// running
43-
esp_timer_delete(_timer);
44-
rmt_driver_uninstall(_channel);
45-
vTaskDelete(_task);
46-
}
39+
rmt_driver_uninstall(_channel);
40+
vTaskDelete(_task);
4741
}
4842

4943
void DHT::setup(uint8_t pin, rmt_channel_t channel) {
5044
_pin = pin;
5145
_channel = channel;
52-
esp_timer_create_args_t _timerConfig;
53-
_timerConfig.arg = static_cast<void*>(this);
54-
_timerConfig.callback = reinterpret_cast<esp_timer_cb_t>(_handleTimer);
55-
_timerConfig.dispatch_method = ESP_TIMER_TASK;
56-
_timerConfig.name = "esp32DHTTimer";
57-
esp_timer_create(&_timerConfig, &_timer);
5846
rmt_config_t config;
5947
config.rmt_mode = RMT_MODE_RX;
6048
config.channel = _channel;
@@ -67,7 +55,7 @@ void DHT::setup(uint8_t pin, rmt_channel_t channel) {
6755
rmt_config(&config);
6856
rmt_driver_install(_channel, 400, 0); // 400 words for ringbuffer containing pulse trains from DHT
6957
rmt_get_ringbuf_handle(_channel, &_ringBuf);
70-
xTaskCreate((TaskFunction_t)&_handleData, "esp32DHT", 2048, this, 5, &_task);
58+
xTaskCreate((TaskFunction_t)&_readSensor, "esp32DHT", 2048, this, 5, &_task);
7159
pinMode(_pin, OUTPUT);
7260
digitalWrite(_pin, HIGH);
7361
}
@@ -81,11 +69,7 @@ void DHT::onError(esp32DHTInternals::OnError_CB callback) {
8169
}
8270

8371
void DHT::read() {
84-
// _pin should be set to OUTPUT and HIGH
85-
digitalWrite(_pin, LOW);
86-
esp_timer_start_once(_timer, 18 * 1000); // timer is in microseconds
87-
_data[0] = _data[1] = _data[2] = _data[3] = _data[4] = 0;
88-
_status = 0;
72+
xTaskNotifyGive(_task);
8973
}
9074

9175
const char* DHT::getError() const {
@@ -107,17 +91,27 @@ const char* DHT::getError() const {
10791
return "UNKNOWN";
10892
}
10993

110-
void DHT::_handleTimer(DHT* instance) {
111-
pinMode(instance->_pin, INPUT);
112-
rmt_rx_start(instance->_channel, 1);
113-
rmt_set_pin(instance->_channel, RMT_MODE_RX, static_cast<gpio_num_t>(instance->_pin)); // reset after using pin as output
114-
xTaskNotifyGive(instance->_task);
115-
}
116-
117-
void DHT::_handleData(DHT* instance) {
94+
void DHT::_readSensor(DHT* instance) {
11895
size_t rx_size = 0;
11996
while (1) {
120-
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // block and wait for notification
97+
// reset
98+
instance->_data[0] =
99+
instance->_data[1] =
100+
instance->_data[2] =
101+
instance->_data[3] =
102+
instance->_data[4] = 0;
103+
instance->_status = 0;
104+
105+
// block and wait for notification
106+
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
107+
108+
// give start signal to sensor
109+
digitalWrite(instance->_pin, LOW);
110+
vTaskDelay(18);
111+
pinMode(instance->_pin, INPUT);
112+
rmt_rx_start(instance->_channel, 1);
113+
rmt_set_pin(instance->_channel, RMT_MODE_RX, static_cast<gpio_num_t>(instance->_pin)); // reset after using pin as output
114+
121115
// blocks until data is available or timeouts after 1000
122116
rmt_item32_t* items = static_cast<rmt_item32_t*>(xRingbufferReceive(instance->_ringBuf, &rx_size, 1000));
123117
if (items) {
@@ -132,6 +126,8 @@ void DHT::_handleData(DHT* instance) {
132126
pinMode(instance->_pin, OUTPUT);
133127
digitalWrite(instance->_pin, HIGH);
134128
}
129+
130+
// return results
135131
instance->_tryCallback();
136132
}
137133
}

src/esp32DHT.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class DHT {
5555
uint8_t _data[5];
5656

5757
private:
58-
static void _handleTimer(DHT* instance);
59-
static void _handleData(DHT* instance);
58+
static void _readSensor(DHT* instance);
6059
void _decode(rmt_item32_t* data, int numItems);
6160
void _tryCallback();
6261
virtual float _getTemperature() = 0;
@@ -67,7 +66,6 @@ class DHT {
6766
rmt_channel_t _channel;
6867
esp32DHTInternals::OnData_CB _onData;
6968
esp32DHTInternals::OnError_CB _onError;
70-
esp_timer_handle_t _timer;
7169
TaskHandle_t _task;
7270
RingbufHandle_t _ringBuf;
7371
};

0 commit comments

Comments
 (0)