Skip to content

Commit

Permalink
Merge pull request #2 from aprowe/Speaker
Browse files Browse the repository at this point in the history
Added Speaker Peripheral
  • Loading branch information
Kezii authored Mar 26, 2024
2 parents 765699b + f903451 commit 0ff2e9d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
91 changes: 91 additions & 0 deletions src/bin/sound.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
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::<SCREEN_WIDTH, SCREEN_HEIGHT>::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();
}
}
19 changes: 18 additions & 1 deletion src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -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

Expand Down Expand Up @@ -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<esp_idf_hal::gpio::AnyIOPin>,
pins.gpio43,
)
.unwrap();

(display, keyboard, speaker)
}

0 comments on commit 0ff2e9d

Please sign in to comment.