summaryrefslogtreecommitdiff
path: root/command.c
blob: 0212ce955ded4e0c9a34fc4c30e622490176e1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * 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"
#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;
	}
    
	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();
    }
}

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;
}