1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/util.h>
12
13 #include <zephyr/drivers/misc/grove_lcd/grove_lcd.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 struct channel_info {
18 int chan;
19 };
20
21 static struct channel_info info[] = {
22 { SENSOR_CHAN_AMBIENT_TEMP, },
23 { SENSOR_CHAN_HUMIDITY, },
24 };
25
main(void)26 int main(void)
27 {
28 const struct device *const glcd = DEVICE_DT_GET(DT_NODELABEL(glcd));
29 const struct device *const th02 = DEVICE_DT_GET_ONE(hoperf_th02);
30 struct sensor_value val[ARRAY_SIZE(info)];
31 unsigned int i;
32 int rc;
33
34 if (!device_is_ready(th02)) {
35 printk("TH02 is not ready\n");
36 return 0;
37 }
38
39 if (!device_is_ready(glcd)) {
40 printk("Grove LCD not ready\n");
41 return 0;
42 }
43
44 /* configure LCD */
45 glcd_function_set(glcd, GLCD_FS_ROWS_2 | GLCD_FS_DOT_SIZE_LITTLE |
46 GLCD_FS_8BIT_MODE);
47 glcd_display_state_set(glcd, GLCD_DS_DISPLAY_ON);
48
49 while (1) {
50 /* fetch sensor samples */
51 rc = sensor_sample_fetch(th02);
52 if (rc) {
53 printk("Failed to fetch sample for device TH02 (%d)\n", rc);
54 }
55
56 for (i = 0U; i < ARRAY_SIZE(info); i++) {
57 rc = sensor_channel_get(th02, info[i].chan, &val[i]);
58 if (rc) {
59 printk("Failed to get data for device TH02 (%d)\n", rc);
60 continue;
61 }
62 }
63
64 char row[16];
65
66 /* clear LCD */
67 (void)memset(row, ' ', sizeof(row));
68 glcd_cursor_pos_set(glcd, 0, 0);
69 glcd_print(glcd, row, sizeof(row));
70 glcd_cursor_pos_set(glcd, 0, 1);
71 glcd_print(glcd, row, sizeof(row));
72
73 /* display temperature on LCD */
74 glcd_cursor_pos_set(glcd, 0, 0);
75 sprintf(row, "T:%.1f%cC", sensor_value_to_double(val),
76 223 /* degree symbol */);
77 glcd_print(glcd, row, strlen(row));
78
79 /* display humidity on LCD */
80 glcd_cursor_pos_set(glcd, 17 - strlen(row), 0);
81 sprintf(row, "RH:%.0f%c", sensor_value_to_double(val + 1),
82 37 /* percent symbol */);
83 glcd_print(glcd, row, strlen(row));
84
85 k_sleep(K_MSEC(2000));
86 }
87 return 0;
88 }
89