/************************* (C) COPYRIGHT 2007 RAISONANCE ******************************
* File Name          :  toggle_with_CircleOS.c
* Author             :  RT
* Date First Issued  :  17/07/07 
* Description        :  toggle implementation with CircleOS
* Revision           :  
****************************************************************************************/

/* Includes ---------------------------------------------------------------------------*/
#include "circle_api.h"
 
/* Private define ---------------------------------------------------------------------*/
#define MAX_TOGGLE_COUNT (0x8)   //this tells the speed of the toggle      
#define NEEDEDVERSION "V 1.0" // Put here the minimal CircleOS version needed by your application

/* Private variable -------------------------------------------------------------------*/
static int CurrentLedMode = LED_OFF;
static int toggle_count=0;

/* Private function --------------------------------------------------------------------*/
enum MENU_code MsgVersion(void);

/* Public variable ---------------------------------------------------------------------*/
const char Application_Name[8+1] = {"toggle"};  // max 8 characters for application name

/*******************************************************************************
* Function Name  : Application_Handler
* Description    : Management of the toggle application
*                 
* Input          : None
* Return         : MENU_CONTINUE
*******************************************************************************/
enum MENU_code Application_Handler ( void ) 
   {
   
   toggle_count++;
  
   if(toggle_count >= MAX_TOGGLE_COUNT)
      {
      toggle_count=0;
   
      CurrentLedMode = (CurrentLedMode == LED_ON) ? LED_OFF : LED_ON;
   
      LED_Set ( LED_GREEN,CurrentLedMode);
      LED_Set ( LED_RED,!CurrentLedMode);
      }

   
    // If button is pushed exit 
   if (  BUTTON_GetState() == BUTTON_PUSHED ) 
      {
      BUTTON_WaitForRelease();   
      BUTTON_SetMode  ( BUTTON_ONOFF_FORMAIN );
      LED_Set ( LED_GREEN,LED_OFF);
      LED_Set ( LED_RED,LED_OFF);
      LCD_SetBackLightOn();
      LCD_SetRotateScreen(1);
      MENU_ClearCurrentCommand();
      DRAW_SetDefaultColor ();
      DRAW_Clear();
      POINTER_SetMode ( POINTER_ON );
      return MENU_LEAVE;
      }
 
   return MENU_CONTINUE;
   }


/*******************************************************************************
* Function Name  : Application_Ini
* Description    : Launch of the toggle application from CircleOS
* Input          : None
* Return         : MENU_CONTINUE_COMMAND
*******************************************************************************/
enum MENU_code Application_Ini ( void ) 
   {
    if(strcmp(UTIL_GetVersion(), NEEDEDVERSION) < 0)
      {
      return MsgVersion();
      }
   
   LCD_SetBackLightOff();
   
   return MENU_CONTINUE_COMMAND;
   }

/*******************************************************************************
* Function Name  : MsgVersion
* Description    : Display the current CircleOS version and the version needed
*                  exit to main menu after 4 secondes
*
* Input          : None
* Return         : MENU_CONTINUE
*******************************************************************************/
enum MENU_code MsgVersion(void)
   {
   int hh,mm,ss,ss2;
   
   DRAW_DisplayString(5,60,"CircleOS",17);
   DRAW_DisplayString(80,60,UTIL_GetVersion(),3);
   DRAW_DisplayString(5,34,NEEDEDVERSION,3);
   DRAW_DisplayString(40,34," required",12);
   
   RTC_GetTime(&hh,&mm,&ss);
   ss = ss + 4;                  // 4 secondes 
   ss = ss%60;
   
   do
      {
      RTC_GetTime(&hh,&mm,&ss2);
      }while (ss2 != ss);           // do while < 4 secondes
   
   return MENU_REFRESH;
   }
