I made a simple "command line interface" through the USB port.
I would be interested to see if anyone has any other ideas. Uses Messenger.h
#include <Messenger.h>
#define MAX_CL_SIZE 9 //max command line interface size in # of bytes -1 for null char
#define PIN_LED 13 //led pin
Messenger message = Messenger(); // Instantiate Messenger object with the default separator (the space character)
void setup() {
// Initiate Serial Communication
Serial.begin(115200);
// Attach the callback function to the Messenger
message.attach(messageReady);
}
void loop() {
char serialInByte;
while (Serial.available()) {
serialInByte = Serial.read ();
if (serialInByte == '\r') //if enter (carraige return) was pressed
{
Serial.println('\0');
}
else
{
Serial.print(serialInByte,BYTE); //echo data back
}
message.process(serialInByte); //process with messenger library
}
}
// Create the callback function
void messageReady() {
char commandIn[MAX_CL_SIZE];
int parameterIn;
// Loop through all the available elements of the message
while (message.available() ) {
message.copyString(commandIn, MAX_CL_SIZE);
parameterIn = message.readInt();
}
if(!processUserCommand(commandIn, parameterIn))
Serial.println("Syntax Error");
Serial.print("> ");
return;
}
bool processUserCommand(char command[MAX_CL_SIZE], int parameter) {
if (!strcmp(command, "nocommand"))
cmdNoCommand();
else if (!strcmp(command, "blinkled"))
cmdBlinkLed(parameter);
else if (!strcmp(command, "setled"))
cmdSetLed(parameter);
else
return false; //error
return true;
}
void cmdNoCommand() {
return;
}
void cmdBlinkLed(int blinkNumber) {
for (int i = 0; i < blinkNumber; i++)
{
setStatusLed(255);
delay(250);
setStatusLed(0);
delay(250);
}
return;
}
void cmdSetLed(int brightness) {
setStatusLed(brightness);
return;
}
void setStatusLed(int brightness) {
pinMode(PIN_LED, OUTPUT);
analogWrite(PIN_LED, brightness);
return;
}