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;
}
void runCommand1Arg(char command, uint8_t argument0){
void runCommand(char command, uint8_t argument0, uint8_t argument1){
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':
Serial.print("\n\rDid you mean 'H'?");
Serial.print("\n\rDid you mean 'H'?\n\re");
return;
case 'x': // toggle pin mode
if (toggleMode(argument0) == UNKNOWN_PIN) Serial.print("e");
@ -144,52 +177,51 @@ void runCommand1Arg(char command, uint8_t argument0){
}
return;
default:
Serial.print("\n\rCommand not found.\n\r");
Serial.print("e");
return;
}
}
void runCommand2Arg(char command, uint8_t argument0, uint8_t argument1){
uint8_t getArgument0(char 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 'm':
return waitForSerialInput(2).toInt();
case 'x':
return waitForSerialInput(2).toInt();
case 'a':
return waitForSerialInput(2).toInt();
case 'r':
return waitForSerialInput(2).toInt();
case 'w':
return waitForSerialInput(2).toInt();
case 'p':
return waitForSerialInput(2).toInt();
case 't':
return waitForSerialInput(2).toInt();
case 'l':
return waitForSerialInput(1).charAt(0);
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(){
uint8_t command = Serial.read();
uint16_t loopCounter;
uint8_t argument0;
uint8_t argument1;
switch ((char)command) {
@ -215,6 +247,10 @@ void getAndRunCommand(){
" H show help\n\r"
" Q lock\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"
" usage: followed by two digit pin number and o or i\n\r"
" set a pin to input or output mode\n\r"
@ -255,57 +291,23 @@ void getAndRunCommand(){
while (true) {
}
return;
case 'h':
case 'L':
Serial.print((char)command);
runCommand1Arg((char)command, 0);
return;
case 'm':
loopCounter = waitForSerialInput(5).toInt();
command = waitForSerialInput(1).charAt(0);
argument0 = getArgument0((char)command);
argument1 = getArgument1((char)command);
Serial.print((char)command);
argument0 = waitForSerialInput(2).toInt();
argument1 = waitForSerialInput(1).charAt(0);
runCommand2Arg((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);
for (loopCounter; loopCounter > 0; loopCounter--){
Serial.print("\n\r");
runCommand((char)command, argument0, argument1);
}
return;
default:
Serial.print((char)command);
Serial.print("\n\rCommand not found.\n\r");
Serial.print("e");
argument0 = getArgument0((char)command);
argument1 = getArgument1((char)command);
runCommand((char)command, argument0, argument1);
return;
}
}
@ -317,7 +319,7 @@ void getAndRunCommand(){
void setup(){
Serial.begin(230400);
// 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(".");
if (i==TX_PIN || i==RX_PIN);
else pinMode(i, INPUT);