sentry_chassis_hzz/modules/alarm/buzzer.c

98 lines
2.5 KiB
C
Raw Normal View History

2023-06-23 17:11:10 +08:00
#include "bsp_pwm.h"
#include "buzzer.h"
#include "bsp_dwt.h"
2023-07-07 19:27:24 +08:00
#include "string.h"
2023-06-23 17:11:10 +08:00
static PWMInstance *buzzer;
2023-07-07 19:27:24 +08:00
static uint8_t idx;
static BuzzzerInstance *buzzer_list[BUZZER_DEVICE_CNT] = {0};
2023-06-23 17:11:10 +08:00
/**
* @brief
2023-07-07 19:27:24 +08:00
*
2023-06-23 17:11:10 +08:00
*/
void buzzer_init()
{
PWM_Init_Config_s buzzer_config = {
2023-07-07 19:27:24 +08:00
.htim = &htim4,
.channel= TIM_CHANNEL_3,
.dutyratio = 0,
.period = 0.001,
2023-06-23 17:11:10 +08:00
};
buzzer = PWMRegister(&buzzer_config);
}
2023-07-07 19:27:24 +08:00
BuzzzerInstance *BuzzerRegister(Buzzer_config_s *config)
{
if (config->alarm_level > BUZZER_DEVICE_CNT) // 超过最大实例数,考虑增加或查看是否有内存泄漏
while (1)
;
BuzzzerInstance *buzzer_temp = (BuzzzerInstance *)malloc(sizeof(BuzzzerInstance));
memset(buzzer_temp, 0, sizeof(BuzzzerInstance));
buzzer_temp->alarm_level = config->alarm_level;
buzzer_temp->loudness = config->loudness;
buzzer_temp->octave = config->octave;
buzzer_temp->alarm_state = ALARM_OFF;
buzzer_list[config->alarm_level] = buzzer_temp;
return buzzer_temp;
}
void AlarmSetStatus(BuzzzerInstance *buzzer, AlarmState_e state)
2023-06-23 17:11:10 +08:00
{
2023-07-07 19:27:24 +08:00
buzzer->alarm_state = state;
}
void BuzzerTask()
{
BuzzzerInstance *buzz;
for (size_t i = 0; i < BUZZER_DEVICE_CNT; ++i)
2023-06-23 17:11:10 +08:00
{
2023-07-07 19:27:24 +08:00
buzz = buzzer_list[i];
if(buzz->alarm_level > ALARM_LEVEL_LOW)
{
continue;
}
if(buzz->alarm_state == ALARM_OFF)
{
PWMSetDutyRatio(buzzer, 0);
}
else
{
PWMSetDutyRatio(buzzer, buzz->loudness);
switch (buzz->octave)
{
case OCTAVE_1:
PWMSetPeriod(buzzer, (float)1/DoFreq);
break;
case OCTAVE_2:
PWMSetPeriod(buzzer, (float)1/ReFreq);
break;
case OCTAVE_3:
PWMSetPeriod(buzzer, (float)1/MiFreq);
break;
case OCTAVE_4:
PWMSetPeriod(buzzer, (float)1/FaFreq);
break;
case OCTAVE_5:
PWMSetPeriod(buzzer, (float)1/SoFreq);
break;
case OCTAVE_6:
PWMSetPeriod(buzzer, (float)1/LaFreq);
break;
case OCTAVE_7:
PWMSetPeriod(buzzer, (float)1/SiFreq);
break;
}
break;
}
2023-06-23 17:11:10 +08:00
}
2023-07-07 19:27:24 +08:00
}
2023-06-23 17:11:10 +08:00