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