added "loop" feature

master
BodgeMaster 2021-05-10 07:16:16 +02:00
parent 920a000304
commit 22b72bc423
1 changed files with 86 additions and 84 deletions

View File

@ -101,10 +101,43 @@ String waitForSerialInput(uint8_t bytes){
return string; return string;
} }
void runCommand1Arg(char command, uint8_t argument0){ void runCommand(char command, uint8_t argument0, uint8_t argument1){
switch (command) { switch (command) {
case 'm': // set pin mode
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;
case 'h': case 'h':
Serial.print("\n\rDid you mean 'H'?"); Serial.print("\n\rDid you mean 'H'?\n\re");
return; return;
case 'x': // toggle pin mode case 'x': // toggle pin mode
if (toggleMode(argument0) == UNKNOWN_PIN) Serial.print("e"); if (toggleMode(argument0) == UNKNOWN_PIN) Serial.print("e");
@ -144,52 +177,51 @@ void runCommand1Arg(char command, uint8_t argument0){
} }
return; return;
default: default:
Serial.print("\n\rCommand not found.\n\r");
Serial.print("e");
return; return;
} }
} }
void runCommand2Arg(char command, uint8_t argument0, uint8_t argument1){ uint8_t getArgument0(char command){
switch (command) { switch (command) {
case 'm': // set pin mode case 'm':
Serial.print(argument1); return waitForSerialInput(2).toInt();
if (argument0 >= NUM_DIGITAL_PINS) Serial.print("e"); case 'x':
else if ((char)argument1=='i') { return waitForSerialInput(2).toInt();
pinMode(argument0, INPUT); case 'a':
Serial.print("s"); return waitForSerialInput(2).toInt();
} else if ((char)argument1=='o') { case 'r':
pinMode(argument0, OUTPUT); return waitForSerialInput(2).toInt();
Serial.print("s"); case 'w':
} else { return waitForSerialInput(2).toInt();
Serial.print("e"); case 'p':
} return waitForSerialInput(2).toInt();
return; case 't':
case 'w': // write digital pin return waitForSerialInput(2).toInt();
if (argument0 >= NUM_DIGITAL_PINS) Serial.print("e"); case 'l':
else if ((char)argument1 == 'h') { return waitForSerialInput(1).charAt(0);
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: default:
return; return 0;
}
}
uint8_t getArgument1(char command){
switch (command) {
case 'm':
return waitForSerialInput(1).charAt(0);
case 'w':
return waitForSerialInput(1).charAt(0);
case 'p':
return waitForSerialInput(3).toInt();
default:
return 0;
} }
} }
void getAndRunCommand(){ void getAndRunCommand(){
uint8_t command = Serial.read(); uint8_t command = Serial.read();
uint16_t loopCounter;
uint8_t argument0; uint8_t argument0;
uint8_t argument1; uint8_t argument1;
switch ((char)command) { switch ((char)command) {
@ -215,6 +247,10 @@ void getAndRunCommand(){
" H show help\n\r" " H show help\n\r"
" Q lock\n\r" " Q lock\n\r"
" lock the Arduino's state by running an infinite loop\n\r" " lock the Arduino's state by running an infinite loop\n\r"
" L loop\n\r"
" usage: followed by a 5 digit uint16_t number,\n\r"
" then one of the lowercas commands with arguments\n\r"
" repeat the command given as an argument\n\r"
" m set mode\n\r" " m set mode\n\r"
" usage: followed by two digit pin number and o or i\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" " set a pin to input or output mode\n\r"
@ -255,57 +291,23 @@ void getAndRunCommand(){
while (true) { while (true) {
} }
return; return;
case 'h': case 'L':
Serial.print((char)command); Serial.print((char)command);
runCommand1Arg((char)command, 0); loopCounter = waitForSerialInput(5).toInt();
return; command = waitForSerialInput(1).charAt(0);
case 'm': argument0 = getArgument0((char)command);
argument1 = getArgument1((char)command);
Serial.print((char)command); Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt(); for (loopCounter; loopCounter > 0; loopCounter--){
argument1 = waitForSerialInput(1).charAt(0); Serial.print("\n\r");
runCommand2Arg((char)command, argument0, argument1); runCommand((char)command, argument0, argument1);
return; }
case 'x':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
runCommand1Arg((char)command, argument0);
return;
case 'a':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
runCommand1Arg((char)command, argument0);
return;
case 'r':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
runCommand1Arg((char)command, argument0);
return;
case 'w':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
argument1 = waitForSerialInput(1).charAt(0);
runCommand2Arg((char)command, argument0, argument1);
return;
case 'p':
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
argument1 = waitForSerialInput(3).toInt();
runCommand2Arg((char)command, argument0, argument1);
return;
case 't':
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; return;
default: default:
Serial.print((char)command); Serial.print((char)command);
Serial.print("\n\rCommand not found.\n\r"); argument0 = getArgument0((char)command);
Serial.print("e"); argument1 = getArgument1((char)command);
runCommand((char)command, argument0, argument1);
return; return;
} }
} }
@ -317,7 +319,7 @@ void getAndRunCommand(){
void setup(){ void setup(){
Serial.begin(230400); Serial.begin(230400);
// set all pins to input and low // set all pins to input and low
for (uint8_t i; i<=NUM_DIGITAL_PINS; i++) { for (uint8_t i=0; i<=NUM_DIGITAL_PINS; i++) {
Serial.print("."); Serial.print(".");
if (i==TX_PIN || i==RX_PIN); if (i==TX_PIN || i==RX_PIN);
else pinMode(i, INPUT); else pinMode(i, INPUT);