diff options
Diffstat (limited to 'lib/chibios-contrib/os/various/devices_lib/sensors/tsl2591.h')
-rw-r--r-- | lib/chibios-contrib/os/various/devices_lib/sensors/tsl2591.h | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/devices_lib/sensors/tsl2591.h b/lib/chibios-contrib/os/various/devices_lib/sensors/tsl2591.h new file mode 100644 index 000000000..8320eb8aa --- /dev/null +++ b/lib/chibios-contrib/os/various/devices_lib/sensors/tsl2591.h | |||
@@ -0,0 +1,238 @@ | |||
1 | /* | ||
2 | TSL2591 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 tsl2591.h | ||
19 | * @brief TSL2591 Light sensor interface module header. | ||
20 | * | ||
21 | * @{ | ||
22 | */ | ||
23 | |||
24 | #ifndef _SENSOR_TSL2591_H_ | ||
25 | #define _SENSOR_TSL2591_H_ | ||
26 | |||
27 | #include <math.h> | ||
28 | #include "i2c_helpers.h" | ||
29 | #include "sensor.h" | ||
30 | |||
31 | |||
32 | /*===========================================================================*/ | ||
33 | /* Driver constants. */ | ||
34 | /*===========================================================================*/ | ||
35 | |||
36 | /** | ||
37 | * @brief Device sensor continuous acquisition support. | ||
38 | */ | ||
39 | #define TSL2591_CONTINUOUS_ACQUISITION_SUPPORTED TRUE | ||
40 | |||
41 | /** | ||
42 | * @brief I2C address. | ||
43 | */ | ||
44 | #define TSL2591_I2CADDR_FIXED 0x29 | ||
45 | |||
46 | /** | ||
47 | * @brief Time necessary for the sensor to boot | ||
48 | */ | ||
49 | #define TSL2591_BOOTUP_TIME 0 | ||
50 | |||
51 | /** | ||
52 | * @brief Time necessary for the sensor to start | ||
53 | */ | ||
54 | #define TSL2591_STARTUP_TIME 0 | ||
55 | |||
56 | |||
57 | |||
58 | /*===========================================================================*/ | ||
59 | /* Driver pre-compile time settings. */ | ||
60 | /*===========================================================================*/ | ||
61 | |||
62 | /*===========================================================================*/ | ||
63 | /* Derived constants and error checks. */ | ||
64 | /*===========================================================================*/ | ||
65 | |||
66 | /** | ||
67 | * @brief Default I2C address (when pin unconfigured) | ||
68 | */ | ||
69 | #define TSL2591_I2CADDR_DEFAULT TSL2591_I2CADDR_FIXED | ||
70 | |||
71 | |||
72 | /*===========================================================================*/ | ||
73 | /* Driver data structures and types. */ | ||
74 | /*===========================================================================*/ | ||
75 | |||
76 | /** | ||
77 | * @brief TSL2591 configuration structure. | ||
78 | */ | ||
79 | typedef struct { | ||
80 | I2CHelper i2c; /* keep it first */ | ||
81 | } TSL2591_config; | ||
82 | |||
83 | /** | ||
84 | * @brief Available integration time | ||
85 | * | ||
86 | * @details Available integration time are: | ||
87 | * 100ms, 200ms, 300ms, 400ms, 500ms and 600ms | ||
88 | */ | ||
89 | typedef enum { | ||
90 | TSL2591_INTEGRATIONTIME_100MS = 0x00, /**< @brief 100ms */ | ||
91 | TSL2591_INTEGRATIONTIME_200MS = 0x01, /**< @brief 200ms */ | ||
92 | TSL2591_INTEGRATIONTIME_300MS = 0x02, /**< @brief 300ms */ | ||
93 | TSL2591_INTEGRATIONTIME_400MS = 0x03, /**< @brief 400ms */ | ||
94 | TSL2591_INTEGRATIONTIME_500MS = 0x04, /**< @brief 500ms */ | ||
95 | TSL2591_INTEGRATIONTIME_600MS = 0x05, /**< @brief 600ms */ | ||
96 | } TSL2591_integration_time_t; | ||
97 | |||
98 | /** | ||
99 | * @brief Available gain | ||
100 | * | ||
101 | * @details Available gain are 1x, 25x, 415x, 10000x | ||
102 | */ | ||
103 | typedef enum { | ||
104 | TSL2591_GAIN_1X = 0x00, /**< @brief 1x gain */ | ||
105 | TSL2591_GAIN_25X = 0x10, /**< @brief 25x gain */ | ||
106 | TSL2591_GAIN_415X = 0x20, /**< @brief 415x gain */ | ||
107 | TSL2591_GAIN_10000X = 0x30, /**< @brief 10000x gain */ | ||
108 | } TSL2591_gain_t; | ||
109 | |||
110 | /** | ||
111 | * @brief TSL2591 configuration structure. | ||
112 | */ | ||
113 | typedef struct { | ||
114 | TSL2591_config *config; | ||
115 | sensor_state_t state; | ||
116 | TSL2591_gain_t gain; | ||
117 | TSL2591_integration_time_t integration_time; | ||
118 | } TSL2591_drv; | ||
119 | |||
120 | /*===========================================================================*/ | ||
121 | /* Driver macros. */ | ||
122 | /*===========================================================================*/ | ||
123 | |||
124 | |||
125 | /*===========================================================================*/ | ||
126 | /* External declarations. */ | ||
127 | /*===========================================================================*/ | ||
128 | |||
129 | /** | ||
130 | * @brief Initialize the sensor driver | ||
131 | */ | ||
132 | void | ||
133 | TSL2591_init(TSL2591_drv *drv, | ||
134 | TSL2591_config *config); | ||
135 | |||
136 | /** | ||
137 | * @brief Start the sensor | ||
138 | */ | ||
139 | msg_t | ||
140 | TSL2591_start(TSL2591_drv *drv); | ||
141 | |||
142 | /** | ||
143 | * @brief Stop the sensor | ||
144 | * | ||
145 | * @details If the sensor support it, it will be put in low energy mode. | ||
146 | */ | ||
147 | msg_t | ||
148 | TSL2591_stop(TSL2591_drv *drv); | ||
149 | |||
150 | /** | ||
151 | * @brief Check that the sensor is really present | ||
152 | */ | ||
153 | msg_t | ||
154 | TSL2591_check(TSL2591_drv *drv); | ||
155 | |||
156 | /** | ||
157 | * @brief Time in milli-seconds necessary for acquiring a naw measure | ||
158 | * | ||
159 | * @returns | ||
160 | * unsigned int time in millis-seconds | ||
161 | */ | ||
162 | unsigned int | ||
163 | TSL2591_getAcquisitionTime(TSL2591_drv *drv); | ||
164 | |||
165 | /** | ||
166 | * @brief Trigger a mesure acquisition | ||
167 | */ | ||
168 | static inline msg_t | ||
169 | TSL2591_startMeasure(TSL2591_drv *drv) { | ||
170 | (void)drv; | ||
171 | return MSG_OK; | ||
172 | }; | ||
173 | |||
174 | |||
175 | msg_t | ||
176 | TSL2591_setGain(TSL2591_drv *drv, | ||
177 | TSL2591_gain_t gain); | ||
178 | |||
179 | msg_t | ||
180 | TSL2591_setIntegrationTime(TSL2591_drv *drv, | ||
181 | TSL2591_integration_time_t time); | ||
182 | |||
183 | /** | ||
184 | * @brief Read the newly acquiered measure | ||
185 | * | ||
186 | * @note According the the sensor design the measure read | ||
187 | * can be any value acquired after the acquisition time | ||
188 | * and the call to readMeasure. | ||
189 | */ | ||
190 | msg_t | ||
191 | TSL2591_readMeasure(TSL2591_drv *drv, | ||
192 | unsigned int illuminance); | ||
193 | |||
194 | |||
195 | /** | ||
196 | * @brief Read temperature and humidity | ||
197 | * | ||
198 | * @details According to the sensor specification/configuration | ||
199 | * (see #TSL2591_CONTINUOUS_ACQUISITION_SUPPORTED), | ||
200 | * if the sensor is doing continuous measurement | ||
201 | * it's value will be requested and returned immediately. | ||
202 | * Otherwise a measure is started, the necessary amount of | ||
203 | * time for acquiring the value is spend sleeping (not spinning), | ||
204 | * and finally the measure is read. | ||
205 | * | ||
206 | * @note In continuous measurement mode, if you just started | ||
207 | * the sensor, you will need to wait getAcquisitionTime() | ||
208 | * in addition to the usual getStartupTime() | ||
209 | |||
210 | * @note If using several sensors, it is better to start all the | ||
211 | * measure together, wait for the sensor having the longuest | ||
212 | * aquisition time, and finally read all the values | ||
213 | */ | ||
214 | msg_t | ||
215 | TSL2591_readIlluminance(TSL2591_drv *drv, | ||
216 | unsigned int *illuminance); | ||
217 | |||
218 | /** | ||
219 | * @brief Return the illuminance value in Lux | ||
220 | * | ||
221 | * @details Use readIlluminance() for returning the humidity value. | ||
222 | * | ||
223 | * @note Prefere readIlluminance()if you need better error handling. | ||
224 | * | ||
225 | * @return Illuminance in Lux | ||
226 | * @retval unsigned int illuminace value | ||
227 | * @retval -1 on failure | ||
228 | */ | ||
229 | static inline unsigned int | ||
230 | TSL2591_getIlluminance(TSL2591_drv *drv) { | ||
231 | unsigned int illuminance = -1; | ||
232 | TSL2591_readIlluminance(drv, &illuminance); | ||
233 | return illuminance; | ||
234 | } | ||
235 | |||
236 | |||
237 | #endif | ||
238 | |||