MCPcopy Create free account
hub / github.com/WiringPi/WiringPi / maxDetectRead

Function maxDetectRead

wiringPi/rht03.c:111–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109 */
110
111static int maxDetectRead (const int pin, unsigned char buffer [4])
112{
113 int i ;
114 unsigned int checksum ;
115 unsigned char localBuf [5] ;
116 struct timeval now, then, took ;
117
118// See how long we took
119
120 gettimeofday (&then, NULL) ;
121
122// Wake up the RHT03 by pulling the data line low, then high
123// Low for 10mS, high for 40uS.
124
125 pinMode (pin, OUTPUT) ;
126 digitalWrite (pin, 0) ; delay (10) ;
127 digitalWrite (pin, 1) ; delayMicroseconds (40) ;
128 pinMode (pin, INPUT) ;
129
130// Now wait for sensor to pull pin low
131
132 if (!maxDetectLowHighWait (pin))
133 return false ;
134
135// and read in 5 bytes (40 bits)
136
137 for (i = 0 ; i < 5 ; ++i)
138 localBuf [i] = maxDetectClockByte (pin) ;
139
140 checksum = 0 ;
141 for (i = 0 ; i < 4 ; ++i)
142 {
143 buffer [i] = localBuf [i] ;
144 checksum += localBuf [i] ;
145 }
146 checksum &= 0xFF ;
147
148// See how long we took
149
150 gettimeofday (&now, NULL) ;
151 timersub (&now, &then, &took) ;
152
153// Total time to do this should be:
154// 10mS + 40µS - reset
155// + 80µS + 80µS - sensor doing its low -> high thing
156// + 40 * (50µS + 27µS (0) or 70µS (1) )
157// = 15010µS
158// so if we take more than that, we've had a scheduling interruption and the
159// reading is probably bogus.
160
161 if ((took.tv_sec != 0) || (took.tv_usec > 16000))
162 return false ;
163
164 return checksum == localBuf [4] ;
165}
166
167
168/*

Callers 1

myReadRHT03Function · 0.70

Calls 6

pinModeFunction · 0.85
digitalWriteFunction · 0.85
delayMicrosecondsFunction · 0.85
delayFunction · 0.70
maxDetectLowHighWaitFunction · 0.70
maxDetectClockByteFunction · 0.70

Tested by

no test coverage detected