color helper functions

This commit is contained in:
Olaf Rempel 2012-03-11 12:10:26 +01:00
parent 48775284b1
commit 0d9b9ba985

162
main.c
View File

@ -41,6 +41,102 @@
* PD3 -> /LED * PD3 -> /LED
*/ */
uint8_t sequence_chase(uint8_t old_value, uint8_t *dir, uint16_t mask)
{
uint8_t value = old_value;
do {
value = (*dir) ? value +1 : value -1;
value &= 0x0F;
if (value == 0x00 || value == 0x0F) {
*dir = (value == 0x00);
}
} while (!((1<<value) & mask) || value == old_value);
return value;
}
uint16_t color_ramp(uint16_t value, uint8_t *color)
{
uint8_t col1 = (value & 0xFF);
uint8_t col2 = 0xFF - col1;
switch (value >> 8) {
default:
value = 0x0000;
/* no break */
case 0: /* red: on, green: ramp up, blue: off */
color[0] = 0xFF;
color[1] = col1;
color[2] = 0x00;
break;
case 1: /* red: ramp down, green: on, blue:off */
color[0] = col2;
color[1] = 0xFF;
color[2] = 0x00;
break;
case 2: /* red: off, green: on, blue: ramp up */
color[0] = 0x00;
color[1] = 0xFF;
color[2] = col1;
break;
case 3: /* red: off, green: ramp down: blue: on */
color[0] = 0x00;
color[1] = col2;
color[2] = 0xFF;
break;
case 4: /* red: ramp up, green: off, blue: on */
color[0] = col1;
color[1] = 0x00;
color[2] = 0xFF;
break;
case 5: /* red: on, green: off, blue: ramp down */
color[0] = 0xFF;
color[1] = 0x00;
color[2] = col2;
break;
}
return value;
}
void color_add(uint8_t *color1, uint8_t *color2, uint8_t *output)
{
uint8_t i;
for (i = 0; i < 3; i++) {
uint16_t tmp = color1[i] + color2[i];
output[i] = (tmp & 0xFF00) ? 0xFF : tmp;
}
}
void color_sub(uint8_t *color1, uint8_t *color2, uint8_t *output)
{
uint8_t i;
for (i = 0; i < 3; i++) {
uint16_t tmp = color1[i] - color2[i];
output[i] = (tmp & 0xFF00) ? 0x00 : tmp;
}
}
void color_div(uint8_t *color, uint8_t div, uint8_t *output)
{
uint8_t i;
for (i = 0; i < 3; i++) {
output[i] = color[i] / div;
if ((output[i] == 0) && (color[i] != 0)) {
output[i] = 0x01;
}
}
}
int main(void) __attribute__ ((noreturn)); int main(void) __attribute__ ((noreturn));
int main(void) int main(void)
{ {
@ -64,76 +160,20 @@ int main(void)
while (1) { while (1) {
mpm_check_transfer(); mpm_check_transfer();
#if 0
/* wait for complete update */ /* wait for complete update */
rgb_update(COLOR_MASK, 1); rgb_update(COLOR_MASK, 1);
// _delay_ms(100); _delay_ms(1);
step++; step++;
if (step == 16) { if (step == 16) {
step = 0; step = 0;
x = sequence_chase(x, &xdir, 0x1F1F);
if (xdir) {
x++;
if (x == 0x05)
x = 0x08;
else if (x == 0x0C)
xdir = 0;
} else {
x--;
if (x == 0x00)
xdir = 1;
else if (x == 0x07)
x = 0x04;
}
} }
uint8_t color[3]; #if 1
uint8_t color[3] = { 0, 0, 0 };
ramp++; ramp = color_ramp(ramp +1, color);
switch (ramp >> 8) {
case 6:
ramp = 0x0000;
/* no break */
case 0: /* red: on, green: ramp up, blue: off */
color[0] = 0xFF;
color[1] = ramp & 0xFF;
color[2] = 0x00;
break;
case 1: /* red: ramp down, green: on, blue:off */
color[0] = 0xFF - (ramp & 0xFF);
color[1] = 0xFF;
color[2] = 0x00;
break;
case 2: /* red: off, green: on, blue: ramp up */
color[0] = 0x00;
color[1] = 0xFF;
color[2] = (ramp & 0xFF);
break;
case 3: /* red: off, green: ramp down: blue: on */
color[0] = 0x00;
color[1] = 0xFF - (ramp & 0xFF);
color[2] = 0xFF;
break;
case 4: /* red: ramp up, green: off, blue: on */
color[0] = (ramp & 0xFF);
color[1] = 0x00;
color[2] = 0xFF;
break;
case 5: /* red: on, green: off, blue: ramp down */
color[0] = 0xFF;
color[1] = 0x00;
color[2] = 0xFF - (ramp & 0xFF);
break;
}
uint8_t i, j; uint8_t i, j;
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {