RocketLogger  1.1
tsl4531.c
Go to the documentation of this file.
1 
31 #include <errno.h>
32 
33 #include "sensor.h"
34 
35 #include "tsl4531.h"
36 
38 
44 int32_t TSL4531_values[sizeof(TSL4531_sensors)] = {0};
45 
51 int TSL4531_init(int sensor_identifier) {
53 
54  if (sensor_bus < 0) {
55  rl_log(ERROR, "TSL4531 I2C bus not initialized properly");
56  return FAILURE;
57  }
58 
59  int result = 0;
60 
61  uint8_t device_address = (uint8_t)sensor_identifier;
62  result = Sensors_initSharedComm(device_address);
63  if (result < 0) {
64  rl_log(ERROR, "TSL4531 I2C initialization failed");
65  return FAILURE;
66  }
67 
68  uint8_t sensor_id = TSL4531_getID();
69  if (sensor_id != TSL4531_ID) {
70  rl_log(ERROR, "TSL4531 with wrong sensor ID: %d", sensor_id);
71  return FAILURE;
72  }
73 
74  result = TSL4531_setParameters(sensor_identifier);
75  if (result < 0) {
76  rl_log(ERROR, "TSL4531 setting configuraiton failed");
77  return FAILURE;
78  }
79 
80  return SUCCESS;
81 }
82 
87 void TSL4531_close(int sensor_identifier) {
88  (void)sensor_identifier; // suppress unused warning
89 }
90 
96 int TSL4531_read(int sensor_identifier) {
97  int sensor_index = TSL4531_getIndex(sensor_identifier);
99 
100  int result;
101 
102  uint8_t device_address = (uint8_t)sensor_identifier;
103  result = Sensors_initSharedComm(device_address);
104  if (result < 0) {
105  rl_log(ERROR, "TSL4531 I2C communication failed");
106  return FAILURE;
107  }
108 
109  // read sensor data word
110  int32_t data = i2c_smbus_read_word_data(
111  sensor_bus, TSL4531_COMMAND | TSL4531_REG_DATALOW);
112  if (data < 0) {
113  rl_log(ERROR, "TSL4531 reading data word failed");
114  return FAILURE;
115  }
116 
117  TSL4531_values[sensor_index] = data & 0xffff;
118 
119  if (TSL4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
120  // Auto-Range
121  enum TSL4531_range range_set = TSL4531_auto_range[sensor_index];
122  switch (range_set) {
123  case TSL4531_RANGE_LOW:
124  if (TSL4531_values[sensor_index] >= TSL4531_RANGE_LOW_MAX) {
126  }
127  break;
129  if (TSL4531_values[sensor_index] >= TSL4531_RANGE_MEDIUM_MAX) {
130  TSL4531_auto_range[sensor_index] = TSL4531_RANGE_HIGH;
131  } else if (TSL4531_values[sensor_index] <
133  TSL4531_auto_range[sensor_index] = TSL4531_RANGE_LOW;
134  }
135  break;
136  case TSL4531_RANGE_HIGH:
137  if (TSL4531_values[sensor_index] <
140  }
141  break;
142  default:
143  rl_log(ERROR, "TSL4531 auto range logic failure");
144  return FAILURE;
145  }
146 
147  int result = TSL4531_sendRange(sensor_identifier,
148  TSL4531_auto_range[sensor_index]);
149  if (result < 0) {
150  TSL4531_auto_range[sensor_index] = range_set;
151  rl_log(ERROR, "TSL4531 auto range update failed");
152  return FAILURE;
153  }
154  }
155 
156  return SUCCESS;
157 }
158 
165 int32_t TSL4531_getValue(int sensor_identifier, int channel) {
166  if (channel > 0) {
167  return 0;
168  }
169 
170  int sensor_index = TSL4531_getIndex(sensor_identifier);
171 
172  return TSL4531_values[sensor_index];
173 }
174 
181 int TSL4531_setRange(int sensor_identifier, int range) {
182  int sensor_index = TSL4531_getIndex(sensor_identifier);
183 
184  int result = TSL4531_sendRange(sensor_identifier, range);
185  if (result < 0) {
186  rl_log(ERROR, "TSL4531 auto range update failed");
187  return FAILURE;
188  }
189 
190  TSL4531_range[sensor_index] = range;
191 
192  return SUCCESS;
193 }
194 
200 int TSL4531_getRange(int sensor_identifier) {
201  int sensor_index = TSL4531_getIndex(sensor_identifier);
202 
203  if (TSL4531_range[sensor_index] == TSL4531_RANGE_AUTO) {
204  return TSL4531_auto_range[sensor_index];
205  } else {
206  return TSL4531_range[sensor_index];
207  }
208 }
209 
215 int TSL4531_getID(void) {
217 
218  int read_result =
219  i2c_smbus_read_byte_data(sensor_bus, TSL4531_COMMAND | TSL4531_REG_ID);
220 
221  if (read_result < 0) {
222  rl_log(ERROR, "TSL4531 I2C error reading ID of sensor");
223  }
224  return read_result;
225 }
226 
232 int TSL4531_setParameters(int sensor_identifier) {
234 
235  int result;
236 
237  result = i2c_smbus_write_byte_data(sensor_bus,
240  if (result < 0) {
241  rl_log(ERROR, "TSL4531 writing control register failed");
242  return FAILURE;
243  }
244 
245  result = TSL4531_setRange(sensor_identifier, TSL4531_RANGE_AUTO);
246  if (result < 0) {
247  rl_log(ERROR, "TSL4531 setting range failed");
248  return FAILURE;
249  }
250 
251  return SUCCESS;
252 }
253 
260 int TSL4531_sendRange(int sensor_identifier, int range) {
261  int sensor_index = TSL4531_getIndex(sensor_identifier);
263 
264  int result;
265 
266  switch (range) {
267  case TSL4531_RANGE_LOW:
268  result = i2c_smbus_write_byte_data(
269  sensor_bus, TSL4531_COMMAND | TSL4531_REG_CONFIG,
271  if (result < 0) {
272  rl_log(ERROR, "TSL4531 writing new range configuration failed");
273  return FAILURE;
274  }
275  TSL4531_multiplier[sensor_index] = TSL4531_MULT_400;
276  break;
278  result = i2c_smbus_write_byte_data(
279  sensor_bus, TSL4531_COMMAND | TSL4531_REG_CONFIG,
281  if (result < 0) {
282  rl_log(ERROR, "TSL4531 writing new range configuration failed");
283  return FAILURE;
284  }
285  TSL4531_multiplier[sensor_index] = TSL4531_MULT_200;
286  break;
287  case TSL4531_RANGE_HIGH:
288  result = i2c_smbus_write_byte_data(
289  sensor_bus, TSL4531_COMMAND | TSL4531_REG_CONFIG,
291  if (result < 0) {
292  rl_log(ERROR, "TSL4531 writing new range configuration failed");
293  return FAILURE;
294  }
295  TSL4531_multiplier[sensor_index] = TSL4531_MULT_100;
296  break;
297  case TSL4531_RANGE_AUTO:
298  result = i2c_smbus_write_byte_data(
299  sensor_bus, TSL4531_COMMAND | TSL4531_REG_CONFIG,
301  if (result < 0) {
302  rl_log(ERROR, "TSL4531 writing new range configuration failed");
303  return FAILURE;
304  }
305  TSL4531_multiplier[sensor_index] = TSL4531_MULT_200;
307  break;
308  default:
309  rl_log(ERROR, "TSL4531 invalid range");
310  return FAILURE;
311  }
312 
313  return SUCCESS;
314 }
315 
321 int TSL4531_getIndex(int sensor_identifier) {
322  unsigned int index = 0;
323  while (index < sizeof(TSL4531_sensors)) {
324  if (sensor_identifier == TSL4531_sensors[index]) {
325  return (int)index;
326  }
327  index++;
328  }
329  return -1;
330 }
#define TSL4531_SAMPLE_CONTINUOUS
Definition: tsl4531.h:70
#define TSL4531_MULT_100
Definition: tsl4531.h:79
int TSL4531_init(int sensor_identifier)
Definition: tsl4531.c:51
#define TSL4531_INT_TIME_100
Definition: tsl4531.h:75
#define TSL4531_RANGE_HYSTERESIS
Definition: tsl4531.h:94
#define FAILURE
Definition: types.h:77
#define TSL4531_REG_CONFIG
Definition: tsl4531.h:63
#define SUCCESS
Definition: types.h:74
#define TSL4531_I2C_ADDRESSES
Definition: tsl4531.h:52
#define TSL4531_LOW_POWER
Definition: tsl4531.h:73
int sensor_bus
Definition: sensor.c:42
enum TSL4531_range TSL4531_auto_range[sizeof(TSL4531_sensors)]
Definition: tsl4531.c:41
#define TSL4531_REG_CONTROL
Definition: tsl4531.h:62
int32_t TSL4531_values[sizeof(TSL4531_sensors)]
Definition: tsl4531.c:44
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:38
TSL4531_range
Definition: tsl4531.h:86
#define TSL4531_MULT_400
Definition: tsl4531.h:81
int Sensors_initSharedComm(uint8_t device_address)
Definition: sensor.c:125
int TSL4531_setParameters(int sensor_identifier)
Definition: tsl4531.c:232
const int TSL4531_sensors[]
Definition: tsl4531.c:37
int Sensors_getSharedBus(void)
Definition: sensor.c:118
#define TSL4531_RANGE_LOW_MAX
Definition: tsl4531.h:92
int32_t TSL4531_getValue(int sensor_identifier, int channel)
Definition: tsl4531.c:165
#define TSL4531_RANGE_MEDIUM_MAX
Definition: tsl4531.h:93
int TSL4531_getRange(int sensor_identifier)
Definition: tsl4531.c:200
int TSL4531_read(int sensor_identifier)
Definition: tsl4531.c:96
#define TSL4531_REG_DATALOW
Definition: tsl4531.h:64
int TSL4531_sendRange(int sensor_identifier, int range)
Definition: tsl4531.c:260
uint8_t TSL4531_multiplier[sizeof(TSL4531_sensors)]
Definition: tsl4531.c:43
#define TSL4531_ID
Definition: tsl4531.h:58
#define TSL4531_INT_TIME_200
Definition: tsl4531.h:76
int TSL4531_getIndex(int sensor_identifier)
Definition: tsl4531.c:321
Error.
Definition: types.h:199
#define TSL4531_MULT_200
Definition: tsl4531.h:80
int TSL4531_setRange(int sensor_identifier, int range)
Definition: tsl4531.c:181
#define TSL4531_REG_ID
Definition: tsl4531.h:66
int channel
Definition: sensor.h:55
void TSL4531_close(int sensor_identifier)
Definition: tsl4531.c:87
#define TSL4531_INT_TIME_400
Definition: tsl4531.h:77
#define TSL4531_COMMAND
Definition: tsl4531.h:60
int TSL4531_getID(void)
Definition: tsl4531.c:215