diff --git a/src/beep.rs b/src/beep.rs index 74e2161..3a47022 100644 --- a/src/beep.rs +++ b/src/beep.rs @@ -12,23 +12,26 @@ pub struct Beep { beat: u8, ringtone: ringtone::Type, channel: LedcChannel, + duty: u32, } impl Beep { pub fn new(pin: P, timer: T, channel: C) -> Result { + let channel = Self::init_channel(pin, timer, channel)?; + + let max_duty = channel.get_max_duty(); return Ok(Beep { - channel: Self::init_channel(pin, timer, channel)?, + channel, beat: 0, + duty: max_duty * 3 / 4, ringtone: ringtone::SILENCE, }); } fn init_channel(pin: P, timer: T, channel: C) -> Result, EspError> { - let config = TimerConfig::default().frequency(1.kHz().into()); + let config = TimerConfig::default().frequency(2.kHz().into()); let timer = Timer::new(timer, &config)?; - let mut channel = Channel::new(channel, timer, pin)?; - let max_duty = channel.get_max_duty(); - channel.set_duty(max_duty * 3 / 4)?; + let channel: Channel, P> = Channel::new(channel, timer, pin)?; return Ok(channel); } @@ -42,7 +45,7 @@ impl Beep { let curr = curr_ringtone[self.beat as usize]; if curr { - self.channel.set_duty(50).expect("Failed to set duty"); + self.channel.set_duty(self.duty).expect("Failed to set duty"); } else { self.channel.set_duty(0).expect("Failed to set duty"); } diff --git a/src/main.rs b/src/main.rs index 553350f..5a5abf8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ #![feature(is_some_with)] use esp_idf_sys as _; -use log::error; +use log::{error, info}; use std::{thread, time::Duration, sync::mpsc, env}; mod beep; @@ -19,14 +19,14 @@ fn main() { let peripherals = esp_idf_hal::peripherals::Peripherals::take().unwrap(); - let gpio5 = peripherals.pins.gpio5; - let gpio6 = peripherals.pins.gpio6; + let blink_pin = peripherals.pins.gpio5; + let beep_pin = peripherals.pins.gpio6; let ledc_timer0 = peripherals.ledc.timer0; let ledc_channel0 = peripherals.ledc.channel0; let dc_out_ctl_pin = peripherals.pins.gpio3; let i2c0 = peripherals.i2c0; - let gpio9 = peripherals.pins.gpio9; - let gpio10 = peripherals.pins.gpio10; + let sda_pin = peripherals.pins.gpio4; + let scl_pin = peripherals.pins.gpio10; let adc1 = peripherals.adc1; let adapter_pin = peripherals.pins.gpio1; @@ -34,12 +34,11 @@ fn main() { let (tx, mut rx) = mpsc::channel(); - println!("Starting screen"); - let display = screen::Screen::new(i2c0, gpio9, gpio10).expect("Failed to create screen"); + info!("Starting"); thread::spawn(move || { let mut blink = - blink::Blink::new(gpio5.into_output().expect("Failed to set GPIO5 as output")); + blink::Blink::new(blink_pin.into_output().expect("Failed to set GPIO5 as output")); blink.play(); }); @@ -49,7 +48,7 @@ fn main() { thread::spawn(move || { let mut beep = beep::Beep::new( - gpio6.into_output().expect("Failed to set GPIO6 as output"), + beep_pin.into_output().expect("Failed to set GPIO6 as output"), ledc_timer0, ledc_channel0, ) @@ -57,6 +56,8 @@ fn main() { beep.play(&mut rx); }); + let display = screen::Screen::new(i2c0, sda_pin, scl_pin).expect("Failed to create screen"); + let dc_out_ctl = dc_out_controller::DcOutController::new( dc_out_ctl_pin .into_output() diff --git a/src/screen.rs b/src/screen.rs index 6871d90..e924580 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -1,4 +1,4 @@ -use anyhow::{Result}; +use anyhow::{anyhow, Result}; use embedded_graphics::{ mono_font::{ascii::FONT_10X20, MonoTextStyle, iso_8859_10::FONT_6X10}, pixelcolor::Rgb565, @@ -21,7 +21,7 @@ use ssd1306::{ }; type Display = Ssd1306< - I2CInterface, gpio::Gpio10>>, + I2CInterface, gpio::Gpio10>>, DisplaySize128x64, BufferedGraphicsMode, >; @@ -33,7 +33,7 @@ pub struct Screen { impl Screen { pub fn new( i2c: i2c::I2C0, - sda: gpio::Gpio9, + sda: gpio::Gpio4, scl: gpio::Gpio10, ) -> Result { let config = ::default().baudrate(400.kHz().into()); @@ -55,8 +55,7 @@ impl Screen { .into_buffered_graphics_mode(); display - .init() - .unwrap(); + .init().map_err(|err| anyhow!("Can not init display: {:?}", err))?; let mut instance = Screen { display };