2024-01-08 22:08:43 +08:00
|
|
|
#include "by_buzzer.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2024-02-07 10:24:11 +08:00
|
|
|
#include "zf_common_headfile.h"
|
|
|
|
|
|
2024-04-16 01:49:04 +08:00
|
|
|
#define BUZZER_TIME (100) // 蜂鸣器音节长度
|
|
|
|
|
#define BUZZER_QUEUE_LENGTH (40)
|
2024-02-07 10:24:11 +08:00
|
|
|
|
2024-04-16 01:49:04 +08:00
|
|
|
uint32_t time_out = 0;
|
2024-02-07 10:24:11 +08:00
|
|
|
uint16_t queue_long = 0;
|
|
|
|
|
uint32_t a[40] = {0};
|
2024-01-08 22:08:43 +08:00
|
|
|
|
|
|
|
|
void queue_init(void)
|
|
|
|
|
{
|
2024-03-03 19:23:21 +08:00
|
|
|
queue_long = 0;
|
2024-01-08 22:08:43 +08:00
|
|
|
memset(a, 0, sizeof(a));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void queue_add_element(int element)
|
|
|
|
|
{
|
2024-02-07 10:24:11 +08:00
|
|
|
if (queue_long < BUZZER_QUEUE_LENGTH) {
|
2024-01-08 22:08:43 +08:00
|
|
|
a[queue_long] = element;
|
|
|
|
|
queue_long += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void queue_pop_element(void)
|
|
|
|
|
{
|
2024-04-16 11:14:12 +08:00
|
|
|
memmove(a, &a[1], (queue_long - 1) * sizeof(a[0])); // FIXME 强迫症震怒,仅移动,未清除原位置上的值
|
2024-01-08 22:08:43 +08:00
|
|
|
if (queue_long > 0) {
|
|
|
|
|
queue_long--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void queue_pop_read(void)
|
|
|
|
|
{
|
|
|
|
|
while (queue_long != 0) {
|
2024-01-16 20:03:21 +08:00
|
|
|
pwm_init(BUZZER_PIN, a[0], 5000);
|
2024-01-08 22:08:43 +08:00
|
|
|
queue_pop_element();
|
|
|
|
|
system_delay_ms(100);
|
2024-01-16 20:03:21 +08:00
|
|
|
pwm_set_duty(BUZZER_PIN, 0);
|
2024-01-08 22:08:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void by_buzzer_init(void)
|
|
|
|
|
{
|
2024-02-07 10:24:11 +08:00
|
|
|
queue_init();
|
2024-01-16 20:03:21 +08:00
|
|
|
pwm_init(BUZZER_PIN, 2000, 0);
|
2024-02-07 10:24:11 +08:00
|
|
|
|
|
|
|
|
by_buzzer_add(1250);
|
|
|
|
|
by_buzzer_add(1500);
|
2024-03-04 22:33:43 +08:00
|
|
|
by_buzzer_add(1700);
|
2024-02-07 10:24:11 +08:00
|
|
|
by_buzzer_add(1500);
|
|
|
|
|
by_buzzer_add(1200);
|
2024-03-04 22:33:43 +08:00
|
|
|
by_buzzer_add(1500);
|
2024-02-07 10:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-16 10:54:44 +08:00
|
|
|
// TODO 异步写入问题,不可重入
|
2024-02-07 10:24:11 +08:00
|
|
|
void by_buzzer_add(uint16_t tone)
|
|
|
|
|
{
|
|
|
|
|
queue_add_element(tone);
|
2024-01-08 22:08:43 +08:00
|
|
|
}
|
2024-02-07 14:12:00 +08:00
|
|
|
|
|
|
|
|
void by_buzzer_run(void)
|
|
|
|
|
{
|
2024-04-16 11:14:12 +08:00
|
|
|
if (queue_long > 0) {
|
2024-04-16 01:49:04 +08:00
|
|
|
|
|
|
|
|
if (0 == time_out) {
|
|
|
|
|
pwm_init(BUZZER_PIN, a[0], 5000);
|
|
|
|
|
queue_pop_element();
|
|
|
|
|
time_out = BUZZER_TIME;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-04-16 11:14:12 +08:00
|
|
|
if (0 == time_out)
|
|
|
|
|
pwm_set_duty(BUZZER_PIN, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (time_out) {
|
|
|
|
|
time_out--;
|
2024-02-07 14:12:00 +08:00
|
|
|
}
|
|
|
|
|
}
|