fix: fix can not boot via change I2C pins.

This commit is contained in:
Ivan Li 2022-05-22 11:15:15 +08:00
parent 991e9c9867
commit a383d511dd
3 changed files with 23 additions and 20 deletions

View File

@ -12,23 +12,26 @@ pub struct Beep<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> {
beat: u8,
ringtone: ringtone::Type,
channel: LedcChannel<P, T, C>,
duty: u32,
}
impl<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> Beep<P, T, C> {
pub fn new(pin: P, timer: T, channel: C) -> Result<Self, EspError> {
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<LedcChannel<P, T, C>, 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<C, T, Timer<T>, P> = Channel::new(channel, timer, pin)?;
return Ok(channel);
}
@ -42,7 +45,7 @@ impl<P: OutputPin, T: ledc::HwTimer, C: ledc::HwChannel> Beep<P, T, C> {
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");
}

View File

@ -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()

View File

@ -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<Master<I2C0, gpio::Gpio9<gpio::Unknown>, gpio::Gpio10<gpio::Unknown>>>,
I2CInterface<Master<I2C0, gpio::Gpio4<gpio::Unknown>, gpio::Gpio10<gpio::Unknown>>>,
DisplaySize128x64,
BufferedGraphicsMode<DisplaySize128x64>,
>;
@ -33,7 +33,7 @@ pub struct Screen {
impl Screen {
pub fn new(
i2c: i2c::I2C0,
sda: gpio::Gpio9<gpio::Unknown>,
sda: gpio::Gpio4<gpio::Unknown>,
scl: gpio::Gpio10<gpio::Unknown>,
) -> Result<Self> {
let config = <i2c::config::MasterConfig as Default>::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 };