Car/Peripheral/Src/buzzer.c

65 lines
1.3 KiB
C
Raw Permalink Normal View History

2024-07-18 11:29:28 +08:00
#include "buzzer.h"
2024-07-21 08:13:13 +08:00
2024-07-18 11:29:28 +08:00
#include "timer.h"
2024-07-19 22:48:14 +08:00
#define OFF 0
#define ON 1
#define ONCE 2
2024-07-18 11:29:28 +08:00
static uint8_t state;
2024-07-19 22:48:14 +08:00
static uint8_t loop_times;
static uint16_t loop_interval;
static uint16_t beep_time;
2024-07-18 11:29:28 +08:00
inline void BUZZER_Start()
{
2024-07-19 22:48:14 +08:00
state = ON;
2024-07-18 11:29:28 +08:00
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);
2024-07-19 22:48:14 +08:00
if (state == ONCE)
2024-07-19 12:33:18 +08:00
TIMER_DelLoopEvent(EVENT_BUZZER);
2024-07-19 22:48:14 +08:00
state = OFF;
2024-07-18 11:29:28 +08:00
}
2024-07-21 08:13:13 +08:00
/// @brief 启动蜂鸣器鸣叫
/// @param time 鸣叫时间单位1毫秒
void BUZZER_StartTimed(uint16_t time)
2024-07-18 11:29:28 +08:00
{
2024-07-19 22:48:14 +08:00
state = ONCE;
2024-07-18 11:29:28 +08:00
BUZZER_Start();
2024-07-19 22:48:14 +08:00
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;
}
2024-07-18 11:29:28 +08:00
}