Skip to content

Instantly share code, notes, and snippets.

@xerz-one
Created October 7, 2018 22:49
Show Gist options
  • Save xerz-one/01accd6ceb7bf9371d2649ccdecefca7 to your computer and use it in GitHub Desktop.
Save xerz-one/01accd6ceb7bf9371d2649ccdecefca7 to your computer and use it in GitHub Desktop.
XerzTerm: A serial terminal for controlling an Arduino (Uno)
/*
XerzTerm - A serial terminal for controlling an Arduino (Uno)
Written in 2018 by Francisco Gómez García <espectalll@kydara.com>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along
with this software. If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
*/
String input = "";
bool inputDone = false;
int INPUT_LEN = 128;
void setup() {
Serial.begin(9600);
input.reserve(INPUT_LEN);
Serial.print("# ");
}
void loop() {
if (inputDone) {
String args[INPUT_LEN / 2] = {};
int argsLen;
parse(input, args, argsLen);
exec(args, argsLen);
input = "";
inputDone = false;
Serial.print("# ");
}
}
void serialEvent() {
while (Serial.available()) {
char inputChar = (char)Serial.read();
input += inputChar;
if (inputChar == '\n') {
inputDone = true;
}
}
}
void parse(String &text, String vars[], int &n) {
int pos[INPUT_LEN / 2] = {};
n = 0;
text.trim();
for (int i = text.indexOf(' ', 0);
i != -1; i = text.indexOf(' ', ++i))
{
pos[n] = i;
n++;
}
for (int i = 0; i <= n; i++)
vars[i] = text.substring(
i > 0 ? pos[i - 1] + 1 : 0,
i < n ? pos[i] : text.length());
n++;
}
void exec(String vars[], int items) {
vars[0].toLowerCase();
int PIN = vars[1].toInt();
if (items >= 1 && vars[0] == "echo") {
for (int i = 1; i < items; i++) Serial.print(vars[i] + " ");
Serial.println();
}
if (items >= 3 && vars[0] == "pinmode" &&
PIN > 0 && PIN <= 13)
pinMode(PIN, vars[2] == "output" ? OUTPUT : INPUT);
if (items >= 2 && vars[0] == "on" &&
PIN > 0 && PIN <= 13)
digitalWrite(PIN, HIGH);
if (items >= 2 && vars[0] == "off" &&
PIN > 0 && PIN <= 13)
digitalWrite(PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment