Skip to content

Commit

Permalink
Use string instead of float
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Oct 22, 2024
1 parent 0bb94e8 commit cc7b340
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/sys/speaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::clk;

use crate::api::fs::{FileIO, IO};

use core::convert::TryInto;
use alloc::string::String;
use x86_64::instructions::port::Port;

#[derive(Debug, Clone)]
Expand All @@ -20,10 +20,13 @@ impl FileIO for Speaker {
}

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
if let Ok(bytes) = buf.try_into() {
match f64::from_be_bytes(bytes) {
0.0 => stop_sound(),
freq => start_sound(freq),
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);
}
Expand All @@ -45,7 +48,6 @@ impl FileIO for Speaker {
const SPEAKER_PORT: u16 = 0x61;

fn start_sound(freq: f64) {
debug!("speaker::start_sound({})", freq);
let divider = (clk::pit_frequency() / freq) as u16;
let channel = 2; // PC Speaker
clk::set_pit_frequency(divider, channel);
Expand All @@ -58,7 +60,6 @@ fn start_sound(freq: f64) {
}

fn stop_sound() {
debug!("speaker::stop_sound()");
let mut speaker: Port<u8> = Port::new(SPEAKER_PORT);
let tmp = unsafe { speaker.read() } & 0xFC;
unsafe { speaker.write(tmp) };
Expand Down
6 changes: 4 additions & 2 deletions src/usr/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::api::process::ExitCode;
use crate::api::fs;
use crate::api::syscall;

use alloc::string::ToString;

const SPEAKER: &'static str = "/dev/speaker";

fn start_sound(freq: f64) -> Result<(), ExitCode> {
let buf = freq.to_be_bytes();
if !fs::is_device(SPEAKER) || fs::write(SPEAKER, &buf).is_err() {
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(ExitCode::Failure)
} else {
Expand Down

0 comments on commit cc7b340

Please sign in to comment.