Go Down

Topic: Unos 0.0.1 - A Simple OS for the Arduino (Read 22314 times) previous topic - next topic

goodinventor

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.

Code: [Select]

//
// 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.

goodinventor

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

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

goodinventor

Sorry for the delay. Attached is the guide.

goodinventor

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

goodinventor

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.

goodinventor

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.

goodinventor

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.

goodinventor


AWOL

#9
Jan 05, 2015, 08:42 am Last Edit: Jan 05, 2015, 09:36 am by AWOL
Quote
Unos is intentionally kept small in order for it to run on even an Arduino board with limited memory.
and
Code: [Select]
int Pin[12] = {2,3,4,5,6,7,8,9,10,11,12,13};seem to me to be at odds.

Code: [Select]
Serial.println("Welcome...");Complete waste of RAM.
Code: [Select]
     if (!gotValidCommand)
     {
       Serial.println("Invalid Command");
       Serial.println("root@unos:~#");
       Serial.print("root@unos:");
     }
Ditto.

Code: [Select]
#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.

goodinventor

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.

goodinventor

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.

AWOL

Quote
Right now there is only one really useful command.
. . . and one potentially dangerous initialisation
Code: [Select]
 for(int i = 0; i < 12; i++)
  {
    pinMode(Pin[i], OUTPUT);
  }

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

AWOL

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.

Go Up