feat: beep.

This commit is contained in:
Ivan Li 2022-08-28 15:09:46 +08:00
parent 3b2497cb7f
commit 84eb469b8b
3 changed files with 33 additions and 24 deletions

View File

@ -53,23 +53,23 @@ impl Beep {
pub fn play(&mut self, ringtone: ringtone::Type) -> anyhow::Result<()> {
if self.state.ringtone != ringtone {
self.state = BeepState::from_ringtone(ringtone);
info!("change: {:?}", ringtone);
} else {
return Ok(());
}
let state = Arc::new(Mutex::new(self.state));
let mut timer = EspTimerService::new()?.timer(move || {
info!("One-shot timer triggered");
match state.lock().as_mut() {
Ok(state) => {
info!("B state.beat: {}", state.beat);
if let Err(err) = Self::play_once(state) {
warn!("{}", err);
}
info!("A state.beat: {}", state.beat);
}
Err(err) => {
warn!("Failed to lock state. {}", err);
}
}
})?;
}).map_err(|err| anyhow::anyhow!("Init Timer Failed. {}", err))?;
timer.every(Duration::from_millis(250))?;
self.watch_timer = Some(timer);
@ -82,6 +82,9 @@ impl Beep {
.map_err(|err| anyhow::anyhow!("Failed to set GPIO 4 as ledc output. {}", err))?;
let hw_timer = unsafe { TIMER0::new() };
let hw_channel = unsafe { CHANNEL0::new() };
if state.ringtone.len() <= state.beat as usize {
state.beat = 0;
}
let curr = state.ringtone[state.beat as usize];
let mut channel = Self::init_channel(pin, hw_timer, hw_channel, if curr < 1000 { 1000.Hz().into()} else {curr.Hz().into()} )
.map_err(|err| anyhow::anyhow!("Failed to initialize channel. {}", err))?;
@ -101,8 +104,8 @@ impl Beep {
}
pub mod ringtone {
pub type Type = [u32; 16];
pub const ADAPTER_DOWN: Type = [2000, 1950, 1900, 1800, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const BATTERY_LOW: Type = [2000, 1950, 0, 0, 1930, 1900, 0, 0, 2000, 1900, 0, 0, 1930, 1900, 0, 0];
pub const ADAPTER_DOWN: Type = [2300, 2000, 2250, 1950, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const BATTERY_LOW: Type = [2000, 1950, 0, 0, 1980, 1900, 0, 0, 2000, 1900, 0, 0, 1980, 1900, 0, 0];
pub const SILENCE: Type = [0; 16];
pub const SHUTDOWN: Type = [2300, 0, 2300, 2000, 2100, 2000, 0, 2300, 2000, 2100, 2000, 1900, 0, 0, 0, 0];
pub const SHUTDOWN: Type = [3450, 3500, 0, 3500, 3050, 3000, 0, 3000, 3050, 1000, 1000, 1000, 0, 0, 0, 0];
}

View File

@ -60,7 +60,29 @@ fn main() {
blink.play();
});
let mut beep = Beep::new();
beep.play(ringtone::ADAPTER_DOWN).expect("Can not beep.");
let _subscription;
if let Some(eventloop) = unsafe { EVENTLOOP.as_mut() } {
_subscription = eventloop
.subscribe(move |message: &VoltageDetectionWorker| {
info!("Event Loop Value");
if let Ok(json_str) = serde_json::to_string(&message) {
if let Err(err) = mq.publish("voltage", json_str.as_bytes()) {
warn!("Can not publish message to MQTT. {}", err);
}
}
if message.battery_voltage < 1000 {
beep.play(ringtone::BATTERY_LOW).expect("Can not beep.");
} else if message.adapter_voltage < 1000 {
beep.play(ringtone::ADAPTER_DOWN).expect("Can not beep.");
} else {
beep.play(ringtone::SILENCE).expect("Can not beep.");
}
})
.expect(" Listening Event Loop Failed");
}
let mut voltage_detection = VoltageDetection::new();
@ -75,21 +97,6 @@ fn main() {
let mut time = Time::new();
time.sync().unwrap();
let _subscription;
if let Some(eventloop) = unsafe { EVENTLOOP.as_mut() } {
_subscription = eventloop
.subscribe(move |message: &VoltageDetectionWorker| {
info!("Event Loop Value");
if let Ok(json_str) = serde_json::to_string(&message) {
if let Err(err) = mq.publish("voltage", json_str.as_bytes()) {
warn!("Can not publish message to MQTT. {}", err);
}
}
})
.expect(" Listening Event Loop Failed");
}
loop {
sleep(Duration::from_millis(1000));
}

View File

@ -55,7 +55,6 @@ impl VoltageDetection {
pub fn watching(&mut self) -> anyhow::Result<()> {
let worker = Arc::new(Mutex::new(self.worker));
let mut timer = EspTimerService::new()?.timer(move || {
info!("One-shot timer triggered");
match worker.lock().as_mut() {
Ok(worker) => {
if let Err(err) = worker.read_once() {