RocketLogger  2.0.1
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 
55 int tsl4531_set_range(int sensor_identifier, int range);
56 
63 int tsl4531_get_range(int sensor_identifier);
64 
72 int tsl4531_get_id(void);
73 
80 int tsl4531_set_parameters(int sensor_identifier);
81 
89 int tsl4531_send_range(int sensor_identifier, int range);
90 
97 int tsl4531_get_index(int sensor_identifier);
98 
103 
109 
114 
118 int32_t tsl4531_values[sizeof(tsl4531_sensors)] = {0};
119 
120 int tsl4531_init(int sensor_identifier) {
121  int sensor_bus = sensors_get_bus();
122 
123  if (sensor_bus < 0) {
124  rl_log(RL_LOG_ERROR, "TSL4531 I2C bus not initialized properly");
125  return sensor_bus;
126  }
127 
128  int result = 0;
129 
130  uint8_t device_address = (uint8_t)sensor_identifier;
131  result = sensors_init_comm(device_address);
132  if (result < 0) {
134  "TSL4531 I2C initialization failed; %d message: %s", errno,
135  strerror(errno));
136  return result;
137  }
138 
139  int sensor_id = tsl4531_get_id();
140  if (sensor_id != TSL4531_ID) {
141  rl_log(RL_LOG_ERROR, "TSL4531 with wrong sensor ID: %d; %d message: %s",
142  sensor_id, errno, strerror(errno));
143  return sensor_id;
144  }
145 
146  result = tsl4531_set_parameters(sensor_identifier);
147  if (result < 0) {
149  "TSL4531 setting configuration failed; %d message: %s", errno,
150  strerror(errno));
151  return result;
152  }
153 
154  return 0;
155 }
156 
157 void tsl4531_deinit(int sensor_identifier) {
158  (void)sensor_identifier; // suppress unused parameter warning
159 }
160 
161 int tsl4531_read(int sensor_identifier) {
162  int sensor_index = tsl4531_get_index(sensor_identifier);
163  int sensor_bus = sensors_get_bus();
164 
165  int result;
166 
167  uint8_t device_address = (uint8_t)sensor_identifier;
168  result = sensors_init_comm(device_address);
169  if (result < 0) {
170  rl_log(RL_LOG_ERROR, "TSL4531 I2C communication failed; %d message: %s",
171  errno, strerror(errno));
172  return result;
173  }
174 
175  // read sensor data word
176  int32_t data = i2c_smbus_read_word_data(
178  if (data < 0) {
179  rl_log(RL_LOG_ERROR, "TSL4531 reading data word failed; %d message: %s",
180  errno, strerror(errno));
181  return data;
182  }
183 
184  tsl4531_values[sensor_index] =
185  (data & 0xffff) * tsl4531_multiplier[sensor_index];
186 
187  if (tsl4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
188  // Auto-Range
189  tsl4531_range_t range_set = tsl4531_auto_range[sensor_index];
190  switch (range_set) {
191  case TSL4531_RANGE_LOW:
192  if (tsl4531_values[sensor_index] >= TSL4531_RANGE_LOW_MAX) {
194  }
195  break;
197  if (tsl4531_values[sensor_index] >= TSL4531_RANGE_MEDIUM_MAX) {
198  tsl4531_auto_range[sensor_index] = TSL4531_RANGE_HIGH;
199  } else if (tsl4531_values[sensor_index] <
201  tsl4531_auto_range[sensor_index] = TSL4531_RANGE_LOW;
202  }
203  break;
204  case TSL4531_RANGE_HIGH:
205  if (tsl4531_values[sensor_index] <
208  }
209  break;
210  default:
211  rl_log(RL_LOG_ERROR, "TSL4531 auto range logic failure");
212  return ERROR;
213  }
214 
215  int result = tsl4531_send_range(sensor_identifier,
216  tsl4531_auto_range[sensor_index]);
217  if (result < 0) {
218  tsl4531_auto_range[sensor_index] = range_set;
219  rl_log(RL_LOG_ERROR, "TSL4531 auto range update failed");
220  return result;
221  }
222  }
223 
224  return 0;
225 }
226 
227 int32_t tsl4531_get_value(int sensor_identifier, int channel) {
228  if (channel > 0) {
229  return 0;
230  }
231 
232  int sensor_index = tsl4531_get_index(sensor_identifier);
233 
234  return tsl4531_values[sensor_index];
235 }
236 
237 int tsl4531_set_range(int sensor_identifier, int range) {
238  int sensor_index = tsl4531_get_index(sensor_identifier);
239 
240  int result = tsl4531_send_range(sensor_identifier, range);
241  if (result < 0) {
242  rl_log(RL_LOG_ERROR, "TSL4531 auto range update failed; %d message: %s",
243  errno, strerror(errno));
244  return result;
245  }
246 
247  tsl4531_range[sensor_index] = range;
248 
249  return 0;
250 }
251 
252 int tsl4531_get_range(int sensor_identifier) {
253  int sensor_index = tsl4531_get_index(sensor_identifier);
254 
255  if (tsl4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
256  return tsl4531_auto_range[sensor_index];
257  } else {
258  return tsl4531_range[sensor_index];
259  }
260 }
261 
262 int tsl4531_get_id(void) {
263  int sensor_bus = sensors_get_bus();
264 
265  int read_result =
266  i2c_smbus_read_byte_data(sensor_bus, TSL4531_COMMAND | TSL4531_REG_ID);
267 
268  if (read_result < 0) {
269  rl_log(RL_LOG_ERROR, "TSL4531 I2C error reading ID of sensor");
270  }
271  return read_result;
272 }
273 
274 int tsl4531_set_parameters(int sensor_identifier) {
275  int sensor_bus = sensors_get_bus();
276 
277  int result;
278 
279  result = i2c_smbus_write_byte_data(sensor_bus,
282  if (result < 0) {
284  "TSL4531 writing control register failed; %d message: %s", errno,
285  strerror(errno));
286  return result;
287  }
288 
289  result = tsl4531_set_range(sensor_identifier, TSL4531_RANGE_AUTO);
290  if (result < 0) {
291  rl_log(RL_LOG_ERROR, "TSL4531 setting range failed; %d message: %s",
292  errno, strerror(errno));
293  return result;
294  }
295 
296  return 0;
297 }
298 
299 int tsl4531_send_range(int sensor_identifier, int range) {
300  int sensor_index = tsl4531_get_index(sensor_identifier);
301  int sensor_bus = sensors_get_bus();
302 
303  int result = -1;
304 
305  switch (range) {
306  case TSL4531_RANGE_LOW:
307  result = i2c_smbus_write_byte_data(
310  if (result < 0) {
312  "TSL4531 writing new range configuration failed; %d "
313  "message: %s",
314  errno, strerror(errno));
315  return result;
316  }
317  tsl4531_multiplier[sensor_index] = TSL4531_MULT_400;
318  break;
320  result = i2c_smbus_write_byte_data(
323  if (result < 0) {
325  "TSL4531 writing new range configuration failed; %d "
326  "message: %s",
327  errno, strerror(errno));
328  return result;
329  }
330  tsl4531_multiplier[sensor_index] = TSL4531_MULT_200;
331  break;
332  case TSL4531_RANGE_HIGH:
333  result = i2c_smbus_write_byte_data(
336  if (result < 0) {
338  "TSL4531 writing new range configuration failed; %d "
339  "message: %s",
340  errno, strerror(errno));
341  return result;
342  }
343  tsl4531_multiplier[sensor_index] = TSL4531_MULT_100;
344  break;
345  case TSL4531_RANGE_AUTO:
346  result = i2c_smbus_write_byte_data(
349  if (result < 0) {
351  "TSL4531 writing new range configuration failed; %d "
352  "message: %s",
353  errno, strerror(errno));
354  return result;
355  }
356  tsl4531_multiplier[sensor_index] = TSL4531_MULT_200;
358  break;
359  default:
360  rl_log(RL_LOG_ERROR, "TSL4531 invalid range");
361  errno = EINVAL;
362  return result;
363  }
364 
365  return 0;
366 }
367 
368 int tsl4531_get_index(int sensor_identifier) {
369  unsigned int index = 0;
370  while (index < sizeof(tsl4531_sensors)) {
371  if (sensor_identifier == tsl4531_sensors[index]) {
372  return (int)index;
373  }
374  index++;
375  }
376  return -1;
377 }
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:252
int32_t tsl4531_get_value(int sensor_identifier, int channel)
Definition: tsl4531.c:227
uint8_t tsl4531_multiplier[sizeof(tsl4531_sensors)]
Definition: tsl4531.c:113
int tsl4531_init(int sensor_identifier)
Definition: tsl4531.c:120
int32_t tsl4531_values[sizeof(tsl4531_sensors)]
Definition: tsl4531.c:118
int tsl4531_get_index(int sensor_identifier)
Definition: tsl4531.c:368
int tsl4531_read(int sensor_identifier)
Definition: tsl4531.c:161
int tsl4531_send_range(int sensor_identifier, int range)
Definition: tsl4531.c:299
const int tsl4531_sensors[]
Definition: tsl4531.c:46
int tsl4531_set_parameters(int sensor_identifier)
Definition: tsl4531.c:274
void tsl4531_deinit(int sensor_identifier)
Definition: tsl4531.c:157
int tsl4531_get_id(void)
Definition: tsl4531.c:262
tsl4531_range_t tsl4531_auto_range[sizeof(tsl4531_sensors)]
Definition: tsl4531.c:107
int tsl4531_set_range(int sensor_identifier, int range)
Definition: tsl4531.c:237
#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