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