MCPcopy Create free account
hub / github.com/DanielOgorchock/ST_Anything / _readSensor

Method _readSensor

Arduino/libraries/DHT/dht.cpp:104–166  ·  view source on GitHub ↗

return values: DHTLIB_OK DHTLIB_ERROR_TIMEOUT

Source from the content-addressed store, hash-verified

102// DHTLIB_OK
103// DHTLIB_ERROR_TIMEOUT
104int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
105{
106 // INIT BUFFERVAR TO RECEIVE DATA
107 uint8_t mask = 128;
108 uint8_t idx = 0;
109
110 // EMPTY BUFFER
111 for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
112
113 // REQUEST SAMPLE
114 pinMode(pin, OUTPUT);
115 digitalWrite(pin, LOW);
116 delay(wakeupDelay);
117 digitalWrite(pin, HIGH);
118 delayMicroseconds(40);
119 pinMode(pin, INPUT);
120
121 // GET ACKNOWLEDGE or TIMEOUT
122 uint16_t loopCnt = DHTLIB_TIMEOUT;
123 while(digitalRead(pin) == LOW)
124 {
125 if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
126 }
127
128 loopCnt = DHTLIB_TIMEOUT;
129 while(digitalRead(pin) == HIGH)
130 {
131 if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
132 }
133
134 // READ THE OUTPUT - 40 BITS => 5 BYTES
135 for (uint8_t i = 40; i != 0; i--)
136 {
137 loopCnt = DHTLIB_TIMEOUT;
138 while(digitalRead(pin) == LOW)
139 {
140 if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
141 }
142
143 uint32_t t = micros();
144
145 loopCnt = DHTLIB_TIMEOUT;
146 while(digitalRead(pin) == HIGH)
147 {
148 if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
149 }
150
151 if ((micros() - t) > 40)
152 {
153 bits[idx] |= mask;
154 }
155 mask >>= 1;
156 if (mask == 0) // next byte?
157 {
158 mask = 128;
159 idx++;
160 }
161 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected