blob: 23eabf80a2f35836736fcec3949485c5de34f9c6 [file] [log] [blame]
Gordon Hendersonda384432013-05-13 19:43:26 +01001/*
2 * maxdetect.c:
3 * Driver for the MaxDetect series sensors
4 *
5 * Copyright (c) 2013 Gordon Henderson.
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 *
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
23 */
24
25//#include <stdio.h>
26//#include <stdlib.h>
27//#include <unistd.h>
28
29#include <wiringPi.h>
30
31#include "maxdetect.h"
32
33#ifndef TRUE
34# define TRUE (1==1)
35# define FALSE (1==2)
36#endif
37
38
39/*
40 * maxDetectLowHighWait:
41 * Wait for a transition from high to low on the bus
42 *********************************************************************************
43 */
44
45static void maxDetectLowHighWait (const int pin)
46{
47 unsigned int timeOut = millis () + 2000 ;
48
49 while (digitalRead (pin) == HIGH)
50 if (millis () > timeOut)
51 return ;
52
53 while (digitalRead (pin) == LOW)
54 if (millis () > timeOut)
55 return ;
56}
57
58
59/*
60 * maxDetectClockByte:
61 * Read in a single byte from the MaxDetect bus
62 *********************************************************************************
63 */
64
65static unsigned int maxDetectClockByte (const int pin)
66{
67 unsigned int byte = 0 ;
68 int bit ;
69
70 for (bit = 0 ; bit < 8 ; ++bit)
71 {
72 maxDetectLowHighWait (pin) ;
73
74// bit starting now - we need to time it.
75
76 delayMicroseconds (30) ;
77 byte <<= 1 ;
78 if (digitalRead (pin) == HIGH) // It's a 1
79 byte |= 1 ;
80 }
81
82 return byte ;
83}
84
85
86/*
87 * maxDetectRead:
88 * Read in and return the 4 data bytes from the MaxDetect sensor.
89 * Return TRUE/FALSE depending on the checksum validity
90 *********************************************************************************
91 */
92
93int maxDetectRead (const int pin, unsigned char buffer [4])
94{
95 int i ;
96 unsigned int checksum ;
97 unsigned char localBuf [5] ;
98
99// Wake up the RHT03 by pulling the data line low, then high
100// Low for 10mS, high for 40uS.
101
102 pinMode (pin, OUTPUT) ;
103 digitalWrite (pin, 0) ; delay (10) ;
104 digitalWrite (pin, 1) ; delayMicroseconds (40) ;
105 pinMode (pin, INPUT) ;
106
107// Now wait for sensor to pull pin low
108
109 maxDetectLowHighWait (pin) ;
110
111// and read in 5 bytes (40 bits)
112
113 for (i = 0 ; i < 5 ; ++i)
114 localBuf [i] = maxDetectClockByte (pin) ;
115
116 checksum = 0 ;
117 for (i = 0 ; i < 4 ; ++i)
118 {
119 buffer [i] = localBuf [i] ;
120 checksum += localBuf [i] ;
121 }
122 checksum &= 0xFF ;
123
124 return checksum == localBuf [4] ;
125}
126
127
128/*
129 * readRHT03:
130 * Read the Temperature & Humidity from an RHT03 sensor
131 *********************************************************************************
132 */
133
134int readRHT03 (const int pin, int *temp, int *rh)
135{
136 static unsigned int nextTime = 0 ;
137 static int lastTemp = 0 ;
138 static int lastRh = 0 ;
139 static int lastResult = TRUE ;
140
141 unsigned char buffer [4] ;
142
143// Don't read more than once a second
144
145 if (millis () < nextTime)
146 {
147 *temp = lastTemp ;
148 *rh = lastRh ;
149 return lastResult ;
150 }
151
152 lastResult = maxDetectRead (pin, buffer) ;
153
154 if (lastResult)
155 {
156 *temp = lastTemp = (buffer [2] * 256 + buffer [3]) ;
157 *rh = lastRh = (buffer [0] * 256 + buffer [1]) ;
158 nextTime = millis () + 2000 ;
159 return TRUE ;
160 }
161 else
162 {
163 return FALSE ;
164 }
165}