feat: 添加声音序列

This commit is contained in:
bmy
2024-01-08 22:08:43 +08:00
parent 6bb5b9eaad
commit bf76c25f41
5 changed files with 77 additions and 2 deletions

42
app/by_buzzer.c Normal file
View File

@@ -0,0 +1,42 @@
#include "by_buzzer.h"
#include "by_rt_button.h"
#include "zf_common_headfile.h"
#include <string.h>
uint16_t queue_long = 0;
const uint32_t max_long = 40;
uint32_t a[40];
void queue_init(void)
{
memset(a, 0, sizeof(a));
}
void queue_add_element(int element)
{
if (queue_long < max_long) {
a[queue_long] = element;
queue_long += 1;
}
}
void queue_pop_element(void)
{
memmove(a, &a[1], queue_long * sizeof(a));
if (queue_long > 0) {
queue_long--;
}
}
void queue_pop_read(void)
{
while (queue_long != 0) {
pwm_init(TIM8_PWM_MAP0_CH1_C6, a[0], 5000);
queue_pop_element();
system_delay_ms(100);
pwm_set_duty(TIM8_PWM_MAP0_CH1_C6, 0);
}
}
void by_buzzer_init(void)
{
pwm_init(TIM8_PWM_MAP0_CH1_C6, 2000, 0);
}

24
app/by_buzzer.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef _BY_BUZZER_H__
#define _BY_BUZZER_H__
#include "by_rt_button.h"
#include "stdio.h"
#include "ch32v30x.h"
#define BY_PRESS_SHORT 2000
#define BY_PRESS_LONG 2500
#define BY_FORWARD 1500
#define BY_BACKWARD 1800
extern void by_buzzer_init(void);
extern void queue_init(void);
extern void queue_add_element(int element);
extern void queue_pop_element(void);
extern void queue_pop_read(void);
extern uint32_t a[40];
extern uint16_t queue_long;
extern const uint32_t max_long;
extern uint8_t queue_flag;
#endif

View File

@@ -36,6 +36,7 @@
#include "zf_common_headfile.h"
#include "by_rt_button.h"
#include "by_imu.h"
#include "by_buzzer.h"
void NMI_Handler(void) __attribute__((interrupt()));
void HardFault_Handler(void) __attribute__((interrupt()));
@@ -199,9 +200,10 @@ void EXTI9_5_IRQHandler(void)
if (SET == gpio_get_level(E10)) {
rotate_button = rotate_button_backward;
queue_add_element(BY_BACKWARD);
} else {
rotate_button = rotate_button_forward;
queue_add_element(BY_FORWARD);
}
EXTI_ClearITPendingBit(EXTI_Line9);
}
@@ -227,8 +229,11 @@ void EXTI15_10_IRQHandler(void)
time_via = system_get_tick() - time_via;
if (time_via > LONG_PRESS_THRESHOLD_TICK) {
rotate_button = rotate_button_press_long;
queue_add_element(BY_PRESS_LONG);
} else {
rotate_button = rotate_button_press_short;
queue_add_element(BY_PRESS_SHORT);
}
time_via = 0;
EXTI_ClearITPendingBit(EXTI_Line11);

View File

@@ -26,6 +26,7 @@
#include "by_rt_button.h"
#include "by_fan_control.h"
#include "./page/cw_page.h"
#include "by_buzzer.h"
int main(void)
{
@@ -37,6 +38,7 @@ int main(void)
by_gpio_init();
by_exit_init();
by_pwm_init();
by_buzzer_init();
// while (imu660ra_init())
// ;
@@ -45,6 +47,8 @@ int main(void)
while (1) {
Page_Run();
queue_pop_read();
if (mt9v03x_finish_flag) {
// 该操作消耗大概 1970 个 tick折合约 110us