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