RocketLogger  2.0.2
sensor.c
Go to the documentation of this file.
1 
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include <fcntl.h>
39 #include <linux/i2c-dev.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 
45 #include "../log.h"
46 
47 #include "bme280.h"
48 #include "tsl4531.h"
49 
50 #include "sensor.h"
51 
53 int sensor_bus = -1;
54 
64  {
65  "TSL4531_left",
70  &tsl4531_init,
72  &tsl4531_read,
74  },
75  {
76  "TSL4531_right",
81  &tsl4531_init,
83  &tsl4531_read,
85  },
86  {
87  "BME280_temp",
92  &bme280_init,
94  &bme280_read,
96  },
97  {
98  "BME280_rh",
103  &bme280_init,
104  &bme280_deinit,
105  &bme280_read,
107  },
108  {
109  "BME280_press",
114  &bme280_init,
115  &bme280_deinit,
116  &bme280_read,
118  },
119 };
120 
121 int sensors_init(void) {
123  return sensor_bus;
124 }
125 
126 void sensors_deinit(void) {
128  int sensor_bus = -1;
129  (void)sensor_bus; // suppress unused parameter warning
130 }
131 
132 int sensors_open_bus(void) {
133  int bus = open(I2C_BUS_FILENAME, O_RDWR);
134  if (bus < 0) {
135  rl_log(RL_LOG_ERROR, "failed to open the I2C bus; %d message: %s", bus,
136  errno, strerror(errno));
137  }
138  return bus;
139 }
140 
141 int sensors_close_bus(int bus) {
142  int result = close(bus);
143  if (result < 0) {
144  rl_log(RL_LOG_ERROR, "failed to close the I2C bus; %d message: %s", bus,
145  errno, strerror(errno));
146  }
147  return result;
148 }
149 
150 int sensors_get_bus(void) { return sensor_bus; }
151 
152 int sensors_init_comm(uint8_t device_address) {
153  return ioctl(sensor_bus, I2C_SLAVE, device_address);
154 }
155 
156 int sensors_scan(bool sensor_available[SENSOR_REGISTRY_SIZE]) {
157 
158  // log message
159  char message[MAX_MESSAGE_LENGTH] =
160  "List of available ambient sensors:\n\t- ";
161 
162  // Scan for available sensors //
163  int sensor_count = 0;
164  int multi_channel_initialized = -1;
165  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
166  // do not initialize multi channel sensors more than once
167  int result = SUCCESS;
168  if (SENSOR_REGISTRY[i].identifier != multi_channel_initialized) {
169  result = SENSOR_REGISTRY[i].init(SENSOR_REGISTRY[i].identifier);
170  }
171 
172  if (result == SUCCESS) {
173  // sensor available
174  sensor_available[i] = true;
175  multi_channel_initialized = SENSOR_REGISTRY[i].identifier;
176  sensor_count++;
177 
178  // message
179  strncat(message, SENSOR_REGISTRY[i].name, sizeof(message) - 1);
180  strncat(message, "\n\t- ", sizeof(message) - 1);
181  } else {
182  // sensor not available
183  sensor_available[i] = false;
184  multi_channel_initialized = -1;
185  }
186  }
187 
188  // message & return
189  if (sensor_count > 0) {
190  message[strlen(message) - 3] = 0;
191  rl_log(RL_LOG_INFO, "%s", message);
192  } else {
193  rl_log(RL_LOG_WARNING, "no ambient sensor found.");
194  }
195  return sensor_count;
196 }
197 
198 int sensors_read(int32_t *const sensor_data,
199  bool const sensor_available[SENSOR_REGISTRY_SIZE]) {
200  int sensor_count = 0;
201  int multi_channel_read = -1;
202  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
203  // only read registered sensors
204  if (sensor_available[i]) {
205  // read multi-channel sensor data only once
206  if (SENSOR_REGISTRY[i].identifier != multi_channel_read) {
207  int res =
208  SENSOR_REGISTRY[i].read(SENSOR_REGISTRY[i].identifier);
209  if (res < 0) {
210  return res;
211  }
212  multi_channel_read = SENSOR_REGISTRY[i].identifier;
213  }
214  sensor_data[sensor_count] = SENSOR_REGISTRY[i].get_value(
215  SENSOR_REGISTRY[i].identifier, SENSOR_REGISTRY[i].channel);
216  sensor_count++;
217  }
218  }
219  return sensor_count;
220 }
221 
222 void sensors_close(bool const sensor_available[SENSOR_REGISTRY_SIZE]) {
223  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
224  if (sensor_available[i]) {
225  SENSOR_REGISTRY[i].deinit(SENSOR_REGISTRY[i].identifier);
226  }
227  }
228 }
int bme280_init(int sensor_identifier)
Definition: bme280.c:145
int32_t bme280_get_value(int sensor_identifier, int channel)
Definition: bme280.c:238
int bme280_read(int sensor_identifier)
Definition: bme280.c:194
void bme280_deinit(int sensor_identifier)
Definition: bme280.c:190
#define BME280_CHANNEL_TEMPERATURE
Definition: bme280.h:42
#define BME280_CHANNEL_PRESSURE
Definition: bme280.h:44
#define BME280_I2C_ADDRESS_LEFT
Definition: bme280.h:37
#define BME280_CHANNEL_HUMIDITY
Definition: bme280.h:43
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
@ RL_LOG_WARNING
Warning.
Definition: log.h:50
@ RL_LOG_INFO
Information.
Definition: log.h:51
#define SUCCESS
Function return value for successful completion.
Definition: rl.h:44
@ RL_UNIT_LUX
Lux (illuminance)
Definition: rl_file.h:90
@ RL_UNIT_INTEGER
Integer channel (numeric)
Definition: rl_file.h:92
@ RL_UNIT_PASCAL
Pascal (pressure)
Definition: rl_file.h:94
@ RL_UNIT_DEG_C
Degree celsius (temperature)
Definition: rl_file.h:91
#define RL_SCALE_MICRO
Definition: rl_file.h:73
#define RL_SCALE_MILLI
Definition: rl_file.h:74
#define RL_SCALE_UNIT
Definition: rl_file.h:75
int sensor_bus
I2C sensor bus identifier for communication.
Definition: sensor.c:53
int sensors_open_bus(void)
Definition: sensor.c:132
int sensors_scan(bool sensor_available[SENSOR_REGISTRY_SIZE])
Definition: sensor.c:156
int sensors_init(void)
Definition: sensor.c:121
int sensors_close_bus(int bus)
Definition: sensor.c:141
const rl_sensor_t SENSOR_REGISTRY[SENSOR_REGISTRY_SIZE]
Definition: sensor.c:63
int sensors_get_bus(void)
Definition: sensor.c:150
void sensors_close(bool const sensor_available[SENSOR_REGISTRY_SIZE])
Definition: sensor.c:222
void sensors_deinit(void)
Definition: sensor.c:126
int sensors_init_comm(uint8_t device_address)
Definition: sensor.c:152
int sensors_read(int32_t *const sensor_data, bool const sensor_available[SENSOR_REGISTRY_SIZE])
Definition: sensor.c:198
#define I2C_BUS_FILENAME
Definition: sensor.h:43
#define SENSOR_REGISTRY_SIZE
Number of sensor registered.
Definition: sensor.h:47
#define MAX_MESSAGE_LENGTH
Definition: sensor.h:40
int(* read)(int)
Definition: sensor.h:62
int identifier
Definition: sensor.h:56
int32_t(* get_value)(int, int)
Definition: sensor.h:63
void(* deinit)(int)
Definition: sensor.h:61
int(* init)(int)
Definition: sensor.h:60
int32_t tsl4531_get_value(int sensor_identifier, int channel)
Definition: tsl4531.c:227
int tsl4531_init(int sensor_identifier)
Definition: tsl4531.c:120
int tsl4531_read(int sensor_identifier)
Definition: tsl4531.c:161
void tsl4531_deinit(int sensor_identifier)
Definition: tsl4531.c:157
#define TSL4531_CHANNEL_DEFAULT
Definition: tsl4531.h:43
#define TSL4531_I2C_ADDRESS_RIGHT
Definition: tsl4531.h:38
#define TSL4531_I2C_ADDRESS_LEFT
Definition: tsl4531.h:37