Skip to content

Commit bf960d5

Browse files
committedSep 25, 2016
# Conflicts: # spark.json
2 parents a488a9b + abaf2ee commit bf960d5

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed
 

‎firmware/PietteTech_DHT.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
/*
2323
Timing of DHT22 SDA signal line after MCU pulls low for 1ms
2424
https://github.com/mtnscott/Spark_DHT/AM2302.pdf
25-
25+
2626
- - - - ----- -- - - -- ------- - -
2727
\ / \ / \ \ /
2828
+ / + / + + /
2929
\ / \ / \ \ /
3030
------ ----- -- - --------
3131
^ ^ ^ ^ ^
3232
| Ts | Tr | Td | Te |
33-
33+
3434
Ts : Start time from MCU changing SDA from Output High to Tri-State (Hi-Z)
3535
Spec: 20-200us Tested: < 65us
3636
Tr : DHT response to MCU controlling SDA and pulling Low and High to
@@ -83,7 +83,7 @@ int PietteTech_DHT::acquire() {
8383
// return last correct measurement, (this read time - last read time) < device limit
8484
return DHTLIB_ACQUIRED;
8585
}
86-
86+
8787
if (_state == STOPPED || _state == ACQUIRED) {
8888
/*
8989
* Setup the initial state machine
@@ -133,9 +133,21 @@ int PietteTech_DHT::acquire() {
133133
return DHTLIB_ERROR_ACQUIRING;
134134
}
135135

136-
int PietteTech_DHT::acquireAndWait() {
136+
int PietteTech_DHT::acquireAndWait(uint32_t timeout=0) {
137137
acquire();
138-
while(acquiring()) ;
138+
uint32_t start = millis();
139+
uint32_t wrapper;
140+
while(acquiring() && (timeout == 0 || (millis() > start && (millis()-start) < timeout)));
141+
if (timeout)
142+
{
143+
if (millis() < start) // millis counter wrapped
144+
{
145+
wrapper = (~start)+1; // Compute elapsed seconds between "start" and counter-wrap to 0
146+
timeout -= wrapper; // Subtract elapsed seconds to 0 from timeout
147+
}
148+
// If millis counter didn't wrap, the next line will be a no-op.
149+
while(acquiring() && (millis() < timeout));
150+
}
139151
return getStatus();
140152
}
141153

‎firmware/PietteTech_DHT.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PietteTech_DHT
6565
void begin(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)());
6666
void isrCallback();
6767
int acquire();
68-
int acquireAndWait();
68+
int acquireAndWait(uint32_t);
6969
float getCelsius();
7070
float getFahrenheit();
7171
float getKelvin();
@@ -79,11 +79,11 @@ class PietteTech_DHT
7979
#if defined(DHT_DEBUG_TIMING)
8080
volatile uint8_t _edges[41];
8181
#endif
82-
82+
8383
private:
8484
void (*isrCallback_wrapper)(void);
8585
void convert();
86-
86+
8787
enum states{RESPONSE=0,DATA=1,ACQUIRED=2,STOPPED=3,ACQUIRING=4};
8888
volatile states _state;
8989
volatile int _status;

‎firmware/examples/DHT_example.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void setup()
5151

5252

5353
// This wrapper is in charge of calling
54-
// mus be defined like this for the lib work
54+
// must be defined like this for the lib work
5555
void dht_wrapper() {
5656
DHT.isrCallback();
5757
}

‎spark.json

100755100644
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "PietteTech_DHT",
3-
"version": 0.0.2”,
3+
"version": "0.0.4",
44
"author": "Scott Piette scott.piette@gmail.com",
55
"license": "GPL v3 (http://www.gnu.org/licenses/gpl.html)",
66
"description": "Interrupt driven DHT11/21/22 Sensor library"

0 commit comments

Comments
 (0)