From ab4ec0895e562e2e26b28841565736623e1b11cf Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 24 Mar 2024 14:43:05 -0700 Subject: [PATCH 1/2] Added Speaker Peripheral --- src/hal.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/hal.rs b/src/hal.rs index fd1446e..ab49308 100644 --- a/src/hal.rs +++ b/src/hal.rs @@ -66,6 +66,7 @@ pub fn cardputer_peripherals<'a>( pins: gpio::Pins, spi2: spi::SPI2, ledc: ledc::LEDC, + i2s: esp_idf_hal::i2s::I2S0, ) -> ( display_driver::ST7789< SPIInterface< @@ -76,6 +77,7 @@ pub fn cardputer_peripherals<'a>( esp_idf_hal::gpio::PinDriver<'static, impl OutputPin, esp_idf_hal::gpio::Output>, >, CardputerKeyboard<'a>, + esp_idf_hal::i2s::I2sDriver<'static, esp_idf_hal::i2s::I2sTx>, ) { // display @@ -124,5 +126,20 @@ pub fn cardputer_peripherals<'a>( let mut keyboard = CardputerKeyboard::new(mux_pins, column_pins); keyboard.init(); - (display, keyboard) + // speaker + + let speaker = esp_idf_hal::i2s::I2sDriver::new_std_tx( + i2s, + &esp_idf_hal::i2s::config::StdConfig::philips( + 48000, + esp_idf_hal::i2s::config::DataBitWidth::Bits8, + ), + pins.gpio41, + pins.gpio42, + None as Option, + pins.gpio43, + ) + .unwrap(); + + (display, keyboard, speaker) } From f9034516ad5ddb4237273fca96800cabdf9bd8c1 Mon Sep 17 00:00:00 2001 From: Alex Rowe Date: Sun, 24 Mar 2024 14:56:19 -0700 Subject: [PATCH 2/2] Added Sound Example --- src/bin/sound.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/bin/sound.rs diff --git a/src/bin/sound.rs b/src/bin/sound.rs new file mode 100644 index 0000000..035f14f --- /dev/null +++ b/src/bin/sound.rs @@ -0,0 +1,91 @@ +use cardputer::{ + hal::cardputer_peripherals, + terminal::FbTerminal, + typing::{KeyboardEvent, Typing}, + SCREEN_HEIGHT, SCREEN_WIDTH, +}; +use esp_idf_hal::{io::Write, peripherals}; + +const SAMPLE_RATE: f64 = 48000.0; +const FREQUENCY: f64 = 440.0; +const AMPLITUDE: f64 = 127.0; + +fn generate_sine_wave(duration_secs: f64) -> Vec { + let num_samples = (duration_secs * SAMPLE_RATE) as usize; + let mut samples = Vec::with_capacity(num_samples); + + let sample_period = 1.0 / SAMPLE_RATE; + + for i in 0..num_samples { + let t = i as f64 * sample_period; + let angular_freq = 2.0 * 3.141593 * FREQUENCY + t * 200.0; + let sample_value = (AMPLITUDE * (angular_freq * t).sin()) as u8; + samples.push(sample_value); + } + + samples +} + +#[allow(clippy::approx_constant)] +fn main() { + esp_idf_svc::sys::link_patches(); + + // esp_idf_hal::i2s::I2sDriver::new_std_tx(i2s, config, bclk, dout, mclk, ws) + let peripherals = peripherals::Peripherals::take().unwrap(); + let (mut display, mut keyboard, mut speaker) = cardputer_peripherals( + peripherals.pins, + peripherals.spi2, + peripherals.ledc, + peripherals.i2s0, + ); + + let mut raw_fb = Box::new([0u16; SCREEN_WIDTH * SCREEN_HEIGHT]); + let mut terminal = + FbTerminal::::new(raw_fb.as_mut_ptr(), &mut display); + + let mut typing = Typing::new(); + + loop { + let evt = keyboard.read_events(); + if let Some(evt) = evt { + if let Some(evts) = typing.eat_keyboard_events(evt) { + match evts { + KeyboardEvent::Ascii(c) => { + terminal.command_line.push(c); + } + KeyboardEvent::Backspace => { + terminal.command_line.pop(); + } + KeyboardEvent::Enter => { + let text = terminal.command_line.get(); + match text { + "b" => { + terminal.println("Beep"); + speaker.tx_enable().unwrap(); + speaker + .write_all( + &generate_sine_wave(1.0), + esp_idf_hal::delay::TickType::new_millis(2000).into(), + ) + .unwrap(); + speaker.flush().unwrap(); + speaker.tx_disable().unwrap(); + } + _ => { + terminal.println("?"); + } + } + + terminal.enter(); + } + KeyboardEvent::ArrowUp => { + terminal.command_line.arrow_up(); + } + _ => {} + } + } + } + + terminal.draw(); + } +}