/**HEADER********************************************************************
* 
* Copyright (c) 2010 Avnet Japan Co.,Ltd (NIKE Borad Project.)
* All Rights Reserved
*
*************************************************************************** 
*
* $FileName: Switch_Task.c$
* $Version : 3.0.8.0$
* $Date    : Nov-24-2008$
*
* Comments:
*
*   
*
*END************************************************************************/

#include "NIKE_demo.h"
#include "event.h"
#include "Signal_Task.h"


/* ------------------------------------------------------------------ 
   --- macro definition                                           --- 
   ------------------------------------------------------------------ */

#define SW_EVENT_SET_LED1_OFF			__BIT(0)
#define SW_EVENT_SET_LED1_FLASH			__BIT(1)
#define SW_EVENT_SET_LED1_ON			__BIT(2)

#define SW_EVENT_SET_LED2_OFF			__BIT(4)
#define SW_EVENT_SET_LED2_FLASH			__BIT(5)
#define SW_EVENT_SET_LED2_ON			__BIT(6)
#define SW_EVENT_SET_PLAY_OFF			__BIT(7)

#define SW_MODE_OFF		0
#define SW_MODE_ON		1
#define SW_MODE_ING		2

#define SW_TIME_2S		(2000/100)

#define SWITCH_MODE_STOP		0
#define SWITCH_MODE_REC			1
#define SWITCH_MODE_PLAY		2

#define LED_DISABLE_MODE		0
#define LED_FLASH_MODE			1
#define LED_ENABLE_MODE			2

/* ------------------------------------------------------------------ 
   --- structure body definition                                  --- 
   ------------------------------------------------------------------ */



/* ------------------------------------------------------------------ 
   --- variable definition                                        --- 
   ------------------------------------------------------------------ */


int led_time;
int led_sel;
uchar get_sw2;
uchar get_sw3;
uchar old_sw2;
uchar old_sw3;

uchar mode_sw2;
uchar mode_sw3;

uchar time_sw2_ing;
uchar time_sw3_ing;

int led1_mode;
int led2_mode;

int Switch_api_mode;

pointer   SwitchEvent_handle;

/* ------------------------------------------------------------------ 
   --- prototype declaration                                      --- 
   ------------------------------------------------------------------ */

void SwitchEvent_Set_LED1_OFF(void);
void SwitchEvent_Set_LED1_FLASH(void);
void SwitchEvent_Set_LED1_ON(void);

void SwitchEvent_Set_LED2_OFF(void);
void SwitchEvent_Set_LED2_FLASH(void);
void SwitchEvent_Set_LED2_ON(void);
void SwitchEvent_Set_PLAY_OFF(void);


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED1_OFF
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED1_OFF(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_OFF);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED1_FLASH
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED1_FLASH(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_FLASH);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED1_ON
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED1_ON(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_ON);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED2_OFF
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED2_OFF(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_OFF);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED2_FLASH
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED2_FLASH(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_FLASH);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_LED2_ON
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_LED2_ON(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_LED1_ON);
}



/*TASK*-----------------------------------------------------------------
*
* Function Name  : SwitchEvent_Set_PLAY_OFF
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void SwitchEvent_Set_PLAY_OFF(void)
{
	_event_set(SwitchEvent_handle, SW_EVENT_SET_PLAY_OFF);
}


/*TASK*-----------------------------------------------------------------
*
* Function Name  : Switch_Poll
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void Switch_Poll(void) 
{
	static _mqx_uint  event_bit;
	//_mqx_int ercd;

	_event_get_value( SwitchEvent_handle, &event_bit);

	if ((event_bit & SW_EVENT_SET_LED1_OFF) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED1_OFF);
		//printf("Event SW_EVENT_SET_LED1_OFF. \n");

		BSP_LED1_CLR();
		led1_mode = LED_DISABLE_MODE;
	}

	if ((event_bit & SW_EVENT_SET_LED1_FLASH) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED1_FLASH);
		//printf("Event SW_EVENT_SET_LED1_FLASH. \n");

		led1_mode = LED_FLASH_MODE;
	}

	if ((event_bit & SW_EVENT_SET_LED1_ON) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED1_ON);
		//printf("Event SW_EVENT_SET_LED1_ON. \n");

		BSP_LED1_SET();
		led1_mode = LED_ENABLE_MODE;
	}


	if ((event_bit & SW_EVENT_SET_LED2_OFF) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED2_OFF);
		//printf("Event SW_EVENT_SET_LED2_OFF. \n");

		BSP_LED2_CLR();
		led2_mode = LED_DISABLE_MODE;
	}

	if ((event_bit & SW_EVENT_SET_LED2_FLASH) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED2_FLASH);
		//printf("Event SW_EVENT_SET_LED2_FLASH. \n");

		led2_mode = LED_FLASH_MODE;
	}

	if ((event_bit & SW_EVENT_SET_LED2_ON) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_LED2_ON);
		//printf("Event SW_EVENT_SET_LED2_ON. \n");

		BSP_LED2_SET();
		led2_mode = LED_ENABLE_MODE;
	}

	if ((event_bit & SW_EVENT_SET_PLAY_OFF) != 0){
		_event_clear( SwitchEvent_handle, SW_EVENT_SET_PLAY_OFF);
		//printf("Event SW_EVENT_SET_PLAY_OFF. \n");

		if (Switch_api_mode == SWITCH_MODE_PLAY){
			SwitchEvent_Set_LED2_OFF();
			Switch_api_mode = SWITCH_MODE_STOP;
		}
	}

	led_time = led_time + 1;

	if (led_time>5){
		led_time = 0;

		if (led_sel == 0){
			led_sel = 1;
		}
		else{
			led_sel = 0;
		}

		if (led1_mode == LED_DISABLE_MODE  && led2_mode == LED_DISABLE_MODE){
			if (led_sel == 0){
				BSP_LED1_CLR();
				BSP_LED2_SET();
			}
			else{
				BSP_LED2_CLR();
				BSP_LED1_SET();
			}
		}
		else{
			if (led1_mode == LED_FLASH_MODE){
				if (led_sel == 0){
					BSP_LED1_CLR();
				}
				else{
					BSP_LED1_SET();
				}
			}

			if (led2_mode == LED_FLASH_MODE){
				if (led_sel == 0){
					BSP_LED2_CLR();
				}
				else{
					BSP_LED2_SET();
				}
			}
		}
	}


	/* SW検出処理 */
	get_sw2 = BSP_GET_SW2();
	get_sw3 = BSP_GET_SW3();

	if (get_sw2 != old_sw2){
		old_sw2 = get_sw2;
		if (get_sw2 == 0){
			if (mode_sw2 != SW_MODE_ON){
				mode_sw2 = SW_MODE_ON;
				time_sw2_ing = SW_TIME_2S;
#if 0
				switch(Switch_api_mode){
					case SWITCH_MODE_STOP:
						SwitchEvent_Set_LED1_FLASH();
						break;

					case SWITCH_MODE_REC:
						break;

					case SWITCH_MODE_PLAY:
						break;

				}
#endif
			}
		}
		else{
			if (mode_sw2 != SW_MODE_OFF){
				mode_sw2 = SW_MODE_OFF;
				time_sw2_ing = 0;
#if 0
				switch(Switch_api_mode){
					case SWITCH_MODE_STOP:
						SwitchEvent_Set_LED1_OFF();
						Switch_api_mode = SWITCH_MODE_STOP;
						break;

					case SWITCH_MODE_REC:
						ercd = SignalEvent_Set_RECODE_REC_STOP();
						SwitchEvent_Set_LED1_OFF();
						Switch_api_mode = SWITCH_MODE_STOP;
						break;

					case SWITCH_MODE_PLAY:
						break;

				}
#endif
			}
		}
	}
	else{
		if(time_sw2_ing != 0){
			time_sw2_ing = time_sw2_ing - 1;
			if (time_sw2_ing==0){
				if (mode_sw2 == SW_MODE_ON){
					/* 長押し検出 */
					mode_sw2 = SW_MODE_ING;
#if 0
					switch(Switch_api_mode){
						case SWITCH_MODE_STOP:
							SwitchEvent_Set_LED1_ON();
							ercd = SignalEvent_Set_RECODE_REC();

							Switch_api_mode = SWITCH_MODE_REC;
							break;

						case SWITCH_MODE_REC:
							break;

						case SWITCH_MODE_PLAY:
							break;
					}
#endif
				}
			}
		}
	}

	if (get_sw3 != old_sw3){
		old_sw3 = get_sw3;

		if (get_sw3 == 0){
			if (mode_sw3 != SW_MODE_ON){
				mode_sw3 = SW_MODE_ON;
				time_sw3_ing = SW_TIME_2S;
#if 0
				switch(Switch_api_mode){
					case SWITCH_MODE_STOP:
						SwitchEvent_Set_LED2_FLASH();
						ercd = SignalEvent_Set_RECODE_PLAY();

						Switch_api_mode = SWITCH_MODE_PLAY;
						break;

					case SWITCH_MODE_REC:
						break;

					case SWITCH_MODE_PLAY:
						break;
				}
#endif
			}
		}
		else{
			if (mode_sw3 != SW_MODE_OFF){
				mode_sw3 = SW_MODE_OFF;
				time_sw3_ing = 0;

#if 0
				switch(Switch_api_mode){
					case SWITCH_MODE_STOP:
						break;

					case SWITCH_MODE_REC:
						break;

					case SWITCH_MODE_PLAY:
						break;
				}
#endif
			}
		}
	}
	else{
		if(time_sw3_ing != 0){
			time_sw3_ing = time_sw3_ing - 1;
			if (time_sw3_ing==0){
				if (mode_sw3 == SW_MODE_ON){
					/* 長押し検出 */
					mode_sw3 = SW_MODE_ING;
#if 0
					switch(Switch_api_mode){
						case SWITCH_MODE_STOP:
							break;

						case SWITCH_MODE_REC:
							break;

						case SWITCH_MODE_PLAY:
							break;
					}
#endif
				}
			}
		}
	}
}



/*TASK*-----------------------------------------------------------------
*
* Function Name  : Switch_Task
* Returned Value : void
* Comments       :
*
*END------------------------------------------------------------------*/
void Switch_Task(uint_32)
{
	_mqx_uint         event_result;

	printf("Start Switch_Task \n");

	led_time = 0;
	led_sel = 0;
	old_sw2 = 0xff;
	old_sw3 = 0xff;
	led1_mode = LED_DISABLE_MODE;
	led2_mode = LED_DISABLE_MODE;
	Switch_api_mode = SWITCH_MODE_STOP;

	/* create event - event.Event */
	event_result = _event_create("Switchevent.Event");
	if (event_result != MQX_OK) { 
		/* event event.Event not be created */
		printf("Switchevent.Event not be created. \n");
	}

	/* open connection to event event.Event */
	event_result = _event_open("Switchevent.Event", &SwitchEvent_handle);
	if (event_result != MQX_OK) { 
		/* could not open event.Event1  */
		printf("could not open Switchevent.Event. \n");
	}

	while (TRUE) {
		Switch_Poll();
		// Delay 100 ms 
		_time_delay(100);
	}
}

