#include "by_buzzer.h" #include #include "zf_common_headfile.h" #define BUZZER_TIME (100) // 蜂鸣器音节长度 #define BUZZER_QUEUE_LENGTH (40) uint32_t time_out = 0; uint16_t queue_long = 0; uint32_t a[40] = {0}; void queue_init(void) { queue_long = 0; memset(a, 0, sizeof(a)); } void queue_add_element(int element) { if (queue_long < BUZZER_QUEUE_LENGTH) { a[queue_long] = element; queue_long += 1; } } void queue_pop_element(void) { memmove(a, &a[1], (queue_long - 1) * sizeof(a[0])); // FIXME 强迫症震怒,仅移动,未清除原位置上的值 if (queue_long > 0) { queue_long--; } } void queue_pop_read(void) { while (queue_long != 0) { pwm_init(BUZZER_PIN, a[0], 5000); queue_pop_element(); system_delay_ms(100); pwm_set_duty(BUZZER_PIN, 0); } } void by_buzzer_init(void) { queue_init(); pwm_init(BUZZER_PIN, 2000, 0); by_buzzer_add(1250); by_buzzer_add(1500); by_buzzer_add(1700); by_buzzer_add(1500); by_buzzer_add(1200); by_buzzer_add(1500); } // TODO 异步写入问题,不可重入 void by_buzzer_add(uint16_t tone) { queue_add_element(tone); } void by_buzzer_run(void) { if (queue_long > 0) { if (0 == time_out) { pwm_init(BUZZER_PIN, a[0], 5000); queue_pop_element(); time_out = BUZZER_TIME; } } else { if (0 == time_out) pwm_set_duty(BUZZER_PIN, 0); } if (time_out) { time_out--; } }