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