Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetClient / Lcd.cpp @ 13

History | View | Annotate | Download (4.43 KB)

1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  4/12/07
6
 */
7

    
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdlib.h>
11
#include "includes/Lcd.h"
12
#include "includes/Client.h"
13

    
14
Lcd::Lcd()
15
{
16
}
17

    
18
void Lcd::printUsage()
19
{
20
  printf("\nUsage of the \"lcd\" command:\n");
21
  printf("lcd init\n");
22
  printf("lcd clearScreen\n");
23
  printf("lcd putByte <byteValue>\n");
24
  printf("lcd putChar <character>\n");
25
  printf("lcd putString <string>\n");
26
  printf("lcd goto <x> <y>\n");
27
  printf("lcd putInt <integer>\n");
28
  printf("\n");
29
}
30

    
31

    
32
int Lcd::parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server)
33
{
34
  if (numTokens < 2)
35
    {
36
      printf("Error: The lcd command did not contain enough fields.\n");
37
      return -1;
38
    }
39

    
40
  char buffer[MAX_COMMAND_LEN];
41
  buffer[0] = '\0';
42
  snprintf(buffer, MAX_COMMAND_LEN, "%d ", SEND_TO_ROBOT);
43
  int len = strlen(buffer);
44

    
45
  if (strcmp(tokens[1], "init") == 0)
46
    {
47
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", LCD_INIT);
48
    }
49
  else if (strcmp(tokens[1], "clearScreen") == 0)
50
    {
51
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", LCD_CLEAR_SCREEN);
52
    }
53
  else if (numTokens < 3)
54
    {
55
      printf("Error: The lcd %s command was invalid or did not contain enough fields.\n", tokens[1]);
56
      return -1;
57
    }
58
  else
59
    {
60
      if (strcmp(tokens[1], "putByte") == 0)
61
        {
62
          char * endPtr = NULL;
63
          int value = strtol(tokens[2], &endPtr, 10);
64
          if (tokens[2][0] == '\0' || *endPtr != '\0')
65
            {
66
              printf("Error: The specified value was invalid.\n");
67
              return -1;
68
            }
69
          
70
          if (value < 0 || value > 255)
71
            {
72
              printf("Error: The specified byte value was too large.\n");
73
              return -1;
74
            }
75

    
76
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", LCD_PUTBYTE, value);
77
        }
78
      else if (strcmp(tokens[1], "putChar") == 0)
79
        {
80
          int charLength = strlen(tokens[2]);
81
          if (charLength <= 0 || charLength > 1)
82
            {
83
              printf("Error: The specified character was invalid.\n");
84
              return -1;
85
            }
86
          
87
          char character = tokens[2][0];
88
          
89
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %c", LCD_PUTCHAR, character);
90
        }
91
      else if (strcmp(tokens[1], "putString") == 0)
92
        {
93
          //TODO: make this faster
94
          char stringBuffer[MAX_INPUT_LENGTH];
95
          stringBuffer[0] = '\0';
96
          for (int i = 2; i < numTokens; i++)
97
            {
98
              strncat(stringBuffer, tokens[i], MAX_INPUT_LENGTH - strlen(stringBuffer));
99
              strcat(stringBuffer, " ");
100
            }
101
          if ((signed)(strlen(stringBuffer)) > MAX_COMMAND_LEN - len - 3)
102
            {
103
              printf("Error: The string you want to send is too long.\n");
104
              return -1;
105
            }
106

    
107
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %s", LCD_PUTSTR, stringBuffer);
108
        }
109
      else if (strcmp(tokens[1], "goto") == 0)
110
        {
111
          if (numTokens < 4)
112
            {
113
              printf("Error: Not enough parameters for the goto command.\n");
114
              return -1;
115
            }
116

    
117
          char * endPtr = NULL;
118
          int xValue = strtol(tokens[2], &endPtr, 10);
119
          if (tokens[2][0] == '\0' || *endPtr != '\0')
120
            {
121
              printf("Error: The specified value was invalid.\n");
122
              return -1;
123
            }
124
          endPtr = NULL;
125
          int yValue = strtol(tokens[2], &endPtr, 10);
126
          if (tokens[2][0] == '\0' || *endPtr != '\0')
127
            {
128
              printf("Error: The specified value was invalid.\n");
129
              return -1;
130
            }
131
          
132
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d %d", LCD_GOTOXY, xValue, yValue);
133
        }
134
      else if (strcmp(tokens[1], "putInt") == 0)
135
        {
136
          char * endPtr = NULL;
137
          int value = strtol(tokens[2], &endPtr, 10);
138
          if (tokens[2][0] == '\0' || *endPtr != '\0')
139
            {
140
              printf("Error: The specified value was invalid.\n");
141
              return -1;
142
            }
143
          
144
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", LCD_PUTINT, value);
145
        }
146
      else
147
        {
148
          printf("Error: invalid lcd command.\n");
149
          return -1;
150
        }
151
    }
152

    
153
  //TODO: remove this print later
154
  printf("Command being sent to server:\n%s\n", buffer);
155
  
156
  server.sendMsg(buffer);
157

    
158
  return 0;
159
}