Sample Code

This section is a listing of sample code, demonstrating most of the main features of legOS. They are probably overly complicated, but attempt to give a thorough demonstration of the relevant points. Roughly speaking, these are in the order given in the rest of the text.

Code Fragments

These are just fragments, for the most part drawn directly from the Section called Demo.c.

Motor Demo Code

There are four parts to this example. The first while loop does a simple linear speed increase. The second while loop puts the bot into a spin by reversing one motor. The last two statements demonstrate the difference between brake and off. You should use your fingers to test the difference between the two if you run it.

/*This needs to be retabbed :(*/
void motor_driver() 
{
  int rover_speed = 0;

  /*speeds up the bot in linear fashion- watch out!*/
  while(rover_speed<MAX_SPEED) 
{
	  
  motor_a_speed(rover_speed);
	  motor_c_speed(rover_speed);
	  
	  lcd_int(rover_speed);
	  
	  motor_a_dir(fwd);
	  motor_c_dir(fwd);
	  
	  msleep(500);  /*adjust here if you need a finer differentiation*/
      
	  rover_speed+=20;
	}
      
      /*slows down the bot in linear fashion*/
      while(rover_speed>=0)
	{
	  motor_a_speed(rover_speed);
	  motor_c_speed(rover_speed);
      
	  lcd_int(rover_speed);
	  
	  motor_a_dir(fwd);
	  motor_c_dir(rev);
	  
	  msleep(500);  /*adjust here if you need a finer differentiation*/
	  
	  rover_speed-=10;
	}
      
      /*test the brake state with your fingers*/
      cputs("brake");
      motor_a_speed(brake);
      motor_c_speed(brake);
      sleep(3);
      
      /*test the off state with your fingers*/
      cputs("off");
      motor_a_speed(off);
      motor_c_speed(off);
      sleep(3);
    }
}

Light Sensor Demo Code

This assumes that input 2 is a light sensor. If pointed at a relatively bright area, it will display "light" on the LCD, and if pointed at a relatively dark area, it will display "dark."

void light_sensor()
{
  cputs("light");  /*indicate which function we are in*/
  msleep(500); 
  
  ds_active(&SENSOR_2);    /* turn on light sensor LED (active mode)*/
  while(1)
    {
      lcd_int(LIGHT_2);
      msleep(250);

      if (LIGHT_2 < 150)
	{     
	  cputs("dark");
	}
      else
	{
	  cputs("light");
	}
      
      msleep(250);
    }
}

Touch Sensor Demo Code

/*
  the touch sensor example:
  assumes that the sensor is attached at the first input port
  when sensor is not pressed, runs motors forward
  when sensor is pressed, runs motors backward briefly
  also outputs appropriate sensor value
*/

void touch_sensor()
{
  cputs("touch");  /*indicate which function we are in*/
  msleep(500); 

  motor_a_speed(100);
  motor_c_speed(100);

  while(1)
    {
      motor_a_dir(fwd);
      motor_c_dir(fwd);

      cputs(TOUCH_1); /*output sensor value*/
     
      if(TOUCH_1!=0)    /*if sensor has been touched*/
	{
	  motor_a_dir(rev);
	  motor_c_dir(rev);
	  sleep(1);
	}
    }
}

Button Demo Code

This code is a set of wakeup functions that are used elsewhere in the code. Wakeup functions are covered in detail in the Section called Program Flow and Control in LegOS.

/* 
   button press functions
*/

wakeup_t button_press_wakeup(wakeup_t data) {
	return PRESSED(button_state(),data);
}

wakeup_t button_release_wakeup(wakeup_t data) {
	return RELEASED(button_state(),data);
}

This code uses the last fragment for debouncing. execi() is explained in the Section called Program Flow and Control in LegOS.

int  task_swapper() 
{

  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/

  pid2=execi(&motor_driver, 0, NULL, 2, DEFAULT_STACK_SIZE);/*start motor example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN); /*wait for release*/
  kill(t2);                                        /*kill motor example*/
  motor_a_speed(off);                              
  motor_c_speed(off);

  pid3=execi(&light_sensor, O, NULL, 3, DEFAULT_STACK_SIZE);/*start light example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/
  kill(t3);                                        /*kill light example*/
  ds_passive(&SENSOR_2);   /* turn off light sensor LED (passive mode again) */


  pid4=execi(&touch_sensor, 0, NULL, 4, DEFAULT_STACK_SIZE);/*start touch example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/
  kill(t4);                                        /*kill touch example*/

  lcd_clear();
  sleep(1);

  cputs("reset");
  sleep(3);	

  return 0;
}
      

Demo.c

This is (or rather, should be ;) a fully compilable program that, when run, demonstrates all the main functionality of legOS. Just cut and paste it into a file called demo.c and then compile and download it as described in the Section called Compiling a legOS program into a .lx file.

/*
  demo.c: a simple program designed to work with examples from the LegOS HOWTO
  http://legOS.sourceforge.net/HOWTO/
  author: Luis Villa
*/
    
#include <conio.h>
#include <unistd.h>
#include <dbutton.h>
#include <dsensor.h>
#include <dmotor.h>
#include <sys/tm.h>
#include <rom/system.h>


pid_t pid1, pid2, pid3, pid4;

/* 
   button press functions
*/

wakeup_t button_press_wakeup(wakeup_t data) {
	return PRESSED(button_state(),data);
}

wakeup_t button_release_wakeup(wakeup_t data) {
	return RELEASED(button_state(),data);
}

/*
  the touch sensor example:
  assumes that the sensor is attached at the first input port
  when sensor is not pressed, runs motors forward
  when sensor is pressed, runs motors backward briefly
  also outputs appropriate sensor value
*/

void touch_sensor()
{
  cputs("touch");  /*indicate which function we are in*/
  msleep(500); 

  motor_a_speed(100);
  motor_c_speed(100);

  while(1)
    {
      motor_a_dir(fwd);
      motor_c_dir(fwd);

      cputs(TOUCH_1); /*output sensor value*/
     
      if(TOUCH_1!=0)    /*if sensor has been touched*/
	{
	  motor_a_dir(rev);
	  motor_c_dir(rev);
	  sleep(1);
	}
    }
}


/*
  the light sensor example:
  assumes that the sensor is attached at the second input port
  if pointed at a relatively bright area, will display "light"
  if pointed at a relatively dark area, will display "dark" 
  Based on a suggestion from Richard Franks
*/

void light_sensor()
{
  cputs("light");  /*indicate which function we are in*/
  msleep(500); 
  
  ds_active(&SENSOR_2);    /* turn on light sensor LED (active mode)*/
  while(1)
    {
      lcd_int(LIGHT_2);
      msleep(250);

      if (LIGHT_2 < 150)
	{     
	  cputs("dark");
	}
      else
	{
	  cputs("light");
	}
      
      msleep(250);
    }
}

/* 
   the motor example:
   the first while loop does a simple linear speed increase
   the second while loop puts the bot into a spin
   the final two statements demonstrate brake and off- 
       use your fingers to test the difference between the two.
   the whole thing loops until PROG is pushed (see task_swapper() for control of this)
*/

void motor_driver() 
{
  int rover_speed;
  while(1)
    {
      rover_speed=0; /*realign here, otherwise you start at a negative value*/

      /*speeds up the bot in linear fashion- watch out!*/
      while(rover_speed<MAX_SPEED) 
	{
	  
	  motor_a_speed(rover_speed);
	  motor_c_speed(rover_speed);
	  
	  lcd_int(rover_speed);
	  
	  motor_a_dir(fwd);
	  motor_c_dir(fwd);
	  
	  msleep(500);  /*adjust here if you need a finer differentiation*/
      
	  rover_speed+=20;
	}
      
      /*slows down the bot in linear fashion*/
      while(rover_speed>=0)
	{
	  motor_a_speed(rover_speed);
	  motor_c_speed(rover_speed);
      
	  lcd_int(rover_speed);
	  
	  motor_a_dir(fwd);
	  motor_c_dir(rev);
	  
	  msleep(500);  /*adjust here if you need a finer differentiation*/
	  
	  rover_speed-=10;
	}
      
      /*test the brake state with your fingers*/
      cputs("brake");
      motor_a_speed(brake);
      motor_c_speed(brake);
      sleep(3);
      
      /*test the off state with your fingers*/
      cputs("off");

      motor_a_speed(off);
      motor_c_speed(off);
      sleep(3);
    }
}

int  task_swapper() 
{

  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/

  pid2=execi(&motor_driver, 0, NULL, 2, DEFAULT_STACK_SIZE);/*start motor example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN); /*wait for release*/
  kill(t2);                                        /*kill motor example*/
  motor_a_speed(off);                              
  motor_c_speed(off);

  pid3=execi(&light_sensor, O, NULL, 3, DEFAULT_STACK_SIZE);/*start light example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/
  kill(t3);                                        /*kill light example*/
  ds_passive(&SENSOR_2);   /* turn off light sensor LED (passive mode again) */


  pid4=execi(&touch_sensor, 0, NULL, 4, DEFAULT_STACK_SIZE);/*start touch example*/
  wait_event(&button_press_wakeup,BUTTON_RUN); /*wait for PROGRAM button*/
  wait_event(&button_release_wakeup,BUTTON_RUN);/*wait for release*/
  kill(t4);                                        /*kill touch example*/

  lcd_clear();
  sleep(1);

  cputs("reset");
  sleep(3);	

  return 0;
}

int main() 
{    
  pid1=execi(&task_swapper, 0, NULL, 1, DEFAULT_STACK_SIZE);  
  return 0;
}