/* File: abro-rcx_data.c
 *
 * Descr: Implementation of external C functions for I/O
 *
 * Author: Jan Lukoschus
 * Date: 2002-06-25
 */

/* Interface to esterel generated code */
#include "abro-rcx.h"

/* legOS definitions */

#include <conio.h>
#include <dbutton.h>
#include <dmotor.h>
#include <dsensor.h>
#include <dsound.h>
#include <sys/battery.h>
#include <sys/lcd.h>
#include <sys/time.h>
#include <unistd.h>

void ABRO_O_LCD_INT(int i)
{
  lcd_int(i);
}

void ABRO_O_LCD_STRING(char *s)
{
  if (s) { cputs(s); }
}

note_t notes[] = {{0, 2}, {PITCH_END, 0}};

void ABRO_O_BEEP(int s)
{
  if ((0<s) && (s<PITCH_MAX))
    {
      notes[0].pitch  = s;
      dsound_play(notes);
    }
}

int ABRO_S_LIGHT()
{
  return LIGHT_2;
}

void initialize_inputs()
{
  if (TOUCH_1)        { ABRO_I_TOUCH1();      }
  if (TOUCH_3)        { ABRO_I_TOUCH3();      }

  {
    int key = ~dbutton();

//    if (key & BUTTON_VIEW)    { ABRO_I_BUTTON_VIEW(); }
    if (key & BUTTON_PROGRAM) { ABRO_I_BUTTON_PRGM(); }
  }
}

int main()
{
  /* Initialization of light sensor */
  ds_active(&SENSOR_2); // enable active light sensing
  msleep(100);          // let values settle

  ABRO_reset(); // reset automaton

  /* loop for automaton execution */
  do
    {
      msleep(P);  // wait one time cycle
      initialize_inputs(); // put values on all inputs
    }
  while (ABRO());

  ds_passive(&SENSOR_2); // shut down active light sensing

  return 0;
}
