| 109 | */ |
| 110 | |
| 111 | static 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 | /* |
no test coverage detected