Hello fellow hackers,
I have gotten a simple terminal program to work on the Arduino. As of yet I have only tested it on the Arduino Uno. Here is the code.
//
// Unos_0_0_1
//
// programmed by George Andrews
//
// (c. 2014)
//
int LED_PIN = 13; // LED pin
int LED_PIN_STATE = LOW; // initial state of LED pin
long LAST_LED_UPDATE = 0; // time of last LED update
unsigned long LED_BLINK_INTERVAL = 500; // time interval for LED to blink
unsigned long TIME_NEEDED = 0;
boolean NewData = false;
char inputChar; // input character
char input[10]; // input buffer
unsigned long CURRENT_TIME = millis();
long LAST_UPDATE = CURRENT_TIME;
int Time(unsigned long TIME_NEEDED)
{
unsigned long INTERVAL = TIME_NEEDED;
if (CURRENT_TIME - LAST_UPDATE > INTERVAL)
{
LAST_UPDATE = CURRENT_TIME;
return 1; // signify that the time is up
}
}
void ReceiveSerialStringWithEndmarker()
{
static byte ndx = 0;
char Endmarker = '\n';
int NumChars = 9;
if (Serial.available() > 0)
{
inputChar = Serial.read();
if (inputChar != Endmarker)
{
input[ndx] = inputChar;
ndx++;
if (ndx >= NumChars)
{
ndx = NumChars - 1;
}
}
else
{
input[ndx] = '\0'; // end string
ndx = 0;
NewData = true;
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT); // LED pin
Serial.println("Welcome...");
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.println("root@unos:~#");
}
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.print("root@unos:");
}
interrupts();
}
void loop()
{
unsigned long time = millis();
ReceiveSerialStringWithEndmarker();
if (NewData)
{
if (input[0] == 's' && input[1] == 'h' && input[2] == 'u' && input[3] == 't' && input[4] == 'd' && input[5] == 'o' && input[6] == 'w' && input[7] == 'n')
{
Serial.println(input);
Serial.println("Shutting down...");
TIME_NEEDED = 30000;
if (Time(TIME_NEEDED) == 1)
{
noInterrupts();
}
}
else if (input[0] == 'r' && input[1] == 'e' && input[2] == 's' && input[3] == 't' && input[4] == 'a' && input[5] == 'r' && input[6] == 't')
{
Serial.println(input);
Serial.print("Restarting...");
TIME_NEEDED = 30000;
if (Time(TIME_NEEDED) == 1)
{
noInterrupts();
}
interrupts();
Serial.println("Welcome...");
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.println("root@unos:~#");
}
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.print("root@unos:");
}
}
else if (input[0] == 'p' && input[1] == 'r' && input[2] == 'i' && input[3] == 'n' && input[4] == 't' && input[5] == ' ')
{
Serial.println(input);
Serial.println(input[6]);
Serial.println("root@unos:~#");
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.print("root@unos:");
}
}
//else if (!strcmp(input,"blink"))
else if (input[0] == 'b' && input[1] == 'l' && input[2] == 'i' && input[3] == 'n' && input[4] == 'k')
{
Serial.println(input);
if (time - LAST_LED_UPDATE > LED_BLINK_INTERVAL)
{
LAST_LED_UPDATE = time;
if (LED_PIN_STATE == LOW)
{
LED_PIN_STATE = HIGH;
}
else
{
LED_PIN_STATE = LOW;
}
digitalWrite(LED_PIN, LED_PIN_STATE);
}
Serial.println("root@unos:~#");
TIME_NEEDED = 3000;
if (Time(TIME_NEEDED) == 1)
{
Serial.print("root@unos:");
}
}
else
{
Serial.println("Error: invalid expression");
Serial.print("root@unos:");
}
NewData = false;
input[0] = '\0';
}
}
Attached is the guide on how to use it.
All suggestions for improvements on the OS and ideas are welcome.