diff options
Diffstat (limited to 'lib/chibios-contrib/os/various/devices_lib/sensors/hdc1000.h')
-rw-r--r-- | lib/chibios-contrib/os/various/devices_lib/sensors/hdc1000.h | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/devices_lib/sensors/hdc1000.h b/lib/chibios-contrib/os/various/devices_lib/sensors/hdc1000.h new file mode 100644 index 000000000..e4eae4c6a --- /dev/null +++ b/lib/chibios-contrib/os/various/devices_lib/sensors/hdc1000.h | |||
@@ -0,0 +1,240 @@ | |||
1 | /* | ||
2 | HDC100x for ChibiOS/RT - Copyright (C) 2016 Stephane D'Alu | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file hdc1000.h | ||
19 | * @brief HDC1000 Temperature/Humidiry sensor interface module header. | ||
20 | * | ||
21 | * When changing sensor settings, you generally need to wait | ||
22 | * for 2 * getAquisitionTime(), as usually the first acquisition | ||
23 | * will be corrupted by the change of settings. | ||
24 | * | ||
25 | * No locking is done. | ||
26 | * | ||
27 | * @{ | ||
28 | */ | ||
29 | |||
30 | #ifndef _SENSOR_HDC1000_H_ | ||
31 | #define _SENSOR_HDC1000_H_ | ||
32 | |||
33 | #include <math.h> | ||
34 | #include <stdbool.h> | ||
35 | #include "i2c_helpers.h" | ||
36 | #include "sensor.h" | ||
37 | |||
38 | |||
39 | /*===========================================================================*/ | ||
40 | /* Driver constants. */ | ||
41 | /*===========================================================================*/ | ||
42 | |||
43 | #define HDC1000_CONTINUOUS_ACQUISITION_SUPPORTED FALSE | ||
44 | |||
45 | /* I2C address */ | ||
46 | #define HDC1000_I2CADDR_1 0x40 | ||
47 | #define HDC1000_I2CADDR_2 0x41 | ||
48 | #define HDC1000_I2CADDR_3 0x42 | ||
49 | #define HDC1000_I2CADDR_4 0x43 | ||
50 | |||
51 | #define HDC1000_SERIAL_SIZE 5 /**< @brief Size of serial (40bits) */ | ||
52 | |||
53 | /** | ||
54 | * @brief Time necessary for the sensor to boot | ||
55 | */ | ||
56 | #define HDC1000_BOOTUP_TIME 15 | ||
57 | |||
58 | /** | ||
59 | * @brief Time necessary for the sensor to start | ||
60 | */ | ||
61 | #define HDC1000_STARTUP_TIME 0 | ||
62 | |||
63 | |||
64 | /*===========================================================================*/ | ||
65 | /* Driver pre-compile time settings. */ | ||
66 | /*===========================================================================*/ | ||
67 | |||
68 | /*===========================================================================*/ | ||
69 | /* Derived constants and error checks. */ | ||
70 | /*===========================================================================*/ | ||
71 | |||
72 | #define HDC1000_I2CADDR_DEFAULT HDC1000_I2CADDR_1 | ||
73 | |||
74 | |||
75 | /*===========================================================================*/ | ||
76 | /* Driver data structures and types. */ | ||
77 | /*===========================================================================*/ | ||
78 | |||
79 | /** | ||
80 | * @brief HDC1000 configuration structure. | ||
81 | */ | ||
82 | typedef struct { | ||
83 | I2CHelper i2c; /* keep it first */ | ||
84 | } HDC1000_config; | ||
85 | |||
86 | /** | ||
87 | * @brief HDC1000 configuration structure. | ||
88 | */ | ||
89 | typedef struct { | ||
90 | HDC1000_config *config; | ||
91 | sensor_state_t state; | ||
92 | unsigned int delay; | ||
93 | uint16_t cfg; | ||
94 | } HDC1000_drv; | ||
95 | |||
96 | /*===========================================================================*/ | ||
97 | /* Driver macros. */ | ||
98 | /*===========================================================================*/ | ||
99 | |||
100 | |||
101 | /*===========================================================================*/ | ||
102 | /* External declarations. */ | ||
103 | /*===========================================================================*/ | ||
104 | |||
105 | /** | ||
106 | * @brief Initialize the sensor driver | ||
107 | */ | ||
108 | void | ||
109 | HDC1000_init(HDC1000_drv *drv, | ||
110 | HDC1000_config *config); | ||
111 | |||
112 | /** | ||
113 | * @brief Start the sensor | ||
114 | */ | ||
115 | msg_t | ||
116 | HDC1000_start(HDC1000_drv *drv); | ||
117 | |||
118 | /** | ||
119 | * @brief Stop the sensor | ||
120 | * | ||
121 | * @details If the sensor support it, it will be put in low energy mode. | ||
122 | */ | ||
123 | msg_t | ||
124 | HDC1000_stop(HDC1000_drv *drv); | ||
125 | |||
126 | /** | ||
127 | * @brief Check that the sensor is really present | ||
128 | */ | ||
129 | msg_t | ||
130 | HDC1000_check(HDC1000_drv *drv); | ||
131 | |||
132 | |||
133 | msg_t | ||
134 | HDC1000_readSerial(HDC1000_drv *drv, uint8_t *serial); | ||
135 | |||
136 | /** | ||
137 | * @brief Control the HD1000 heater. | ||
138 | */ | ||
139 | msg_t | ||
140 | HDC1000_setHeater(HDC1000_drv *drv, | ||
141 | bool on); | ||
142 | |||
143 | |||
144 | |||
145 | /** | ||
146 | * @brief Time in milli-seconds necessary for acquiring a naw measure | ||
147 | * | ||
148 | * @returns | ||
149 | * unsigned int time in millis-seconds | ||
150 | */ | ||
151 | static inline unsigned int | ||
152 | HDC1000_getAcquisitionTime(HDC1000_drv *drv) { | ||
153 | return drv->delay; | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * @brief Trigger a mesure acquisition | ||
158 | */ | ||
159 | msg_t | ||
160 | HDC1000_startMeasure(HDC1000_drv *drv); | ||
161 | |||
162 | /** | ||
163 | * @brief Read the newly acquiered measure | ||
164 | * | ||
165 | * @note According the the sensor design the measure read | ||
166 | * can be any value acquired after the acquisition time | ||
167 | * and the call to readMeasure. | ||
168 | */ | ||
169 | msg_t | ||
170 | HDC1000_readMeasure(HDC1000_drv *drv, | ||
171 | float *temperature, float *humidity); | ||
172 | |||
173 | |||
174 | /** | ||
175 | * @brief Read temperature and humidity | ||
176 | * | ||
177 | * @details According to the sensor specification/configuration | ||
178 | * (see #HDC1000_CONTINUOUS_ACQUISITION_SUPPORTED), | ||
179 | * if the sensor is doing continuous measurement | ||
180 | * it's value will be requested and returned immediately. | ||
181 | * Otherwise a measure is started, the necessary amount of | ||
182 | * time for acquiring the value is spend sleeping (not spinning), | ||
183 | * and finally the measure is read. | ||
184 | * | ||
185 | * @note In continuous measurement mode, if you just started | ||
186 | * the sensor, you will need to wait getAcquisitionTime() | ||
187 | * in addition to the usual #HDC1000_STARTUP_TIME | ||
188 | |||
189 | * @note If using several sensors, it is better to start all the | ||
190 | * measure together, wait for the sensor having the longuest | ||
191 | * aquisition time, and finally read all the values | ||
192 | */ | ||
193 | msg_t | ||
194 | HDC1000_readTemperatureHumidity(HDC1000_drv *drv, | ||
195 | float *temperature, float *humidity); | ||
196 | |||
197 | /** | ||
198 | * @brief Return the humidity value in percent. | ||
199 | * | ||
200 | * @details Use readTemperatureHumidity() for returning the humidity value. | ||
201 | * | ||
202 | * @note Prefere readTemperatureHumidity(), if you need both temperature | ||
203 | * and humidity, or if you need better error handling. | ||
204 | * | ||
205 | * @returns | ||
206 | * float humidity percent | ||
207 | * NAN on failure | ||
208 | */ | ||
209 | static inline float | ||
210 | HDC1000_getHumidity(HDC1000_drv *drv) { | ||
211 | float humidity = NAN; | ||
212 | HDC1000_readTemperatureHumidity(drv, NULL, &humidity); | ||
213 | return humidity; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * @brief Return the temperature value in °C. | ||
218 | * | ||
219 | * @details Use readTemperatureHumidity() for returning the humidity value. | ||
220 | * | ||
221 | * @note Prefere readTemperatureHumidity(), if you need both temperature | ||
222 | * and humidity, or if you need better error handling. | ||
223 | * | ||
224 | * @returns | ||
225 | * float humidity percent | ||
226 | * NAN on failure | ||
227 | */ | ||
228 | static inline float | ||
229 | HDC1000_getTemperature(HDC1000_drv *drv) { | ||
230 | float temperature = NAN; | ||
231 | HDC1000_readTemperatureHumidity(drv, &temperature, NULL); | ||
232 | return temperature; | ||
233 | } | ||
234 | |||
235 | |||
236 | #endif | ||
237 | |||
238 | /** | ||
239 | * @} | ||
240 | */ | ||