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

Function maxDetectRead

devLib/maxdetect.c:113–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

readRHT03Function · 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