RandomUsefulStuff/manual_control/manual_control.ino

145 lines
4.3 KiB
C++

const int pinCount = 13;
String color_null ="\033[0m";
String color_red ="\033[0;31m";
String color_blue ="\033[1;34m";
String color_green="\033[0;32m";
String color_cyan ="\033[0;36m";
int pins[pinCount];
void setPin(int pinNumber){
if (getPin(pinNumber)==LOW){
digitalWrite(pinNumber, HIGH);
pins[pinNumber-1]=HIGH;
}else{
digitalWrite(pinNumber, LOW);
pins[pinNumber-1]=LOW;
}
}
void setPin(int pinNumber, int pinStatus){
digitalWrite(pinNumber, pinStatus);
pins[pinNumber-1]=pinStatus;
}
int getPin(int pinNumber){
return pins[pinNumber-1];
}
void setup() { //initial setup and reset
for (int i=1; i<pinCount+1; i++){ //set all pins and their corresponding array element LOW
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
pins[i]=LOW;
} // setup serial connection
Serial.begin(115200);
Serial.print(color_null+"-=[ Arduino Serial Manual Pin Control ]=-\n\r");
}
void loop() {
Serial.print(">");
char lastChar=(char) 0;
String string = "";
while ((int)lastChar!=13){
lastChar=Serial.read();
if ((int)lastChar!=-1){
string = string + lastChar;
Serial.write(lastChar);
}
}
Serial.print("\n");
int pin = 0;
pin = string.toInt();
if (pin>pinCount){ //But I want to use Port 32768!
Serial.print("Error: Not supported.\n\r");
}else if (pin<1){ //filter non-pin-number input
if (string.indexOf("help")==0){ //lock the current pin settings
Serial.print("All commands:\n\r");
Serial.print(color_blue +"[Command ]"+color_null+"|"+color_blue+"[Description ]\n\r");
Serial.print(color_cyan +" help "+color_null+"| display this information\n\r");
Serial.print(color_cyan +" high "+color_null+"| sets a pin high (asks for pin number separately)\n\r");
Serial.print(color_cyan +" low "+color_null+"| sets a pin low (asks for pin number separately)\n\r");
Serial.print(color_cyan +" read "+color_null+"| reads the state of a pin (asks for pin number separately)\n\r");
Serial.print(color_cyan +" reset "+color_null+"| run the \"setup\" function again\n\r");
Serial.print(color_cyan +" stop "+color_null+"| starts an infinite loop used to lock the state of the controller\n\r");
Serial.print(color_green+"<any number>"+color_null+"| toggle that pin\n\r");
Serial.print(color_red+"\n\rWarning:"+color_null+" This program has only rudimentary error handling.\n\r Therefore you might end up having unpredictable results when something\n\r goes wrong.\n\r");
delay(100);
}else if (string.indexOf("stop")==0){ //lock the current (pin) state
Serial.print("Locking state... Good bye!\n\r");
delay(100);
Serial.end();
delay(100);
while (true){
;
}
}else if (string.indexOf("reset")==0){ //Well, does this even need an explanation?
Serial.print("Resetting...\n\r");
delay(100);
Serial.end();
delay(100);
setup();
} else if (string.indexOf("high")==0){ // set a pin high
Serial.print("Which pin? ");
String s = "";
char c=(char)0;
while ((int)c!=13){
c=Serial.read();
if ((int)c!=-1){
s = s + c;
Serial.write(c);
}
}
setPin(s.toInt(),HIGH);
Serial.print("\r\n");
} else if (string.indexOf("read")==0){ // set a pin high
Serial.print("Which pin? ");
String s = "";
char c=(char)0;
while ((int)c!=13){
c=Serial.read();
if ((int)c!=-1){
s = s + c;
Serial.write(c);
}
}
Serial.print("\n\rPin ");
Serial.print(s);
Serial.print(": ");
Serial.print(getPin(s.toInt()));
Serial.print("\n\r");
} else if (string.indexOf("low")==0){ // set a pin low
Serial.print("Which pin? ");
String s = "";
char c=(char)0;
while ((int)c!=13){
c=Serial.read();
if ((int)c!=-1){
s = s + c;
Serial.write(c);
}
}
setPin(s.toInt(),LOW);
Serial.print("\r\n");
} else {
Serial.print("Unknown command.\n\rFor a list of available commands type \"help\".\n\r");
}
}else{
setPin(pin);
Serial.print("Pin ");
Serial.print(pin);
Serial.print(": ");
Serial.print(getPin(pin));
Serial.print("\n\r");
}
}