/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：buzzer.c
 * 描  述  ：蜂鸣器驱动程序
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/03/06 
*********************************************************************************/
#include "buzzer.h"
#include "timer.h"

#define BEEP_PeriphClock		RCC_APB2Periph_GPIOB
#define BEEP_PORT						GPIOB
#define BEEP_PIN						GPIO_Pin_2

uint8_t beep_startUpMark = 0;										//开机提示标志
uint8_t beep_shortBeepMark = 0;									//蜂鸣器短响标志
uint8_t beep_longBeepMark = 0;									//蜂鸣器长响标志
uint8_t beep_closeDoorBeepMark = 0;							//蜂鸣器关门响标志
uint8_t beep_warningBeepMark = 0;								//蜂鸣器错误警告提示响标志
	
/*
***********************************************************************
*	函 数 名: KeyInit
*	功能说明: 开关IO口初始化
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void BeepInit(void)
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 	RCC_APB2PeriphClockCmd(BEEP_PeriphClock, ENABLE);	

	
	GPIO_InitStructure.GPIO_Pin = BEEP_PIN;			 				
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	
	GPIO_Init(BEEP_PORT, &GPIO_InitStructure);
	
	GPIO_ResetBits(BEEP_PORT, BEEP_PIN);	
	
	beep_startUpMark = 1;						//标志正在开机启动提示
}

/*
***********************************************************************
*	函 数 名: SetBuzzerStatus
*	功能说明: 设置蜂鸣器工作状态
*	形    参：param -> 设置的状态，0关，1：开
*	返 回 值: 无
***********************************************************************
*/
void SetBuzzerStatus(uint8_t param)
{
	if(param)
	{
		GPIO_SetBits(BEEP_PORT, BEEP_PIN);	
// 		printf("\r\n开蜂鸣器\r\n");
	}else{
		GPIO_ResetBits(BEEP_PORT, BEEP_PIN);
// 		printf("\r\n关蜂鸣器\r\n");
	}
}


/*
***********************************************************************
*	函 数 名: BEEP_ShortBeep
*	功能说明: 蜂鸣器短响
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void BEEP_ShortBeep(void)
{
	static uint8_t beepStatus = 0x00;
	
	static timer beepOnTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&beepOnTimer, 2);
	}
	
	switch(beepStatus)
	{
		case 0x00:
			if(beep_shortBeepMark == 1)
			{
				beep_shortBeepMark = 0;
				SetBuzzerStatus(1);												//打开蜂鸣器
				
				timer_restart(&beepOnTimer);
				beepStatus = 0x01;
				break;
			}
			break;
		
		case 0x01:
			if(timer_expired(&beepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				beepStatus = 0x00;
				break;
			}
			break;
			
		default:
			SetBuzzerStatus(0);												//关闭蜂鸣器
			beepStatus = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BEEP_LongBeep
*	功能说明: 蜂鸣器长响
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void BEEP_LongBeep(void)
{
	static uint8_t beepStatus = 0x00;
	
	static timer beepOnTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&beepOnTimer, 10);
	}
	
	switch(beepStatus)
	{
		case 0x00:
			if(beep_longBeepMark == 1)
			{
				beep_longBeepMark = 0;
				SetBuzzerStatus(1);												//打开蜂鸣器
				
				timer_restart(&beepOnTimer);
				beepStatus = 0x01;
				break;
			}
			break;
		
		case 0x01:
			if(timer_expired(&beepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				beepStatus = 0x00;
				break;
			}
			break;
			
		default:
			SetBuzzerStatus(0);												//关闭蜂鸣器
			beepStatus = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BEEP_StartUpBeep
*	功能说明: 蜂鸣器开机提示声
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void BEEP_StartUpBeep(void)
{
	static uint8_t beepStatus = 0x00;
	
	static timer beepOnTimer1;
	static timer beepOnTimer2;
	static timer beepOffTimer1;
	static timer beepOffTimer2;
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&beepOnTimer1, 1);
		timer_set(&beepOnTimer2, 2);
		timer_set(&beepOffTimer1, 1);
		timer_set(&beepOffTimer2, 2);
	}
	
	switch(beepStatus)
	{
		case 0x00:
			if(beep_startUpMark == 1)
			{
				beep_startUpMark = 0;
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&beepOnTimer1);
				beepStatus = 0x01;
				break;
			}
			break;
		
		case 0x01:
			if(timer_expired(&beepOnTimer1))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&beepOffTimer1);
				beepStatus = 0x02;
				break;
			}
			break;
			
		case 0x02:
			if(timer_expired(&beepOffTimer1))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&beepOnTimer2);
				beepStatus = 0x03;
				break;
			}
			break;
			
		case 0x03:
			if(timer_expired(&beepOnTimer2))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&beepOffTimer2);
				beepStatus = 0x04;
				break;
			}
			break;
			
		case 0x04:
			if(timer_expired(&beepOffTimer2))
			{
				SetBuzzerStatus(1);	
				timer_restart(&beepOnTimer2);
				beepStatus = 0x05;
				break;
			}
			break;
			
		case 0x05:
			if(timer_expired(&beepOnTimer2))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				beep_startUpMark = 0;
				beepStatus = 0x00;
				break;
			}
			break;
			
		default:
			SetBuzzerStatus(0);												//关闭蜂鸣器
			beepStatus = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BEEP_CloseDoorBeep
*	功能说明: 蜂鸣器关门提示声
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void BEEP_CloseDoorBeep(void)
{
	static uint8_t beepStatus = 0x00;
	
	static timer closeDoorBeepOnTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&closeDoorBeepOnTimer, 5);
	}

	switch(beepStatus)
	{
		case 0x00:
			if(beep_closeDoorBeepMark == 1)
			{
				beep_closeDoorBeepMark = 0;
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&closeDoorBeepOnTimer);
				beepStatus = 0x01;
				break;
			}
			break;
			
		case 0x01:
			if(timer_expired(&closeDoorBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&closeDoorBeepOnTimer);
				beepStatus = 0x02;
				break;
			}
			break;
			
		case 0x02:
			if(timer_expired(&closeDoorBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&closeDoorBeepOnTimer);
				beepStatus = 0x03;
				break;
			}
			break;
			
		case 0x03:
			if(timer_expired(&closeDoorBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&closeDoorBeepOnTimer);
				beepStatus = 0x04;
				break;
			}
			break;
			
		case 0x04:
			if(timer_expired(&closeDoorBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&closeDoorBeepOnTimer);
				beepStatus = 0x05;
				break;
			}
			break;
			
		case 0x05:
			if(timer_expired(&closeDoorBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				beep_closeDoorBeepMark = 0;
				beepStatus = 0x00;
				break;
			}
			break;
			
		default:
			SetBuzzerStatus(0);												//关闭蜂鸣器
			beepStatus = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BEEP_WarningBeep
*	功能说明: 蜂鸣器警告提示声
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
static void BEEP_WarningBeep(void)
{
	static uint8_t beepStatus = 0x00;
	
	static timer warningBeepOnTimer;
	static timer warningBeepOffTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&warningBeepOnTimer, 2);
		timer_set(&warningBeepOffTimer, 2);
	}
	
	switch(beepStatus)
	{
		case 0x00:
			if(beep_warningBeepMark == 1)
			{
				beep_warningBeepMark = 0;
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x01;
				break;
			}
			break;
			
		case 0x01:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x02;
				break;
			}
			break;
			
		case 0x02:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x03;
				break;
			}
			break;
			
		case 0x03:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x04;
				break;
			}
			break;
			
		case 0x04:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x05;
				break;
			}
			break;
			
		case 0x05:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x06;
				break;
			}
			break;
			
		case 0x06:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x07;
				break;
			}
			break;
			
		case 0x07:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x08;
				break;
			}
			break;
			
		case 0x08:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(1);												//打开蜂鸣器
				timer_restart(&warningBeepOnTimer);
				beepStatus = 0x09;
				break;
			}
			break;
			
		case 0x09:
			if(timer_expired(&warningBeepOnTimer))
			{
				SetBuzzerStatus(0);												//关闭蜂鸣器
				beep_warningBeepMark = 0;
				beepStatus = 0x00;
			}
			break;
			
		default:
			SetBuzzerStatus(0);												//关闭蜂鸣器
			beep_warningBeepMark = 0;
			beepStatus = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: BeepTask
*	功能说明: 
*	形    参：无
*	返 回 值: 返回对应开关的键值
***********************************************************************
*/
void BeepTask(void)
{	
	BEEP_ShortBeep();
	BEEP_LongBeep();
	BEEP_StartUpBeep();
	BEEP_CloseDoorBeep();
	BEEP_WarningBeep();
}

/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/
