Firmware SDK
twr_module_climate.c
1 #include <twr_module_climate.h>
2 #include <twr_tmp112.h>
3 #include <twr_sht20.h>
4 #include <twr_opt3001.h>
5 #include <twr_mpl3115a2.h>
6 #include <twr_sht30.h>
7 
8 static struct
9 {
10  void (*event_handler)(twr_module_climate_event_t, void *);
11  void *event_param;
13  twr_tmp112_t tmp112;
14  twr_opt3001_t opt3001;
15  twr_mpl3115a2_t mpl3115a2;
16  union {
17  twr_sht20_t _20;
18  twr_sht30_t _30;
19  } sht;
20  struct {
21  twr_tick_t thermometer;
22  twr_tick_t hygrometer;
23  } update_interval;
24 
25 } _twr_module_climate;
26 
27 static void _twr_module_climate_tmp112_event_handler(twr_tmp112_t *self, twr_tmp112_event_t event, void *event_param);
28 
29 static void _twr_module_climate_sht20_event_handler(twr_sht20_t *self, twr_sht20_event_t event, void *event_param);
30 
31 static void _twr_module_climate_sht30_event_handler(twr_sht30_t *self, twr_sht30_event_t event, void *event_param);
32 
33 static void _twr_module_climate_opt3001_event_handler(twr_opt3001_t *self, twr_opt3001_event_t event, void *event_param);
34 
35 static void _twr_module_climate_mpl3115a2_event_handler(twr_mpl3115a2_t *self, twr_mpl3115a2_event_t event, void *event_param);
36 
38 {
39  memset(&_twr_module_climate, 0, sizeof(_twr_module_climate));
40 
41  _twr_module_climate.update_interval.thermometer = TWR_TICK_INFINITY;
42  _twr_module_climate.update_interval.hygrometer = TWR_TICK_INFINITY;
43 
44  twr_tmp112_init(&_twr_module_climate.tmp112, TWR_I2C_I2C0, 0x48);
45  twr_tmp112_set_event_handler(&_twr_module_climate.tmp112, _twr_module_climate_tmp112_event_handler, NULL);
46 
47  twr_sht20_init(&_twr_module_climate.sht._20, TWR_I2C_I2C0, 0x40);
48  twr_sht20_set_event_handler(&_twr_module_climate.sht._20, _twr_module_climate_sht20_event_handler, NULL);
49 
50  twr_opt3001_init(&_twr_module_climate.opt3001, TWR_I2C_I2C0, 0x44);
51  twr_opt3001_set_event_handler(&_twr_module_climate.opt3001, _twr_module_climate_opt3001_event_handler, NULL);
52 
53  twr_mpl3115a2_init(&_twr_module_climate.mpl3115a2, TWR_I2C_I2C0, 0x60);
54  twr_mpl3115a2_set_event_handler(&_twr_module_climate.mpl3115a2, _twr_module_climate_mpl3115a2_event_handler, NULL);
55 }
56 
57 void twr_module_climate_set_event_handler(void (*event_handler)(twr_module_climate_event_t, void *), void *event_param)
58 {
59  _twr_module_climate.event_handler = event_handler;
60  _twr_module_climate.event_param = event_param;
61 }
62 
64 {
65  _twr_module_climate.update_interval.thermometer = interval;
66  _twr_module_climate.update_interval.hygrometer = interval;
67 
68  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
69  {
70  twr_tmp112_set_update_interval(&_twr_module_climate.tmp112, interval);
71  twr_sht20_set_update_interval(&_twr_module_climate.sht._20, interval);
72  }
73  else
74  {
75  twr_sht30_set_update_interval(&_twr_module_climate.sht._30, interval);
76  }
77  twr_opt3001_set_update_interval(&_twr_module_climate.opt3001, interval);
78  twr_mpl3115a2_set_update_interval(&_twr_module_climate.mpl3115a2, interval);
79 }
80 
82 {
83  _twr_module_climate.update_interval.thermometer = interval;
84  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
85  {
86  twr_tmp112_set_update_interval(&_twr_module_climate.tmp112, interval);
87  }
88 }
89 
91 {
92  _twr_module_climate.update_interval.hygrometer = interval;
93  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
94  {
95  twr_sht20_set_update_interval(&_twr_module_climate.sht._20, interval);
96  }
97  else
98  {
99  twr_sht30_set_update_interval(&_twr_module_climate.sht._30, interval);
100  }
101 }
102 
104 {
105  twr_opt3001_set_update_interval(&_twr_module_climate.opt3001, interval);
106 }
107 
109 {
110  twr_mpl3115a2_set_update_interval(&_twr_module_climate.mpl3115a2, interval);
111 }
112 
114 {
115  bool ret = true;
116 
117  ret &= twr_tmp112_measure(&_twr_module_climate.tmp112);
118 
119  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
120  {
121  ret &= twr_sht20_measure(&_twr_module_climate.sht._20);
122  }
123  else
124  {
125  ret &= twr_sht30_measure(&_twr_module_climate.sht._30);
126  }
127 
128  ret &= twr_opt3001_measure(&_twr_module_climate.opt3001);
129 
130  ret &= twr_mpl3115a2_measure(&_twr_module_climate.mpl3115a2);
131 
132  return ret;
133 }
134 
136 {
137  return twr_tmp112_measure(&_twr_module_climate.tmp112);
138 }
139 
141 {
142  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
143  {
144  return twr_sht20_measure(&_twr_module_climate.sht._20);
145  }
146  return twr_sht30_measure(&_twr_module_climate.sht._30);
147 }
148 
150 {
151  return twr_opt3001_measure(&_twr_module_climate.opt3001);
152 }
153 
155 {
156  return twr_mpl3115a2_measure(&_twr_module_climate.mpl3115a2);
157 }
158 
160 {
161  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
162  {
163  return twr_tmp112_get_temperature_celsius(&_twr_module_climate.tmp112, celsius);
164  }
165  return twr_sht30_get_temperature_celsius(&_twr_module_climate.sht._30, celsius);
166 }
167 
169 {
170  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
171  {
172  return twr_tmp112_get_temperature_fahrenheit(&_twr_module_climate.tmp112, fahrenheit);
173  }
174  return twr_sht30_get_temperature_fahrenheit(&_twr_module_climate.sht._30, fahrenheit);
175 }
176 
178 {
179  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
180  {
181  return twr_tmp112_get_temperature_kelvin(&_twr_module_climate.tmp112, kelvin);
182  }
183  return twr_sht30_get_temperature_kelvin(&_twr_module_climate.sht._30, kelvin);
184 }
185 
187 {
188  if (_twr_module_climate.revision == TWR_MODULE_CLIMATE_REVISION_R1)
189  {
190  return twr_sht20_get_humidity_percentage(&_twr_module_climate.sht._20, percentage);
191  }
192  return twr_sht30_get_humidity_percentage(&_twr_module_climate.sht._30, percentage);
193 }
194 
196 {
197  return twr_opt3001_get_illuminance_lux(&_twr_module_climate.opt3001, lux);
198 }
199 
201 {
202  return twr_mpl3115a2_get_altitude_meter(&_twr_module_climate.mpl3115a2, meter);
203 }
204 
206 {
207  return twr_mpl3115a2_get_pressure_pascal(&_twr_module_climate.mpl3115a2, pascal);
208 }
209 
210 static void _twr_module_climate_tmp112_event_handler(twr_tmp112_t *self, twr_tmp112_event_t event, void *event_param)
211 {
212  (void) self;
213  (void) event_param;
214 
215  if (_twr_module_climate.event_handler == NULL)
216  {
217  return;
218  }
219 
220  if (event == TWR_TMP112_EVENT_UPDATE)
221  {
222  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_THERMOMETER, _twr_module_climate.event_param);
223  }
224  else if (event == TWR_TMP112_EVENT_ERROR)
225  {
226  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_THERMOMETER, _twr_module_climate.event_param);
227  }
228 }
229 
230 static void _twr_module_climate_sht20_event_handler(twr_sht20_t *self, twr_sht20_event_t event, void *event_param)
231 {
232  (void) self;
233  (void) event_param;
234 
235  if (_twr_module_climate.event_handler == NULL)
236  {
237  return;
238  }
239 
240  if (event == TWR_SHT20_EVENT_UPDATE)
241  {
242  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_HYGROMETER, _twr_module_climate.event_param);
243  }
244  else if (event == TWR_SHT20_EVENT_ERROR)
245  {
246  twr_tmp112_deinit(&_twr_module_climate.tmp112);
247  twr_sht20_deinit(&_twr_module_climate.sht._20);
248 
249  _twr_module_climate.revision = TWR_MODULE_CLIMATE_REVISION_R2;
250 
251  twr_sht30_init(&_twr_module_climate.sht._30, TWR_I2C_I2C0, 0x45);
252  twr_sht30_set_event_handler(&_twr_module_climate.sht._30, _twr_module_climate_sht30_event_handler, NULL);
253  twr_sht30_set_update_interval(&_twr_module_climate.sht._30, _twr_module_climate.update_interval.hygrometer);
254 
255  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_HYGROMETER, _twr_module_climate.event_param);
256  }
257 }
258 
259 static void _twr_module_climate_sht30_event_handler(twr_sht30_t *self, twr_sht30_event_t event, void *event_param)
260 {
261  (void) self;
262  (void) event_param;
263 
264  if (_twr_module_climate.event_handler == NULL)
265  {
266  return;
267  }
268 
269  if (event == TWR_SHT30_EVENT_UPDATE)
270  {
271  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_THERMOMETER, _twr_module_climate.event_param);
272  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_HYGROMETER, _twr_module_climate.event_param);
273  }
274  else if (event == TWR_SHT30_EVENT_ERROR)
275  {
276  twr_sht30_deinit(&_twr_module_climate.sht._30);
277 
278  _twr_module_climate.revision = TWR_MODULE_CLIMATE_REVISION_R1;
279 
280  twr_sht20_init(&_twr_module_climate.sht._20, TWR_I2C_I2C0, 0x40);
281  twr_sht20_set_event_handler(&_twr_module_climate.sht._20, _twr_module_climate_sht20_event_handler, NULL);
282  twr_sht20_set_update_interval(&_twr_module_climate.sht._20, _twr_module_climate.update_interval.hygrometer);
283 
284  twr_tmp112_init(&_twr_module_climate.tmp112, TWR_I2C_I2C0, 0x48);
285  twr_tmp112_set_event_handler(&_twr_module_climate.tmp112, _twr_module_climate_tmp112_event_handler, NULL);
286  twr_tmp112_set_update_interval(&_twr_module_climate.tmp112 , _twr_module_climate.update_interval.thermometer);
287 
288  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_THERMOMETER, _twr_module_climate.event_param);
289  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_HYGROMETER, _twr_module_climate.event_param);
290  }
291 }
292 
293 static void _twr_module_climate_opt3001_event_handler(twr_opt3001_t *self, twr_opt3001_event_t event, void *event_param)
294 {
295  (void) self;
296  (void) event_param;
297 
298  if (_twr_module_climate.event_handler == NULL)
299  {
300  return;
301  }
302 
303  if (event == TWR_OPT3001_EVENT_UPDATE)
304  {
305  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_LUX_METER, _twr_module_climate.event_param);
306  }
307  else if (event == TWR_OPT3001_EVENT_ERROR)
308  {
309  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_LUX_METER, _twr_module_climate.event_param);
310  }
311 }
312 
313 static void _twr_module_climate_mpl3115a2_event_handler(twr_mpl3115a2_t *self, twr_mpl3115a2_event_t event, void *event_param)
314 {
315  (void) self;
316  (void) event_param;
317 
318  if (_twr_module_climate.event_handler == NULL)
319  {
320  return;
321  }
322 
323  if (event == TWR_MPL3115A2_EVENT_UPDATE)
324  {
325  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_UPDATE_BAROMETER, _twr_module_climate.event_param);
326  }
327  else if (event == TWR_MPL3115A2_EVENT_ERROR)
328  {
329  _twr_module_climate.event_handler(TWR_MODULE_CLIMATE_EVENT_ERROR_BAROMETER, _twr_module_climate.event_param);
330  }
331 }
bool twr_sht30_get_temperature_celsius(twr_sht30_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_sht30.c:120
bool twr_opt3001_get_illuminance_lux(twr_opt3001_t *self, float *lux)
Get measured illuminance in lux.
Definition: twr_opt3001.c:83
Error event.
Definition: twr_sht20.h:16
bool twr_module_climate_measure_barometer(void)
Start barometer measurement manually.
twr_module_climate_revision_t
Climate Module hardware revision.
bool twr_module_climate_get_temperature_fahrenheit(float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
void twr_module_climate_set_update_interval_lux_meter(twr_tick_t interval)
Set measurement interval for lux meter.
bool twr_module_climate_get_pressure_pascal(float *pascal)
Get measured pressure in Pascal.
bool twr_tmp112_measure(twr_tmp112_t *self)
Start measurement manually.
Definition: twr_tmp112.c:57
struct twr_mpl3115a2_t twr_mpl3115a2_t
MPL3115A2 instance.
Definition: twr_mpl3115a2.h:25
bool twr_module_climate_get_humidity_percentage(float *percentage)
Get measured humidity as percentage.
bool twr_module_climate_get_illuminance_lux(float *lux)
Get measured illuminance in lux.
bool twr_sht20_get_humidity_percentage(twr_sht20_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_sht20.c:86
Update event.
Definition: twr_tmp112.h:19
void twr_module_climate_set_update_interval_all_sensors(twr_tick_t interval)
Set measurement interval for all sensors.
bool twr_module_climate_get_temperature_celsius(float *celsius)
Get measured temperature in degrees of Celsius.
void twr_mpl3115a2_init(twr_mpl3115a2_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize MPL3115A2.
Definition: twr_mpl3115a2.c:11
void twr_sht20_deinit(twr_sht20_t *self)
Deinitialize SHT20.
Definition: twr_sht20.c:31
bool twr_sht30_measure(twr_sht30_t *self)
Start measurement manually.
Definition: twr_sht30.c:59
void twr_sht30_deinit(twr_sht30_t *self)
Deinitialize SHT30.
Definition: twr_sht30.c:30
void twr_mpl3115a2_set_update_interval(twr_mpl3115a2_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_mpl3115a2.c:41
twr_opt3001_event_t
Callback events.
Definition: twr_opt3001.h:13
void twr_module_climate_set_update_interval_hygrometer(twr_tick_t interval)
Set measurement interval for hygrometer.
void twr_module_climate_set_update_interval_thermometer(twr_tick_t interval)
Set measurement interval for thermometer.
twr_module_climate_event_t
Callback events.
struct twr_opt3001_t twr_opt3001_t
OPT3001 instance.
Definition: twr_opt3001.h:25
bool twr_sht30_get_temperature_fahrenheit(twr_sht30_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
Definition: twr_sht30.c:134
twr_mpl3115a2_event_t
Callback events.
Definition: twr_mpl3115a2.h:13
void twr_sht20_set_event_handler(twr_sht20_t *self, void(*event_handler)(twr_sht20_t *, twr_sht20_event_t, void *), void *event_param)
Set callback function.
Definition: twr_sht20.c:38
twr_sht20_event_t
Callback events.
Definition: twr_sht20.h:13
void twr_tmp112_set_update_interval(twr_tmp112_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_tmp112.c:41
void twr_module_climate_set_update_interval_barometer(twr_tick_t interval)
Set measurement interval for barometer.
void twr_opt3001_set_update_interval(twr_opt3001_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_opt3001.c:41
twr_sht30_event_t
Callback events.
Definition: twr_sht30.h:16
void twr_sht30_init(twr_sht30_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT30.
Definition: twr_sht30.c:15
void twr_opt3001_set_event_handler(twr_opt3001_t *self, void(*event_handler)(twr_opt3001_t *, twr_opt3001_event_t, void *), void *event_param)
Set callback function.
Definition: twr_opt3001.c:35
bool twr_mpl3115a2_get_pressure_pascal(twr_mpl3115a2_t *self, float *pascal)
Get measured pressured in Pascal.
Definition: twr_mpl3115a2.c:85
bool twr_mpl3115a2_measure(twr_mpl3115a2_t *self)
Start measurement manually.
Definition: twr_mpl3115a2.c:57
bool twr_module_climate_measure_all_sensors(void)
Start measurement of all sensors manually.
Error event.
Definition: twr_tmp112.h:16
bool twr_module_climate_get_temperature_kelvin(float *kelvin)
Get measured temperature in kelvin.
void twr_opt3001_init(twr_opt3001_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize OPT3001 driver.
Definition: twr_opt3001.c:11
bool twr_module_climate_measure_thermometer(void)
Start thermometer measurement manually.
twr_tmp112_event_t
Callback events.
Definition: twr_tmp112.h:13
bool twr_opt3001_measure(twr_opt3001_t *self)
Start measurement manually.
Definition: twr_opt3001.c:57
bool twr_module_climate_measure_hygrometer(void)
Start hygrometer measurement manually.
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
void twr_module_climate_set_event_handler(void(*event_handler)(twr_module_climate_event_t, void *), void *event_param)
Set callback function.
bool twr_sht20_measure(twr_sht20_t *self)
Start measurement manually.
Definition: twr_sht20.c:60
struct twr_sht20_t twr_sht20_t
SHT20 instance.
Definition: twr_sht20.h:25
void twr_sht30_set_update_interval(twr_sht30_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_sht30.c:43
bool twr_sht30_get_humidity_percentage(twr_sht30_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_sht30.c:85
I2C channel I2C0.
Definition: twr_i2c.h:18
void twr_tmp112_set_event_handler(twr_tmp112_t *self, void(*event_handler)(twr_tmp112_t *, twr_tmp112_event_t, void *), void *event_param)
Set callback function.
Definition: twr_tmp112.c:35
bool twr_tmp112_get_temperature_kelvin(twr_tmp112_t *self, float *kelvin)
Get measured temperature in kelvin.
Definition: twr_tmp112.c:111
Update event.
Definition: twr_sht20.h:19
Error event.
Definition: twr_sht30.h:19
bool twr_module_climate_measure_lux_meter(void)
Start lux meter measurement manually.
void twr_sht30_set_event_handler(twr_sht30_t *self, void(*event_handler)(twr_sht30_t *, twr_sht30_event_t, void *), void *event_param)
Set callback function.
Definition: twr_sht30.c:37
bool twr_tmp112_get_temperature_fahrenheit(twr_tmp112_t *self, float *fahrenheit)
Get measured temperature in degrees of Fahrenheit.
Definition: twr_tmp112.c:97
bool twr_sht30_get_temperature_kelvin(twr_sht30_t *self, float *kelvin)
Get measured temperature in kelvin.
Definition: twr_sht30.c:148
Update event.
Definition: twr_sht30.h:22
void twr_sht20_set_update_interval(twr_sht20_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_sht20.c:44
#define TWR_TICK_INFINITY
Maximum timestamp value.
Definition: twr_tick.h:12
void twr_module_climate_init(void)
Initialize HARDWARIO Climate Module.
bool twr_mpl3115a2_get_altitude_meter(twr_mpl3115a2_t *self, float *meter)
Get measured altitude in meters.
Definition: twr_mpl3115a2.c:71
void twr_sht20_init(twr_sht20_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize SHT20.
Definition: twr_sht20.c:16
void twr_tmp112_init(twr_tmp112_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TMP112.
Definition: twr_tmp112.c:11
bool twr_tmp112_get_temperature_celsius(twr_tmp112_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_tmp112.c:83
struct twr_sht30_t twr_sht30_t
SHT30 instance.
Definition: twr_sht30.h:28
void twr_tmp112_deinit(twr_tmp112_t *self)
Deinitialize TMP112.
Definition: twr_tmp112.c:26
bool twr_module_climate_get_altitude_meter(float *meter)
Get measured altitude in meters.
struct twr_tmp112_t twr_tmp112_t
TMP112 instance.
Definition: twr_tmp112.h:25
void twr_mpl3115a2_set_event_handler(twr_mpl3115a2_t *self, void(*event_handler)(twr_mpl3115a2_t *, twr_mpl3115a2_event_t, void *), void *event_param)
Set callback function.
Definition: twr_mpl3115a2.c:35