/* * Copyright 2012 (C) Christian Franke * * 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 . * */ #include #include #include #include "pwm.h" #include "serial.h" #include "sequencer.h" #include "util.h" #include "shiftbrite.h" static void command_error(void) { serial_send("?\r\n", 3, 1); } static void command_success(void) { serial_send(":)\r\n", 4, 0); } static void handle_immediate(char *buffer, uint16_t len) { if (len < sizeof("irrggbb\r") - 1) { command_error(); return; } uint8_t new_red, new_blue, new_green; if (parse_hex_number(&buffer[1], &new_red) || parse_hex_number(&buffer[3], &new_green) || parse_hex_number(&buffer[5], &new_blue)) { command_error(); return; } sequencer_disable(); uint8_t sb_no; if ((len > sizeof("irrggbb\r") - 1) && parse_hex_number(&buffer[7], &sb_no)==0 && sb_no) { sb_setcolor(sb_no-1, new_red, new_green, new_blue); command_success(); } else { pwm_set_rgb(new_red, new_green, new_blue); command_success(); } } static void handle_sequence(char *buffer, uint16_t len) { uint8_t steps = 0; buffer += 1; len -= 1; while (len >= 10) { uint8_t tout, lamp, new_red, new_blue, new_green; if (parse_hex_number(&buffer[0], &tout) || parse_hex_number(&buffer[2], &lamp) || parse_hex_number(&buffer[4], &new_red) || parse_hex_number(&buffer[6], &new_blue) || parse_hex_number(&buffer[8], &new_green)) { command_error(); return; } sequencer_set(steps, tout, lamp, new_red, new_blue, new_green); steps += 1; buffer += 10; len -= 10; } sequencer_len(steps); sequencer_enable(); } uint16_t serial_rx_cb(char *buffer, uint16_t len) { switch (buffer[len - 1]) { case '\r': case '\n': serial_send("\r\n", 2, 0); break; default: serial_send(&buffer[len - 1], 1, 0); return 0; } switch (buffer[0]) { case 'i': case 'I': handle_immediate(buffer, len); break; case 'q': case 'Q': serial_toggle_verbose(); command_success(); break; case 's': case 'S': handle_sequence(buffer, len); break; default: command_error(); } return len; }