MPM controlled 16ch RGB LED dimmer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

187 lines
5.2 KiB

/***************************************************************************
* 16ch RGB 8bit PWM controller *
* *
* Copyright (C) 2011 - 2012 by Olaf Rempel *
* razzor AT kopf MINUS tisch DOT de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "rgb16mpm.h"
/*
* using ATmega32 @8MHz:
* Fuse H: 0xD9 (no bootloader, jtag disabled)
* Fuse L: 0xD4 (int. 8MHz Osz, fast rising power, no BOD)
*
* PA0..7 -> COL1..8
* PC0..7 -> COL9..16
* PB0 / PD7(OC2) -> ROW2/GREEN (OC2 not used)
* PB1 / PD5(OC1A) -> ROW1/RED (OC1A not used)
* PB2 / PD4(OC1B) -> ROW4 (OC1B not used)
* PB3(OC0) / PD6 -> ROW3/BLUE (OC0 not used)
* PD0 -> RXD
* PD1 -> TXD
* PD2 -> /RX_TX
* 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++) {
if (color[i] != 0) {
output[i] = color[i] / div;
if (output[i] == 0) {
output[i] = 0x01;
}
} else {
output[i] = 0x00;
}
}
}
static uint8_t chan_decay[16][3];
int main(void) __attribute__ ((noreturn));
int main(void)
{
DDRD = (1<<LED);
PORTD = (1<<LED);
eeprom_read();
rgb_init();
mpm_init();
sei();
uint8_t x = 0;
uint8_t xdir = 1;
uint16_t ramp = 0;
/* wait for complete update */
rgb_update(COLOR_MASK, 1);
while (1) {
mpm_check_transfer();
/* wait for complete update */
rgb_update(COLOR_MASK, 1);
_delay_ms(50);
#if 1
x = sequence_chase(x, &xdir, 0x1F1F);
ramp = color_ramp(ramp +8, chan_value[x]);
color_div(chan_value[x], 4, chan_decay[x]);
uint8_t i;
for (i = 0; i < 16; i++) {
if (x != i) {
color_sub(chan_value[i], chan_decay[i], chan_value[i]);
}
}
#endif
}
}