arduino-json-client: diff f7028da1 4c9d4ada

Branch: master

Commit: f7028da104a97b180348f1dd4d62401d60c9fa81

Author: Chris Warburton <ChrisWarbo@gmail.com>
Date: Wed Jul 13 09:26:09 PM UTC 2011
Parent: 4c9d4adafd3e0c3ba5e7427a4f7725788c58838a
Log message:

    Successfully used to communicate with a C++ program. Ironed out some
    kinks in the process, such as initialisation and unneccessarily long
    delay() calls.

    1: diff --git a/serial_sketch/serial_sketch.pde b/serial_sketch/serial_sketch.pde
    2: index 3cb25e9..637b419 100644
    3: --- a/serial_sketch/serial_sketch.pde
    4: +++ b/serial_sketch/serial_sketch.pde
    5: @@ -19,8 +19,8 @@
    6:  void setup()
    7:  {
    8:    delay(1000);    // Keep this here so we don't flood the serial line
    9: -  Serial.println("Starting...");
   10:    Serial.begin(9600);    // Set up Serial library at 9600 bps
   11: +  Serial.println("{\"status\":\"ready\"}");
   12:  }
   13:  
   14:  void loop()
   15: @@ -36,8 +36,8 @@ void loop()
   16:    else {
   17:      // Perform whatever actions are defined for this input
   18:      read_commands(input);
   19: +    free(input);
   20:    }
   21: -  free(input);
   22:  }
   23:  
   24:  char read_char()
   25: @@ -47,7 +47,7 @@ char read_char()
   26:    // forever if you don't send it anything)
   27:    char data = -1;
   28:    while ((Serial.available() < 0) || (data < 0)) {
   29: -    delay(25);
   30: +    delay(1);
   31:      data = Serial.read();
   32:    }
   33:    return data;
   34: @@ -96,15 +96,17 @@ char* read_json()
   35:      if (read_so_far > pointer_size)
   36:      {
   37:        // Try to increase the size of our JSON pointer
   38: -      // NOTE: We could be more efficient here, eg. doubling the
   39: -      // pointer size at each realloc, but we favour minimum
   40: -      // memory usage, so we just bump it up by 1 each time.
   41: -      char* new_result = (char*) realloc(result, (pointer_size+1));
   42: +      // NOTE: There are various strategies we could use here
   43: +      // depending on what we want to conserve. Since we only
   44: +      // build one JSON string at a time, and free it after
   45: +      // we're done with it, we don't need to be massively
   46: +      // conservative in our memory usage
   47: +      char* new_result = (char*) realloc(result, (pointer_size*2));
   48:        if (new_result)
   49:        {
   50:          // We succeeded in allocating enough memory. Let's use it.
   51:          result = new_result;
   52: -        pointer_size++;
   53: +        pointer_size = pointer_size * 2;
   54:        }
   55:        else
   56:        {
   57: @@ -520,6 +522,9 @@ void run_command(char* name, char* value) {
   58:    if (compare_strings(name,"mode")) {
   59:      run_mode(value);    // Set pin mode
   60:    }
   61: +  if (compare_strings(name,"query")) {
   62: +    run_query(value);
   63: +  }
   64:  }
   65:  
   66:  void run_read(char* value) {
   67: @@ -543,6 +548,7 @@ void run_read(char* value) {
   68:        if (value[index] == '"') {
   69:          // We have a string. Let's see if it's what we're after
   70:          if (compare_strings(value+index, "pin")) {
   71: +          //Serial.println("Found pin");
   72:            // This is the number of the pin to read
   73:            // Find the associated digits
   74:            index = index + value_length(value+index);    // Skip over the name
   75: @@ -553,6 +559,7 @@ void run_read(char* value) {
   76:              index++;    // Skip the colon
   77:            }
   78:            else {
   79: +            //Serial.println("...1...");
   80:              return;     // No colon. Abort.
   81:            }
   82:            while (value[index] == ' ') {
   83: @@ -573,6 +580,7 @@ void run_read(char* value) {
   84:            continue;    // Retest the while condition
   85:          }
   86:          if (compare_strings(value+index, "type")) {
   87: +          //Serial.println("Found type");
   88:            // This is the type of pin to read.
   89:            // Find out whether it's analogue or digital.
   90:            index = index + value_length(value+index);
   91: @@ -602,10 +610,9 @@ void run_read(char* value) {
   92:          }
   93:        }
   94:      }
   95: -    if (pin > 0) {
   96: +    if (pin >= 0) {
   97:        // Send our result over USB
   98:        // {"pinValue":{"type":"digital", "pin":123, "value":123}}
   99: -      Serial.println();
  100:        Serial.print("{\"pinValue\":{\"type\":");
  101:        if (type == 1) {
  102:          Serial.print("\"digital\"");
  103: @@ -861,3 +868,16 @@ void run_mode(char* value) {
  104:      }
  105:    }
  106:  }
  107: +
  108: +void run_query(char* value) {
  109: +  // We use "query" as a generic name when all we want
  110: +  // to send is a value. We simply branch based on the
  111: +  // contents of value (we'd use a switch, but we would
  112: +  // have to overload comparison with compare_strings)
  113: +  if (compare_strings(value, "status")) {
  114: +    // This is a generic ping request. We just report
  115: +    // that we're ready so that whatever's on the other
  116: +    // end knows that it can send commands to us.
  117: +    Serial.println("{\"status\":\"ready\"}");
  118: +  }
  119: +}

Generated by git2html.