FWIW, the raw prototype
(is there a better place/way to post code in the forum?)
Hope you don't ban me from the forum for doing this! :-)
#include <Ports.h>
#include <RF12.h>
Port myPort1(1);
Port myPort2(2);
Port myPort3(3);
Port myPort4(4);
Port selectedPort(1);
unsigned short i, pinIndex;
unsigned short node, port, pin, lastPin, lastNode, lastPort;
unsigned long argumentOne, argumentTwo, argumentThree, lastArgumentOne, lastArgumentTwo, lastArgumentThree;
word value;
enum actions { HelpGet,
Set,
Get,
StatusGet,
NOP, Error } action;
enum states { ChooseNode, ChoosePort, ChoosePinOrPlug, ChooseArgumentOne, ChooseArgumentTwo, ChooseArgumentThree } state;
enum modifiers { Analog, Digital, NA } modifier; // is it a digital pin, an analog or not
enum directions { In, Out } dir;
unsigned short actionModifier, lastActionModifier;
unsigned short pinLastMode[8] = { In, In, In, In,
In, In, In, In };
unsigned short pinLastAction[8] = { Digital, Digital, Digital, Digital,
Digital, Digital, Digital, Digital };
unsigned short pinAbility[8] = { Analog, Digital, Analog, Digital,
Analog, Digital, Analog, Digital };
void Process_Get() {
Serial.println(" Process_Get ");
value = 0;
if ( node != 0 ) { Serial.println("E: Remote nodes not implemented yet!"); }
if ( ( node + pin + port ) == 0 ) {
Serial.println(" Re-load last values");
pin = lastPin;
node = lastNode;
port = lastPort;
argumentOne = lastArgumentOne;
argumentTwo = lastArgumentTwo;
argumentThree = lastArgumentThree;
actionModifier = lastActionModifier;
}
switch (port) {
case 1 : selectedPort = myPort1; break;
case 2 : selectedPort = myPort2; break;
case 3 : selectedPort = myPort3; break;
case 4 : selectedPort = myPort4; break;
default : Serial.print("E: port not valid ");
};
if ( pinLastMode[ port*pin ] != In ) {
if ( pinAbility[ port*pin ] == Analog ) {
selectedPort.mode2(INPUT);
} else {
selectedPort.mode(INPUT);
}
}
if ( pinAbility[ port*pin ] == Analog ) {
if ( actionModifier == Digital ) {
Serial.print(" digiRead2: ");
value = (word)selectedPort.digiRead2();
} else {
// default will be analog, only if digital modifier is specified then adigital readback will happen
Serial.print(" anaRead: ");
value = selectedPort.anaRead();
}
} else {
if ( actionModifier == Analog ) {
Serial.print(" E: no analog read possible at digital only pin! ");
} else {
Serial.print(" digiRead: ");
value = (word)selectedPort.digiRead();
}
}
Serial.println(value);
Process_Done();
}
void Process_Set() {
Serial.println(" Process_Set ");
value = 0;
if ( node != 0 ) { Serial.println("E: Remote nodes not implemented yet!"); }
if ( ( node + pin + port ) == 0 ) {
Serial.println("E: cannot set unspecified item");
}
switch (port) {
case 1 : selectedPort = myPort1; break;
case 2 : selectedPort = myPort2; break;
case 3 : selectedPort = myPort3; break;
case 4 : selectedPort = myPort4; break;
default : Serial.print("E: port not valid ");
};
if ( pinLastMode[ port*pin ] != Out ) {
if ( pinAbility[ port*pin ] == Analog ) {
selectedPort.mode2(OUTPUT);
} else {
selectedPort.mode(OUTPUT);
}
}
// Port digiWrite() set value of the DIO pin D_DigitalWrite
// Port anaWrite() set up PWM on DIO pin (only specific ports) D_PwmWrite
// Port digiWrite2() set digital level on the AIO pin A_DigitalWrite
if ( pinAbility[ port*pin ] == Analog ) {
selectedPort.digiWrite2((byte)argumentOne);
} else {
selectedPort.digiWrite((byte)argumentOne);
}
Process_Done();
}
void Process_Done() {
Serial.println(" Process_Done");
state = ChooseNode;
lastPin = pin; lastNode = node; lastPort = port;
lastArgumentOne = argumentOne; lastArgumentTwo = argumentTwo;
lastActionModifier = actionModifier;
pin = 0; node = 0; port = 0;
argumentOne = 0; argumentTwo = 0;
actionModifier = NA;
}
void Process_StatusGet() {
SerialPrint_Version();
Serial.println("Mappings: ");
}
void SerialPrint_Version() {
Serial.println("\n[JeeNodeIoCommander 200912302326]");
}
void Process_HelpGet() {
SerialPrint_Version();
Serial.println(" 0.1.A.1! - set channel a to digital 1 (any non zero argument will set to 1)");
Serial.println(" 0.2.D.0! - set channel a to digital 0 ");
Serial.println(" 0.3.A.analog? - returns analog reading ");
Serial.println(" 0.1.A.digital? - return digital value 1 or 0");
Serial.println(" 0.3.A? - returns analog reading ");
Serial.println(" 0.1.A.analog.200.100? - returns a in mode last used with parameters 200 and 100 - TODO ");
Serial.println(" 0.1.A.digital.200.100? - returns a in mode last used with parameters 200 and 100 - TODO ");
Serial.println(" ");
Serial.println(" H? - this help screen");
Serial.println(" ");
}
void setup() {
Serial.begin(57600);
Process_HelpGet();
state = ChooseNode;
}
void loop() {
if (Serial.available()) { // if data is received
i = Serial.read();
Serial.print((char)i);
action = NOP;
switch (state) {
case ChooseNode :
Serial.println(" ChooseNode ");
if ( (i <= '9') && ( i >= '0' ) ) {
// accumulate digits
node = 10 * node + i - '0';
if ( node > 99 ) { Serial.println(" E: node number to high 99 is max"); }
} else if (i == '.') {
state = ChoosePort;
} else if ( (i == 'H') || (i == 'h') ) {
action = HelpGet;
} else if (i == '?') {
action = Get;
} else {
action = Error; Serial.println(" E: incorrect input");
}
break;
case ChoosePort :
Serial.println(" ChoosePort ");
if ( (i <= '9') && ( i >= '0' ) ) {
// accumulate digits
port = 10 * port + i - '0';
if ( port > 4 ) { Serial.println(" E: port number must be one of 1,2,3,4"); }
} else if (i == '.') {
state = ChoosePinOrPlug;
} else if (i == '?') {
action = HelpGet;
} else {
action = Error; Serial.println(" E: incorrect input");
}
break;
case ChoosePinOrPlug :
Serial.println(" ChoosePinOrPlug ");
if (i == 'A') {
pin = Analog;
state = ChoosePinOrPlug;
} else if (i == 'D') {
pin = Digital;
state = ChoosePinOrPlug;
} else if (i == 'I') {
pin = Digital;
state = ChoosePinOrPlug;
} else if (i == '.') {
state = ChooseArgumentOne;
} else if (i == '?') {
action = Get;
} else {
action = Error; Serial.println(" E: illegal pin chosen");
}
break;
case ChooseArgumentOne :
Serial.println(" ChooseArgumentOne ");
if ( (i <= '9') && ( i >= '0' ) ) {
// accumulate digits
argumentOne = 10 * argumentOne + i - '0';
if ( ( argumentOne == 1 ) || ( argumentOne == 0 ) ) {
actionModifier = Digital;
}
} else if (i == '!') {
action = Set;
} else if (i == '?') {
action = Get;
} else if (i == '.') {
state = ChooseArgumentTwo;
} else if ( (i == 'a') && (actionModifier == NA) ) {
actionModifier = Analog;
} else if ( (i == 'd') && (actionModifier == NA) ) {
actionModifier = Digital;
}
break;
case ChooseArgumentTwo :
Serial.println(" ChooseArgumentTwo ");
if ( (i <= '9') && ( i >= '0' ) ) {
// accumulate digits
argumentTwo = 10 * argumentTwo + i - '0';
if ( ( argumentTwo == 1 ) || ( argumentTwo == 0 ) ) {
actionModifier = Digital;
}
} else if (i == '!') {
action = Set;
} else if (i == '?') {
action = Get;
}
break;
case ChooseArgumentThree :
Serial.println(" ChooseArgumentThree ");
if ( (i <= '9') && ( i >= '0' ) ) {
// accumulate digits
argumentThree = 10 * argumentThree + i - '0';
} else if (i == '!') {
action = Set;
} else if (i == '?') {
action = Get;
}
break;
default : state = ChooseNode; Serial.println("E: undef state"); // return E for Error
}
if (action != NOP) {
Serial.print(" node: "); Serial.print(node);
Serial.print(" port: "); Serial.print(port);
Serial.print(" pin: "); Serial.print(pin);
Serial.print(" arg1: "); Serial.print(argumentOne);
Serial.print(" arg2: "); Serial.print(argumentTwo);
Serial.print(" arg3: "); Serial.print(argumentThree);
Serial.print(" actionModifier: "); Serial.println(actionModifier);
}
switch (action) {
case NOP : break;
case HelpGet : Process_HelpGet(); break;
case StatusGet : Process_StatusGet(); break;
case Get : Process_Get(); break;
case Set : Process_Set(); break;
case Error :
pin = 0; node = 0; port = 0;
argumentOne = 0; argumentTwo = 0;
actionModifier = NA;
state = ChooseNode;
default : state = ChooseNode; Serial.println("E: unknown action");
};
}
}