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