diff options
Diffstat (limited to 'command.c')
-rw-r--r-- | command.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/command.c b/command.c new file mode 100644 index 0000000..91489e6 --- /dev/null +++ b/command.c @@ -0,0 +1,84 @@ +/* + * Copyright 2012 (C) Christian Franke <nobody at nowhere dot ws> + * + * 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 <avr/io.h> +#include <stdint.h> +#include <string.h> + +#include "pwm.h" +#include "serial.h" +#include "util.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; + } + + pwm_set_rgb(new_red, new_green, new_blue); + command_success(); +} + +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; + default: + command_error(); + } + + return len; +} |