Car/Peripheral/Src/buzzer.c
2024-07-19 22:48:14 +08:00

62 lines
1.3 KiB
C

#include "buzzer.h"
#include "timer.h"
#define OFF 0
#define ON 1
#define ONCE 2
static uint8_t state;
static uint8_t loop_times;
static uint16_t loop_interval;
static uint16_t beep_time;
inline void BUZZER_Start()
{
state = ON;
HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET);
}
inline void BUZZER_Stop()
{
HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET);
if (state == ONCE)
TIMER_DelLoopEvent(EVENT_BUZZER);
state = OFF;
}
void BUZZER_StartTimed(uint8_t time)
{
state = ONCE;
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Stop, time);
}
void BUZZER_Callback();
void BUZZER_StartNTimes(uint8_t loop, uint16_t interval, uint16_t time)
{
loop_times = loop;
loop_interval = interval;
beep_time = time;
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, time);
}
void BUZZER_Callback()
{
switch (state)
{
case ON:
BUZZER_Stop();
if (--loop_times)
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, loop_interval);
else
TIMER_DelLoopEvent(EVENT_BUZZER);
break;
case OFF:
BUZZER_Start();
TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Callback, beep_time);
break;
}
}