RocketLogger 2.1.3
tsl4531.c
Go to the documentation of this file.
1
32#include <errno.h>
33#include <stdint.h>
34#include <string.h>
35
36#include <i2c/smbus.h>
37
38#include "../log.h"
39#include "sensor.h"
40
41#include "tsl4531.h"
42
47
55int tsl4531_set_range(int sensor_identifier, int range);
56
63int tsl4531_get_range(int sensor_identifier);
64
71int tsl4531_get_id(void);
72
79int tsl4531_set_parameters(int sensor_identifier);
80
88int tsl4531_send_range(int sensor_identifier, int range);
89
96int tsl4531_get_index(int sensor_identifier);
97
102
108
113
117int32_t tsl4531_values[sizeof(tsl4531_sensors)] = {0};
118
119int tsl4531_init(int sensor_identifier) {
121
122 if (sensor_bus < 0) {
123 rl_log(RL_LOG_ERROR, "TSL4531 I2C bus not initialized properly");
124 return sensor_bus;
125 }
126
127 int result = 0;
128
129 uint8_t device_address = (uint8_t)sensor_identifier;
130 result = sensors_init_comm(device_address);
131 if (result < 0) {
133 "TSL4531 I2C initialization failed; %d message: %s", errno,
134 strerror(errno));
135 return result;
136 }
137
138 int sensor_id = tsl4531_get_id();
139 if (sensor_id != TSL4531_ID) {
140 rl_log(RL_LOG_ERROR, "TSL4531 with wrong sensor ID: %d; %d message: %s",
141 sensor_id, errno, strerror(errno));
142 return sensor_id;
143 }
144
145 result = tsl4531_set_parameters(sensor_identifier);
146 if (result < 0) {
148 "TSL4531 setting configuration failed; %d message: %s", errno,
149 strerror(errno));
150 return result;
151 }
152
153 return 0;
154}
155
156void tsl4531_deinit(int sensor_identifier) {
157 (void)sensor_identifier; // suppress unused parameter warning
158}
159
160int tsl4531_read(int sensor_identifier) {
161 int sensor_index = tsl4531_get_index(sensor_identifier);
163
164 int result;
165
166 uint8_t device_address = (uint8_t)sensor_identifier;
167 result = sensors_init_comm(device_address);
168 if (result < 0) {
169 rl_log(RL_LOG_ERROR, "TSL4531 I2C communication failed; %d message: %s",
170 errno, strerror(errno));
171 return result;
172 }
173
174 // read sensor data word
175 int32_t data = i2c_smbus_read_word_data(
177 if (data < 0) {
178 rl_log(RL_LOG_ERROR, "TSL4531 reading data word failed; %d message: %s",
179 errno, strerror(errno));
180 return data;
181 }
182
183 tsl4531_values[sensor_index] =
184 (data & 0xffff) * tsl4531_multiplier[sensor_index];
185
186 if (tsl4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
187 // Auto-Range
188 tsl4531_range_t range_set = tsl4531_auto_range[sensor_index];
189 switch (range_set) {
191 if (tsl4531_values[sensor_index] >= TSL4531_RANGE_LOW_MAX) {
193 }
194 break;
196 if (tsl4531_values[sensor_index] >= TSL4531_RANGE_MEDIUM_MAX) {
198 } else if (tsl4531_values[sensor_index] <
201 }
202 break;
204 if (tsl4531_values[sensor_index] <
207 }
208 break;
209 default:
210 rl_log(RL_LOG_ERROR, "TSL4531 auto range logic failure");
211 return ERROR;
212 }
213
214 int result = tsl4531_send_range(sensor_identifier,
215 tsl4531_auto_range[sensor_index]);
216 if (result < 0) {
217 tsl4531_auto_range[sensor_index] = range_set;
218 rl_log(RL_LOG_ERROR, "TSL4531 auto range update failed");
219 return result;
220 }
221 }
222
223 return 0;
224}
225
226int32_t tsl4531_get_value(int sensor_identifier, int channel) {
227 if (channel > 0) {
228 return 0;
229 }
230
231 int sensor_index = tsl4531_get_index(sensor_identifier);
232
233 return tsl4531_values[sensor_index];
234}
235
236int tsl4531_set_range(int sensor_identifier, int range) {
237 int sensor_index = tsl4531_get_index(sensor_identifier);
238
239 int result = tsl4531_send_range(sensor_identifier, range);
240 if (result < 0) {
241 rl_log(RL_LOG_ERROR, "TSL4531 auto range update failed; %d message: %s",
242 errno, strerror(errno));
243 return result;
244 }
245
246 tsl4531_range[sensor_index] = range;
247
248 return 0;
249}
250
251int tsl4531_get_range(int sensor_identifier) {
252 int sensor_index = tsl4531_get_index(sensor_identifier);
253
254 if (tsl4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
255 return tsl4531_auto_range[sensor_index];
256 } else {
257 return tsl4531_range[sensor_index];
258 }
259}
260
261int tsl4531_get_id(void) {
263
264 int read_result =
265 i2c_smbus_read_byte_data(sensor_bus, TSL4531_COMMAND | TSL4531_REG_ID);
266
267 if (read_result < 0) {
268 rl_log(RL_LOG_ERROR, "TSL4531 I2C error reading ID of sensor");
269 }
270 return read_result;
271}
272
273int tsl4531_set_parameters(int sensor_identifier) {
275
276 int result;
277
278 result = i2c_smbus_write_byte_data(sensor_bus,
281 if (result < 0) {
283 "TSL4531 writing control register failed; %d message: %s", errno,
284 strerror(errno));
285 return result;
286 }
287
288 result = tsl4531_set_range(sensor_identifier, TSL4531_RANGE_AUTO);
289 if (result < 0) {
290 rl_log(RL_LOG_ERROR, "TSL4531 setting range failed; %d message: %s",
291 errno, strerror(errno));
292 return result;
293 }
294
295 return 0;
296}
297
298int tsl4531_send_range(int sensor_identifier, int range) {
299 int sensor_index = tsl4531_get_index(sensor_identifier);
301
302 int result = -1;
303
304 switch (range) {
306 result = i2c_smbus_write_byte_data(
309 if (result < 0) {
311 "TSL4531 writing new range configuration failed; %d "
312 "message: %s",
313 errno, strerror(errno));
314 return result;
315 }
316 tsl4531_multiplier[sensor_index] = TSL4531_MULT_400;
317 break;
319 result = i2c_smbus_write_byte_data(
322 if (result < 0) {
324 "TSL4531 writing new range configuration failed; %d "
325 "message: %s",
326 errno, strerror(errno));
327 return result;
328 }
329 tsl4531_multiplier[sensor_index] = TSL4531_MULT_200;
330 break;
332 result = i2c_smbus_write_byte_data(
335 if (result < 0) {
337 "TSL4531 writing new range configuration failed; %d "
338 "message: %s",
339 errno, strerror(errno));
340 return result;
341 }
342 tsl4531_multiplier[sensor_index] = TSL4531_MULT_100;
343 break;
345 result = i2c_smbus_write_byte_data(
348 if (result < 0) {
350 "TSL4531 writing new range configuration failed; %d "
351 "message: %s",
352 errno, strerror(errno));
353 return result;
354 }
355 tsl4531_multiplier[sensor_index] = TSL4531_MULT_200;
357 break;
358 default:
359 rl_log(RL_LOG_ERROR, "TSL4531 invalid range");
360 errno = EINVAL;
361 return result;
362 }
363
364 return 0;
365}
366
367int tsl4531_get_index(int sensor_identifier) {
368 unsigned int index = 0;
369 while (index < sizeof(tsl4531_sensors)) {
370 if (sensor_identifier == tsl4531_sensors[index]) {
371 return (int)index;
372 }
373 index++;
374 }
375 return -1;
376}
rl_calibration_t data
The actual calibration data.
Definition calibration.h:9
int rl_log(rl_log_level_t log_level, char const *const format,...)
Definition log.c:82
@ RL_LOG_ERROR
Error.
Definition log.h:49
#define ERROR
Function return value for errors (use errno to indicate the error)
Definition rl.h:46
int sensor_bus
I2C sensor bus identifier for communication.
Definition sensor.c:53
int sensors_get_bus(void)
Definition sensor.c:150
int sensors_init_comm(uint8_t device_address)
Definition sensor.c:152
int tsl4531_get_range(int sensor_identifier)
Definition tsl4531.c:251
int32_t tsl4531_get_value(int sensor_identifier, int channel)
Definition tsl4531.c:226
uint8_t tsl4531_multiplier[sizeof(tsl4531_sensors)]
Definition tsl4531.c:112
int tsl4531_init(int sensor_identifier)
Definition tsl4531.c:119
int32_t tsl4531_values[sizeof(tsl4531_sensors)]
Definition tsl4531.c:117
int tsl4531_get_index(int sensor_identifier)
Definition tsl4531.c:367
int tsl4531_read(int sensor_identifier)
Definition tsl4531.c:160
int tsl4531_send_range(int sensor_identifier, int range)
Definition tsl4531.c:298
const int tsl4531_sensors[]
Definition tsl4531.c:46
int tsl4531_set_parameters(int sensor_identifier)
Definition tsl4531.c:273
void tsl4531_deinit(int sensor_identifier)
Definition tsl4531.c:156
int tsl4531_get_id(void)
Definition tsl4531.c:261
tsl4531_range_t tsl4531_auto_range[sizeof(tsl4531_sensors)]
Definition tsl4531.c:106
int tsl4531_set_range(int sensor_identifier, int range)
Definition tsl4531.c:236
#define TSL4531_ID
Definition tsl4531.h:46
#define TSL4531_INT_TIME_200
Definition tsl4531.h:64
#define TSL4531_REG_DATALOW
Definition tsl4531.h:52
#define TSL4531_SAMPLE_CONTINUOUS
Definition tsl4531.h:58
#define TSL4531_INT_TIME_100
Definition tsl4531.h:63
#define TSL4531_RANGE_MEDIUM_MAX
Definition tsl4531.h:87
enum tsl4531_range tsl4531_range_t
Definition tsl4531.h:84
#define TSL4531_I2C_ADDRESSES
Definition tsl4531.h:40
#define TSL4531_MULT_200
Definition tsl4531.h:68
tsl4531_range
Definition tsl4531.h:74
@ TSL4531_RANGE_AUTO
Definition tsl4531.h:78
@ TSL4531_RANGE_LOW
Definition tsl4531.h:75
@ TSL4531_RANGE_HIGH
Definition tsl4531.h:77
@ TSL4531_RANGE_MEDIUM
Definition tsl4531.h:76
#define TSL4531_REG_CONTROL
Definition tsl4531.h:50
#define TSL4531_REG_CONFIG
Definition tsl4531.h:51
#define TSL4531_REG_ID
Definition tsl4531.h:54
#define TSL4531_RANGE_LOW_MAX
Definition tsl4531.h:86
#define TSL4531_INT_TIME_400
Definition tsl4531.h:65
#define TSL4531_COMMAND
Definition tsl4531.h:48
#define TSL4531_LOW_POWER
Definition tsl4531.h:61
#define TSL4531_MULT_400
Definition tsl4531.h:69
#define TSL4531_RANGE_HYSTERESIS
Definition tsl4531.h:88
#define TSL4531_MULT_100
Definition tsl4531.h:67