2015-07-14 09:51:50 +02:00
|
|
|
/* 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h> // Standard Integer Types
|
|
|
|
#include <stdio.h> // Standard IO facilities
|
|
|
|
#include <stdlib.h> // General utilities
|
|
|
|
#include <stdbool.h> // Boolean
|
|
|
|
#include <string.h> // Strings
|
|
|
|
|
|
|
|
#include <avr/io.h> // AVR device-specific IO definitions
|
|
|
|
#include <util/delay.h> // Convenience functions for busy-wait delay loops
|
|
|
|
#include <avr/interrupt.h> // Interrupts
|
|
|
|
#include <avr/wdt.h> // Watchdog timer handling
|
|
|
|
#include <avr/pgmspace.h> // Program Space Utilities
|
|
|
|
#include <avr/sleep.h> // Power Management and Sleep Modes
|
|
|
|
|
|
|
|
#include "main.h" // main definitions
|
2015-07-14 09:54:54 +02:00
|
|
|
#include "uart.h" // basic UART functions
|
|
|
|
#include "ws2812b.h" // to control WS2812B LEDs
|
|
|
|
|
|
|
|
/* global variables */
|
2015-07-17 10:07:49 +02:00
|
|
|
volatile uint8_t uart_in[19*2]; // input from USART, enough space for two atmolight messages (one as buffer)
|
2015-07-17 10:01:43 +02:00
|
|
|
volatile uint8_t uart_in_i = 0; // UART input index
|
2015-07-14 09:54:54 +02:00
|
|
|
|
|
|
|
/* flags, set in the interrupts and handled in the main program */
|
|
|
|
volatile bool uart_flag = false; // data on UART has been received
|
|
|
|
volatile char uart_char = 0; // UART data
|
2015-07-14 09:51:50 +02:00
|
|
|
|
|
|
|
/* switch off LED */
|
|
|
|
void led_off(void)
|
|
|
|
{
|
|
|
|
LED_PORT &= ~(1<<LED_IO); // remove power to LED
|
|
|
|
}
|
|
|
|
|
|
|
|
/* switch on LED */
|
|
|
|
void led_on(void)
|
|
|
|
{
|
|
|
|
LED_PORT |= (1<<LED_IO); // provide power to LED
|
|
|
|
}
|
|
|
|
|
|
|
|
/* toggle LED */
|
|
|
|
void led_toggle(void)
|
|
|
|
{
|
|
|
|
LED_PIN |= (1<<LED_IO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disable watchdog when booting */
|
|
|
|
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
|
|
|
|
void wdt_init(void)
|
|
|
|
{
|
|
|
|
MCUSR = 0;
|
|
|
|
wdt_disable();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:54:54 +02:00
|
|
|
/* receive UART input */
|
|
|
|
ISR(USART_RX_vect) { /* UART receive interrupt */
|
2015-07-17 10:01:43 +02:00
|
|
|
if (uart_in_i<sizeof(uart_in)) {
|
|
|
|
uart_in[uart_in_i] = getchar(); // save current character
|
|
|
|
uart_in_i++; // got to next one
|
|
|
|
uart_flag = true; // warm main programm
|
2015-07-14 09:54:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 09:51:50 +02:00
|
|
|
/* initialize GPIO */
|
|
|
|
void io_init(void)
|
|
|
|
{
|
|
|
|
/* use UART as terminal */
|
|
|
|
uart_init();
|
|
|
|
stdout = &uart_output;
|
|
|
|
stdin = &uart_input;
|
|
|
|
|
|
|
|
/* gpio */
|
|
|
|
/* LED */
|
|
|
|
LED_DDR |= (1<<LED_IO); // LED is driven by pin (set as output)
|
|
|
|
led_off();
|
2015-07-14 09:54:54 +02:00
|
|
|
|
2015-07-14 09:51:50 +02:00
|
|
|
sei(); /* enable interrupts */
|
2015-07-14 09:54:54 +02:00
|
|
|
|
|
|
|
/* WS2812B LEDs */
|
|
|
|
ws2812b_init();
|
|
|
|
ws2812b_off();
|
|
|
|
ws2812b_show();
|
2015-07-14 09:51:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2015-07-14 09:54:54 +02:00
|
|
|
|
2015-07-14 09:51:50 +02:00
|
|
|
io_init(); // initialize IOs
|
2015-07-14 09:54:54 +02:00
|
|
|
|
|
|
|
led_on();
|
2015-07-17 10:01:43 +02:00
|
|
|
printf(PSTR("welcome on the VLC+AtmoLight+WS2812B CuVoodoo AtmoLight\n"));
|
2015-07-14 09:54:54 +02:00
|
|
|
led_off();
|
|
|
|
|
2015-07-14 09:51:50 +02:00
|
|
|
while (true) { // endless loop for micro-controller
|
2015-07-14 09:54:54 +02:00
|
|
|
while (uart_flag) {
|
2015-07-17 10:01:43 +02:00
|
|
|
uart_flag = false;
|
|
|
|
if (((uart_in_i==1) && !(uart_in[0]==0xff)) ||
|
|
|
|
((uart_in_i==2) && !(uart_in[1]==0x00)) ||
|
|
|
|
((uart_in_i==3) && !(uart_in[2]==0x00)) ||
|
|
|
|
((uart_in_i==4) && !(uart_in[3]==0x0f))) { // wait for start sequence
|
|
|
|
led_on(); // indicate something is wrong
|
|
|
|
uart_in_i = 0; // reset if start sequence is not detected
|
2015-07-14 09:54:54 +02:00
|
|
|
} else {
|
2015-07-17 10:01:43 +02:00
|
|
|
led_off();
|
|
|
|
/* process colors on channels */
|
|
|
|
if (uart_in_i==10) { // left atmolight channel received (WS2812B channel 2)
|
|
|
|
for (uint8_t led=0; led<11; led++) {
|
|
|
|
ws2812b_set_led_color(2,led,uart_in[7],uart_in[8],uart_in[9]);
|
|
|
|
}
|
|
|
|
} else if (uart_in_i==13) { // right atmolight channel received (WS2812B channel 5)
|
|
|
|
for (uint8_t led=0; led<11; led++) {
|
|
|
|
ws2812b_set_led_color(5,led,uart_in[10],uart_in[11],uart_in[12]);
|
|
|
|
}
|
|
|
|
} else if (uart_in_i==16) { // top atmolight channel received (WS2812B channel 0+1)
|
|
|
|
for (uint8_t led=0; led<11; led++) {
|
|
|
|
ws2812b_set_led_color(0,led,uart_in[13],uart_in[14],uart_in[15]);
|
|
|
|
}
|
|
|
|
for (uint8_t led=0; led<8; led++) {
|
|
|
|
ws2812b_set_led_color(1,led,uart_in[13],uart_in[14],uart_in[15]);
|
|
|
|
}
|
|
|
|
} else if (uart_in_i==19) { // bottom atmolight channel received (WS2812B channel 3+4)
|
|
|
|
for (uint8_t led=0; led<11; led++) {
|
|
|
|
ws2812b_set_led_color(3,led,uart_in[16],uart_in[17],uart_in[18]);
|
|
|
|
}
|
|
|
|
for (uint8_t led=0; led<8; led++) {
|
|
|
|
ws2812b_set_led_color(4,led,uart_in[16],uart_in[17],uart_in[18]);
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 10:07:49 +02:00
|
|
|
if (uart_in_i>=19) { // end of message
|
2015-07-17 10:01:43 +02:00
|
|
|
ws2812b_show(); // show the new colors
|
2015-07-17 10:07:49 +02:00
|
|
|
if (uart_in_i>19) { // move buffered message
|
|
|
|
for (uint8_t i=19; i<uart_in_i; i++) {
|
|
|
|
uart_in[i-19] = uart_in[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uart_in_i -= 19; // start with next message
|
2015-07-14 09:54:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 09:51:50 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|