Firmware SDK
twr_tag_humidity.c
1 #include <twr_tag_humidity.h>
2 
3 static void _twr_tag_humidity_event_handler_hts221(twr_hts221_t *child, twr_hts221_event_t event, void *event_param);
4 
5 static void _twr_tag_humidity_event_handler_hdc2080(twr_hdc2080_t *child, twr_hdc2080_event_t event, void *event_param);
6 
7 static void _twr_tag_humidity_event_handler_sht20(twr_sht20_t *child, twr_sht20_event_t event, void *event_param);
8 
9 static void _twr_tag_humidity_event_handler_sht30(twr_sht30_t *child, twr_sht30_event_t event, void *event_param);
10 
12 {
13  memset(self, 0, sizeof(*self));
14 
15  self->_revision = revision;
16 
17  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
18  {
19  twr_hts221_init(&self->_sensor.hts221, i2c_channel, 0x5f);
20 
21  twr_hts221_set_event_handler(&self->_sensor.hts221, _twr_tag_humidity_event_handler_hts221, self);
22  }
23  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
24  {
25  twr_hdc2080_init(&self->_sensor.hdc2080, i2c_channel, i2c_address == TWR_TAG_HUMIDITY_I2C_ADDRESS_DEFAULT ? 0x40 : 0x41);
26 
27  twr_hdc2080_set_event_handler(&self->_sensor.hdc2080, _twr_tag_humidity_event_handler_hdc2080, self);
28  }
29  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
30  {
31  twr_sht20_init(&self->_sensor.sht20, i2c_channel, 0x40);
32 
33  twr_sht20_set_event_handler(&self->_sensor.sht20, _twr_tag_humidity_event_handler_sht20, self);
34  }
35  else
36  {
37  twr_sht30_init(&self->_sensor.sht30, i2c_channel, i2c_address == TWR_TAG_HUMIDITY_I2C_ADDRESS_DEFAULT ? 0x44 : 0x45);
38 
39  twr_sht30_set_event_handler(&self->_sensor.sht30, _twr_tag_humidity_event_handler_sht30, self);
40  }
41 }
42 
43 void twr_tag_humidity_set_event_handler(twr_tag_humidity_t *self, void (*event_handler)(twr_tag_humidity_t *, twr_tag_humidity_event_t, void *), void *event_param)
44 {
45  self->_event_handler = event_handler;
46  self->_event_param = event_param;
47 }
48 
50 {
51  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
52  {
53  twr_hts221_set_update_interval(&self->_sensor.hts221, interval);
54  }
55  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
56  {
57  twr_hdc2080_set_update_interval(&self->_sensor.hdc2080, interval);
58  }
59  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
60  {
61  twr_sht20_set_update_interval(&self->_sensor.sht20, interval);
62  }
63  else
64  {
65  twr_sht30_set_update_interval(&self->_sensor.sht30, interval);
66  }
67 }
68 
70 {
71  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
72  {
73  return twr_hts221_measure(&self->_sensor.hts221);
74  }
75  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
76  {
77  return twr_hdc2080_measure(&self->_sensor.hdc2080);
78  }
79  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
80  {
81  return twr_sht20_measure(&self->_sensor.sht20);
82  }
83  else
84  {
85  return twr_sht30_measure(&self->_sensor.sht30);
86  }
87 }
88 
90 {
91  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
92  {
93  return false;
94  }
95  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
96  {
97  return twr_hdc2080_get_temperature_raw(&self->_sensor.hdc2080, raw);
98  }
99  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
100  {
101  return twr_sht20_get_temperature_raw(&self->_sensor.sht20, raw);
102  }
103  else
104  {
105  return twr_sht30_get_temperature_raw(&self->_sensor.sht30, raw);
106  }
107 }
108 
110 {
111  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
112  {
113  return false;
114  }
115  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
116  {
117  return twr_hdc2080_get_temperature_celsius(&self->_sensor.hdc2080, celsius);
118  }
119  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
120  {
121  return twr_sht20_get_temperature_celsius(&self->_sensor.sht20, celsius);
122  }
123  else
124  {
125  return twr_sht30_get_temperature_celsius(&self->_sensor.sht30, celsius);
126  }
127 }
128 
130 {
131  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
132  {
133  return false;
134  }
135  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
136  {
137  return twr_hdc2080_get_humidity_raw(&self->_sensor.hdc2080, raw);
138  }
139  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
140  {
141  return twr_sht20_get_humidity_raw(&self->_sensor.sht20, raw);
142  }
143  else
144  {
145  return twr_sht30_get_humidity_raw(&self->_sensor.sht30, raw);
146  }
147 }
148 
150 {
151  if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R1)
152  {
153  return twr_hts221_get_humidity_percentage(&self->_sensor.hts221, percentage);
154  }
155  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R2)
156  {
157  return twr_hdc2080_get_humidity_percentage(&self->_sensor.hdc2080, percentage);
158  }
159  else if (self->_revision == TWR_TAG_HUMIDITY_REVISION_R3)
160  {
161  return twr_sht20_get_humidity_percentage(&self->_sensor.sht20, percentage);
162  }
163  else
164  {
165  return twr_sht30_get_humidity_percentage(&self->_sensor.sht30, percentage);
166  }
167 }
168 
169 static void _twr_tag_humidity_event_handler_hts221(twr_hts221_t *child, twr_hts221_event_t event, void *event_param)
170 {
171  (void) child;
172 
173  twr_tag_humidity_t *self = event_param;
174 
175  if (self->_event_handler != NULL)
176  {
177  if (event == TWR_HTS221_EVENT_UPDATE)
178  {
179  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_UPDATE, self->_event_param);
180  }
181  else if (event == TWR_HTS221_EVENT_ERROR)
182  {
183  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_ERROR, self->_event_param);
184  }
185  }
186 }
187 
188 static void _twr_tag_humidity_event_handler_hdc2080(twr_hdc2080_t *child, twr_hdc2080_event_t event, void *event_param)
189 {
190  (void) child;
191 
192  twr_tag_humidity_t *self = event_param;
193 
194  if (self->_event_handler != NULL)
195  {
196  if (event == TWR_HDC2080_EVENT_UPDATE)
197  {
198  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_UPDATE, self->_event_param);
199  }
200  else if (event == TWR_HDC2080_EVENT_ERROR)
201  {
202  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_ERROR, self->_event_param);
203  }
204  }
205 }
206 
207 static void _twr_tag_humidity_event_handler_sht20(twr_sht20_t *child, twr_sht20_event_t event, void *event_param)
208 {
209  (void) child;
210 
211  twr_tag_humidity_t *self = event_param;
212 
213  if (self->_event_handler != NULL)
214  {
215  if (event == TWR_SHT20_EVENT_UPDATE)
216  {
217  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_UPDATE, self->_event_param);
218  }
219  else if (event == TWR_SHT20_EVENT_ERROR)
220  {
221  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_ERROR, self->_event_param);
222  }
223  }
224 }
225 
226 static void _twr_tag_humidity_event_handler_sht30(twr_sht30_t *child, twr_sht30_event_t event, void *event_param)
227 {
228  (void) child;
229 
230  twr_tag_humidity_t *self = event_param;
231 
232  if (self->_event_handler != NULL)
233  {
234  if (event == TWR_SHT30_EVENT_UPDATE)
235  {
236  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_UPDATE, self->_event_param);
237  }
238  else if (event == TWR_SHT30_EVENT_ERROR)
239  {
240  self->_event_handler(self, TWR_TAG_HUMIDITY_EVENT_ERROR, self->_event_param);
241  }
242  }
243 }
bool twr_sht30_get_temperature_celsius(twr_sht30_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_sht30.c:120
Error event.
Definition: twr_sht20.h:16
twr_hdc2080_event_t
Callback events.
Definition: twr_hdc2080.h:13
bool twr_tag_humidity_measure(twr_tag_humidity_t *self)
Start measurement manually.
void twr_hts221_set_update_interval(twr_hts221_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_hts221.c:90
bool twr_hdc2080_measure(twr_hdc2080_t *self)
Start measurement manually.
Definition: twr_hdc2080.c:58
bool twr_sht30_get_temperature_raw(twr_sht30_t *self, uint16_t *raw)
Get measured temperature as raw value.
Definition: twr_sht30.c:108
bool twr_sht20_get_humidity_percentage(twr_sht20_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_sht20.c:86
twr_tag_humidity_revision_t
Humidity Tag hardware revision.
Hardware revision R2.
bool twr_hdc2080_get_humidity_percentage(twr_hdc2080_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_hdc2080.c:84
bool twr_tag_humidity_get_humidity_raw(twr_tag_humidity_t *self, uint16_t *raw)
Get measured humidity as raw value.
bool twr_sht30_measure(twr_sht30_t *self)
Start measurement manually.
Definition: twr_sht30.c:59
bool twr_tag_humidity_get_temperature_celsius(twr_tag_humidity_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
void twr_hdc2080_init(twr_hdc2080_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize HDC2080.
Definition: twr_hdc2080.c:12
void twr_tag_humidity_set_event_handler(twr_tag_humidity_t *self, void(*event_handler)(twr_tag_humidity_t *, twr_tag_humidity_event_t, void *), void *event_param)
Set callback function.
bool twr_hdc2080_get_humidity_raw(twr_hdc2080_t *self, uint16_t *raw)
Get measured humidity as raw value.
Definition: twr_hdc2080.c:72
void twr_hdc2080_set_event_handler(twr_hdc2080_t *self, void(*event_handler)(twr_hdc2080_t *, twr_hdc2080_event_t, void *), void *event_param)
Set callback function.
Definition: twr_hdc2080.c:36
void twr_hts221_set_event_handler(twr_hts221_t *self, void(*event_handler)(twr_hts221_t *, twr_hts221_event_t, void *), void *event_param)
Set callback function.
Definition: twr_hts221.c:84
void twr_tag_humidity_init(twr_tag_humidity_t *self, twr_tag_humidity_revision_t revision, twr_i2c_channel_t i2c_channel, twr_tag_humidity_i2c_address_t i2c_address)
Initialize HARDWARIO Humidity Module.
struct twr_tag_humidity_t twr_tag_humidity_t
HARDWARIO Humidity Module instance.
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
Update event.
Definition: twr_hts221.h:19
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
Hardware revision R1.
Error event.
Definition: twr_hts221.h:16
bool twr_sht20_get_temperature_raw(twr_sht20_t *self, uint16_t *raw)
Get measured temperature as raw value.
Definition: twr_sht20.c:109
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
twr_hts221_event_t
Callback events.
Definition: twr_hts221.h:13
bool twr_sht20_measure(twr_sht20_t *self)
Start measurement manually.
Definition: twr_sht20.c:60
twr_i2c_channel_t
I2C channels.
Definition: twr_i2c.h:15
void twr_tag_humidity_set_update_interval(twr_tag_humidity_t *self, twr_tick_t interval)
Set measurement interval.
struct twr_sht20_t twr_sht20_t
SHT20 instance.
Definition: twr_sht20.h:25
void twr_hdc2080_set_update_interval(twr_hdc2080_t *self, twr_tick_t interval)
Set measurement interval.
Definition: twr_hdc2080.c:42
struct twr_hdc2080_t twr_hdc2080_t
HDC2080 instance.
Definition: twr_hdc2080.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_hts221_measure(twr_hts221_t *self)
Start measurement manually.
Definition: twr_hts221.c:106
bool twr_sht30_get_humidity_percentage(twr_sht30_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_sht30.c:85
bool twr_sht20_get_temperature_celsius(twr_sht20_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_sht20.c:121
bool twr_tag_humidity_get_humidity_percentage(twr_tag_humidity_t *self, float *percentage)
Get measured humidity as percentage.
Update event.
Definition: twr_sht20.h:19
struct twr_hts221_t twr_hts221_t
HTS221 instance.
Definition: twr_hts221.h:25
twr_tag_humidity_i2c_address_t
I2C address.
Error event.
Definition: twr_sht30.h:19
Hardware revision R3.
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
void twr_hts221_init(twr_hts221_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize HTS221.
Definition: twr_hts221.c:53
bool twr_sht30_get_humidity_raw(twr_sht30_t *self, uint16_t *raw)
Get measured humidity as raw value.
Definition: twr_sht30.c:73
bool twr_hdc2080_get_temperature_raw(twr_hdc2080_t *self, uint16_t *raw)
Get measured temperature as raw value.
Definition: twr_hdc2080.c:103
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
twr_tag_humidity_event_t
Callback events.
bool twr_tag_humidity_get_temperature_raw(twr_tag_humidity_t *self, uint16_t *raw)
Get measured temperature as raw value.
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
bool twr_hdc2080_get_temperature_celsius(twr_hdc2080_t *self, float *celsius)
Get measured temperature in degrees of Celsius.
Definition: twr_hdc2080.c:115
struct twr_sht30_t twr_sht30_t
SHT30 instance.
Definition: twr_sht30.h:28
bool twr_hts221_get_humidity_percentage(twr_hts221_t *self, float *percentage)
Get measured humidity as percentage.
Definition: twr_hts221.c:120
bool twr_sht20_get_humidity_raw(twr_sht20_t *self, uint16_t *raw)
Get measured humidity as raw value.
Definition: twr_sht20.c:74