From cc7b340e429a8565be38373a1c8d58a96dd22fc4 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Tue, 22 Oct 2024 11:29:29 +0200 Subject: [PATCH] Use string instead of float --- src/sys/speaker.rs | 15 ++++++++------- src/usr/beep.rs | 6 ++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/sys/speaker.rs b/src/sys/speaker.rs index 786272c5..73337dce 100644 --- a/src/sys/speaker.rs +++ b/src/sys/speaker.rs @@ -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)] @@ -20,10 +20,13 @@ impl FileIO for Speaker { } fn write(&mut self, buf: &[u8]) -> Result { - 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); } @@ -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); @@ -58,7 +60,6 @@ fn start_sound(freq: f64) { } fn stop_sound() { - debug!("speaker::stop_sound()"); let mut speaker: Port = Port::new(SPEAKER_PORT); let tmp = unsafe { speaker.read() } & 0xFC; unsafe { speaker.write(tmp) }; diff --git a/src/usr/beep.rs b/src/usr/beep.rs index 043471d4..d15f3f98 100644 --- a/src/usr/beep.rs +++ b/src/usr/beep.rs @@ -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 {