Skip to content

Commit

Permalink
Move beep program to userspace
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 22, 2024
1 parent cc7b340 commit dfd47a4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
Binary file added dsk/bin/beep
Binary file not shown.
1 change: 1 addition & 0 deletions dsk/var/pkg/beep
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
107 changes: 107 additions & 0 deletions src/bin/beep.rs
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
);
}
4 changes: 2 additions & 2 deletions src/usr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod beep;
// pub mod beep; // TODO: Remove file
pub mod calc;
pub mod chess;
pub mod copy;
Expand All @@ -13,7 +13,7 @@ pub mod elf;
pub mod encode;
pub mod env;
pub mod find;
//pub mod geodate;
//pub mod geodate; // TODO: Remove file
pub mod hash;
pub mod help;
pub mod hex;
Expand Down
2 changes: 1 addition & 1 deletion src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ fn dispatch(args: &[&str], config: &mut Config) -> Result<(), ExitCode> {
"" => Ok(()),
"2048" => usr::pow::main(args),
"alias" => cmd_alias(args, config),
"beep" => usr::beep::main(args),
//"beep" => usr::beep::main(args),
"calc" => usr::calc::main(args),
"chess" => usr::chess::main(args),
"copy" => usr::copy::main(args),
Expand Down

0 comments on commit dfd47a4

Please sign in to comment.