From c5d382fafe33f9eb9d768395ee0a5054fb5763d8 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 8 May 2022 14:48:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ADC=20=E9=87=87=E6=A0=B7=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=AD=90=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E8=9C=82=E9=B8=A3=E5=99=A8=E6=8A=A5=E8=AD=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/beep.rs | 42 +++++++++++++++++++++++++++++++++++------- src/main.rs | 9 ++++++--- src/manager.rs | 25 ++++++++++++++++++++----- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/beep.rs b/src/beep.rs index 0e87338..dd2771b 100644 --- a/src/beep.rs +++ b/src/beep.rs @@ -11,6 +11,8 @@ type LedcChannel = Channel, P>; pub struct Beep { state: bool, stopped: bool, + beat: u8, + ringtone: ringtone::Type, channel: LedcChannel, } @@ -20,6 +22,8 @@ impl Beep { state: false, stopped: false, channel: Self::init_channel(pin, timer, channel)?, + beat: 0, + ringtone: ringtone::SILENCE, }); } @@ -41,16 +45,40 @@ impl Beep { } } - pub fn play(&mut self) { + pub fn play(&mut self, rx: &mut std::sync::mpsc::Receiver) { loop { - if self.stopped { - break; + let curr_ringtone = rx.try_recv().unwrap_or_else(|_| self.ringtone); + if !curr_ringtone.eq(&mut self.ringtone) { + self.beat = 0; + self.ringtone = curr_ringtone; } - self.toggle().unwrap(); - thread::sleep(Duration::from_millis(500)); - self.toggle().unwrap(); - thread::sleep(Duration::from_millis(1500)); + let curr = curr_ringtone[self.beat as usize]; + if curr { + self.channel.set_duty(50).expect("Failed to set duty"); + } else { + self.channel.set_duty(0).expect("Failed to set duty"); + } + thread::sleep(Duration::from_millis(100)); + + self.beat += 1; + if self.beat == 16 { + self.beat = 0; + } } } } +pub mod ringtone { + + pub type Type = [bool; 16]; + + pub const POWER_DOWN: Type = [ + true, true, true, true, false, false, false, false, false, false, false, false, false, + false, false, false, + ]; + pub const BATTERY_LOW: Type = [ + true, true, false, false, true, true, false, false, true, true, false, false, true, true, + false, false, + ]; + pub const SILENCE: Type = [false; 16]; +} diff --git a/src/main.rs b/src/main.rs index d2a74fd..e0aa80b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use esp_idf_sys as _; -use std::{thread, time::Duration}; +use std::{thread, time::Duration, sync::mpsc}; mod beep; mod blink; @@ -26,6 +26,8 @@ fn main() { let adapter_pin = peripherals.pins.gpio1; let battery_pin = peripherals.pins.gpio2; + let (tx, mut rx) = mpsc::channel(); + println!("Starting screen"); let display = screen::Screen::new(i2c0, gpio9, gpio10).expect("Failed to create screen"); @@ -46,10 +48,10 @@ fn main() { ledc_channel0, ) .expect("Failed to create beep"); - beep.play(); + beep.play(&mut rx); }); - let mut dc_out_ctl = dc_out_controller::DcOutController::new( + let dc_out_ctl = dc_out_controller::DcOutController::new( dc_out_ctl_pin .into_output() .expect("Failed to set GPIO3 as output"), @@ -60,6 +62,7 @@ fn main() { adc1, adapter_pin.into_analog_atten_11db().expect("Failed to set GPIO1 as analog input"), battery_pin.into_analog_atten_11db().expect("Failed to set GPIO2 as analog input"), + tx, ).expect("Failed to create manager"); loop { manager.handling_once().expect("Failed to handle once"); diff --git a/src/manager.rs b/src/manager.rs index efc824c..14547fb 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,14 +1,14 @@ -use std::{thread, time::Duration}; +use std::{sync::mpsc, thread, time::Duration}; use embedded_hal::digital::blocking::OutputPin; -use embedded_hal_0_2::{adc::OneShot, digital::v2::InputPin}; +use embedded_hal_0_2::adc::OneShot; use esp_idf_hal::{ - adc::{Adc, Atten11dB, PoweredAdc, ADC1}, - gpio::{Gpio1, Gpio2}, delay, + adc::{Atten11dB, PoweredAdc, ADC1}, + gpio::{Gpio1, Gpio2}, }; use esp_idf_sys::EspError; -use crate::{dc_out_controller::DcOutController, screen::Screen}; +use crate::{beep::ringtone, dc_out_controller::DcOutController, screen::Screen}; type AdapterGpio = Gpio1>; type BatteryGpio = Gpio2>; @@ -22,6 +22,7 @@ where adc: PoweredAdc, adapter_pin: AdapterGpio, battery_pin: BatteryGpio, + tx: mpsc::Sender, } impl Manager @@ -34,6 +35,7 @@ where adc1: ADC1, adapter_pin: AdapterGpio, battery_pin: BatteryGpio, + tx: mpsc::Sender, ) -> Result { let adc = PoweredAdc::new( adc1, @@ -45,6 +47,7 @@ where adc, adapter_pin, battery_pin, + tx, }); } @@ -69,7 +72,19 @@ where if adapter < 1000.0 { self.dc_out_controller.off().expect("Can not turn off Out"); + if battery < 1000.0 { + self.tx + .send(ringtone::BATTERY_LOW) + .expect("Can not send message"); + } else { + self.tx + .send(ringtone::POWER_DOWN) + .expect("Can not send message"); + } } else { + self.tx + .send(ringtone::SILENCE) + .expect("Can not send message"); self.dc_out_controller.on().expect("Can not turn on Out"); } self.screen