started reworking sketch to incooperate a loop feature

master
Jan Danielzick 2021-05-09 21:39:31 +02:00
parent 8827530a3b
commit 266e27072a
1 changed files with 119 additions and 100 deletions

View File

@ -101,25 +101,106 @@ String waitForSerialInput(uint8_t bytes){
return string;
}
void runCommand(char command, String argument){
void runCommand1Arg(char command, uint8_t argument0){
switch (command) {
case 'h':
Serial.print("\n\rDid you mean 'H'?");
return;
case 'x': // toggle pin mode
if (toggleMode(argument0) == UNKNOWN_PIN) Serial.print("e");
else Serial.print("s");
return;
case 'a': // read analog pin
if (argument0>=NUM_ANALOG_INPUTS || analogInputToDigitalPin(argument0)<0){
Serial.print("e");
return;
}
Serial.print(analogRead(analogInputToDigitalPin(argument0)));
Serial.print("s");
return;
case 'r': // read digital value
if (argument0 >= NUM_DIGITAL_PINS) Serial.print("e");
else {
Serial.print(digitalRead(argument0));
Serial.print("s");
}
return;
case 't': // toggle a pin
if (toggleState(argument0)==UNKNOWN_PIN) Serial.print("e");
else Serial.print("s");
return;
case 'l':
if (argument0=='h') {
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("s");
} else if (argument0=='l') {
digitalWrite(LED_BUILTIN, LOW);
Serial.print("s");
} else if (argument0=='t') {
toggleState(LED_BUILTIN);
Serial.print("s");
} else {
Serial.print("e");
}
return;
default:
return;
}
}
void runCommand2Arg(char command, uint8_t argument0, uint8_t argument1){
switch (command) {
case 'm': // set pin mode
Serial.print(argument0);
Serial.print(argument1);
if (argument0 >= NUM_DIGITAL_PINS) Serial.print("e");
else if ((char)argument1=='i') {
pinMode(argument0, INPUT);
Serial.print("s");
} else if ((char)argument1=='o') {
pinMode(argument0, OUTPUT);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 'w': // write digital pin
if (argument0 >= NUM_DIGITAL_PINS) Serial.print("e");
else if ((char)argument1 == 'h') {
digitalWrite(argument0, HIGH);
Serial.print("s");
} else if ((char)argument1 == 'l') {
digitalWrite(argument0, LOW);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 'p': // set a pwm pin
if (digitalPinHasPWM(argument0)) {
analogWrite(argument0, argument1);
Serial.print("s");
} else {
Serial.print("e");
}
return;
default:
return;
}
}
void getAndRunCommand(){
uint8_t command = Serial.read();
int analogPin;
String mode;
uint8_t pin;
String state;
uint8_t pwmState;
uint8_t argument0;
uint8_t argument1;
switch ((char)command) {
case 'R': // run setup() again
Serial.print("R");
Serial.print((char)command);
Serial.end();
setup();
return;
case 'h': // print help information
Serial.print("h");
case 'H': // print help information
Serial.print((char)command);
Serial.print(F("\n\r"
"status (output from serial line)\n\r"
" <letter of command> command acknowledgement\n\r"
@ -132,7 +213,9 @@ void getAndRunCommand(){
"commands (input to serial line)\n\r"
" R pseudo-reset\n\r"
" run setup again\n\r"
" h show help\n\r"
" H show help\n\r"
" Q lock\n\r"
" lock the Arduino's state by running an infinite loop\n\r"
" m set mode\n\r"
" usage: followed by two digit pin number and o or i\n\r"
" set a pin to input or output mode\n\r"
@ -165,103 +248,39 @@ void getAndRunCommand(){
" l change led state\n\r"
" usage: followed by h (high), l (low) or t (toggle)\n\r"
" sets or toggles the state of LED_BUILTIN\n\r"
" q lock\n\r"
" lock the Arduino's state by running an infinite loop\n\r"
"\n\rs"));
return;
case 'm': // set pin mode
Serial.print("m");
pin = waitForSerialInput(2).toInt();
mode = waitForSerialInput(1);
if (pin >= NUM_DIGITAL_PINS) Serial.print("e");
else if (mode=="i") {
pinMode(pin, INPUT);
Serial.print("s");
} else if (mode=="o") {
pinMode(pin, OUTPUT);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 'x': // toggle pin mode
Serial.print("x");
if (toggleMode((uint8_t)waitForSerialInput(2).toInt()) == UNKNOWN_PIN) Serial.print("e");
else Serial.print("s");
return;
case 'a': // read analog pin
Serial.print("a");
analogPin = waitForSerialInput(2).toInt();
if (analogPin>=NUM_ANALOG_INPUTS || analogInputToDigitalPin(analogPin)<0){
Serial.print("e");
return;
}
Serial.print(analogRead(analogInputToDigitalPin(analogPin)));
case 'Q':
Serial.print((char)command);
Serial.print("s");
return;
case 'r': // read digital value
Serial.print("r");
pin = waitForSerialInput(2).toInt();
if (pin >= NUM_DIGITAL_PINS) Serial.print("e");
else {
Serial.print(digitalRead(pin));
Serial.print("s");
}
return;
case 'w': // write digital pin
Serial.print("w");
pin = waitForSerialInput(2).toInt();
state = waitForSerialInput(1);
if (pin >= NUM_DIGITAL_PINS) Serial.print("e");
else if (state == "h") {
digitalWrite(pin, HIGH);
Serial.print("s");
} else if (state == "l") {
digitalWrite(pin, LOW);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 'p': // set a pwm pin
Serial.print("p");
pin = waitForSerialInput(2).toInt();
pwmState = waitForSerialInput(3).toInt();
if (digitalPinHasPWM(pin)) {
analogWrite(pin, pwmState);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 't': // toggle a pin
Serial.print("t");
if (toggleState(waitForSerialInput(2).toInt())==UNKNOWN_PIN) Serial.print("e");
else Serial.print("s");
return;
case 'l':
Serial.print("l");
state = waitForSerialInput(1);
if (state=="h") {
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("s");
} else if (state=="l") {
digitalWrite(LED_BUILTIN, LOW);
Serial.print("s");
} else if (state=="t") {
toggleState(LED_BUILTIN);
Serial.print("s");
} else {
Serial.print("e");
}
return;
case 'q':
Serial.print("qs");
while (true) {
}
return;
case 'h':
Serial.print((char)command);
runCommand1Arg((char)command, 0);
return;
case 'm':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
argument1 = waitForSerialInput(1).charAt(0);
runCommand2Arg('m', argument0, argument1);
return;
case 'x':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
runCommand1Arg((char)command, argument0);
return;
case 'l':
Serial.print((char)command);
argument0 = waitForSerialInput(1).charAt(0);
runCommand1Arg((char)command, argument0);
return;
default:
Serial.print((char)command);
Serial.print("\n\rCommand not found.\n\r");
Serial.print("e");
return;
}
}