
#include "Sound_Scheduler.h"

uint8_t   SoundEnableCode[SND_TOTAL_NUMBER];
SoundSchedulingStruct       SoundScheduling;
SoundSeatbeltCtrlStruct     SoundSeatbelt;

void Sound_Scheduler_Init(void)
{
  uint8_t i;

  for (i = 0; i < SND_TOTAL_NUMBER; i++)
    SoundEnableCode[i] = 0;

  Sound_Clear();

  SoundScheduling.Current = SND_NONE;
  SoundScheduling.Next    = SND_NONE;
  SoundScheduling.ReqCode = 0;
  SoundScheduling.StopReq = 0;

  SoundSeatbelt.DrMode  = SND_SEATBELT_DISABLE;
  SoundSeatbelt.DrTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
  SoundSeatbelt.PaMode = SND_SEATBELT_DISABLE;
  SoundSeatbelt.PaTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
}


//ReqCode：请求码,0 - 表示无效请求,不请求播放声音
//                非0值 - 请求播放声音,该请求码将被记录,对于只播放一次的声音,如果与上一次请求播放该声音的请求码相同
//                        声音就不会再被重复播放

void Sound_Request(uint8_t Sound, uint8_t ReqCode)
{
  uint8_t PriorityRef;
  uint8_t PriorityNew;
 
  if ((Sound < SND_TOTAL_NUMBER) && (ReqCode))
  {
    if ((SoundEnableCode[Sound] != ReqCode) || (SoundList[Sound].Type != SND_TYPE_NORMAL))
    {
    /*
      if (SoundList[Sound].Type == SND_TYPE_SEATBELT) //安全带报警音处理
      {
        if (Sound == SND_FASTEN_SEATBELT_DR)
          SoundSeatbelt.DrMode = SND_SEATBELT_CONTINUOUS;
        else if (Sound == SND_FASTEN_SEATBELT_DR_120s)
        {
          if (SoundSeatbelt.DrMode == SND_SEATBELT_TIME_UP)
            return;

          SoundSeatbelt.DrMode = SND_SEATBELT_TIMING;
        }
        else if (Sound == SND_FASTEN_SEATBELT_PA)
          SoundSeatbelt.PaMode = SND_SEATBELT_CONTINUOUS;
        else if (Sound == SND_FASTEN_SEATBELT_PA_120s)
        {
          if (SoundSeatbelt.PaMode == SND_SEATBELT_TIME_UP)
            return;

          SoundSeatbelt.PaMode = SND_SEATBELT_TIMING;
        }

        Sound = SND_FASTEN_SEATBELT_DR;
      }
     */
      if (SoundScheduling.Next < SND_TOTAL_NUMBER)    //如果已有即将要播放的声音
      {
        if (Sound != SoundScheduling.Next)
        {
          PriorityRef = Sound_Priority_Query(SoundList[SoundScheduling.Next].Src);
          PriorityNew = Sound_Priority_Query(SoundList[Sound].Src);

          if (PriorityNew < PriorityRef)
          {
            SoundScheduling.Next = Sound;             //请求的声音具有更高的优先级
            SoundScheduling.ReqCode = ReqCode;
          }
          else if (PriorityNew == PriorityRef)
          {
            if (SoundList[Sound].Type == SND_TYPE_RADAR)
            {
              SoundScheduling.Next = Sound;           //同优先级雷达声可相互打断
              SoundScheduling.ReqCode = ReqCode;
            }
          }
        }
      }
      else                                            //如果没有即将要播放的声音
      {
        if (Sound != SoundScheduling.Current)
        {
          PriorityRef = Sound_Priority_Query(SND_SRC_CURRENT);
          PriorityNew = Sound_Priority_Query(SoundList[Sound].Src);

          if (PriorityNew < PriorityRef)
          {
            SoundScheduling.Next = Sound;             //请求的声音具有更高的优先级
            SoundScheduling.ReqCode = ReqCode;
          }
          else if (PriorityNew == PriorityRef)
          {
            if (SoundList[Sound].Type == SND_TYPE_RADAR)
            {
              SoundScheduling.Next = Sound;           //同优先级雷达声可相互打断
              SoundScheduling.ReqCode = ReqCode;
            }
          }
        }
      }
    }
  }
}

void Sound_Delete(uint8_t Sound)
{
  if (Sound < SND_TOTAL_NUMBER)
  {
    /*
    if (SoundList[Sound].Type == SND_TYPE_SEATBELT) //安全带报警音处理
    {
      if (Sound == SND_FASTEN_SEATBELT_DR)
      {
        if (SoundSeatbelt.DrMode == SND_SEATBELT_CONTINUOUS)
          SoundSeatbelt.DrMode = SND_SEATBELT_DISABLE;
      }
      else if (Sound == SND_FASTEN_SEATBELT_DR_120s)
      {
        if ((SoundSeatbelt.DrMode == SND_SEATBELT_TIMING) || (SoundSeatbelt.DrMode == SND_SEATBELT_TIME_UP))
        {
          SoundSeatbelt.DrMode  = SND_SEATBELT_DISABLE;
          SoundSeatbelt.DrTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
        }
      }
      else if (Sound == SND_FASTEN_SEATBELT_PA)
      {
        if (SoundSeatbelt.PaMode == SND_SEATBELT_CONTINUOUS)
          SoundSeatbelt.PaMode = SND_SEATBELT_DISABLE;
      }
      else if (Sound == SND_FASTEN_SEATBELT_PA_120s)
      {
        if ((SoundSeatbelt.PaMode == SND_SEATBELT_TIMING) || (SoundSeatbelt.PaMode == SND_SEATBELT_TIME_UP))
        {
          SoundSeatbelt.PaMode = SND_SEATBELT_DISABLE;
          SoundSeatbelt.PaTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
        }
      }

      if ((SoundSeatbelt.DrMode == SND_SEATBELT_DISABLE) && \
          (SoundSeatbelt.PaMode == SND_SEATBELT_DISABLE))
        Sound = SND_FASTEN_SEATBELT_DR;
      else
        return;
    }
    */
    if (SoundEnableCode[Sound])                      //是已播放过的声音
    {
      if (Sound == SoundScheduling.Current)
      {
        SoundScheduling.StopReq = 1;
        return;
      }

      if (Sound == SoundScheduling.Next)
        SoundScheduling.Next = SND_NONE;

      SoundEnableCode[Sound] = 0;
    }
  }
}

//50ms
void Sound_Scheduling_Service(void)
{
  //处理声音停止请求
  if (SoundScheduling.StopReq)
  {
    if (SoundScheduling.Current < SND_TOTAL_NUMBER)
    {
      if (Sound_Stop(SoundList[SoundScheduling.Current].Src) == 0)
      {
        SoundScheduling.Current = SND_NONE;
        SoundScheduling.StopReq = 0;
        SoundEnableCode[SoundScheduling.Current] = 0;
      }
    }
  }

  //使用查询优先级方法查看当前声音是否还在播放
  if (SoundScheduling.Current < SND_TOTAL_NUMBER)
  {
    if (Sound_Priority_Query(SND_SRC_CURRENT) == 0xFF)
      SoundScheduling.Current = SND_NONE;
  }

  //播放请求的声音
  if (SoundScheduling.Next < SND_TOTAL_NUMBER)
  {
    if (SoundScheduling.Current < SND_TOTAL_NUMBER)   //如果当前有声音正在播放,则停止播放该声音
      Sound_Stop(SoundList[SoundScheduling.Current].Src);

    if (Sound_Play(SoundList[SoundScheduling.Next].Src) == 0)             //如果请求的声音播放成功
    {
      SoundEnableCode[SoundScheduling.Next] = SoundScheduling.ReqCode;    //声音已播放
      SoundScheduling.Current = SoundScheduling.Next;
      SoundScheduling.Next    = SND_NONE;
    }
  }

/*
  //安全带声音计时
  if (SoundScheduling.Current == SND_FASTEN_SEATBELT_DR)
  {
    if (SoundSeatbelt.DrMode == SND_SEATBELT_TIMING)
    {
      if (SoundSeatbelt.DrTimer)
        SoundSeatbelt.DrTimer--;
      else
      {
        SoundSeatbelt.DrMode  = SND_SEATBELT_TIME_UP;
        SoundSeatbelt.DrTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
      }
    }

    if (SoundSeatbelt.PaMode == SND_SEATBELT_TIMING)
    {
      if (SoundSeatbelt.PaTimer)
        SoundSeatbelt.PaTimer--;
      else
      {
        SoundSeatbelt.PaMode = SND_SEATBELT_TIME_UP;
        SoundSeatbelt.PaTimer = (uint16_t)(SND_SEATBELT_120s_TIME / 50);
      }
    }

    if (((SoundSeatbelt.DrMode == SND_SEATBELT_DISABLE)  || \
         (SoundSeatbelt.DrMode == SND_SEATBELT_TIME_UP)) && \
        ((SoundSeatbelt.PaMode == SND_SEATBELT_DISABLE)  || \
         (SoundSeatbelt.PaMode == SND_SEATBELT_TIME_UP)))
      SoundScheduling.StopReq = 1;
  }
  */
}
