-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
152 lines (125 loc) · 2.99 KB
/
console.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "main.h"
#include "console.h"
#include "ADXL345.h"
#include "TLC5947DAP.h"
#define MAX_COMMAND_LENGTH 32
typedef char (*CommandFunc)(char *);
typedef struct {
char *name;
CommandFunc func;
} CommandArray;
char get_command(char *command, int maxlength);
char execute_command(char *cmdstr, CommandArray *commands);
char helpCmd(char *);
char ledCmdRgb(char *);
char ledCmdHsb(char *);
char accelCmd(char *);
char aboutCmd(char *);
CommandArray mainMenu[] =
{
{ "help", helpCmd },
{ "rgb", ledCmdRgb },
{ "hsb", ledCmdHsb },
{ "accel", accelCmd },
{ "about", aboutCmd },
{ NULL, NULL },
};
char aboutCmd(char *arg)
{
printf_P(PSTR("This awesome piece of awesomeness designed by Ardent Heavy Industries!\n"));
printf_P(PSTR("Hardware by Nathan Hunsperger\n"));
printf_P(PSTR("Software by Tom Cauchois\n"));
return 0;
}
char helpCmd(char *arg)
{
int i;
printf_P(PSTR("Commands:\n"));
for(i = 0; mainMenu[i].name != NULL; ++i)
printf_P(PSTR(" %s\n"), mainMenu[i].name);
return 0;
}
char ledCmdRgb(char *arg)
{
unsigned long r,g,b;
r = strtoul(arg, &arg, 16);
g = strtoul(arg, &arg, 16);
b = strtoul(arg, &arg, 16);
printf_P(PSTR("Setting LEDs (0x%lx, 0x%lx, 0x%lx)\n"), r, g, b);
send_rgb((uint16_t)r, (uint16_t)g, (uint16_t)b);
return 0;
}
char ledCmdHsb(char *arg)
{
unsigned long h,s,b;
h = strtoul(arg, &arg, 16);
s = strtoul(arg, &arg, 16);
b = strtoul(arg, &arg, 16);
printf_P(PSTR("Setting LEDs HSB (0x%lx, 0x%lx, 0x%lx)\n"), h, s, b);
send_hsb((uint16_t)h, (uint16_t)s, (uint16_t)b);
return 0;
}
char accelCmd(char *arg)
{
int16_t x,y,z;
adxl345_getxyz(&x, &y, &z);
printf_P(PSTR("Accel: (%d, %d, %d)\n"), x, y, z);
return 0;
}
void dispatch_console(void)
{
static char command[MAX_COMMAND_LENGTH] = {0};
static char init = 0;
int length = strlen(command);
if(init == 0)
{
printf_P(PSTR("> "));
init = 1;
}
if(get_command(command+length, MAX_COMMAND_LENGTH-length) == 0)
{
execute_command(command, mainMenu);
memset(command, 0, sizeof(command));
printf_P(PSTR("> "));
}
}
char get_command(char *command, int maxlength)
{
char input=0, length=0;
if(uart_charwaiting() != 0)
return 1;
input = uart_getchar();
while(input != '\r' && input != '\n')
{
*command = input;
printf_P(PSTR("%c"), *command++);
if(++length == maxlength - 1)
break;
if(uart_charwaiting() != 0)
return 1;
input = uart_getchar();
}
printf_P(PSTR("\n"));
return 0;
}
char execute_command(char *cmdstr, CommandArray *commands)
{
int i;
while(*cmdstr == ' ') ++cmdstr;
if(*cmdstr == 0)
return 0;
for(i = 0; commands[i].name != NULL; ++i)
{
if(!strncmp(cmdstr, commands[i].name, strlen(commands[i].name)))
{
return commands[i].func(cmdstr + strlen(commands[i].name));
}
}
printf_P(PSTR("Unknown command! Type 'help' for a list of commands\n"));
return 1;
}