v0.2:重写项目。 #6

Merged
Ivan merged 20 commits from v0.2 into master 2022-09-12 11:58:32 +08:00
3 changed files with 33 additions and 24 deletions
Showing only changes of commit 84eb469b8b - Show all commits

View File

@ -53,23 +53,23 @@ impl Beep {
pub fn play(&mut self, ringtone: ringtone::Type) -> anyhow::Result<()> { pub fn play(&mut self, ringtone: ringtone::Type) -> anyhow::Result<()> {
if self.state.ringtone != ringtone { if self.state.ringtone != ringtone {
self.state = BeepState::from_ringtone(ringtone); self.state = BeepState::from_ringtone(ringtone);
info!("change: {:?}", ringtone);
} else {
return Ok(());
} }
let state = Arc::new(Mutex::new(self.state)); let state = Arc::new(Mutex::new(self.state));
let mut timer = EspTimerService::new()?.timer(move || { let mut timer = EspTimerService::new()?.timer(move || {
info!("One-shot timer triggered");
match state.lock().as_mut() { match state.lock().as_mut() {
Ok(state) => { Ok(state) => {
info!("B state.beat: {}", state.beat);
if let Err(err) = Self::play_once(state) { if let Err(err) = Self::play_once(state) {
warn!("{}", err); warn!("{}", err);
} }
info!("A state.beat: {}", state.beat);
} }
Err(err) => { Err(err) => {
warn!("Failed to lock state. {}", err); warn!("Failed to lock state. {}", err);
} }
} }
})?; }).map_err(|err| anyhow::anyhow!("Init Timer Failed. {}", err))?;
timer.every(Duration::from_millis(250))?; timer.every(Duration::from_millis(250))?;
self.watch_timer = Some(timer); 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))?; .map_err(|err| anyhow::anyhow!("Failed to set GPIO 4 as ledc output. {}", err))?;
let hw_timer = unsafe { TIMER0::new() }; let hw_timer = unsafe { TIMER0::new() };
let hw_channel = unsafe { CHANNEL0::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 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()} ) 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))?; .map_err(|err| anyhow::anyhow!("Failed to initialize channel. {}", err))?;
@ -101,8 +104,8 @@ impl Beep {
} }
pub mod ringtone { pub mod ringtone {
pub type Type = [u32; 16]; 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 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, 1930, 1900, 0, 0, 2000, 1900, 0, 0, 1930, 1900, 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 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(); blink.play();
}); });
let mut beep = Beep::new(); let mut beep = Beep::new();
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."); 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(); let mut voltage_detection = VoltageDetection::new();
@ -75,21 +97,6 @@ fn main() {
let mut time = Time::new(); let mut time = Time::new();
time.sync().unwrap(); 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 { loop {
sleep(Duration::from_millis(1000)); sleep(Duration::from_millis(1000));
} }

View File

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