Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_module_climate.c
1 #include <bc_module_climate.h>
2 #include <bc_tmp112.h>
3 #include <bc_sht20.h>
4 #include <bc_opt3001.h>
5 #include <bc_mpl3115a2.h>
6 #include <bc_sht30.h>
7 
8 static struct
9 {
10  void (*event_handler)(bc_module_climate_event_t, void *);
11  void *event_param;
13  bc_tmp112_t tmp112;
14  bc_opt3001_t opt3001;
15  bc_mpl3115a2_t mpl3115a2;
16  union {
17  bc_sht20_t _20;
18  bc_sht30_t _30;
19  } sht;
20  struct {
21  bc_tick_t thermometer;
22  bc_tick_t hygrometer;
23  } update_interval;
24 
25 } _bc_module_climate;
26 
27 static void _bc_module_climate_tmp112_event_handler(bc_tmp112_t *self, bc_tmp112_event_t event, void *event_param);
28 
29 static void _bc_module_climate_sht20_event_handler(bc_sht20_t *self, bc_sht20_event_t event, void *event_param);
30 
31 static void _bc_module_climate_sht30_event_handler(bc_sht30_t *self, bc_sht30_event_t event, void *event_param);
32 
33 static void _bc_module_climate_opt3001_event_handler(bc_opt3001_t *self, bc_opt3001_event_t event, void *event_param);
34 
35 static void _bc_module_climate_mpl3115a2_event_handler(bc_mpl3115a2_t *self, bc_mpl3115a2_event_t event, void *event_param);
36 
38 {
39  memset(&_bc_module_climate, 0, sizeof(_bc_module_climate));
40 
41  _bc_module_climate.update_interval.thermometer = BC_TICK_INFINITY;
42  _bc_module_climate.update_interval.hygrometer = BC_TICK_INFINITY;
43 
44  bc_tmp112_init(&_bc_module_climate.tmp112, BC_I2C_I2C0, 0x48);
45  bc_tmp112_set_event_handler(&_bc_module_climate.tmp112, _bc_module_climate_tmp112_event_handler, NULL);
46 
47  bc_sht20_init(&_bc_module_climate.sht._20, BC_I2C_I2C0, 0x40);
48  bc_sht20_set_event_handler(&_bc_module_climate.sht._20, _bc_module_climate_sht20_event_handler, NULL);
49 
50  bc_opt3001_init(&_bc_module_climate.opt3001, BC_I2C_I2C0, 0x44);
51  bc_opt3001_set_event_handler(&_bc_module_climate.opt3001, _bc_module_climate_opt3001_event_handler, NULL);
52 
53  bc_mpl3115a2_init(&_bc_module_climate.mpl3115a2, BC_I2C_I2C0, 0x60);
54  bc_mpl3115a2_set_event_handler(&_bc_module_climate.mpl3115a2, _bc_module_climate_mpl3115a2_event_handler, NULL);
55 }
56 
57 void bc_module_climate_set_event_handler(void (*event_handler)(bc_module_climate_event_t, void *), void *event_param)
58 {
59  _bc_module_climate.event_handler = event_handler;
60  _bc_module_climate.event_param = event_param;
61 }
62 
64 {
65  _bc_module_climate.update_interval.thermometer = interval;
66  _bc_module_climate.update_interval.hygrometer = interval;
67 
68  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
69  {
70  bc_tmp112_set_update_interval(&_bc_module_climate.tmp112, interval);
71  bc_sht20_set_update_interval(&_bc_module_climate.sht._20, interval);
72  }
73  else
74  {
75  bc_sht30_set_update_interval(&_bc_module_climate.sht._30, interval);
76  }
77  bc_opt3001_set_update_interval(&_bc_module_climate.opt3001, interval);
78  bc_mpl3115a2_set_update_interval(&_bc_module_climate.mpl3115a2, interval);
79 }
80 
82 {
83  _bc_module_climate.update_interval.thermometer = interval;
84  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
85  {
86  bc_tmp112_set_update_interval(&_bc_module_climate.tmp112, interval);
87  }
88 }
89 
91 {
92  _bc_module_climate.update_interval.hygrometer = interval;
93  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
94  {
95  bc_sht20_set_update_interval(&_bc_module_climate.sht._20, interval);
96  }
97  else
98  {
99  bc_sht30_set_update_interval(&_bc_module_climate.sht._30, interval);
100  }
101 }
102 
104 {
105  bc_opt3001_set_update_interval(&_bc_module_climate.opt3001, interval);
106 }
107 
109 {
110  bc_mpl3115a2_set_update_interval(&_bc_module_climate.mpl3115a2, interval);
111 }
112 
114 {
115  bool ret = true;
116 
117  ret &= bc_tmp112_measure(&_bc_module_climate.tmp112);
118 
119  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
120  {
121  ret &= bc_sht20_measure(&_bc_module_climate.sht._20);
122  }
123  else
124  {
125  ret &= bc_sht30_measure(&_bc_module_climate.sht._30);
126  }
127 
128  ret &= bc_opt3001_measure(&_bc_module_climate.opt3001);
129 
130  ret &= bc_mpl3115a2_measure(&_bc_module_climate.mpl3115a2);
131 
132  return ret;
133 }
134 
136 {
137  return bc_tmp112_measure(&_bc_module_climate.tmp112);
138 }
139 
141 {
142  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
143  {
144  return bc_sht20_measure(&_bc_module_climate.sht._20);
145  }
146  return bc_sht30_measure(&_bc_module_climate.sht._30);
147 }
148 
150 {
151  return bc_opt3001_measure(&_bc_module_climate.opt3001);
152 }
153 
155 {
156  return bc_mpl3115a2_measure(&_bc_module_climate.mpl3115a2);
157 }
158 
160 {
161  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
162  {
163  return bc_tmp112_get_temperature_celsius(&_bc_module_climate.tmp112, celsius);
164  }
165  return bc_sht30_get_temperature_celsius(&_bc_module_climate.sht._30, celsius);
166 }
167 
169 {
170  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
171  {
172  return bc_tmp112_get_temperature_fahrenheit(&_bc_module_climate.tmp112, fahrenheit);
173  }
174  return bc_sht30_get_temperature_fahrenheit(&_bc_module_climate.sht._30, fahrenheit);
175 }
176 
178 {
179  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
180  {
181  return bc_tmp112_get_temperature_kelvin(&_bc_module_climate.tmp112, kelvin);
182  }
183  return bc_sht30_get_temperature_kelvin(&_bc_module_climate.sht._30, kelvin);
184 }
185 
187 {
188  if (_bc_module_climate.revision == BC_MODULE_CLIMATE_REVISION_R1)
189  {
190  return bc_sht20_get_humidity_percentage(&_bc_module_climate.sht._20, percentage);
191  }
192  return bc_sht30_get_humidity_percentage(&_bc_module_climate.sht._30, percentage);
193 }
194 
196 {
197  return bc_opt3001_get_illuminance_lux(&_bc_module_climate.opt3001, lux);
198 }
199 
201 {
202  return bc_mpl3115a2_get_altitude_meter(&_bc_module_climate.mpl3115a2, meter);
203 }
204 
206 {
207  return bc_mpl3115a2_get_pressure_pascal(&_bc_module_climate.mpl3115a2, pascal);
208 }
209 
210 static void _bc_module_climate_tmp112_event_handler(bc_tmp112_t *self, bc_tmp112_event_t event, void *event_param)
211 {
212  (void) self;
213  (void) event_param;
214 
215  if (_bc_module_climate.event_handler == NULL)
216  {
217  return;
218  }
219 
220  if (event == BC_TMP112_EVENT_UPDATE)
221  {
222  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_THERMOMETER, _bc_module_climate.event_param);
223  }
224  else if (event == BC_TMP112_EVENT_ERROR)
225  {
226  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_THERMOMETER, _bc_module_climate.event_param);
227  }
228 }
229 
230 static void _bc_module_climate_sht20_event_handler(bc_sht20_t *self, bc_sht20_event_t event, void *event_param)
231 {
232  (void) self;
233  (void) event_param;
234 
235  if (_bc_module_climate.event_handler == NULL)
236  {
237  return;
238  }
239 
240  if (event == BC_SHT20_EVENT_UPDATE)
241  {
242  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_HYGROMETER, _bc_module_climate.event_param);
243  }
244  else if (event == BC_SHT20_EVENT_ERROR)
245  {
246  bc_tmp112_deinit(&_bc_module_climate.tmp112);
247  bc_sht20_deinit(&_bc_module_climate.sht._20);
248 
249  _bc_module_climate.revision = BC_MODULE_CLIMATE_REVISION_R2;
250 
251  bc_sht30_init(&_bc_module_climate.sht._30, BC_I2C_I2C0, 0x45);
252  bc_sht30_set_event_handler(&_bc_module_climate.sht._30, _bc_module_climate_sht30_event_handler, NULL);
253  bc_sht30_set_update_interval(&_bc_module_climate.sht._30, _bc_module_climate.update_interval.hygrometer);
254 
255  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_HYGROMETER, _bc_module_climate.event_param);
256  }
257 }
258 
259 static void _bc_module_climate_sht30_event_handler(bc_sht30_t *self, bc_sht30_event_t event, void *event_param)
260 {
261  (void) self;
262  (void) event_param;
263 
264  if (_bc_module_climate.event_handler == NULL)
265  {
266  return;
267  }
268 
269  if (event == BC_SHT30_EVENT_UPDATE)
270  {
271  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_THERMOMETER, _bc_module_climate.event_param);
272  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_HYGROMETER, _bc_module_climate.event_param);
273  }
274  else if (event == BC_SHT30_EVENT_ERROR)
275  {
276  bc_sht30_deinit(&_bc_module_climate.sht._30);
277 
278  _bc_module_climate.revision = BC_MODULE_CLIMATE_REVISION_R1;
279 
280  bc_sht20_init(&_bc_module_climate.sht._20, BC_I2C_I2C0, 0x40);
281  bc_sht20_set_event_handler(&_bc_module_climate.sht._20, _bc_module_climate_sht20_event_handler, NULL);
282  bc_sht20_set_update_interval(&_bc_module_climate.sht._20, _bc_module_climate.update_interval.hygrometer);
283 
284  bc_tmp112_init(&_bc_module_climate.tmp112, BC_I2C_I2C0, 0x48);
285  bc_tmp112_set_event_handler(&_bc_module_climate.tmp112, _bc_module_climate_tmp112_event_handler, NULL);
286  bc_tmp112_set_update_interval(&_bc_module_climate.tmp112 , _bc_module_climate.update_interval.thermometer);
287 
288  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_THERMOMETER, _bc_module_climate.event_param);
289  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_HYGROMETER, _bc_module_climate.event_param);
290  }
291 }
292 
293 static void _bc_module_climate_opt3001_event_handler(bc_opt3001_t *self, bc_opt3001_event_t event, void *event_param)
294 {
295  (void) self;
296  (void) event_param;
297 
298  if (_bc_module_climate.event_handler == NULL)
299  {
300  return;
301  }
302 
303  if (event == BC_OPT3001_EVENT_UPDATE)
304  {
305  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_LUX_METER, _bc_module_climate.event_param);
306  }
307  else if (event == BC_OPT3001_EVENT_ERROR)
308  {
309  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_LUX_METER, _bc_module_climate.event_param);
310  }
311 }
312 
313 static void _bc_module_climate_mpl3115a2_event_handler(bc_mpl3115a2_t *self, bc_mpl3115a2_event_t event, void *event_param)
314 {
315  (void) self;
316  (void) event_param;
317 
318  if (_bc_module_climate.event_handler == NULL)
319  {
320  return;
321  }
322 
323  if (event == BC_MPL3115A2_EVENT_UPDATE)
324  {
325  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_UPDATE_BAROMETER, _bc_module_climate.event_param);
326  }
327  else if (event == BC_MPL3115A2_EVENT_ERROR)
328  {
329  _bc_module_climate.event_handler(BC_MODULE_CLIMATE_EVENT_ERROR_BAROMETER, _bc_module_climate.event_param);
330  }
331 }
bool bc_module_climate_measure_all_sensors(void)
Start measurement of all sensors manually.
void bc_opt3001_set_event_handler(bc_opt3001_t *self, void(*event_handler)(bc_opt3001_t *, bc_opt3001_event_t, void *), void *event_param)
Set callback function.
Definition: bc_opt3001.c:26
bool bc_sht30_get_humidity_percentage(bc_sht30_t *self, float *percentage)
Get measured humidity as percentage.
Definition: bc_sht30.c:83
Error event.
Definition: bc_sht30.h:19
bool bc_module_climate_get_altitude_meter(float *meter)
Get measured altitude in meters.
void bc_tmp112_init(bc_tmp112_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TMP112.
Definition: bc_tmp112.c:11
uint64_t bc_tick_t
Timestamp data type.
Definition: bc_tick.h:16
void bc_sht30_deinit(bc_sht30_t *self)
Deinitialize SHT30.
Definition: bc_sht30.c:28
Error event.
Definition: bc_sht20.h:16
struct bc_sht30_t bc_sht30_t
SHT30 instance.
Definition: bc_sht30.h:28
void bc_opt3001_set_update_interval(bc_opt3001_t *self, bc_tick_t interval)
Set measurement interval.
Definition: bc_opt3001.c:32
bc_tmp112_event_t
Callback events.
Definition: bc_tmp112.h:13
Update event.
Definition: bc_sht20.h:19
bool bc_module_climate_measure_thermometer(void)
Start thermometer measurement manually.
struct bc_mpl3115a2_t bc_mpl3115a2_t
MPL3115A2 instance.
Definition: bc_mpl3115a2.h:25
bool bc_module_climate_get_temperature_celsius(float *celsius)
Get measured temperature in degrees of Celsius.
I2C channel I2C0.
Definition: bc_i2c.h:18
bool bc_sht20_measure(bc_sht20_t *self)
Start measurement manually.
Definition: bc_sht20.c:60
bool bc_mpl3115a2_get_pressure_pascal(bc_mpl3115a2_t *self, float *pascal)
Get measured pressured in Pascal.
Definition: bc_mpl3115a2.c:76
bool bc_opt3001_measure(bc_opt3001_t *self)
Start measurement manually.
Definition: bc_opt3001.c:48
void bc_module_climate_set_update_interval_thermometer(bc_tick_t interval)
Set measurement interval for thermometer.
Update event.
Definition: bc_sht30.h:22
bool bc_module_climate_get_pressure_pascal(float *pascal)
Get measured pressure in Pascal.
void bc_module_climate_set_update_interval_barometer(bc_tick_t interval)
Set measurement interval for barometer.
bc_module_climate_revision_t
Climate Module hardware revision.
Update event.
Definition: bc_tmp112.h:19
bc_sht30_event_t
Callback events.
Definition: bc_sht30.h:16
void bc_module_climate_set_update_interval_all_sensors(bc_tick_t interval)
Set measurement interval for all sensors.
bc_sht20_event_t
Callback events.
Definition: bc_sht20.h:13
void bc_module_climate_set_update_interval_hygrometer(bc_tick_t interval)
Set measurement interval for hygrometer.
bool bc_opt3001_get_illuminance_lux(bc_opt3001_t *self, float *lux)
Get measured illuminance in lux.
Definition: bc_opt3001.c:74
void bc_sht30_init(bc_sht30_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT30.
Definition: bc_sht30.c:13
bool bc_tmp112_get_temperature_fahrenheit(bc_tmp112_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
Definition: bc_tmp112.c:97
void bc_tmp112_set_update_interval(bc_tmp112_t *self, bc_tick_t interval)
Set measurement interval.
Definition: bc_tmp112.c:41
struct bc_tmp112_t bc_tmp112_t
TMP112 instance.
Definition: bc_tmp112.h:25
bc_mpl3115a2_event_t
Callback events.
Definition: bc_mpl3115a2.h:13
bool bc_sht30_get_temperature_fahrenheit(bc_sht30_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
Definition: bc_sht30.c:132
void bc_sht30_set_update_interval(bc_sht30_t *self, bc_tick_t interval)
Set measurement interval.
Definition: bc_sht30.c:41
Hardware revision R2.
void bc_mpl3115a2_init(bc_mpl3115a2_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize MPL3115A2.
Definition: bc_mpl3115a2.c:11
Error event.
Definition: bc_tmp112.h:16
void bc_opt3001_init(bc_opt3001_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize OPT3001 driver.
Definition: bc_opt3001.c:11
bool bc_tmp112_get_temperature_kelvin(bc_tmp112_t *self, float *kelvin)
Get measured temperature in kelvin.
Definition: bc_tmp112.c:111
void bc_sht20_init(bc_sht20_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT20.
Definition: bc_sht20.c:16
bool bc_tmp112_get_temperature_celsius(bc_tmp112_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: bc_tmp112.c:83
void bc_sht30_set_event_handler(bc_sht30_t *self, void(*event_handler)(bc_sht30_t *, bc_sht30_event_t, void *), void *event_param)
Set callback function.
Definition: bc_sht30.c:35
bc_module_climate_event_t
Callback events.
bool bc_module_climate_measure_lux_meter(void)
Start lux meter measurement manually.
bool bc_sht20_get_humidity_percentage(bc_sht20_t *self, float *percentage)
Get measured humidity as percentage.
Definition: bc_sht20.c:86
bc_opt3001_event_t
Callback events.
Definition: bc_opt3001.h:13
void bc_module_climate_set_update_interval_lux_meter(bc_tick_t interval)
Set measurement interval for lux meter.
void bc_tmp112_set_event_handler(bc_tmp112_t *self, void(*event_handler)(bc_tmp112_t *, bc_tmp112_event_t, void *), void *event_param)
Set callback function.
Definition: bc_tmp112.c:35
bool bc_mpl3115a2_get_altitude_meter(bc_mpl3115a2_t *self, float *meter)
Get measured altitude in meters.
Definition: bc_mpl3115a2.c:62
struct bc_sht20_t bc_sht20_t
SHT20 instance.
Definition: bc_sht20.h:25
Error event.
Definition: bc_opt3001.h:16
void bc_sht20_set_update_interval(bc_sht20_t *self, bc_tick_t interval)
Set measurement interval.
Definition: bc_sht20.c:44
void bc_mpl3115a2_set_event_handler(bc_mpl3115a2_t *self, void(*event_handler)(bc_mpl3115a2_t *, bc_mpl3115a2_event_t, void *), void *event_param)
Set callback function.
Definition: bc_mpl3115a2.c:26
struct bc_opt3001_t bc_opt3001_t
OPT3001 instance.
Definition: bc_opt3001.h:25
bool bc_module_climate_get_humidity_percentage(float *percentage)
Get measured humidity as percentage.
bool bc_module_climate_get_temperature_fahrenheit(float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
#define BC_TICK_INFINITY
Maximum timestamp value.
Definition: bc_tick.h:12
void bc_module_climate_init(void)
Initialize HARDWARIO Climate Module.
bool bc_module_climate_get_temperature_kelvin(float *kelvin)
Get measured temperature in kelvin.
bool bc_sht30_measure(bc_sht30_t *self)
Start measurement manually.
Definition: bc_sht30.c:57
bool bc_sht30_get_temperature_kelvin(bc_sht30_t *self, float *kelvin)
Get measured temperature in kelvin.
Definition: bc_sht30.c:146
bool bc_module_climate_get_illuminance_lux(float *lux)
Get measured illuminance in lux.
bool bc_module_climate_measure_hygrometer(void)
Start hygrometer measurement manually.
bool bc_sht30_get_temperature_celsius(bc_sht30_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: bc_sht30.c:118
bool bc_module_climate_measure_barometer(void)
Start barometer measurement manually.
bool bc_mpl3115a2_measure(bc_mpl3115a2_t *self)
Start measurement manually.
Definition: bc_mpl3115a2.c:48
void bc_mpl3115a2_set_update_interval(bc_mpl3115a2_t *self, bc_tick_t interval)
Set measurement interval.
Definition: bc_mpl3115a2.c:32
bool bc_tmp112_measure(bc_tmp112_t *self)
Start measurement manually.
Definition: bc_tmp112.c:57
Update event.
Definition: bc_opt3001.h:19
void bc_sht20_set_event_handler(bc_sht20_t *self, void(*event_handler)(bc_sht20_t *, bc_sht20_event_t, void *), void *event_param)
Set callback function.
Definition: bc_sht20.c:38
void bc_sht20_deinit(bc_sht20_t *self)
Deinitialize SHT20.
Definition: bc_sht20.c:31
Hardware revision R1.
void bc_tmp112_deinit(bc_tmp112_t *self)
Deinitialize TMP112.
Definition: bc_tmp112.c:26
void bc_module_climate_set_event_handler(void(*event_handler)(bc_module_climate_event_t, void *), void *event_param)
Set callback function.