#include "menu_interface.h"

void Menu_Control_Init(PlayerTypeDef *player)
{
	GPIO_Switch_Init(&player->SwTheme, GPIO_SwTheme, GPIO_Pin_SwTheme, SW_ON);
	GPIO_Switch_Init(&player->SwPlay, GPIO_SwPlay, GPIO_Pin_SwPlay, SW_ON);
	GPIO_Switch_Init(&player->SwForward, GPIO_SwForward, GPIO_Pin_SwForward, SW_ON);
	GPIO_Switch_Init(&player->SwBackward, GPIO_SwBackward, GPIO_Pin_SwBackward, SW_ON);
	GPIO_Switch_Init(&player->SwSpeed, GPIO_SwSpeed, GPIO_Pin_SwSpeed, SW_ON);
	GPIO_Switch_Init(&player->SwLoop, GPIO_SwLoop, GPIO_Pin_SwLoop, SW_ON);	
	
  player->SwTheme.Sw_FilterDelay = 70;
  player->SwPlay.Sw_FilterDelay = 70;
  player->SwForward.Sw_FilterDelay = 70;
  player->SwBackward.Sw_FilterDelay = 70;
  player->SwSpeed.Sw_FilterDelay = 70;
  player->SwLoop.Sw_FilterDelay = 70;
  
	player->speed = 1;
 
}

extern int menu_white_theme;
void Menu_Control_ReadGPIO(PlayerTypeDef *player)
{
#ifdef GPIO_CONTROL
	/* Обработка кнопки темы меню */
	if(GPIO_Read_Swich(&player->SwTheme))
	{
		if(player->SwTheme.Sw_GrandPrevState == 0)
			menu_white_theme ^= 1;
	}
	
	/* Обработка кнопки плей */
	if(GPIO_Read_Swich(&player->SwPlay))
	{
		if(player->SwPlay.Sw_GrandPrevState == 0)
			player->play ^= 1;
			
		player->pressed_start = 1;
	}
	else
	{
		player->pressed_start = 0;
	}

	/* Обработка кнопки прокрутки вперед */
	if(GPIO_Read_Swich(&player->SwForward))
	{
		player->pressed_forward = 1;
	}
	else
	{
		player->pressed_forward = 0;
	}
	
	/* Обработка кнопки прокрутки назад */
	if(GPIO_Read_Swich(&player->SwBackward))
	{
		player->pressed_backward = 1;
	}
	else
	{
		player->pressed_backward = 0;
	}
	
	/* Обработка кнопки зацикливания */
	if(GPIO_Read_Swich(&player->SwLoop))
	{
		if(player->SwLoop.Sw_GrandPrevState == 0)
			player->loop ^= 1;
	}
	
	/* Обработка кнопки скорости */
	if(GPIO_Read_Swich(&player->SwSpeed))
	{
		if(player->SwSpeed.Sw_GrandPrevState == 0)			
			player->speed += 0.5;
		
		
		if(player->speed > 0.05)
			player->speed = 0.5;
		
	}
#endif
}


float sim_music_long_sec = 10;
float sim_music_step = 0;
void Menu_Control_Music(PlayerTypeDef *player)
{
  if(player->currenttime > 1)
  {
    player->currenttime = 0;
    if(player->loop == 0)
      player->play = 0;
  }
  else if(player->currenttime < 0)
  {
    if(player->loop)
      player->currenttime = 1;
    else
      player->currenttime = 0;
  }
  
  
  sim_music_step = 1/(sim_music_long_sec*100);
  // если шкатулка запущена
  if(player->play)
  {
    // если кнопки перемотки не нажаты, то подтягивает заданную скорость воспроизведения
    if((player->pressed_backward == 0) && (player->pressed_forward == 0) )
    {
      sim_music_step *= player->speed;
    }
  }
  else
  {
    sim_music_step = 0;
  }
  
  // перемотка назад
  if((player->pressed_backward == 1) && (player->pressed_forward == 0) )
  {
    if(player->currenttime > 0)
    {
      sim_music_step = -1/(sim_music_long_sec*100);
    }
  }
  
  // перемотка вперед
  if((player->pressed_forward == 1) && (player->pressed_backward == 0) )
  {
    sim_music_step = 1/(sim_music_long_sec*100)*2;
  }
  
  
  static uint32_t prevtick = 0;
  if(uwTick - prevtick > 10)
  {
    prevtick = uwTick;
    player->currenttime += sim_music_step;
  }
}