Arduino-SerialCommand icon indicating copy to clipboard operation
Arduino-SerialCommand copied to clipboard

Added processCharacter method

Open robnewton opened this issue 9 years ago • 1 comments

Separated buffer building and command parsing from serial port reading.

robnewton avatar Dec 22 '14 03:12 robnewton

Usage is shown below. In my example, I am using VirtualWire to transmit the command wirelessly from a host Arduino connected to the PC to a second receiver Arduino which is not connected to the PC but within wireless range of the first.

#include <VirtualWire.h>
#include <SerialCommand.h>

// Variables 
SerialCommand sCmd;

void setup() {
  // Listen on serial connection for messages from the PC
  Serial.begin(9600); 
  Serial.println("Setup");

  // Setup callbacks for SerialCommand commands
  sCmd.addCommand("P", processCommand);   // Process the command
  sCmd.setDefaultHandler(unrecognized);     // Handler for command that isn't matched

  // Required for DR3100 
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(12);

  //speed of data transfer Kbps - bits per sec 
  vw_setup(4000); 

  // Start the receiver PLL running
  vw_rx_start();
}

void loop(){
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) {
    int i;
    for (i = 0; i < buflen; i++){
      sCmd.processCharacter(char(buf[i]));
    }
  }
}

robnewton avatar Dec 22 '14 03:12 robnewton