Unos 0.0.1 - A Simple OS for the Arduino

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.

Sorry, but apparently I couldn't attach the guide because the forum doesn't support .docx extension.

goodinventor:
Sorry, but apparently I couldn't attach the guide because the forum doesn't support .docx extension.

A document in plain .txt format, or a PDF would be more generally readable

...R

Sorry for the delay. Attached is the guide.

Unos 0.0.1 Guide.txt (2.17 KB)

In order to save people the trouble of copying, pasting, and making a new program, I attach the complete code here.

Unos_0_0_1.ino (3.6 KB)

Users, please give me feedback so I can know what works well. I would also like to know if it works on other Arduino boards. Any feedback or new idea is welcome.

Attention fellow users, I will be posting the updated version: Unos 0.0.2. Will keep you all posted. Once again, please report to me any issues or ideas with the program. Thank you all.

As promised the new and improved version of Unos is now available: Unos 0.0.2. All new ideas and tips are welcome. Attached is the guide and the code itself.

Unos 0.0.2 Guide.txt (2.19 KB)

Here is the code.

Unos_0_0_2.ino (4.45 KB)

Unos is intentionally kept small in order for it to run on even an Arduino board with limited memory.

andint Pin[12] = {2,3,4,5,6,7,8,9,10,11,12,13};seem to me to be at odds.

Serial.println("Welcome...");Complete waste of RAM.

     if (!gotValidCommand)
     {
       Serial.println("Invalid Command");
       Serial.println("root@unos:~#");
       Serial.print("root@unos:");
     }

Ditto.

#define NUMBER_OF_COMMANDS 5 
...
char *command[NUMBER_OF_COMMANDS] = {"shutdown", "restart", "print", "pinwrite"};

{cough}

Despite the thread title, this is not an OS; at best, it is a rehash of Firmata and its ilk.

Please stop cross-posting.

But they were separate topics, since they were separate programs. AWOL, currently it doesn't look like much because it has hardly developed yet. Right now there is only one really useful command.

AWOL, I see that you have found a way to improve on memory. I'll try it on the next version and be more careful when using memory.

Right now there is only one really useful command.

. . . and one potentially dangerous initialisation

 for(int i = 0; i < 12; i++)
  {
    pinMode(Pin[i], OUTPUT);
  }

Sorry, but I cannot resist temptation any longer ...

What you have is NOT an operating system. I'm not even sure what it is, or for whom it might be useful.

I favour the idea of newcomers using the Arduino system to learn to program and do useful (or fun) things with a microprocessor. I don't see how your system assists that learning.

An operating system is a program (or collection of programs) that runs invisibly in the background to manage and share the resources on a computer between several applications and perhaps between several users.

To my mind a key feature of any Arduino operating system would be that it does not use either of the functions setup() or loop() so that the user can use them in the normal way. Perhaps another way of saying this is that the OS should be a library containing useful functions to make the rest of the programming experience easier.

And I have to add my opinion that a key strength of the Arduino is that it does NOT have an operating system to get in the way of managing attached hardware.

...R

Robin2:
And I have to add my opinion that a key strength of the Arduino is that it does NOT have an operating system to get in the way of managing attached hardware.

Amen.

I stayed in 'lurk' mode where I was waiting for Unos release 0.0.3 (in a new thread of course) which would hopefully have 'ls' and 'df' command. :stuck_out_tongue:

rockwallaby, I am sure you have been waiting for such commands. I'll probably try to cut down on memory in the next release, then after that I'll start using interrupt handlers and memory-related commands. This program is not a library. It is designed to make simple tasks possible to execute without direct programming.

It is designed to make simple tasks possible to execute without direct programming.

Have you looked at Firmata?
No real sense in reinventing the wheel.

Yes, I have. It is rather limited, though.

goodinventor:
Yes, I have. It is rather limited, though.

SimpleDigitalFirmata sketch 4830 bytes (which allows the user to set their own pinModes correctly) vs. Unos 0.0.2 5230 bytes.