root / trunk / code / projects / colonet / ColonetClient / Buzzer.cpp @ 13
History | View | Annotate | Download (2.07 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
* @author Jason Knichel
|
4 |
*
|
5 |
* 3/22/07
|
6 |
*/
|
7 |
|
8 |
#include <stdio.h> |
9 |
#include <string.h> |
10 |
#include <stdlib.h> |
11 |
#include "includes/Buzzer.h" |
12 |
|
13 |
Buzzer::Buzzer() |
14 |
{ |
15 |
} |
16 |
|
17 |
void Buzzer::printUsage()
|
18 |
{ |
19 |
printf("\nUsage of the \"buzzer\" command:\n");
|
20 |
printf("buzzer init\n");
|
21 |
printf("buzzer setValue <value>\n");
|
22 |
printf("buzzer setFreq <frequency>\n");
|
23 |
printf("buzzer chirp\n");
|
24 |
printf("buzzer off\n");
|
25 |
printf("\n");
|
26 |
} |
27 |
|
28 |
|
29 |
int Buzzer::parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server) |
30 |
{ |
31 |
if (numTokens < 2) |
32 |
{ |
33 |
printf("Error: The buzzer command did not contain enough fields.\n");
|
34 |
return -1; |
35 |
} |
36 |
|
37 |
char buffer[MAX_COMMAND_LEN];
|
38 |
buffer[0] = '\0'; |
39 |
snprintf(buffer,MAX_COMMAND_LEN, "%d ", SEND_TO_ROBOT);
|
40 |
int len = strlen(buffer);
|
41 |
|
42 |
if (strcmp(tokens[1], "init") == 0) |
43 |
{ |
44 |
snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_INIT);
|
45 |
} |
46 |
else if (strcmp(tokens[1], "setValue") == 0 || strcmp(tokens[1], "setFreq") == 0) |
47 |
{ |
48 |
if (numTokens < 3) |
49 |
{ |
50 |
printf("Error: That buzzer command needs arguments.\n");
|
51 |
return -1; |
52 |
} |
53 |
|
54 |
char * endPtr = NULL; |
55 |
int value = strtol(tokens[2], &endPtr, 10); |
56 |
if (tokens[2][0] == '\0' || *endPtr != '\0') |
57 |
{ |
58 |
printf("Error: The specified value was invalid.\n");
|
59 |
return -1; |
60 |
} |
61 |
|
62 |
if (strcmp(tokens[1], "setValue") == 0) |
63 |
{ |
64 |
snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", BUZZER_SET_VAL, value);
|
65 |
} |
66 |
else
|
67 |
{ |
68 |
snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", BUZZER_SET_FREQ, value);
|
69 |
} |
70 |
} |
71 |
else if (strcmp(tokens[1], "chirp") == 0) |
72 |
{ |
73 |
snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_CHIRP);
|
74 |
} |
75 |
else if (strcmp(tokens[1], "off") == 0) |
76 |
{ |
77 |
snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_OFF);
|
78 |
} |
79 |
else
|
80 |
{ |
81 |
printf("Error: invalid buzzer command.\n");
|
82 |
return -1; |
83 |
} |
84 |
|
85 |
|
86 |
//TODO: remove this print later
|
87 |
printf("Command being sent to server:\n%s\n", buffer);
|
88 |
|
89 |
server.sendMsg(buffer); |
90 |
|
91 |
return 0; |
92 |
} |