RocketLogger  1.1.6
sensor.c
Go to the documentation of this file.
1 
32 #include <stdint.h>
33 
34 #include <linux/i2c-dev.h>
35 
36 #include "../log.h"
37 
38 #include "bme280.h"
39 #include "tsl4531.h"
40 
41 #include "sensor.h"
42 
43 int sensor_bus = -1;
44 
54  {
58  },
59  {
63  },
64  {
68  },
69  {
73  },
74  {
78  },
79 };
80 
85 int Sensors_openBus(void) {
86  int bus = open(I2C_BUS_FILENAME, O_RDWR);
87  if (bus < 0) {
88  rl_log(ERROR, "failed to open the I2C bus", bus);
89  }
90  return bus;
91 }
92 
98 int Sensors_closeBus(int bus) {
99  int result = close(bus);
100  if (result < 0) {
101  rl_log(ERROR, "failed to close the I2C bus", bus);
102  }
103  return result;
104 }
105 
112  return sensor_bus;
113 }
114 
119 int Sensors_getSharedBus(void) { return sensor_bus; }
120 
126 int Sensors_initSharedComm(uint8_t device_address) {
127  return ioctl(sensor_bus, I2C_SLAVE, device_address);
128 }
129 
136  int sensor_bus = -1;
137  (void)sensor_bus; // suppress unused warning
138 }
139 
145 int Sensors_scan(int sensors_available[]) {
146 
147  // log message
148  char message[MAX_MESSAGE_LENGTH] =
149  "List of available ambient sensors:\n\t- ";
150 
151  // Scan for available sensors //
152  int sensor_count = 0;
153 
154  // scan
155  int mutli_channel_initialized = -1;
156  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
157  // do not initialize multi channel sensors more than once
158  int result = 0;
159  if (sensor_registry[i].identifier != mutli_channel_initialized) {
160  result = sensor_registry[i].init(sensor_registry[i].identifier);
161  } else {
162  result = SUCCESS;
163  }
164 
165  if (result == SUCCESS) {
166  // sensor available
167  sensors_available[i] = sensor_registry[i].identifier;
168  mutli_channel_initialized = sensor_registry[i].identifier;
169  sensor_count++;
170 
171  // message
172  strcat(message, sensor_registry[i].name);
173  strcat(message, "\n\t- ");
174  } else {
175  // sensor not available
176  sensors_available[i] = -1;
177  mutli_channel_initialized = -1;
178  }
179  }
180 
181  // message & return
182  if (sensor_count == 0) {
183  rl_log(WARNING, "no ambient sensor found...");
184  } else {
185  message[strlen(message) - 3] = 0;
186  rl_log(INFO, "%s", message);
187  printf("\n\n%s\n", message);
188  }
189  return sensor_count;
190 }
191 
196 void Sensors_close(int sensors_available[]) {
197  int i;
198  for (i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
199  if (sensors_available[i] >= 0) {
200  sensor_registry[i].close(sensor_registry[i].identifier);
201  }
202  }
203 }
void Sensors_close(int sensors_available[])
Definition: sensor.c:196
int TSL4531_init(int sensor_identifier)
Definition: tsl4531.c:52
int BME280_read(int sensor_identifier)
Definition: bme280.c:99
#define SUCCESS
Definition: types.h:75
Degree celcius (temperature)
Definition: rl_file.h:90
int BME280_init(int sensor_identifier)
Definition: bme280.c:49
#define BME280_CHANNEL_PRESSURE
Definition: bme280.h:57
int identifier
Definition: sensor.h:55
int sensor_bus
Definition: sensor.c:43
#define SENSOR_REGISTRY_SIZE
Number of sensor registred.
Definition: sensor.h:46
void Sensors_closeSharedBus(void)
Definition: sensor.c:134
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:39
char name[SENSOR_NAME_LENGTH]
Definition: sensor.h:54
#define MAX_MESSAGE_LENGTH
Definition: sensor.h:39
int Sensors_initSharedComm(uint8_t device_address)
Definition: sensor.c:126
int Sensors_getSharedBus(void)
Definition: sensor.c:119
int32_t TSL4531_getValue(int sensor_identifier, int channel)
Definition: tsl4531.c:167
#define BME280_CHANNEL_TEMPERATURE
Definition: bme280.h:55
#define TSL4531_CHANNEL_DEFAULT
Definition: tsl4531.h:56
int Sensors_scan(int sensors_available[])
Definition: sensor.c:145
const struct rl_sensor sensor_registry[SENSOR_REGISTRY_SIZE]
Definition: sensor.c:53
Warning.
Definition: types.h:201
int TSL4531_read(int sensor_identifier)
Definition: tsl4531.c:97
Pascal (preasure)
Definition: rl_file.h:93
#define TSL4531_I2C_ADDRESS_RIGHT
Definition: tsl4531.h:51
int Sensors_openBus(void)
Definition: sensor.c:85
Error.
Definition: types.h:200
void(* close)(int)
Definition: sensor.h:60
#define TSL4531_I2C_ADDRESS_LEFT
Definition: tsl4531.h:50
#define BME280_CHANNEL_HUMIDITY
Definition: bme280.h:56
#define I2C_BUS_FILENAME
Definition: sensor.h:42
int(* init)(int)
Definition: sensor.h:59
Integer channel (numeric)
Definition: rl_file.h:91
void BME280_close(int sensor_identifier)
Definition: bme280.c:90
void TSL4531_close(int sensor_identifier)
Definition: tsl4531.c:88
Lux (illuminance)
Definition: rl_file.h:89
int32_t BME280_getValue(int sensor_identifier, int channel)
Definition: bme280.c:147
Information.
Definition: types.h:202
#define BME280_I2C_ADDRESS_LEFT
Definition: bme280.h:50
#define RL_SCALE_MILLI
Definition: rl_file.h:50
int Sensors_closeBus(int bus)
Definition: sensor.c:98
int Sensors_initSharedBus(void)
Definition: sensor.c:110
#define RL_SCALE_MICRO
Definition: rl_file.h:49
#define RL_SCALE_NONE
Definition: rl_file.h:51