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