CommandDefinitions.h
/*****************************************************************************
*
* This class defines commands that can be processed by processes that read
* and executed commands arriving from Serial input, Software Serial input
* and HC-05 Bluetooth input.
*
* This file is designed to be expanded and extended. Each major consumer
* of this code can focus on a specific subset of the commands and only
* include that code; hence the #if compiler directives scattered throughout
* this code.
*
* Consuming clents need to identify the commands they want to implement
* Using simple define statements:
*
* #define SUPPORT_DIGITAL_MANAGEMENT true
* #define SUPPORT_ANALOG_MANAGEMENT false
* #define SUPPORT_LED_MANAGEMENT true
* #define SUPPORT_MOTOR_MANAGEMENT true
* #define SUPPORT_PAUSE_MANAGEMENT false
* #define SUPPORT_NAVIGATION_MANAGEMENT false
*
*
*/
// CommandDefinitions.h
#ifndef _COMMANDDEFINITIONS_h
#define _COMMANDDEFINITIONS_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
class CommandDefintions {
public:
// Constructor
CommandDefintions();
// Initialize command structures
void init();
// Templates
struct operation {
char *name;
char code;
};
struct selector {
char *name;
char code;
};
struct action {
char *name;
char code;
};
#if SUPPORT_DIGITAL_MANAGEMENT
/*
* Digital Management supports setting and reading digital pins 0..13
*/
struct digitalCommand {
operation opr;
selector sel[14];
action act[3];
};
struct digitalCommand *pin;
void showDigitalCommands();
#endif
#if SUPPORT_ANALOG_MANAGEMENT
#endif
#if SUPPORT_LED_MANAGEMENT
struct ledCommand {
operation opr;
selector sel[3];
action act[3];
};
struct ledCommand *led;
void showLEDCommands();
#endif
#if SUPPORT_MOTOR_MANAGEMENT
struct motorCommand {
operation opr;
selector sel[3];
action act[5];
};
motorCommand *motor;
void showMotorCommands();
#endif
#if SUPPORT_PAUSE_MANAGEMENT
#endif
#if SUPPORT_NAVIGATION_MANAGEMENT
#endif
private:
};
#endif
CommandDefinitions.cpp
//
//
//
#include "CommandDefinitions.h"
CommandDefintions::CommandDefintions() {
}
void CommandDefintions::init() {
#if SUPPORT_MOTOR_MANAGEMENT
initializeMotorCommands();
#endif
#if SUPPORT_LED_MANAGEMENT
initializeLEDCommands();
#endif
#if SUPPORT_DIGITAL_MANAGEMENT
initializeDigitalCommands();
#endif
}
#if SUPPORT_MOTOR_MANAGEMENT
void initializeMotorCommands() {
Serial.println("Motor Operation");
motor->opr = { (char *) "MOTOR",'M' };
Serial.println("Motor Selectors");
motor->sel[0] = { (char *) "LEFT", 'L' };
motor->sel[1] = { (char *) "RIGHT", 'R' };
motor->sel[2] = { (char *) "BOTH", 'B' };
Serial.println("Motor Actions");
motor->act[0] = { (char *) "FORWARD", 'F' };
motor->act[1] = { (char *) "BACKWARD", 'B' },
motor->act[2] = { (char *) "STOP",'S' };
motor->act[3] = { (char *) "FASTER",'+' };
motor->act[4] = { (char *) "SLOWER", '-' };
Serial.println("Motor Initialization completed");
}
void CommandDefintions::showMotorCommands() {
int selectorCount = sizeof(motor->sel) / sizeof(motor->sel[0]);
int actionCount = sizeof(motor->act) / sizeof(motor->act[0]);
Serial.print("Motor Command: ");
Serial.print(motor->opr.name);
Serial.print("(");
Serial.print(motor->opr.code);
Serial.print(") has ");
Serial.print(selectorCount);
Serial.print(" selectors and ");
Serial.print(actionCount);
Serial.println(" actions:");
Serial.print("SEL:");
for (int i = 0; i < selectorCount; i++) {
Serial.print(motor->sel[i].name);
Serial.print("(");
Serial.print(motor->sel[i].code);
Serial.print(") ");
}
Serial.println();
Serial.print("ACT:");
for (int i = 0; i < actionCount; i++) {
Serial.println(motor->act[i].name);
Serial.print("(");
Serial.print(motor->act[i].code);
Serial.print(") ");
}
Serial.println();
}
#endif
#if SUPPORT_LED_MANAGEMENT
void initializeLEDCommands() {
led->opr = { (char *) "LED", 'L' };
led->sel[0] = { (char *) "RED", 'R' };
led->sel[1] = { (char *) "YELLOW", 'Y' };
led->sel[2] = { (char *) "GREEN", 'G'};
led->act[0] = { (char *) "ON",'+' };
led->act[1] = { (char *) "OFF", '-' };
}
void CommandDefintions::showLEDCommands() {
Serial.print(led->opr.name);
Serial.print(" ");
int selectorCount = sizeof(led->sel) / sizeof(led->sel[0]);
for (int i = 0; i < selectorCount; i++) {
Serial.println(led->sel[i].name);
}
int actionCount = sizeof(led->act) / sizeof(led->act[0]);
for (int i = 0; i < actionCount; i++) {
Serial.println(led->act[i].name);
}
}
#endif
#if SUPPORT_DIGITAL_MANAGEMENT
void initializeDigitalCommands() {
pin->opr.name = { (char *) "D_PIN", 'D' };
pin->sel[0] = { (char *) "D0", char(0) };
pin->sel[1] = { (char *) "D1", char(1) };
pin->sel[2] = { (char *) "D2", char(2) };
pin->sel[3] = { (char *) "D3", char(3) };
pin->sel[4] = { (char *) "D4", char(4) };
pin->sel[5] = { (char *) "D5", char(5) };
pin->sel[5] = { (char *) "D6", char(6) };
pin->sel[5] = { (char *) "D7", char(7) };
pin->sel[5] = { (char *) "D8", char(8) };
pin->sel[5] = { (char *) "D9", char(9) };
pin->sel[5] = { (char *) "D10", char(10) };
pin->sel[5] = { (char *) "D11", char(11) };
pin->sel[5] = { (char *) "D12", char(12) };
pin->sel[5] = { (char *) "D13", char(13) };
pin->act[0] = { (char *) "ON", '+' };
pin->act[1] = { (char *) "ON", '+' };
pin->act[2] = { (char *) "STATUE", '+' };
}
void CommandDefintions::showDigitalCommands() {
Serial.print(pin->opr.name);
Serial.print(" ");
int selectorCount = sizeof(pin->sel) / sizeof(pin->sel[0]);
for (int i = 0; i < selectorCount; i++) {
Serial.println(pin->sel[i].name);
}
int actionCount = sizeof(pin->act) / sizeof(pin->act[0]);
for (int i = 0; i < actionCount; i++) {
Serial.println(pin->act[i].name);
}
}
#endif
CommandData.ino
/*****************************************************************************
*
*
*
*/
// CAPABILITY SUPPORT
#define PROCESS_SERIAL_COMMANDS false
#define PROCESS_SSP_COMMANDS false
#define SUPPORT_DIGITAL_MANAGEMENT true
#define SUPPORT_ANALOG_MANAGEMENT false
#define SUPPORT_LED_MANAGEMENT true
#define SUPPORT_MOTOR_MANAGEMENT true
#define SUPPORT_PAUSE_MANAGEMENT false
#define SUPPORT_NAVIGATION_MANAGEMENT false
#if PROCESS_SERIAL_COMMANDS
#endif
#if PROCESS_PROCESS_COMMANDS
#endif
#include "CommandDefinitions.h"
CommandDefintions *commands;
void setup()
{
Serial.begin(115200);
commands = new CommandDefintions();
commands->init();
#if SUPPORT_MOTOR_MANAGEMENT
commands->showMotorCommands();
#endif
#if SUPPORT_LED_MANAGEMENT
commands->showLEDCommands();
#endif
#if SUPPORT_DIGITAL_MANAGEMENT
commands->showDigitalCommands();
#endif
}
void loop()
{
}