#include "Voice_Play_Buzzer.h"

///////////声音播放顺序////////////////////////////////////////////////////////
typedef enum
{
	Buzzer_Leisure = 0,
	Buzzer_Charge,		   //充电
	Buzzer_Charge_Waiting, //充电 等待

	Buzzer_Play_ON,			//正常播放
	Buzzer_Play_ON_Waiting, //正常播放  等待

	Buzzer_Fade,		 //淡出
	Buzzer_Fade_Waiting, //淡出  等待
} _BuzzerStep;

//具体播放控制结构体
typedef struct
{
	INT8U Buzzer_Variety_Index; //根据当前播放报警对应的音源索引号，计算出在音源属性表中的序号
	INT8U BuzzerAlarmIndex;		//_SoundAlarm   //当前播放的报警索引//和传入的对比决定是否停止
	INT16U BuzzerTimeCount;		//计时声音播放时间变量    //单个声音类型播放时的计时

	INT8U Buzzer_Syllable_Count; //音节计数变量
	INT8U Buzzer_Syllable_MAX;   //当前播放的声音报警 音节数量

} _BuzzerAlarmInfor;

_BuzzerAlarmInfor BuzzerAlarmCurrent; //具体播放声音时控制变量
INT8U Buzzer_Alarm_MAX_Num = 0;		  //外部传入声音报警最大数量
//外部传入//双音节音源的数量//数值上与单音节的第一个索引号相等
INT8U Buzzer_Single_Syllable_First = 0;
INT8U Buzzer_Step = Buzzer_Leisure; //蜂鸣器播放步骤控制变量

/////////////////////////////////////////////////蜂鸣器播放部分开始//////////////////////////////////////////////////////////////////
//蜂鸣器休眠时执行的操作//由外部调用
//强制执行相关操作//不考虑当前是否有声音在播放//外部考虑
//暂时无用，淡出时间结束时会关闭两个引脚
void Buzzer_Play_Sleep(void)
{
	Buzzer_Positive_Enable(0);
	//Buzzer_Negative_Set_Duty(0);
	Buzzer_Negative_Enable(0);
}
//返回当前蜂鸣器播放状态
//0空闲状态，可执行休眠动作//非零，正在播放某个音源，不建议执行休眠动作
INT8U Buzzer_Get_PlayStatus(void)
{
	return Buzzer_Step;
}
//蜂鸣器相关初始化//首次上电调用一次即可
//MAX_NUM //外部传入声音报警最大数量
//Single_First//外部传入//双音节音源的数量//数值上与单音节的第一个索引号相等
void Buzzer_Play_Init(_SoundAlarm MAX_NUM, _BuzzerVariety Single_First)
{

	Buzzer_Alarm_MAX_Num = MAX_NUM; //外部传入声音报警最大数量
	//外部传入//双音节音源的数量//数值上与单音节的第一个索引号相等
	Buzzer_Single_Syllable_First = Single_First;

	Buzzer_Step = Buzzer_Leisure; //蜂鸣器播放步骤控制变量

	BuzzerAlarmCurrent.BuzzerAlarmIndex = Buzzer_Alarm_MAX_Num;
	BuzzerAlarmCurrent.BuzzerTimeCount = 0;
	BuzzerAlarmCurrent.Buzzer_Variety_Index = Buzzer_Variety_MAX + Buzzer_Single_Syllable_First;
	BuzzerAlarmCurrent.Buzzer_Syllable_MAX = 0;
	BuzzerAlarmCurrent.Buzzer_Syllable_Count = 0;
}
///声音播放计时函数 计时//几毫秒调用一次就传入多少
void Buzzer_Play_Time_Count(INT8U Time_NUM)
{
	if (BuzzerAlarmCurrent.BuzzerTimeCount < (65530 - Time_NUM)) //65536
	{
		BuzzerAlarmCurrent.BuzzerTimeCount += Time_NUM;
	}
}

//与闪烁的灯同步//传入信号？
//具体声音播放函数//传入要播放的报警索引//PWM初始化完成之后方可调用
void Buzzer_Play_Handle(_SoundAlarm SoundAlarmNew)
{
	switch (Buzzer_Step)
	{
	case Buzzer_Leisure:
	{
		//执行到这时，之前的声音播放动作肯定结束了播放完整或者LOOP的被打断
		//那么此时无论新来的报警是哪个，与之前播放的相不相同，只要有效就执行
		if (SoundAlarmNew < Buzzer_Alarm_MAX_Num) //声音报警非空
		{
			Buzzer_Step = Buzzer_Charge;
		}
		else
		{
			break;
		}

		BuzzerAlarmCurrent.BuzzerAlarmIndex = SoundAlarmNew;
		BuzzerAlarmCurrent.BuzzerTimeCount = 0;
		BuzzerAlarmCurrent.Buzzer_Syllable_Count = 0;
		//
		//双音节音源对应属性表中的元素序号
		if (BuzzerAlarmVarietyInforTable[BuzzerAlarmCurrent.BuzzerAlarmIndex].Variety < Buzzer_Single_Syllable_First)
		{
			BuzzerAlarmCurrent.Buzzer_Syllable_MAX = 2;
			BuzzerAlarmCurrent.Buzzer_Variety_Index = BuzzerAlarmVarietyInforTable[BuzzerAlarmCurrent.BuzzerAlarmIndex].Variety * 2;
		}
		else //单音节音源对应属性表中的元素序号
		{
			BuzzerAlarmCurrent.Buzzer_Syllable_MAX = 1;
			BuzzerAlarmCurrent.Buzzer_Variety_Index = BuzzerAlarmVarietyInforTable[BuzzerAlarmCurrent.BuzzerAlarmIndex].Variety + Buzzer_Single_Syllable_First;
		}
	}
	case Buzzer_Charge:
	{

		INT16U Fre_buf = 0;
		Buzzer_Negative_Enable(0);
		Buzzer_Positive_Enable(1);

		Fre_buf = BuzzerAttribute[BuzzerAlarmCurrent.Buzzer_Variety_Index + BuzzerAlarmCurrent.Buzzer_Syllable_Count].Frequency;

		Buzzer_Negative_Set_Frequency(Fre_buf);
		Buzzer_Negative_Set_Duty(50);
		Buzzer_Step = Buzzer_Charge_Waiting;
		//break; //如果充电时间是零，那么就不必多余执行一次该函数就能执行到对应步骤
	}
	case Buzzer_Charge_Waiting:
	{
		if (BuzzerAlarmCurrent.BuzzerTimeCount >= BuzzerAttribute[BuzzerAlarmCurrent.Buzzer_Variety_Index + BuzzerAlarmCurrent.Buzzer_Syllable_Count].Charge_Time)
		{
			BuzzerAlarmCurrent.BuzzerTimeCount = 0;
			Buzzer_Step = Buzzer_Play_ON;
		}
		else
		{
			break;
		}
	}

	case Buzzer_Play_ON:
	{
		Buzzer_Negative_Enable(1);
		//Buzzer_Positive_Enable(1);//充电时已经开启
		Buzzer_Play_Start(BuzzerAlarmCurrent.BuzzerAlarmIndex);
		Buzzer_Step = Buzzer_Play_ON_Waiting;
		//break;//如果播放时间为0，就不必多执行一次该函数，可以立即执行到对应步骤
	}
	case Buzzer_Play_ON_Waiting:
	{
		//长鸣音
		if (Buzzer_LoopOnTime == BuzzerAttribute[BuzzerAlarmCurrent.Buzzer_Variety_Index + BuzzerAlarmCurrent.Buzzer_Syllable_Count].PlayON_Time)
		{
			//传入了新报警
			if (BuzzerAlarmCurrent.BuzzerAlarmIndex != SoundAlarmNew)
			{
				BuzzerAlarmCurrent.BuzzerTimeCount = 0;
				Buzzer_Step = Buzzer_Fade;
			}
			else
			{
				break;
			}
		}
		else //单次循环播放的音源
		{
			if (BuzzerAlarmCurrent.BuzzerTimeCount >= BuzzerAttribute[BuzzerAlarmCurrent.Buzzer_Variety_Index + BuzzerAlarmCurrent.Buzzer_Syllable_Count].PlayON_Time)
			{
				BuzzerAlarmCurrent.BuzzerTimeCount = 0;
				Buzzer_Step = Buzzer_Fade;
			}
			else
			{
				break;
			}
		}
	}
	case Buzzer_Fade:
	{
		//Buzzer_Negative_Enable(1);
		Buzzer_Positive_Enable(0); //
		Buzzer_Step = Buzzer_Fade_Waiting;
		//break;
	}
	case Buzzer_Fade_Waiting:
	{
		//即使是长鸣音，也判断淡出时间是否到达
		if (BuzzerAlarmCurrent.BuzzerTimeCount >= BuzzerAttribute[BuzzerAlarmCurrent.Buzzer_Variety_Index + BuzzerAlarmCurrent.Buzzer_Syllable_Count].Fade_Time)
		{
			BuzzerAlarmCurrent.BuzzerTimeCount = 0;
			//单个音节播放完成，关闭蜂鸣器两端引脚，以备下次使用
			Buzzer_Negative_Enable(0);
			//Buzzer_Positive_Enable(0);
			//单个音节播放完成，检查是否还有需要播放的音节
			BuzzerAlarmCurrent.Buzzer_Syllable_Count++;
			//仍有剩余音节需要播放
			if (BuzzerAlarmCurrent.Buzzer_Syllable_Count < BuzzerAlarmCurrent.Buzzer_Syllable_MAX)
			{
				Buzzer_Step = Buzzer_Charge;
			}
			else //所有音节都播放完毕了
			{
				Buzzer_Step = Buzzer_Leisure;
				Buzzer_Play_Done(BuzzerAlarmCurrent.BuzzerAlarmIndex);

				BuzzerAlarmCurrent.BuzzerAlarmIndex = Buzzer_Alarm_MAX_Num;
				BuzzerAlarmCurrent.Buzzer_Variety_Index = Buzzer_Variety_MAX + Buzzer_Single_Syllable_First;
				BuzzerAlarmCurrent.Buzzer_Syllable_MAX = 0;
				BuzzerAlarmCurrent.Buzzer_Syllable_Count = 0;

				//break; //跳出，重新调用该函数时可重新判断当前需要播放的报警
			}
		}
		break;
	}

	default:
	{

		Buzzer_Step = Buzzer_Leisure; //蜂鸣器播放步骤控制变量

		BuzzerAlarmCurrent.BuzzerAlarmIndex = Buzzer_Alarm_MAX_Num;
		BuzzerAlarmCurrent.BuzzerTimeCount = 0;
		BuzzerAlarmCurrent.Buzzer_Variety_Index = Buzzer_Variety_MAX + Buzzer_Single_Syllable_First;
		BuzzerAlarmCurrent.Buzzer_Syllable_MAX = 0;
		BuzzerAlarmCurrent.Buzzer_Syllable_Count = 0;
		break;
	}
	}
}

/////////////////////////////////////////////////蜂鸣器播放部分结束/////////////////////////////////////////////////////////////////
