screenlight/firmware/main.c

153 lines
4.5 KiB
C

/* 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
#include "uart.h" // basic UART functions
#include "ws2812b.h" // to control WS2812B LEDs
/* global variables */
volatile uint8_t uart_in[19]; // input from USART, enough space for one atmolight message
volatile uint8_t uart_in_i = 0; // UART input index
/* 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
/* 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;
}
/* receive UART input */
ISR(USART_RX_vect) { /* UART receive interrupt */
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
}
}
/* 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();
sei(); /* enable interrupts */
/* WS2812B LEDs */
ws2812b_init();
ws2812b_off();
ws2812b_show();
}
int main(void)
{
io_init(); // initialize IOs
led_on();
printf(PSTR("welcome on the VLC+AtmoLight+WS2812B CuVoodoo AtmoLight\n"));
led_off();
while (true) { // endless loop for micro-controller
while (uart_flag) {
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
} else {
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]);
}
}
if (uart_in_i>=sizeof(uart_in)) { // end of message
uart_in_i = 0; // start with next message
ws2812b_show(); // show the new colors
}
}
}
}
return 0;
}