#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; } /// @brief 启动蜂鸣器鸣叫 /// @param time 鸣叫时间,单位1毫秒 void BUZZER_StartTimed(uint16_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; } }