/********************************************************************************************/
/*  AT_LED.c   -  Corso Wireless CPU ELETTRONICA IN  										*/
/********************************************************************************************/

/***************************************************************************/
/*  File       : at_led.c                                             */
/*-------------------------------------------------------------------------*/
/*  Object     : Customer application                                      */
/*                                                                         */
/*  contents   : Customer main procedures                                  */
/*                                                                         */
/*  Change     :                                                           */
/***************************************************************************/

#include "adl_global.h"


/***************************************************************************/
/*  Mandatory variables                                                    */
/*-------------------------------------------------------------------------*/
/*  wm_apmCustomStackSize                                                  */
/*-------------------------------------------------------------------------*/
/***************************************************************************/
const u16 wm_apmCustomStackSize = 1024;


#define NUM_GPIO 1		// number of GPIOs sets
static adl_ioDefs_t Gpio_Config[NUM_GPIO] = {
	{ADL_IO_GPIO | 0 | ADL_IO_DIR_OUT | ADL_IO_LEV_LOW}
// set GPIO0 as output low
};
static s32 myGpioOut_Handle;		// handle to GPIO0 out
static adl_tmr_t* tim_handle;		// timer handle for led
static u8 timer_led;			// timer led 0 to 1000ms


/**********************************/
/*  Function   : Led_TimerHandler */
/**********************************/
void Led_TimerHandler ( u8 ID, void * Context )
{
   adl_ioDefs_t Gpio_to_write = ADL_IO_GPIO | 0 ;

   TRACE (( 1, "Led timer" ));

   // Read output and toggle led
   if(adl_ioReadSingle( myGpioOut_Handle,&Gpio_to_write))
      // Reset output
	adl_ioWriteSingle ( myGpioOut_Handle, &Gpio_to_write, FALSE );
   else
      // Set output
	adl_ioWriteSingle ( myGpioOut_Handle, &Gpio_to_write , TRUE );
}

/**********************************/
/*  Function   : cmdATLED_Handler */
/**********************************/
void cmdATLED_Handler (adl_atCmdPreParser_t *param)
{
  u16 time_ms;
  timer_led = wm_atoi(ADL_GET_PARAM(param,0));
  TRACE (( 1, "AT+LED ricevuto: Stop led timer" ));
  adl_tmrUnSubscribe( tim_handle,Led_TimerHandler,ADL_TMR_TYPE_100MS);		// Stop timer led

  if(timer_led == 0 || timer_led > 255)
	{
	TRACE (( 1, "Stop led timer" ));
	return;
	}

  time_ms = timer_led * 10;  // trasformo in centinaia di ms (unità base del timer)
  TRACE (( 1, "AT+LED Start led timer:%d s",timer_led ));
  tim_handle = adl_tmrSubscribe( TRUE,time_ms,ADL_TMR_TYPE_100MS,Led_TimerHandler );
}


/**********************************/
/*  Function   : adl_main         */
/**********************************/
void adl_main( adl_InitType_e InitType )
{
	TRACE (( 1, "Subscribe GPIO_0"));
	// Subcribe GPIO0 output
	myGpioOut_Handle = adl_ioSubscribe(NUM_GPIO,Gpio_Config,0,0,0);
	TRACE (( 1, "handler returns %d", myGpioOut_Handle ));


	TRACE (( 1, "AT+LED Subscribe" ));
	// Sottoscrive il comando AT+LED=<parametro>, la maschera ADL_CMD_TYPE_PARA|0X0011 indica che è un comando del tipo AT+cmd=X con un solo parametro ammesso
	adl_atCmdSubscribe("at+led",cmdATLED_Handler,ADL_CMD_TYPE_PARA|0X0011);
}
