-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add speaker device for the beep command * Use u16 for PIT_DIVIDER * Use string instead of float * Move beep program to userspace
- Loading branch information
Showing
12 changed files
with
213 additions
and
33 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/bin/beep | ||
/tmp/beep/tetris.sh | ||
/tmp/beep/starwars.sh | ||
/tmp/beep/mario.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate alloc; | ||
|
||
use moros::entry_point; | ||
use moros::{error, eprintln, eprint, println, print}; | ||
|
||
use moros::api::console::Style; | ||
use moros::api::process::ExitCode; | ||
use moros::api::fs; | ||
use moros::api::syscall; | ||
|
||
use alloc::string::ToString; | ||
|
||
entry_point!(main); | ||
|
||
const SPEAKER: &'static str = "/dev/speaker"; | ||
|
||
fn start_sound(freq: f64) -> Result<(), ()> { | ||
let buf = freq.to_string(); | ||
if !fs::is_device(SPEAKER) || fs::write(SPEAKER, buf.as_bytes()).is_err() { | ||
error!("Could not write to '{}'", SPEAKER); | ||
Err(()) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn stop_sound() -> Result<(), ()> { | ||
start_sound(0.0) | ||
} | ||
|
||
fn beep(freq: f64, len: f64) -> Result<(), ()> { | ||
start_sound(freq)?; | ||
syscall::sleep(len); | ||
stop_sound() | ||
} | ||
|
||
pub fn main(args: &[&str]) { | ||
let mut freq = 440.0; | ||
let mut len = 200.0; | ||
let mut i = 1; | ||
let n = args.len(); | ||
while i < n { | ||
match args[i] { | ||
"-h" | "--help" => { | ||
help(); | ||
return; | ||
} | ||
"-f" | "--freq" => { | ||
if i + 1 < n { | ||
i += 1; | ||
if let Ok(value) = args[i].parse() { | ||
freq = value; | ||
} else { | ||
error!("Could not parse freq"); | ||
syscall::exit(ExitCode::Failure); | ||
} | ||
} else { | ||
error!("Missing freq"); | ||
syscall::exit(ExitCode::UsageError); | ||
} | ||
} | ||
"-l" | "--len" => { | ||
if i + 1 < n { | ||
i += 1; | ||
if let Ok(value) = args[i].parse() { | ||
len = value; | ||
} else { | ||
error!("Could not parse len"); | ||
syscall::exit(ExitCode::Failure); | ||
} | ||
} else { | ||
error!("Missing len"); | ||
syscall::exit(ExitCode::UsageError); | ||
} | ||
} | ||
_ => {} | ||
} | ||
i += 1; | ||
} | ||
|
||
if beep(freq, len / 1000.0).is_err() { | ||
syscall::exit(ExitCode::Failure); | ||
} | ||
} | ||
|
||
fn help() { | ||
let csi_option = Style::color("aqua"); | ||
let csi_title = Style::color("yellow"); | ||
let csi_reset = Style::reset(); | ||
println!( | ||
"{}Usage:{} beep {}<options>{1}", | ||
csi_title, csi_reset, csi_option | ||
); | ||
println!(); | ||
println!("{}Options:{}", csi_title, csi_reset); | ||
println!( | ||
" {0}-f{1}, {0}--freq <hertz>{1} Tone frequency", | ||
csi_option, csi_reset | ||
); | ||
println!( | ||
" {0}-l{1}, {0}--len <milliseconds>{1} Tone length", | ||
csi_option, csi_reset | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,5 +54,6 @@ pub mod pic; | |
pub mod process; | ||
pub mod rng; | ||
pub mod serial; | ||
pub mod speaker; | ||
pub mod syscall; | ||
pub mod vga; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use super::clk; | ||
|
||
use crate::api::fs::{FileIO, IO}; | ||
|
||
use alloc::string::String; | ||
use x86_64::instructions::port::Port; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Speaker; | ||
|
||
impl Speaker { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl FileIO for Speaker { | ||
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, ()> { | ||
Err(()) | ||
} | ||
|
||
fn write(&mut self, buf: &[u8]) -> Result<usize, ()> { | ||
if let Ok(s) = String::from_utf8(buf.to_vec()) { | ||
if let Ok(n) = s.parse() { | ||
if n > 0.0 { | ||
start_sound(n); | ||
} else { | ||
stop_sound(); | ||
} | ||
} | ||
return Ok(8); | ||
} | ||
Err(()) | ||
} | ||
|
||
fn close(&mut self) {} | ||
|
||
fn poll(&mut self, event: IO) -> bool { | ||
match event { | ||
IO::Read => false, | ||
IO::Write => true, | ||
} | ||
} | ||
} | ||
|
||
// See: https://wiki.osdev.org/PC_Speaker | ||
|
||
const SPEAKER_PORT: u16 = 0x61; | ||
|
||
fn start_sound(freq: f64) { | ||
let divider = (clk::pit_frequency() / freq) as u16; | ||
let channel = 2; // PC Speaker | ||
clk::set_pit_frequency(divider, channel); | ||
|
||
let mut speaker: Port<u8> = Port::new(SPEAKER_PORT); | ||
let tmp = unsafe { speaker.read() }; | ||
if tmp != (tmp | 3) { | ||
unsafe { speaker.write(tmp | 3) }; | ||
} | ||
} | ||
|
||
fn stop_sound() { | ||
let mut speaker: Port<u8> = Port::new(SPEAKER_PORT); | ||
let tmp = unsafe { speaker.read() } & 0xFC; | ||
unsafe { speaker.write(tmp) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters