#include "OUTPUT.h"

#define BUZZER_GPIO GPIOC
#define BUZZER_PIN GPIO_Pin_14
#define RELAY_GPIO GPIOB
#define RELAY_PIN GPIO_Pin_0

static void BUZZER(uint32_t state);

static uint32_t buzzer_timer=0;

void BUZZER_Init(void)
{
	GPIO_InitTypeDef GP;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
	GP.GPIO_Pin = BUZZER_PIN;
	GP.GPIO_Mode = GPIO_Mode_OUT;
	GP.GPIO_OType = GPIO_OType_PP;
	GP.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_Init(BUZZER_GPIO, &GP);
	GPIO_ResetBits(BUZZER_GPIO,BUZZER_PIN);
}

void RELAY_Init(void)
{
	GPIO_InitTypeDef GP;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	GP.GPIO_Pin = RELAY_PIN;
	GP.GPIO_Mode = GPIO_Mode_OUT;
	GP.GPIO_OType = GPIO_OType_PP;
	GP.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_Init(RELAY_GPIO, &GP);
	GPIO_ResetBits(RELAY_GPIO,RELAY_PIN);
}

static void BUZZER(uint32_t state)
{
	if(state)GPIO_SetBits(BUZZER_GPIO,BUZZER_PIN);
	else GPIO_ResetBits(BUZZER_GPIO,BUZZER_PIN);
}

void RELAY(uint32_t state)
{
	if(state)GPIO_SetBits(RELAY_GPIO,RELAY_PIN);
	else GPIO_ResetBits(RELAY_GPIO,RELAY_PIN);
}

void Beep (uint32_t milliseconds)
{
	buzzer_timer=milliseconds;
}

void BUZZER_PROCESS(void)
{
	if(buzzer_timer)
		{
		buzzer_timer--;
		BUZZER(1);
		}
	else BUZZER(0);
}
