sventon subversion web client - http://www.sventon.org
[show recent changes]
 
  Go to revisionGo to path
Help
Rev: HEAD (656) - svn://greenwoodma.servehttp.com/open-source / blog-code / arduino / CmdMsg / CmdMsg.ino
Show File - CmdMsg.ino  [show properties]
spinner
const int MAX_LEN = 64;
//The set of commands we understand
const char ECHO = 'E';
const char ADD = 'A';
const char SUBTRACT = 'S';
//true if there is a new command to be processed
boolean cmdReady = false;
10 
11  //the buffer that holds incoming data
12  char buffer[MAX_LEN+1];
13 
14  //the offset in the buffer where the next byte should be placed
15  int offset = 0;
16 
17  //the set of token separators that split the cmd into sections
18  const char separator_tokens[] = { ':' };
19 
20  /**
21   * Called once when the sketch is first run
22   */
23  void setup() {
24    // initialize serial:
25    Serial.begin(9600);
26  }
27 
28  /**
29   * The main processing loop that gets called over and over again
30   */
31  void loop() {
32 
33    if (cmdReady) {
34      //there is a command waiting to be processed...
35 
36      //the last part of the command (only used by the tokenizer)
37      //char* last;
38 
39      //get the actual command (i.e. the first token)
40      char* current = strtok(buffer,separator_tokens);
41 
42      //switch based on the command
43      switch (*current) {
44        case ECHO:
45        {
46          //get the next token from the command
47          current = strtok(NULL,separator_tokens);
48 
49          if (current == NULL) {
50            //there are no more tokens so say so
51            Serial.println("Nothing to ECHO!");
52          }
53          else {
54            //echo each of the parts back to the PC
55            while (current != NULL) {
56              Serial.println(current);
57              current = strtok(NULL,separator_tokens);
58            }
59          }
60 
61          break;
62        }
63 
64        case ADD:
65        {
66          //get and convert to ints the two numbers to add
67          int x = atoi(strtok(NULL,separator_tokens));
68          int y = atoi(strtok(NULL,separator_tokens));
69 
70          //do the addition
71          int z = x+y;
72 
73          //return the result
74          Serial.println(z);
75 
76          break;
77        }
78 
79        case SUBTRACT:
80        {
81          //get and convert to ints the two numbers to add
82          //NOTE: we should probably do some error checking
83          int x = atoi(strtok(NULL,separator_tokens));
84          int y = atoi(strtok(NULL,separator_tokens));
85 
86          //do the subtraction
87          int z = x-y;
88 
89          //return the result
90          Serial.println(z);
91 
92          break;
93        }
94 
95        default:
96          //this only happens if the command is unknown
97          //which should never happen!
98          Serial.print("Unknown Command!");
99      }
100 
101      //we have processed the command
102      cmdReady = false;
103    }
104 
105    //do other stuff that has to be done no matter what
106  }
107 
108  /**
109   * A SerialEvent occurs whenever a new data comes in the
110   * hardware serial RX, and this method is called between
111   * successive calls of loop()
112   */
113  void serialEvent() {
114    while (Serial.available() && !cmdReady) {
115      //if there are bytes available and there isn't a completed
116      //command waiting to be processed...
117 
118      //get the new byte:
119      char inChar = (char)Serial.read();
120 
121      //if the incoming character is a newline, set a flag
122      //so the main loop can do something about it:
123      if (inChar == '\n') {
124        //a new line char signifies the end of the command so...
125 
126        //null terminate the string
127        buffer[offset] = NULL;
128 
129        //reset the offset ready for next time
130        offset = 0;
131 
132        //flag up that there is a command ready to process
133        cmdReady = true;
134      } else {
135        //add the character to the end of the command
136        buffer[offset] = inChar;
137 
138        //move the offset on ready for the next character
139        ++offset;
140      }
141    }
142  }

feed icon

sventon 2.1.4