1 /*
2 * Copyright (c) 2015 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8
9 #include <zephyr/sys/printk.h>
10
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/i2c.h>
13
14 #include <zephyr/drivers/misc/grove_lcd/grove_lcd.h>
15
16 /**
17 * @file Display a counter through I2C and Grove LCD.
18 */
19
20 /* sleep time in msec */
21 #define SLEEPTIME 100
22
clamp_rgb(int val)23 uint8_t clamp_rgb(int val)
24 {
25 if (val > 255) {
26 return 255;
27 } else if (val < 0) {
28 return 0;
29 } else {
30 return val;
31 }
32 }
33
main(void)34 int main(void)
35 {
36 const struct device *const glcd = DEVICE_DT_GET(DT_NODELABEL(glcd));
37 char str[20];
38 int rgb[] = { 0x0, 0x0, 0x0 };
39 uint8_t rgb_chg[3];
40 const uint8_t rgb_step = 16U;
41 uint8_t set_config;
42 int i, j, m;
43 int cnt;
44
45 if (!device_is_ready(glcd)) {
46 printk("Grove LCD: Device not ready.\n");
47 return 0;
48 }
49
50 /* Now configure the LCD the way we want it */
51
52 set_config = GLCD_FS_ROWS_2
53 | GLCD_FS_DOT_SIZE_LITTLE
54 | GLCD_FS_8BIT_MODE;
55
56 glcd_function_set(glcd, set_config);
57
58 set_config = GLCD_DS_DISPLAY_ON | GLCD_DS_CURSOR_ON | GLCD_DS_BLINK_ON;
59
60 glcd_display_state_set(glcd, set_config);
61
62 /* Setup variables /*/
63 for (i = 0; i < sizeof(str); i++) {
64 str[i] = '\0';
65 }
66
67 /* Starting counting */
68 cnt = 0;
69
70 while (1) {
71 glcd_cursor_pos_set(glcd, 0, 0);
72
73 /* RGB values are from 0 - 511.
74 * First half means incrementally brighter.
75 * Second half is opposite (i.e. goes darker).
76 */
77
78 /* Update the RGB values for backlight */
79 m = (rgb[2] > 255) ? (512 - rgb[2]) : (rgb[2]);
80 rgb_chg[2] = clamp_rgb(m);
81
82 m = (rgb[1] > 255) ? (512 - rgb[1]) : (rgb[1]);
83 rgb_chg[1] = clamp_rgb(m);
84
85 m = (rgb[0] > 255) ? (512 - rgb[0]) : (rgb[0]);
86 rgb_chg[0] = clamp_rgb(m);
87
88 glcd_color_set(glcd, rgb_chg[0], rgb_chg[1], rgb_chg[2]);
89
90 /* Display the counter
91 *
92 * well... sprintf() might be easier,
93 * but this is more fun.
94 */
95 m = cnt;
96 i = 1000000000;
97 j = 0;
98 while (i > 0) {
99 str[j] = '0' + (m / i);
100
101 m = m % i;
102 i = i / 10;
103 j++;
104 }
105 cnt++;
106
107 glcd_print(glcd, str, j);
108
109 /* Rotate RGB values */
110 rgb[2] += rgb_step;
111 if (rgb[2] > 511) {
112 rgb[2] = 0;
113 rgb[1] += rgb_step;
114 }
115 if (rgb[1] > 511) {
116 rgb[1] = 0;
117 rgb[0] += rgb_step;
118 }
119 if (rgb[0] > 511) {
120 rgb[0] = 0;
121 }
122
123 /* wait a while */
124 k_msleep(SLEEPTIME);
125 }
126 return 0;
127 }
128