Skip to content

Commit

Permalink
完善buzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleGuest committed May 27, 2024
1 parent f06b92d commit edcd5c7
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 151 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@
- [ ] 对打球
- [ ] ...

## 接线

### MPU6050

| MPU6050 | MCU |
| ------- | ----- |
| VCC | 3.3V |
| GND | GND |
| SCL | GPIO5 |
| SDA | GPIO4 |

### 无缘蜂鸣器

| 陶瓷片无缘蜂鸣器 | MCU |
| ---------------- | ------ |
| V+ | GPIO11 |
| V- | GND |

### WS2812

| WS2812 | MCU |
| ------ | ----- |
| IN | GPIO3 |
| V+ | 3.3V |
| V- | GND |

## 三方库

- https://github.com/smart-leds-rs/ws2812-spi-rs,修改了部分源码
1 change: 1 addition & 0 deletions cube/src/bagua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl BaGua {
.any(|(x, y)| !(-0.3..=0.3).contains(&x) && !(-0.3..=0.3).contains(&y))
{
app.ledc.write_bytes(Self::random());
app.buzzer.dice().await;
}
Timer::after_millis(800).await;

Expand Down
158 changes: 91 additions & 67 deletions cube/src/buzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,89 @@ use embassy_time::Timer;
use esp_hal::{
gpio::{GpioPin, Output, PushPull},
ledc::{
channel::{Channel, ChannelIFace},
LowSpeed,
channel::{self, config::PinConfig, Channel, ChannelIFace},
timer::{self, TimerIFace},
LowSpeed, LEDC,
},
prelude::_fugit_RateExtU32,
};
use log::info;

use crate::RNG;

/// 蜂鸣器
pub struct Buzzer<'d> {
pub open: bool,
// ledc: LEDC<'d>,
channel: Channel<'d, LowSpeed, GpioPin<Output<PushPull>, 8>>,
pin: GpioPin<Output<PushPull>, 11>,
ledc: LEDC<'d>,
}

impl<'d> Buzzer<'d> {
pub fn new(
// ledc: LEDC<'d>,
channel: Channel<'d, LowSpeed, GpioPin<Output<PushPull>, 8>>,
) -> Self {
pub fn new(ledc: LEDC<'d>, pin: GpioPin<Output<PushPull>, 11>) -> Self {
Self {
open: true,
// ledc,
channel,
ledc,
pin,
}
}

pub fn open(&mut self) {
fn open(&mut self) {
self.open = true;
}

pub fn close(&mut self) {
fn close(&mut self) {
self.open = false;
}

/// 发声
/// frequency: 发声频率,单位HZ
/// duration: 发声时长,负数表示一直发声,单位微妙
pub fn tone(&mut self, frequency: u64, duration: u64) {
if !self.open {
return;
}
pub fn change(&mut self) {
self.open = !self.open
}

self.channel
.start_duty_fade(0, 100, duration as u16)
/// FIXME: esp_hal::ledc 暂时仅支持固定频率输出,不同频率需要重新配置定时器和通道
fn drive(&mut self, frequency: u32, duty_pct: u8) {
// 定时器配置:指定 PWM 信号的频率和占空比分辨率
let mut lstimer0 = self.ledc.get_timer::<LowSpeed>(timer::Number::Timer0);
lstimer0
.configure(timer::config::Config {
duty: timer::config::Duty::Duty13Bit,
clock_source: timer::LSClockSource::APBClk,
frequency: frequency.Hz(),
})
.unwrap();
// 通道配置:绑定定时器和输出 PWM 信号的 GPIO
let mut channel0 = self
.ledc
.get_channel(channel::Number::Channel0, &mut self.pin);
channel0
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct,
pin_config: PinConfig::PushPull,
})
.unwrap();
while self.channel.is_duty_fade_running() {}
}

///停止发声
/// 发声
/// frequency: 发声频率,单位HZ
/// duration: 发声时长,单位毫秒
pub async fn tone(&mut self, frequency: u32, duration: u64) {
self.drive(frequency, 50);
Timer::after_millis(duration).await;
self.no_tone();
}

/// 停止发声
pub fn no_tone(&mut self) {
self.close();
self.drive(1, 0);
}

/// 菜单选择音效
pub async fn menu_select(&mut self) {
if self.open {
// self.tone(1500, 100);
self.tone(1500, 500);
}
Timer::after_millis(200).await;
}

/// 菜单选择音效2
pub async fn menu_select_2(&mut self) {
if !self.open {
return;
}
self.tone(100, 50);
self.tone(1500, 500).await;
self.no_tone();
Timer::after_millis(50).await;
}

/// 菜单确认音效
Expand All @@ -81,7 +94,7 @@ impl<'d> Buzzer<'d> {
return;
}
for i in (400..2000).step_by(100) {
self.tone(i, 50);
self.tone(i, 50).await;
Timer::after_millis(10).await;
}
}
Expand All @@ -92,7 +105,7 @@ impl<'d> Buzzer<'d> {
return;
}
for i in (200..=3000).rev().step_by(200) {
self.tone(i, 50);
self.tone(i, 50).await;
Timer::after_millis(10).await;
}
}
Expand All @@ -103,15 +116,26 @@ impl<'d> Buzzer<'d> {
return;
}
for i in (200..=3000).rev().step_by(400) {
self.tone(i, 50);
self.tone(i, 50).await;
Timer::after_millis(10).await;
}
}

/// 骰子音效
pub async fn dice(&mut self) {
if !self.open {
return;
}
for i in (200..=3000).rev().step_by(400) {
self.tone(i, 50).await;
Timer::after_millis(10).await;
}
}

/// 迷宫移动音效
pub async fn maze_move(&mut self) {
if self.open {
self.tone(5000, 50);
self.tone(5000, 50).await;
Timer::after_millis(50).await;
self.no_tone();
} else {
Expand All @@ -121,31 +145,31 @@ impl<'d> Buzzer<'d> {

/// 休眠开启音效
pub async fn hibernation(&mut self) {
self.tone(8000, 100);
self.tone(8000, 100).await;
Timer::after_millis(100).await;
self.tone(2500, 100);
self.tone(2500, 100).await;
Timer::after_millis(100).await;
self.tone(800, 100);
self.tone(800, 100).await;
Timer::after_millis(100).await;
self.no_tone();
}

/// 开机音效
pub async fn power_on(&mut self) {
self.tone(800, 200);
self.tone(800, 200).await;
Timer::after_millis(200).await;
self.tone(2500, 100);
self.tone(2500, 100).await;
Timer::after_millis(100).await;
self.tone(8000, 200);
self.tone(8000, 200).await;
Timer::after_millis(200).await;
self.no_tone();
}

/// 唤醒音效
pub async fn wakeup(&mut self) {
self.tone(1500, 200);
self.tone(1500, 200).await;
Timer::after_millis(200).await;
self.tone(8000, 200);
self.tone(8000, 200).await;
Timer::after_millis(200).await;
self.no_tone();
}
Expand All @@ -155,15 +179,14 @@ impl<'d> Buzzer<'d> {
if !self.open {
return;
}
self.tone(8000, 50);
Timer::after_millis(50).await;
self.tone(8000, 50).await;
self.no_tone();
}

/// 沙漏像素反弹音效
pub async fn timer_pixel_rebound(&mut self) {
if self.open {
self.tone(4000, 50);
self.tone(4000, 50).await;
Timer::after_millis(50).await;
self.no_tone();
} else {
Expand All @@ -177,7 +200,7 @@ impl<'d> Buzzer<'d> {
return;
}
for _ in 0..3 {
self.tone(6000, 100);
self.tone(6000, 100).await;
// lc.bitmap(all_led_on);
// lc.UpLoad();
Timer::after_millis(1).await;
Expand All @@ -190,7 +213,7 @@ impl<'d> Buzzer<'d> {
// lc.setIntensity(2);
// lc.bitmap(all_led_on);
// lc.UpLoad();
self.tone(6000, 150);
self.tone(6000, 150).await;
Timer::after_millis(50).await;
// lc.clearDisplay();
Timer::after_millis(100).await;
Expand All @@ -205,17 +228,17 @@ impl<'d> Buzzer<'d> {
}
// lc.setIntensity(3);
Timer::after_millis(100).await;
self.tone(2000, 1000);
self.tone(2000, 1000).await;
Timer::after_millis(15).await;
self.no_tone();
// lc.setIntensity(0);
Timer::after_millis(50).await;
self.tone(3000, 1000);
self.tone(3000, 1000).await;
Timer::after_millis(15).await;
self.no_tone();
// lc.setIntensity(3);
Timer::after_millis(25).await;
self.tone(2000, 1000);
self.tone(2000, 1000).await;
Timer::after_millis(15).await;
self.no_tone();
// lc.setIntensity(0);
Expand All @@ -226,49 +249,50 @@ impl<'d> Buzzer<'d> {
if !self.open {
return;
}
self.tone(500, 1000);
self.tone(500, 1000).await;
Timer::after_millis(160).await;
self.tone(300, 1000);
self.tone(300, 1000).await;
Timer::after_millis(160).await;
self.tone(100, 1000);
self.tone(100, 1000).await;
Timer::after_millis(200).await;
self.no_tone();
}

/// 休眠音效
pub fn sleep(&mut self) {
pub async fn sleep(&mut self) {
if !self.open {
return;
}
self.tone(6000, 50);
self.tone(6000, 50).await;
}

/// 休眠音效2
pub fn sleep2(&mut self) {
pub async fn sleep2(&mut self) {
if !self.open {
return;
}
self.tone(
unsafe {
CubeRng(RNG.assume_init_mut().random() as u64).random_range(3000..=9000) as u64
CubeRng(RNG.assume_init_mut().random() as u64).random_range(3000..=9000) as u32
},
50,
);
)
.await;
}

/// 眨眼音效
pub fn blinky(&mut self) {
pub async fn blinky(&mut self) {
if !self.open {
return;
}
self.tone(8000, 50);
self.tone(8000, 50).await;
}

/// 眨眼音效2
pub fn blinky2(&mut self) {
pub async fn blinky2(&mut self) {
if !self.open {
return;
}
self.tone(5000, 50);
self.tone(5000, 50).await;
}
}
1 change: 1 addition & 0 deletions cube/src/dice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl Dice {
.any(|(x, y)| !(-0.3..=0.3).contains(&x) && !(-0.3..=0.3).contains(&y))
{
app.ledc.write_bytes(Self::random());
app.buzzer.dice().await;
}
Timer::after_millis(800).await;

Expand Down
Loading

0 comments on commit edcd5c7

Please sign in to comment.