#include "buzzer.h"


void play_ones(uint16_t freq,uint8_t dutya)
{
	/*
	TIM4->ARR  = (uint32_t)((2000000/freq)-1);
	TIM4->CCR3 = (uint32_t)(((2000000/freq)-1)*((float)dutya/100));
	*/
	if(freq!=0)
	{
		buzzer_t;
	}else
	{
		buzzer_io(0);
	}
}

struct notes
{
	uint16_t freq;
	uint8_t duty;
	uint16_t deley;
	struct notes *next_note;
};

struct notes *notes;

void add_a_note(uint16_t freq,uint8_t duty,uint16_t deley)
{
    struct notes* buff;   
    buff = (struct notes*)malloc(sizeof(struct notes));
    if (buff != NULL)
    {
        buff->freq = freq;
				buff->duty = duty;
				buff->deley = deley;
        buff->next_note = NULL;

        if (notes == NULL)
        {
            notes = buff;
        }
        else
        {
            struct notes *t = notes;
            while (t->next_note != NULL)
            {
                t = t->next_note;
            }
            t->next_note = buff;
        }
    }
}

void delhead()
{
    if (notes == NULL)
    {
        return;
    }

    if (notes->next_note == NULL)
    {
        free(notes);
        notes = NULL;
    }
    else
    {
        struct notes *t = notes;
        notes = notes->next_note;
        free(t);

    }

}



char buzzer_play_server()
{

	static char busy_flag=0;
	static uint32_t play_delay;
	
	if(notes == NULL)
	{
		play_ones(0,0);
	}else
	{
		if(busy_flag==0)
		{
			busy_flag=1;
			play_delay=HAL_GetTick()+notes->deley;			
		}
		if(busy_flag==1)
		{
			if(HAL_GetTick()>play_delay)
			{
				busy_flag=0;
				delhead();
			}
		}
		play_ones(notes->freq,notes->duty);
	}
return busy_flag;
}
void input_misue(const int *d,uint16_t len,uint8_t duty)
{
	len=len/2;
	for(uint16_t a=0;a<len;a++)
	{
		add_a_note(d[a*2],duty,d[(a*2)+1]);
		//while(buzzer_play_server());
	}

}
