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/init.h>
9 #include <stdio.h>
10 #include <zephyr/drivers/sensor.h>
11
12 #ifdef CONFIG_GROVE_LCD_RGB
13 #include <zephyr/drivers/misc/grove_lcd/grove_lcd.h>
14 #include <stdio.h>
15 #include <string.h>
16 #endif
17
18 #define SLEEP_TIME K_MSEC(1000)
19
main(void)20 int main(void)
21 {
22 const struct device *const dev = DEVICE_DT_GET_ONE(seeed_grove_temperature);
23 struct sensor_value temp;
24 int read;
25
26 if (!device_is_ready(dev)) {
27 printk("sensor: device not ready.\n");
28 return 0;
29 }
30
31 #ifdef CONFIG_GROVE_LCD_RGB
32 const struct device *glcd;
33
34 glcd = device_get_binding(GROVE_LCD_NAME);
35 if (glcd == NULL) {
36 printf("Failed to get Grove LCD\n");
37 return 0;
38 }
39
40 /* configure LCD */
41 glcd_function_set(glcd, GLCD_FS_ROWS_2 | GLCD_FS_DOT_SIZE_LITTLE |
42 GLCD_FS_8BIT_MODE);
43 glcd_display_state_set(glcd, GLCD_DS_DISPLAY_ON);
44 #endif
45
46 while (1) {
47
48 read = sensor_sample_fetch(dev);
49 if (read) {
50 printk("sample fetch error\n");
51 continue;
52 }
53 sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
54 #ifdef CONFIG_GROVE_LCD_RGB
55 char row[16];
56
57 /* clear LCD */
58 (void)memset(row, ' ', sizeof(row));
59 glcd_cursor_pos_set(glcd, 0, 0);
60 glcd_print(glcd, row, sizeof(row));
61 glcd_cursor_pos_set(glcd, 0, 1);
62 glcd_print(glcd, row, sizeof(row));
63
64 /* display temperature on LCD */
65 glcd_cursor_pos_set(glcd, 0, 0);
66 #ifdef CONFIG_REQUIRES_FLOAT_PRINTF
67 sprintf(row, "T:%.2f%cC",
68 sensor_value_to_double(&temp),
69 223 /* degree symbol */);
70 #else
71 sprintf(row, "T:%d%cC", temp.val1,
72 223 /* degree symbol */);
73 #endif
74 glcd_print(glcd, row, strlen(row));
75
76 #endif
77
78 #ifdef CONFIG_REQUIRES_FLOAT_PRINTF
79 printf("Temperature: %.2f C\n", sensor_value_to_double(&temp));
80 #else
81 printk("Temperature: %d\n", temp.val1);
82 #endif
83 k_sleep(SLEEP_TIME);
84 }
85 }
86