184 lines
5.2 KiB
C
184 lines
5.2 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 */
|
|
const uint8_t leds_per_channel[WS2812B_NB_CHANNELS] = {11,5,9,11,5,9}; // the led strips (top 1, top 2, left, bottom 1, bottom 2, right)
|
|
uint8_t nb_leds = 0; // the total number of LEDs (will be calculated on the number of LEDs per channel)
|
|
volatile uint8_t uart_in[16]; // input from USART, enough space for one fnordlight command or sync sequence
|
|
|
|
/* 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
|
|
volatile bool command_flag = false; // a command has been received
|
|
|
|
/* 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 */
|
|
uart_char = getchar(); // save current character
|
|
uart_flag = true; // warm main programm
|
|
}
|
|
|
|
/* process command */
|
|
void command_action(void)
|
|
{
|
|
/* parse command */
|
|
switch (uart_in[1]) {
|
|
case 0x01: // set LED colors
|
|
if (uart_in[0]<nb_leds) { // set LED color
|
|
for (uint8_t channel = 0; channel<sizeof(leds_per_channel); channel++) { // find right channel
|
|
if (uart_in[0]<leds_per_channel[channel]) { // find right LED
|
|
ws2812b_set_led_color(channel,uart_in[0],uart_in[4],uart_in[5],uart_in[6]);
|
|
break;
|
|
} else {
|
|
uart_in[0] -= leds_per_channel[channel]; // go to next channel
|
|
}
|
|
}
|
|
} else { // use additional LED setting as signal to show the LEDs
|
|
ws2812b_show();
|
|
}
|
|
break;
|
|
case 0x08: // stop color changing
|
|
ws2812b_show(); // use this as flush command
|
|
break;
|
|
case 0x80: // start bootloader
|
|
case 0x87: // start application
|
|
ws2812b_off();
|
|
ws2812b_show();
|
|
break;
|
|
default:
|
|
break; // all other commands are not relevant
|
|
}
|
|
}
|
|
|
|
/* 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)
|
|
{
|
|
uint8_t uart_in_i = 0; // UART input index
|
|
uint8_t escape_count = 0; // number of escape bytes received
|
|
|
|
io_init(); // initialize IOs
|
|
|
|
/* count the total number of LEDs */
|
|
for (uint8_t i=0; i<sizeof(leds_per_channel); i++) {
|
|
nb_leds += leds_per_channel[i];
|
|
}
|
|
|
|
led_on();
|
|
printf(PSTR("welcome on the VLC+fnordlicht+WS2812B AtmoLight\n"));
|
|
led_off();
|
|
|
|
while (true) { // endless loop for micro-controller
|
|
while (uart_flag) {
|
|
/* go to next character but next got pass the last
|
|
* only sync sequences can be longer than 15, but should only be 16
|
|
* but sometimes (bug) they are longer
|
|
* then ignore the escape byte until the sequence ends
|
|
*/
|
|
uart_in[uart_in_i] = uart_char;
|
|
if (uart_in_i<sizeof(uart_in)-1) {
|
|
uart_in_i++;
|
|
}
|
|
|
|
if (uart_char==0x1b) { // escape byte received
|
|
if (escape_count<15) { // there should be maximum 15 escape bytes. ignore all additional
|
|
escape_count++; // count number of escape bytes
|
|
}
|
|
} else {
|
|
if (escape_count>=15) { // end of sync sequence
|
|
uart_in_i = 0; // reset input but don't handle sync sequence
|
|
}
|
|
escape_count = 0; // restart count
|
|
}
|
|
if (uart_in_i>=15 && escape_count<15) { // end of command
|
|
command_flag = true; // notify a command is ready
|
|
uart_in_i = 0; // reset input
|
|
}
|
|
uart_flag = false;
|
|
}
|
|
while (command_flag) { // UART input command is ready
|
|
led_on();
|
|
command_action(); // process command
|
|
command_flag = false; // clear flag
|
|
led_off();
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|