Skip to content

Commit 7d01247

Browse files
committed
applications: clime: iaq: Add CO2 LED indication, X10 backup support
Signed-off-by: Martin Hubáček <[email protected]>
1 parent ebf09f3 commit 7d01247

15 files changed

+564
-182
lines changed

applications/clime/src/app_backup.c

+272-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
*/
66

77
#include "app_backup.h"
8+
#include "app_config.h"
89
#include "app_data.h"
10+
#include "app_work.h"
911

1012
/* CHESTER includes */
13+
#include <chester/ctr_rtc.h>
1114
#include <chester/drivers/ctr_z.h>
15+
#include <chester/drivers/ctr_x10.h>
1216

1317
/* Zephyr includes */
1418
#include <zephyr/device.h>
@@ -25,6 +29,137 @@ LOG_MODULE_REGISTER(app_backup, LOG_LEVEL_DBG);
2529

2630
#if defined(CONFIG_SHIELD_CTR_Z)
2731

32+
void handler_ctr_z(const struct device *dev, enum ctr_z_event backup_event, void *param)
33+
{
34+
int ret;
35+
36+
if (backup_event != CTR_Z_EVENT_DC_CONNECTED &&
37+
backup_event != CTR_Z_EVENT_DC_DISCONNECTED) {
38+
return;
39+
}
40+
41+
struct app_data_backup *backup = &g_app_data.backup;
42+
43+
app_work_backup_update();
44+
45+
app_data_lock();
46+
47+
backup->line_present = backup_event == CTR_Z_EVENT_DC_CONNECTED;
48+
49+
if (backup->event_count < APP_DATA_MAX_BACKUP_EVENTS) {
50+
struct app_data_backup_event *event = &backup->events[backup->event_count];
51+
52+
ret = ctr_rtc_get_ts(&event->timestamp);
53+
if (ret) {
54+
LOG_ERR("Call `ctr_rtc_get_ts` failed: %d", ret);
55+
app_data_unlock();
56+
return;
57+
}
58+
59+
event->connected = backup->line_present;
60+
backup->event_count++;
61+
62+
LOG_INF("Event count: %d", backup->event_count);
63+
} else {
64+
LOG_WRN("Measurement full");
65+
app_data_unlock();
66+
return;
67+
}
68+
69+
LOG_INF("Backup: %d", (int)backup->line_present);
70+
71+
if (g_app_config.backup_report_connected && backup_event == CTR_Z_EVENT_DC_CONNECTED) {
72+
app_work_send_with_rate_limit();
73+
}
74+
75+
if (g_app_config.backup_report_disconnected &&
76+
backup_event == CTR_Z_EVENT_DC_DISCONNECTED) {
77+
app_work_send_with_rate_limit();
78+
}
79+
80+
app_data_unlock();
81+
}
82+
83+
int app_backup_init(void)
84+
{
85+
int ret;
86+
87+
static const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(ctr_z));
88+
89+
if (!device_is_ready(dev)) {
90+
LOG_ERR("Device not ready");
91+
return -ENODEV;
92+
}
93+
94+
ret = ctr_z_set_handler(dev, handler_ctr_z, NULL);
95+
if (ret) {
96+
LOG_ERR("Call `ctr_z_set_handler` failed: %d", ret);
97+
return ret;
98+
}
99+
100+
ret = ctr_z_enable_interrupts(dev);
101+
if (ret) {
102+
LOG_ERR("Call `ctr_z_enable_interrupts` failed: %d", ret);
103+
return ret;
104+
}
105+
106+
uint32_t serial_number;
107+
ret = ctr_z_get_serial_number(dev, &serial_number);
108+
if (ret) {
109+
LOG_ERR("Call `ctr_z_get_serial_number` failed: %d", ret);
110+
return ret;
111+
}
112+
113+
LOG_INF("Serial number: %08x", serial_number);
114+
115+
uint16_t hw_revision;
116+
ret = ctr_z_get_hw_revision(dev, &hw_revision);
117+
if (ret) {
118+
LOG_ERR("Call `ctr_z_get_hw_revision` failed: %d", ret);
119+
return ret;
120+
}
121+
122+
LOG_INF("HW revision: %04x", hw_revision);
123+
124+
uint32_t hw_variant;
125+
ret = ctr_z_get_hw_variant(dev, &hw_variant);
126+
if (ret) {
127+
LOG_ERR("Call `ctr_z_get_hw_variant` failed: %d", ret);
128+
return ret;
129+
}
130+
131+
LOG_INF("HW variant: %08x", hw_variant);
132+
133+
uint32_t fw_version;
134+
ret = ctr_z_get_fw_version(dev, &fw_version);
135+
if (ret) {
136+
LOG_ERR("Call `ctr_z_get_fw_version` failed: %d", ret);
137+
return ret;
138+
}
139+
140+
LOG_INF("FW version: %08x", fw_version);
141+
142+
char vendor_name[32];
143+
ret = ctr_z_get_vendor_name(dev, vendor_name, sizeof(vendor_name));
144+
if (ret) {
145+
LOG_ERR("Call `ctr_z_get_vendor_name` failed: %d", ret);
146+
return ret;
147+
}
148+
149+
LOG_INF("Vendor name: %s", vendor_name);
150+
151+
char product_name[32];
152+
ret = ctr_z_get_product_name(dev, product_name, sizeof(product_name));
153+
if (ret) {
154+
LOG_ERR("Call `ctr_z_get_product_name` failed: %d", ret);
155+
return ret;
156+
}
157+
158+
LOG_INF("Product name: %s", product_name);
159+
160+
return 0;
161+
}
162+
28163
int app_backup_sample(void)
29164
{
30165
int ret;
@@ -82,6 +217,142 @@ int app_backup_sample(void)
82217
return ret;
83218
}
84219

220+
#endif /* defined(CONFIG_SHIELD_CTR_Z) */
221+
222+
#if defined(CONFIG_SHIELD_CTR_X10)
223+
224+
static void handler_ctr_x10(const struct device *dev, enum ctr_x10_event backup_event,
225+
void *user_data)
226+
{
227+
int ret;
228+
229+
if (backup_event != CTR_X10_EVENT_LINE_CONNECTED &&
230+
backup_event != CTR_X10_EVENT_LINE_DISCONNECTED) {
231+
return;
232+
}
233+
234+
struct app_data_backup *backup = &g_app_data.backup;
235+
236+
app_work_backup_update();
237+
238+
app_data_lock();
239+
240+
backup->line_present = backup_event == CTR_X10_EVENT_LINE_CONNECTED;
241+
242+
if (backup->event_count < APP_DATA_MAX_BACKUP_EVENTS) {
243+
struct app_data_backup_event *event = &backup->events[backup->event_count];
244+
245+
ret = ctr_rtc_get_ts(&event->timestamp);
246+
if (ret) {
247+
LOG_ERR("Call `ctr_rtc_get_ts` failed: %d", ret);
248+
app_data_unlock();
249+
return;
250+
}
251+
252+
event->connected = backup->line_present;
253+
backup->event_count++;
254+
255+
LOG_INF("Event count: %d", backup->event_count);
256+
} else {
257+
LOG_WRN("Measurement full");
258+
app_data_unlock();
259+
return;
260+
}
261+
262+
LOG_INF("Backup: %d", (int)backup->line_present);
263+
264+
if (g_app_config.backup_report_connected && backup_event == CTR_X10_EVENT_LINE_CONNECTED) {
265+
app_work_send_with_rate_limit();
266+
}
267+
268+
if (g_app_config.backup_report_disconnected &&
269+
backup_event == CTR_X10_EVENT_LINE_DISCONNECTED) {
270+
app_work_send_with_rate_limit();
271+
}
272+
273+
app_data_unlock();
274+
}
275+
276+
int app_backup_init(void)
277+
{
278+
int ret;
279+
280+
static const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(ctr_x10));
281+
282+
if (!device_is_ready(dev)) {
283+
LOG_ERR("Device not ready");
284+
return -ENODEV;
285+
}
286+
287+
ret = ctr_x10_set_handler(dev, handler_ctr_x10, NULL);
288+
if (ret) {
289+
LOG_ERR("Call `ctr_z_set_handler` failed: %d", ret);
290+
return ret;
291+
}
292+
293+
return 0;
294+
}
295+
296+
int app_backup_sample(void)
297+
{
298+
int ret;
299+
300+
static const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(ctr_x10));
301+
302+
if (!device_is_ready(dev)) {
303+
LOG_ERR("Device not ready");
304+
ret = -ENODEV;
305+
goto error;
306+
}
307+
308+
bool dc_input_connected;
309+
ret = ctr_x10_get_line_present(dev, &dc_input_connected);
310+
if (ret) {
311+
LOG_ERR("Call `ctr_x10_get_line_present` failed: %d", ret);
312+
} else {
313+
LOG_INF("Line present: %d", dc_input_connected);
314+
}
315+
316+
int line_voltage_mv;
317+
ret = ctr_x10_get_line_voltage(dev, &line_voltage_mv);
318+
if (ret) {
319+
LOG_ERR("Call `ctr_x10_get_line_voltage` failed: %d", ret);
320+
goto error;
321+
} else {
322+
LOG_INF("Line voltage: %d mv", line_voltage_mv);
323+
}
324+
325+
int battery_voltage_mv;
326+
ret = ctr_x10_get_battery_voltage(dev, &battery_voltage_mv);
327+
if (ret) {
328+
LOG_ERR("Call `ctr_x10_get_battery_voltage` failed: %d", ret);
329+
goto error;
330+
} else {
331+
LOG_INF("Battery voltage: %d mv", battery_voltage_mv);
332+
}
333+
334+
app_data_lock();
335+
g_app_data.backup.line_present = dc_input_connected;
336+
g_app_data.backup.line_voltage = line_voltage_mv / 1000.f;
337+
g_app_data.backup.battery_voltage = battery_voltage_mv / 1000.f;
338+
app_data_unlock();
339+
340+
return 0;
341+
342+
error:
343+
app_data_lock();
344+
g_app_data.backup.line_present = false;
345+
g_app_data.backup.line_voltage = NAN;
346+
g_app_data.backup.battery_voltage = NAN;
347+
app_data_unlock();
348+
349+
return ret;
350+
}
351+
352+
#endif /* defined(CONFIG_SHIELD_CTR_X10) */
353+
354+
#if defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10)
355+
85356
int app_backup_clear(void)
86357
{
87358
app_data_lock();
@@ -91,4 +362,4 @@ int app_backup_clear(void)
91362
return 0;
92363
}
93364

94-
#endif /* defined(CONFIG_SHIELD_CTR_Z) */
365+
#endif /* defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10) */

applications/clime/src/app_backup.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
extern "C" {
1212
#endif
1313

14+
int app_backup_init(void);
1415
int app_backup_sample(void);
1516
int app_backup_clear(void);
1617

applications/clime/src/app_cbor.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static int encode(zcbor_state_t *zs)
236236
}
237237
#endif /* defined(CONFIG_APP_TAMPER) */
238238

239-
#if defined(CONFIG_SHIELD_CTR_Z)
239+
#if defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10)
240240
zcbor_uint32_put(zs, MSG_KEY_BACKUP);
241241
{
242242
zcbor_map_start_encode(zs, ZCBOR_VALUE_IS_INDEFINITE_LENGTH);
@@ -288,7 +288,7 @@ static int encode(zcbor_state_t *zs)
288288

289289
zcbor_map_end_encode(zs, ZCBOR_VALUE_IS_INDEFINITE_LENGTH);
290290
}
291-
#endif /* defined(CONFIG_SHIELD_CTR_Z) */
291+
#endif /* defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10) */
292292

293293
zcbor_uint32_put(zs, MSG_KEY_NETWORK);
294294
{

applications/clime/src/app_config.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,22 @@ static struct app_config m_app_config_interim = {
4242
.interval_aggreg = 300,
4343
.interval_report = 1800,
4444

45-
#if defined(CONFIG_SHIELD_CTR_S2) || defined(CONFIG_SHIELD_CTR_Z)
45+
#if defined(CONFIG_SHIELD_CTR_S1)
46+
.iaq_led_thr_warning = 800.f,
47+
.iaq_led_thr_alarm = 1600.f,
48+
.iaq_led_hst = 50.f,
49+
#endif /* defined(CONFIG_SHIELD_CTR_S1) */
50+
51+
#if defined(CONFIG_SHIELD_CTR_S2) || defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10)
4652
.event_report_delay = 1,
4753
.event_report_rate = 30,
48-
#endif /* defined(CONFIG_SHIELD_CTR_S2) || defined(CONFIG_SHIELD_CTR_Z) */
54+
#endif /* defined(CONFIG_SHIELD_CTR_S2) || defined(CONFIG_SHIELD_CTR_Z) || \
55+
defined(CONFIG_SHIELD_CTR_X10) */
4956

50-
#if defined(CONFIG_SHIELD_CTR_Z)
57+
#if defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10)
5158
.backup_report_connected = true,
5259
.backup_report_disconnected = true,
53-
#endif /* defined(CONFIG_SHIELD_CTR_Z) */
60+
#endif /* defined(CONFIG_SHIELD_CTR_Z) || defined(CONFIG_SHIELD_CTR_X10) */
5461
};
5562

5663
static void print_app_config_mode(const struct shell *shell)
@@ -218,9 +225,9 @@ int app_config_cmd_config_show(const struct shell *shell, size_t argc, char **ar
218225
{
219226
print_app_config_mode(shell);
220227

221-
#define CONFIG_PARAM_INT(_name_d, _name_u, _min, _max, _help) print_##_name_u(shell);
228+
#define CONFIG_PARAM_INT(_name_d, _name_u, _min, _max, _help) print_##_name_u(shell);
222229
#define CONFIG_PARAM_FLOAT(_name_d, _name_u, _min, _max, _help) print_##_name_u(shell);
223-
#define CONFIG_PARAM_BOOL(_name_d, _name_u, _help) print_##_name_u(shell);
230+
#define CONFIG_PARAM_BOOL(_name_d, _name_u, _help) print_##_name_u(shell);
224231

225232
CONFIG_PARAM_LIST()
226233

0 commit comments

Comments
 (0)