蜂鸣器移植到modules层

This commit is contained in:
chenfu 2023-06-23 17:11:10 +08:00
parent 04975af252
commit f51b6fec3d
2 changed files with 95 additions and 0 deletions

88
modules/alarm/buzzer.c Normal file
View File

@ -0,0 +1,88 @@
#include "bsp_pwm.h"
#include "buzzer.h"
#include "bsp_dwt.h"
static PWMInstance *buzzer;
static alarm_level_e now_alarm_level = ALARM_OFFLINE;
void BuzzerOn(PWMInstance *buzzer );
/**
*
* @brief
* @param alarm_level
*/
void BuzzerPlay(alarm_level_e alarm_level)
{
static alarm_level_e last_alarm_level = ALARM_LEVEL_LOW;
if(((int)DWT_GetTimeline_s() % 5)<1) //每5秒检查一次
{
last_alarm_level = ALARM_LEVEL_LOW;
now_alarm_level = ALARM_OFFLINE;
}
if(last_alarm_level <= now_alarm_level) //如果当前报警级别大于等于上一次报警级别,则更新报警级别
{
now_alarm_level = alarm_level;
}
last_alarm_level = alarm_level;
}
/**
* @brief
*
*/
void buzzer_init()
{
PWM_Init_Config_s buzzer_config = {
.htim = &htim4,
.channel= TIM_CHANNEL_3,
.period = 1,
.pulse = 10000,
.callback = BuzzerOn,
};
buzzer = PWMRegister(&buzzer_config);
}
/**
* @brief
*
* @param buzzer
*/
//*@todo: 优化报警声应类似D__,DDD,B__,BBB等报警声
void BuzzerOn(PWMInstance *buzzer )
{
switch (now_alarm_level)
{
case ALARM_LEVEL_LOW:
PWMSetPeriod(buzzer, 1);
PWMSetPulse(buzzer, 10000);
break;
case ALARM_LEVEL_BELOW_MEDIUM:
PWMSetPeriod(buzzer, 2);
PWMSetPulse(buzzer, 10000);
break;
case ALARM_LEVEL_MEDIUM:
PWMSetPeriod(buzzer, 3);
PWMSetPulse(buzzer, 10000);
break;
case ALARM_LEVEL_ABOVE_MEDIUM:
PWMSetPeriod(buzzer, 4);
PWMSetPulse(buzzer, 10000);
break;
case ALARM_LEVEL_HIGH:
PWMSetPeriod(buzzer, 5);
PWMSetPulse(buzzer, 10000);
break;
default:
PWMSetPulse(buzzer, 0);
break;
}
}

7
modules/alarm/buzzer.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef BUZZER_H
#define BUZZER_H
#include "daemon.h"
void buzzer_init();
#endif // !BUZZER_H