From bebb509e9758b10dbe216cb04836440b21ab447b Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Tue, 17 Sep 2024 16:13:57 +0300 Subject: [PATCH 01/18] servomotor api --- Cargo.toml | 1 + Makefile | 2 ++ apis/interface/servo/Cargo.toml | 16 +++++++++++++++ apis/interface/servo/src/lib.rs | 36 +++++++++++++++++++++++++++++++++ build_scripts/src/lib.rs | 1 + examples/servo.rs | 15 ++++++++++++++ runner/src/elf2tab.rs | 2 +- src/lib.rs | 4 ++++ 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 apis/interface/servo/Cargo.toml create mode 100644 apis/interface/servo/src/lib.rs create mode 100644 examples/servo.rs diff --git a/Cargo.toml b/Cargo.toml index 2accd7a9..4dd201a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ libtock_i2c_master = { path = "apis/peripherals/i2c_master" } libtock_i2c_master_slave = { path = "apis/peripherals/i2c_master_slave" } libtock_key_value = { path = "apis/storage/key_value" } libtock_leds = { path = "apis/interface/leds" } +libtock_servo = { path = "apis/interface/servo" } libtock_low_level_debug = { path = "apis/kernel/low_level_debug" } libtock_ninedof = { path = "apis/sensors/ninedof" } libtock_platform = { path = "platform" } diff --git a/Makefile b/Makefile index 397a7fd5..d28c954e 100644 --- a/Makefile +++ b/Makefile @@ -273,6 +273,8 @@ $(eval $(call platform_build,nucleo_f446re,thumbv7em-none-eabi)) $(eval $(call platform_build,nrf52840,thumbv7em-none-eabi)) $(eval $(call platform_flash,nrf52840,thumbv7em-none-eabi)) $(eval $(call platform_build,raspberry_pi_pico,thumbv6m-none-eabi)) +$(eval $(call platform_build,pico_explorer_base,thumbv6m-none-eabi)) +$(eval $(call platform_flash,pico_explorer_base,thumbv6m-none-eabi)) $(eval $(call platform_build,nano33ble,thumbv6m-none-eabi)) $(eval $(call platform_build,nano_rp2040_connect,thumbv6m-none-eabi)) $(eval $(call platform_build,stm32f3discovery,thumbv7em-none-eabi)) diff --git a/apis/interface/servo/Cargo.toml b/apis/interface/servo/Cargo.toml new file mode 100644 index 00000000..71c8ed0e --- /dev/null +++ b/apis/interface/servo/Cargo.toml @@ -0,0 +1,16 @@ +cargo-features = ["edition2024"] +[package] +name = "libtock_servo" +version = "0.1.0" +authors = ["Tock Project Developers "] +license = "Apache-2.0 OR MIT" +edition = "2024" +repository = "https://www.github.com/tock/libtock-rs" +rust-version.workspace = true +description = "libtock servomotor driver" + +[dependencies] +libtock_platform = { path = "../../../platform" } + +[dev-dependencies] +libtock_unittest = { path = "../../../unittest" } diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs new file mode 100644 index 00000000..5c61bf81 --- /dev/null +++ b/apis/interface/servo/src/lib.rs @@ -0,0 +1,36 @@ +#![no_std] + +use libtock_platform::{CommandReturn, ErrorCode, Syscalls}; +//use libtock_unittest::command_return::success; + +pub struct Servo(S); + +impl Servo { + pub fn servo_exists() -> Result<(), ErrorCode> { + let val = S::command(DRIVER_NUM, 0, 0, 0).is_success(); + if val == true { + Ok(()) + } else { + //println!("the driver could not be found"); + Err(ErrorCode::Fail) + } + } + pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> { + S::command(DRIVER_NUM, 1, index, angle).to_result() + } + pub fn get_angle(index: u32) -> Result<(), ErrorCode> { + S::command(DRIVER_NUM, 2, index, 0).to_result() + } +} + +// #[cfg(test)] +// mod tests; + +// ----------------------------------------------------------------------------- +// Driver number and command IDs +// ----------------------------------------------------------------------------- + +const DRIVER_NUM: u32 = 0x90009; + +// Command IDs +//const SET_ANGLE: u16 = 1; diff --git a/build_scripts/src/lib.rs b/build_scripts/src/lib.rs index e7185782..4d2212f4 100644 --- a/build_scripts/src/lib.rs +++ b/build_scripts/src/lib.rs @@ -20,6 +20,7 @@ const PLATFORMS: &[(&str, &str, &str, &str, &str)] = &[ ("nucleo_f446re" , "0x08040000", "255K" , "0x20004000", "176K" ), ("opentitan" , "0x20030000", "32M" , "0x10006000", "126K" ), ("raspberry_pi_pico" , "0x10040000", "256K" , "0x20012000", "192K" ), + ("pico_explorer_base" , "0x10040000", "256K" , "0x20012000", "192K" ), ("stm32f3discovery" , "0x08020000", "0x0020000", "0x20004000", "48K" ), ("stm32f412gdiscovery", "0x08030000", "256K" , "0x20004000", "112K" ), ("nano33ble" , "0x00050000", "704K" , "0x20005000", "240K" ), diff --git a/examples/servo.rs b/examples/servo.rs new file mode 100644 index 00000000..5a8d7f59 --- /dev/null +++ b/examples/servo.rs @@ -0,0 +1,15 @@ +#![no_main] +#![no_std] +use libtock::alarm::{Alarm, Milliseconds}; +use libtock::runtime::{set_main, stack_size}; +use libtock::servo::Servo; +use libtock_platform::yield_id; + +set_main! {main} +stack_size! {0x200} + +fn main() { + for i in 0..180 { + let _ = Servo::set_angle(1, i); + } +} diff --git a/runner/src/elf2tab.rs b/runner/src/elf2tab.rs index 7723101b..ddbbda35 100644 --- a/runner/src/elf2tab.rs +++ b/runner/src/elf2tab.rs @@ -6,7 +6,7 @@ use std::process::Command; fn get_platform_architecture(platform: &str) -> Option<&'static str> { match platform { - "raspberry_pi_pico" | "nano_rp2040_connect" => Some("cortex-m0"), + "raspberry_pi_pico" | "pico_explorer_base" | "nano_rp2040_connect" => Some("cortex-m0"), "apollo3" | "clue_nrf52840" | "hail" diff --git a/src/lib.rs b/src/lib.rs index 0e4acd36..853de44d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,10 @@ pub mod leds { use libtock_leds as leds; pub type Leds = leds::Leds; } +pub mod servo { + use libtock_servo as servo; + pub type Servo = servo::Servo; +} pub mod low_level_debug { use libtock_low_level_debug as lldb; pub type LowLevelDebug = lldb::LowLevelDebug; From e8c1896966a51fd3ff4b6f2a45c9074aecb03d56 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Thu, 19 Sep 2024 16:16:43 +0300 Subject: [PATCH 02/18] adding comments --- Cargo.toml | 1 + examples/servo.rs | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3036540..77c2c030 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,6 +70,7 @@ exclude = ["tock"] members = [ "apis/interface/buttons", "apis/interface/buzzer", + "apis/interface/servo", "apis/interface/console", "apis/interface/leds", "apis/kernel/low_level_debug", diff --git a/examples/servo.rs b/examples/servo.rs index 5a8d7f59..46f50ad1 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -1,15 +1,51 @@ #![no_main] #![no_std] +use core::fmt::Write; use libtock::alarm::{Alarm, Milliseconds}; +use libtock::console::Console; use libtock::runtime::{set_main, stack_size}; use libtock::servo::Servo; -use libtock_platform::yield_id; +use libtock_platform::ErrorCode; set_main! {main} stack_size! {0x200} fn main() { + //Checks if the driver exists. + if Err(ErrorCode::Fail) == Servo::servo_exists() { + writeln!(Console::writer(), "The driver could not be found").unwrap(); + return; + } + + let index: u32 = 0; // the first index available. + + // Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change). + // "i" represents the angle we set the servomotor at. for i in 0..180 { - let _ = Servo::set_angle(1, i); + let val1 = Servo::set_angle(index, i); // stores the value returned by set_angle + let val2 = Servo::get_angle(index); // stores the value returned by get_angle + + if val1 == Err(ErrorCode::Fail) { + writeln!( + Console::writer(), + "The provided angle exceeds the servo's limit" + ) + .unwrap(); + // } else if val2 == Err(ErrorCode::Invalid) { + // writeln!(Console::writer(), "The servo cannot return it's angle").unwrap(); + } else if val1 == Err(ErrorCode::NoDevice) { + writeln!( + Console::writer(), + "The index exceeds the number of provided servomotors" + ) + .unwrap(); + } else if val2 == Err(ErrorCode::NoDevice) { + writeln!( + Console::writer(), + "The index exceeds the number of provided servomotors" + ) + .unwrap(); + } + Alarm::sleep_for(Milliseconds(100)).unwrap(); } } From 3362eec59abc2de53745ea208a362a809d78e2ca Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 12:00:32 +0300 Subject: [PATCH 03/18] comments update --- examples/servo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/servo.rs b/examples/servo.rs index 46f50ad1..a42d7fc1 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -31,8 +31,8 @@ fn main() { "The provided angle exceeds the servo's limit" ) .unwrap(); - // } else if val2 == Err(ErrorCode::Invalid) { - // writeln!(Console::writer(), "The servo cannot return it's angle").unwrap(); + } else if val2 == Err(ErrorCode::Invalid) { + writeln!(Console::writer(), "The servo cannot return it's angle").unwrap(); } else if val1 == Err(ErrorCode::NoDevice) { writeln!( Console::writer(), From 2543b65a7a043fdcd684ffb719bb0df83eef17ea Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 12:24:11 +0300 Subject: [PATCH 04/18] comments update --- examples/servo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/servo.rs b/examples/servo.rs index a42d7fc1..6b14be76 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -31,7 +31,7 @@ fn main() { "The provided angle exceeds the servo's limit" ) .unwrap(); - } else if val2 == Err(ErrorCode::Invalid) { + } else if val2 == Err(ErrorCode::NoSupport) { writeln!(Console::writer(), "The servo cannot return it's angle").unwrap(); } else if val1 == Err(ErrorCode::NoDevice) { writeln!( From e0e332dc3cc99db61e26bb827efdf82852b957a1 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 14:41:51 +0300 Subject: [PATCH 05/18] update --- Makefile | 2 -- runner/src/elf2tab.rs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d28c954e..397a7fd5 100644 --- a/Makefile +++ b/Makefile @@ -273,8 +273,6 @@ $(eval $(call platform_build,nucleo_f446re,thumbv7em-none-eabi)) $(eval $(call platform_build,nrf52840,thumbv7em-none-eabi)) $(eval $(call platform_flash,nrf52840,thumbv7em-none-eabi)) $(eval $(call platform_build,raspberry_pi_pico,thumbv6m-none-eabi)) -$(eval $(call platform_build,pico_explorer_base,thumbv6m-none-eabi)) -$(eval $(call platform_flash,pico_explorer_base,thumbv6m-none-eabi)) $(eval $(call platform_build,nano33ble,thumbv6m-none-eabi)) $(eval $(call platform_build,nano_rp2040_connect,thumbv6m-none-eabi)) $(eval $(call platform_build,stm32f3discovery,thumbv7em-none-eabi)) diff --git a/runner/src/elf2tab.rs b/runner/src/elf2tab.rs index ddbbda35..7723101b 100644 --- a/runner/src/elf2tab.rs +++ b/runner/src/elf2tab.rs @@ -6,7 +6,7 @@ use std::process::Command; fn get_platform_architecture(platform: &str) -> Option<&'static str> { match platform { - "raspberry_pi_pico" | "pico_explorer_base" | "nano_rp2040_connect" => Some("cortex-m0"), + "raspberry_pi_pico" | "nano_rp2040_connect" => Some("cortex-m0"), "apollo3" | "clue_nrf52840" | "hail" From a020e786c8c310af3abdd781cac688a1b303f453 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 14:59:32 +0300 Subject: [PATCH 06/18] comments update --- examples/servo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/servo.rs b/examples/servo.rs index 6b14be76..590c9def 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -32,7 +32,7 @@ fn main() { ) .unwrap(); } else if val2 == Err(ErrorCode::NoSupport) { - writeln!(Console::writer(), "The servo cannot return it's angle").unwrap(); + writeln!(Console::writer(), "The servo cannot return its angle").unwrap(); } else if val1 == Err(ErrorCode::NoDevice) { writeln!( Console::writer(), From c3c884c23cf99d14f7e1afb459be975f766239f4 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 18:39:56 +0300 Subject: [PATCH 07/18] update --- apis/interface/servo/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/apis/interface/servo/Cargo.toml b/apis/interface/servo/Cargo.toml index 71c8ed0e..e0514485 100644 --- a/apis/interface/servo/Cargo.toml +++ b/apis/interface/servo/Cargo.toml @@ -1,4 +1,3 @@ -cargo-features = ["edition2024"] [package] name = "libtock_servo" version = "0.1.0" From fcb88091ed9a57b8f28e03d5fb6eeccb256baa12 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Fri, 20 Sep 2024 18:52:06 +0300 Subject: [PATCH 08/18] update --- build_scripts/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/build_scripts/src/lib.rs b/build_scripts/src/lib.rs index 4d2212f4..e7185782 100644 --- a/build_scripts/src/lib.rs +++ b/build_scripts/src/lib.rs @@ -20,7 +20,6 @@ const PLATFORMS: &[(&str, &str, &str, &str, &str)] = &[ ("nucleo_f446re" , "0x08040000", "255K" , "0x20004000", "176K" ), ("opentitan" , "0x20030000", "32M" , "0x10006000", "126K" ), ("raspberry_pi_pico" , "0x10040000", "256K" , "0x20012000", "192K" ), - ("pico_explorer_base" , "0x10040000", "256K" , "0x20012000", "192K" ), ("stm32f3discovery" , "0x08020000", "0x0020000", "0x20004000", "48K" ), ("stm32f412gdiscovery", "0x08030000", "256K" , "0x20004000", "112K" ), ("nano33ble" , "0x00050000", "704K" , "0x20005000", "240K" ), From 372e94ba7eba3131fe349a30b2ea1c80d3d54bba Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Tue, 24 Sep 2024 16:32:54 +0300 Subject: [PATCH 09/18] unittests addition --- apis/interface/servo/Cargo.toml | 1 + apis/interface/servo/src/lib.rs | 19 ++++----- apis/interface/servo/src/tests.rs | 32 ++++++++++++++++ unittest/src/fake/mod.rs | 2 + unittest/src/fake/servo/mod.rs | 64 +++++++++++++++++++++++++++++++ unittest/src/fake/servo/tests.rs | 47 +++++++++++++++++++++++ 6 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 apis/interface/servo/src/tests.rs create mode 100644 unittest/src/fake/servo/mod.rs create mode 100644 unittest/src/fake/servo/tests.rs diff --git a/apis/interface/servo/Cargo.toml b/apis/interface/servo/Cargo.toml index e0514485..71c8ed0e 100644 --- a/apis/interface/servo/Cargo.toml +++ b/apis/interface/servo/Cargo.toml @@ -1,3 +1,4 @@ +cargo-features = ["edition2024"] [package] name = "libtock_servo" version = "0.1.0" diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index 5c61bf81..d41fd28a 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -1,13 +1,12 @@ #![no_std] -use libtock_platform::{CommandReturn, ErrorCode, Syscalls}; -//use libtock_unittest::command_return::success; +use libtock_platform::{ErrorCode, Syscalls}; pub struct Servo(S); impl Servo { pub fn servo_exists() -> Result<(), ErrorCode> { - let val = S::command(DRIVER_NUM, 0, 0, 0).is_success(); + let val = S::command(DRIVER_NUM, SERVO_EXISTS, 0, 0).is_success(); if val == true { Ok(()) } else { @@ -16,15 +15,15 @@ impl Servo { } } pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> { - S::command(DRIVER_NUM, 1, index, angle).to_result() + S::command(DRIVER_NUM, SET_ANGLE, index, angle).to_result() } - pub fn get_angle(index: u32) -> Result<(), ErrorCode> { - S::command(DRIVER_NUM, 2, index, 0).to_result() + pub fn get_angle(index: u32) -> Result { + S::command(DRIVER_NUM, GET_ANGLE, index, 0).to_result() } } -// #[cfg(test)] -// mod tests; +#[cfg(test)] +mod tests; // ----------------------------------------------------------------------------- // Driver number and command IDs @@ -33,4 +32,6 @@ impl Servo { const DRIVER_NUM: u32 = 0x90009; // Command IDs -//const SET_ANGLE: u16 = 1; +const SERVO_EXISTS: u32 = 0; +const SET_ANGLE: u32 = 1; +const GET_ANGLE: u32 = 2; diff --git a/apis/interface/servo/src/tests.rs b/apis/interface/servo/src/tests.rs new file mode 100644 index 00000000..247030fc --- /dev/null +++ b/apis/interface/servo/src/tests.rs @@ -0,0 +1,32 @@ +use libtock_platform::ErrorCode; +use libtock_unittest::fake; + +type Servo = super::Servo; + +#[test] +fn no_driver() { + let _kernel = fake::Kernel::new(); + assert_eq!(Servo::servo_exists(), Err(ErrorCode::Fail)) +} +#[test] +fn servo_exists() { + let kernel = fake::Kernel::new(); + let driver = fake::Servo::<2>::new(); + kernel.add_driver(&driver); + assert_eq!(Servo::servo_exists(), Ok(())); +} +#[test] +fn set_angle() { + let kernel = fake::Kernel::new(); + let driver = fake::Servo::<2>::new(); + kernel.add_driver(&driver); + assert_eq!(Servo::set_angle(1, 90), Ok(())); +} +#[test] +fn get_angle() { + let kernel = fake::Kernel::new(); + let driver = fake::Servo::<2>::new(); + kernel.add_driver(&driver); + assert_eq!(Servo::set_angle(1, 45), Ok(())); + assert_eq!(Servo::get_angle(1), Ok(45)); +} diff --git a/unittest/src/fake/mod.rs b/unittest/src/fake/mod.rs index ba042ab0..0da24264 100644 --- a/unittest/src/fake/mod.rs +++ b/unittest/src/fake/mod.rs @@ -23,6 +23,7 @@ mod leds; mod low_level_debug; mod ninedof; mod proximity; +mod servo; mod sound_pressure; mod syscall_driver; mod syscalls; @@ -42,6 +43,7 @@ pub use leds::Leds; pub use low_level_debug::{LowLevelDebug, Message}; pub use ninedof::{NineDof, NineDofData}; pub use proximity::Proximity; +pub use servo::Servo; pub use sound_pressure::SoundPressure; pub use syscall_driver::SyscallDriver; pub use syscalls::Syscalls; diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs new file mode 100644 index 00000000..3d870657 --- /dev/null +++ b/unittest/src/fake/servo/mod.rs @@ -0,0 +1,64 @@ +use std::cell::Cell; + +use crate::DriverInfo; +use libtock_platform::{CommandReturn, ErrorCode}; + +pub struct Servo { + servo: [Cell; NUM_SERVO], +} + +impl Servo { + pub fn new() -> std::rc::Rc> { + #[allow(clippy::declare_interior_mutable_const)] + const ANGLE: Cell = Cell::new(0); + std::rc::Rc::new(Servo { + servo: [ANGLE; NUM_SERVO], + }) + } +} + +impl crate::fake::SyscallDriver for Servo { + fn info(&self) -> DriverInfo { + DriverInfo::new(DRIVER_NUM) + } + + fn command(&self, command_num: u32, servo_index: u32, angle: u32) -> CommandReturn { + match command_num { + 0 => crate::command_return::success(), + 1 => { + if servo_index >= NUM_SERVO as u32 { + crate::command_return::failure(ErrorCode::NoDevice) + } else if angle <= 180 { + self.servo[servo_index as usize].set(angle as u16); + crate::command_return::success() + } else { + crate::command_return::failure(ErrorCode::Fail) + } + } + // Return the current angle. + 2 => { + if servo_index >= NUM_SERVO as u32 { + crate::command_return::failure(ErrorCode::NoDevice) + } else { + let angle = self.servo[servo_index as usize].get(); + crate::command_return::success_u32(angle as u32) + } + } + _ => crate::command_return::failure(ErrorCode::NoSupport), + } + } +} + +#[cfg(test)] +mod tests; + +// ----------------------------------------------------------------------------- +// Implementation details below +// ----------------------------------------------------------------------------- + +const DRIVER_NUM: u32 = 0x90009; + +// Command numbers +const SERVO_EXISTS: u32 = 0; +const SET_ANGLE: u32 = 1; +const GET_ANGLE: u32 = 2; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs new file mode 100644 index 00000000..d2ae46f7 --- /dev/null +++ b/unittest/src/fake/servo/tests.rs @@ -0,0 +1,47 @@ +use crate::fake; +use fake::servo::*; +use libtock_platform::CommandReturn; + +// Tests the command implementation. +#[test] +fn command() { + use fake::SyscallDriver; + let servo = Servo::<1>::new(); + let value = servo.command(SERVO_EXISTS, 0, 0); + assert_eq!(CommandReturn::is_success(&value), true); + assert_eq!( + CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), + true + ); + assert_eq!( + CommandReturn::get_success_u32(&servo.command(GET_ANGLE, 0, 0)), + Some(90) + ); +} + +#[test] +fn kernel_integration() { + use libtock_platform::Syscalls; + let kernel = fake::Kernel::new(); + let servo = Servo::<1>::new(); + kernel.add_driver(&servo); + let value = fake::Syscalls::command(DRIVER_NUM, SERVO_EXISTS, 0, 0); + assert_eq!(CommandReturn::is_success(&value), true); + assert_eq!( + fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 2, 90).get_failure(), + Some(ErrorCode::NoDevice) + ); + assert_eq!( + fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 181).get_failure(), + Some(ErrorCode::Fail) + ); + assert!(fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 90).is_success()); + assert_eq!( + fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 0, 0).get_success_u32(), + Some(90) + ); + assert_eq!( + fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 2, 0).get_failure(), + Some(ErrorCode::NoDevice) + ); +} From 8e26dad7229b1f5ba27b8b730d5cc0baa4307432 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Wed, 25 Sep 2024 13:18:38 +0300 Subject: [PATCH 10/18] comments update --- apis/interface/servo/Cargo.toml | 3 +-- apis/interface/servo/src/lib.rs | 30 ++++++++++++++++++++++++++---- apis/interface/servo/src/tests.rs | 6 +++--- examples/servo.rs | 2 +- unittest/src/fake/servo/mod.rs | 2 +- unittest/src/fake/servo/tests.rs | 4 ++-- 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/apis/interface/servo/Cargo.toml b/apis/interface/servo/Cargo.toml index 71c8ed0e..e9323033 100644 --- a/apis/interface/servo/Cargo.toml +++ b/apis/interface/servo/Cargo.toml @@ -1,10 +1,9 @@ -cargo-features = ["edition2024"] [package] name = "libtock_servo" version = "0.1.0" authors = ["Tock Project Developers "] license = "Apache-2.0 OR MIT" -edition = "2024" +edition = "2021" repository = "https://www.github.com/tock/libtock-rs" rust-version.workspace = true description = "libtock servomotor driver" diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index d41fd28a..3bd46b51 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -5,18 +5,40 @@ use libtock_platform::{ErrorCode, Syscalls}; pub struct Servo(S); impl Servo { - pub fn servo_exists() -> Result<(), ErrorCode> { - let val = S::command(DRIVER_NUM, SERVO_EXISTS, 0, 0).is_success(); + /// Check whether the driver exists. + pub fn exists() -> Result<(), ErrorCode> { + let val = S::command(DRIVER_NUM, EXISTS, 0, 0).is_success(); if val == true { Ok(()) } else { - //println!("the driver could not be found"); Err(ErrorCode::Fail) } } + + /// Changes the angle of the servo. + /// Return values: + /// + /// - `Ok(())`: The attempt at changing the angle was successful. + /// - `FAIL`: Cannot change the angle. + /// - `INVAL`: The value exceeds u16, indicating it's incorrect + /// since servomotors can only have a maximum of 360 degrees. + /// - `NODEVICE`: The index exceeds the number of servomotors provided. + /// # Arguments + /// - `angle` - the variable that receives the angle + /// (in degrees from 0 to 180) from the servo driver. + /// - `index` - the variable that receives the index of the servomotor. pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> { S::command(DRIVER_NUM, SET_ANGLE, index, angle).to_result() } + + /// Returns the angle of the servo. + /// Return values: + /// + /// - `angle`: The value, in angles from 0 to 360, of the servo. + /// - `NOSUPPORT`: The servo cannot return its angle. + /// - `NODEVICE`: The index exceeds the number of servomotors provided. + /// # Arguments + /// - `index` - the variable that receives the index of the servomotor. pub fn get_angle(index: u32) -> Result { S::command(DRIVER_NUM, GET_ANGLE, index, 0).to_result() } @@ -32,6 +54,6 @@ mod tests; const DRIVER_NUM: u32 = 0x90009; // Command IDs -const SERVO_EXISTS: u32 = 0; +const EXISTS: u32 = 0; const SET_ANGLE: u32 = 1; const GET_ANGLE: u32 = 2; diff --git a/apis/interface/servo/src/tests.rs b/apis/interface/servo/src/tests.rs index 247030fc..ac2540ad 100644 --- a/apis/interface/servo/src/tests.rs +++ b/apis/interface/servo/src/tests.rs @@ -6,14 +6,14 @@ type Servo = super::Servo; #[test] fn no_driver() { let _kernel = fake::Kernel::new(); - assert_eq!(Servo::servo_exists(), Err(ErrorCode::Fail)) + assert_eq!(Servo::exists(), Err(ErrorCode::Fail)) } #[test] -fn servo_exists() { +fn exists() { let kernel = fake::Kernel::new(); let driver = fake::Servo::<2>::new(); kernel.add_driver(&driver); - assert_eq!(Servo::servo_exists(), Ok(())); + assert_eq!(Servo::exists(), Ok(())); } #[test] fn set_angle() { diff --git a/examples/servo.rs b/examples/servo.rs index 590c9def..6840cc7d 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -12,7 +12,7 @@ stack_size! {0x200} fn main() { //Checks if the driver exists. - if Err(ErrorCode::Fail) == Servo::servo_exists() { + if Err(ErrorCode::Fail) == Servo::exists() { writeln!(Console::writer(), "The driver could not be found").unwrap(); return; } diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs index 3d870657..79072581 100644 --- a/unittest/src/fake/servo/mod.rs +++ b/unittest/src/fake/servo/mod.rs @@ -59,6 +59,6 @@ mod tests; const DRIVER_NUM: u32 = 0x90009; // Command numbers -const SERVO_EXISTS: u32 = 0; +const EXISTS: u32 = 0; const SET_ANGLE: u32 = 1; const GET_ANGLE: u32 = 2; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index d2ae46f7..2b21c7ff 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -7,7 +7,7 @@ use libtock_platform::CommandReturn; fn command() { use fake::SyscallDriver; let servo = Servo::<1>::new(); - let value = servo.command(SERVO_EXISTS, 0, 0); + let value = servo.command(EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), @@ -25,7 +25,7 @@ fn kernel_integration() { let kernel = fake::Kernel::new(); let servo = Servo::<1>::new(); kernel.add_driver(&servo); - let value = fake::Syscalls::command(DRIVER_NUM, SERVO_EXISTS, 0, 0); + let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 2, 90).get_failure(), From a208bccdf5e8aa94c8e4e0d8f780197a7a88d19d Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Wed, 25 Sep 2024 17:04:32 +0300 Subject: [PATCH 11/18] function that returns the number of servomotors available --- apis/interface/servo/src/lib.rs | 9 +++++++-- apis/interface/servo/src/tests.rs | 7 +++++++ examples/servo.rs | 10 +++++++++- unittest/src/fake/servo/mod.rs | 10 ++++++---- unittest/src/fake/servo/tests.rs | 10 +++++++++- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index 3bd46b51..d55b9c53 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -14,6 +14,10 @@ impl Servo { Err(ErrorCode::Fail) } } + /// Returns the number of the servomotors available. + pub fn servo_number() -> Result { + S::command(DRIVER_NUM, SERVO_NUMBER, 0, 0).to_result() + } /// Changes the angle of the servo. /// Return values: @@ -55,5 +59,6 @@ const DRIVER_NUM: u32 = 0x90009; // Command IDs const EXISTS: u32 = 0; -const SET_ANGLE: u32 = 1; -const GET_ANGLE: u32 = 2; +const SERVO_NUMBER: u32 = 1; +const SET_ANGLE: u32 = 2; +const GET_ANGLE: u32 = 3; diff --git a/apis/interface/servo/src/tests.rs b/apis/interface/servo/src/tests.rs index ac2540ad..65bc202b 100644 --- a/apis/interface/servo/src/tests.rs +++ b/apis/interface/servo/src/tests.rs @@ -16,6 +16,13 @@ fn exists() { assert_eq!(Servo::exists(), Ok(())); } #[test] +fn servo_number() { + let kernel = fake::Kernel::new(); + let driver = fake::Servo::<2>::new(); + kernel.add_driver(&driver); + assert_eq!(Servo::servo_number(), Ok(2)); +} +#[test] fn set_angle() { let kernel = fake::Kernel::new(); let driver = fake::Servo::<2>::new(); diff --git a/examples/servo.rs b/examples/servo.rs index 6840cc7d..1398c2d3 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -8,7 +8,7 @@ use libtock::servo::Servo; use libtock_platform::ErrorCode; set_main! {main} -stack_size! {0x200} +stack_size! {0x300} fn main() { //Checks if the driver exists. @@ -16,6 +16,14 @@ fn main() { writeln!(Console::writer(), "The driver could not be found").unwrap(); return; } + let servo_number = Servo::servo_number().unwrap(); + + writeln!( + Console::writer(), + "The number of available servomotors is {:?}", + servo_number + ) + .unwrap(); let index: u32 = 0; // the first index available. diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs index 79072581..7f4e0dee 100644 --- a/unittest/src/fake/servo/mod.rs +++ b/unittest/src/fake/servo/mod.rs @@ -25,7 +25,8 @@ impl crate::fake::SyscallDriver for Servo { fn command(&self, command_num: u32, servo_index: u32, angle: u32) -> CommandReturn { match command_num { 0 => crate::command_return::success(), - 1 => { + 1 => crate::command_return::success_u32(NUM_SERVO as u32), + 2 => { if servo_index >= NUM_SERVO as u32 { crate::command_return::failure(ErrorCode::NoDevice) } else if angle <= 180 { @@ -36,7 +37,7 @@ impl crate::fake::SyscallDriver for Servo { } } // Return the current angle. - 2 => { + 3 => { if servo_index >= NUM_SERVO as u32 { crate::command_return::failure(ErrorCode::NoDevice) } else { @@ -60,5 +61,6 @@ const DRIVER_NUM: u32 = 0x90009; // Command numbers const EXISTS: u32 = 0; -const SET_ANGLE: u32 = 1; -const GET_ANGLE: u32 = 2; +const SERVO_NUMBER: u32 = 1; +const SET_ANGLE: u32 = 2; +const GET_ANGLE: u32 = 3; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index 2b21c7ff..8ac130ee 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -9,6 +9,10 @@ fn command() { let servo = Servo::<1>::new(); let value = servo.command(EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); + assert_eq!( + CommandReturn::get_success_u32(&servo.command(SERVO_NUMBER, 0, 0)), + Some(1) + ); assert_eq!( CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), true @@ -28,7 +32,11 @@ fn kernel_integration() { let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 2, 90).get_failure(), + CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_NUMBER, 0, 0)), + Some(1) + ); + assert_eq!( + fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 1, 90).get_failure(), Some(ErrorCode::NoDevice) ); assert_eq!( From 5ebe3e8598c469ae0b1beb90b028bf491cc295ed Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Thu, 26 Sep 2024 12:03:18 +0300 Subject: [PATCH 12/18] update --- apis/interface/servo/src/lib.rs | 6 +++--- apis/interface/servo/src/tests.rs | 4 ++-- examples/servo.rs | 4 ++-- unittest/src/fake/servo/mod.rs | 2 +- unittest/src/fake/servo/tests.rs | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index d55b9c53..21920930 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -15,8 +15,8 @@ impl Servo { } } /// Returns the number of the servomotors available. - pub fn servo_number() -> Result { - S::command(DRIVER_NUM, SERVO_NUMBER, 0, 0).to_result() + pub fn servo_count() -> Result { + S::command(DRIVER_NUM, SERVO_COUNT, 0, 0).to_result() } /// Changes the angle of the servo. @@ -59,6 +59,6 @@ const DRIVER_NUM: u32 = 0x90009; // Command IDs const EXISTS: u32 = 0; -const SERVO_NUMBER: u32 = 1; +const SERVO_COUNT: u32 = 1; const SET_ANGLE: u32 = 2; const GET_ANGLE: u32 = 3; diff --git a/apis/interface/servo/src/tests.rs b/apis/interface/servo/src/tests.rs index 65bc202b..33a778bc 100644 --- a/apis/interface/servo/src/tests.rs +++ b/apis/interface/servo/src/tests.rs @@ -16,11 +16,11 @@ fn exists() { assert_eq!(Servo::exists(), Ok(())); } #[test] -fn servo_number() { +fn servo_count() { let kernel = fake::Kernel::new(); let driver = fake::Servo::<2>::new(); kernel.add_driver(&driver); - assert_eq!(Servo::servo_number(), Ok(2)); + assert_eq!(Servo::servo_count(), Ok(2)); } #[test] fn set_angle() { diff --git a/examples/servo.rs b/examples/servo.rs index 1398c2d3..039fdb2c 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -16,12 +16,12 @@ fn main() { writeln!(Console::writer(), "The driver could not be found").unwrap(); return; } - let servo_number = Servo::servo_number().unwrap(); + let servo_count = Servo::servo_count().unwrap(); writeln!( Console::writer(), "The number of available servomotors is {:?}", - servo_number + servo_count ) .unwrap(); diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs index 7f4e0dee..b7b493a1 100644 --- a/unittest/src/fake/servo/mod.rs +++ b/unittest/src/fake/servo/mod.rs @@ -61,6 +61,6 @@ const DRIVER_NUM: u32 = 0x90009; // Command numbers const EXISTS: u32 = 0; -const SERVO_NUMBER: u32 = 1; +const SERVO_COUNT: u32 = 1; const SET_ANGLE: u32 = 2; const GET_ANGLE: u32 = 3; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index 8ac130ee..95cec333 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -10,7 +10,7 @@ fn command() { let value = servo.command(EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&servo.command(SERVO_NUMBER, 0, 0)), + CommandReturn::get_success_u32(&servo.command(SERVO_COUNT, 0, 0)), Some(1) ); assert_eq!( @@ -32,7 +32,7 @@ fn kernel_integration() { let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_NUMBER, 0, 0)), + CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_COUNT, 0, 0)), Some(1) ); assert_eq!( From 561323bc16d624c23b9d7f7cdf53e5695ff123cb Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Tue, 8 Oct 2024 21:52:37 +0300 Subject: [PATCH 13/18] replacing constants name with command id number for fake tests --- unittest/src/fake/servo/mod.rs | 8 ++++---- unittest/src/fake/servo/tests.rs | 25 +++++++++++-------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs index b7b493a1..e7e12258 100644 --- a/unittest/src/fake/servo/mod.rs +++ b/unittest/src/fake/servo/mod.rs @@ -60,7 +60,7 @@ mod tests; const DRIVER_NUM: u32 = 0x90009; // Command numbers -const EXISTS: u32 = 0; -const SERVO_COUNT: u32 = 1; -const SET_ANGLE: u32 = 2; -const GET_ANGLE: u32 = 3; +// const EXISTS: u32 = 0; +// const SERVO_COUNT: u32 = 1; +// const SET_ANGLE: u32 = 2; +// const GET_ANGLE: u32 = 3; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index 95cec333..c1f9004c 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -7,18 +7,15 @@ use libtock_platform::CommandReturn; fn command() { use fake::SyscallDriver; let servo = Servo::<1>::new(); - let value = servo.command(EXISTS, 0, 0); + let value = servo.command(0, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&servo.command(SERVO_COUNT, 0, 0)), + CommandReturn::get_success_u32(&servo.command(1, 0, 0)), Some(1) ); + assert_eq!(CommandReturn::is_success(&servo.command(2, 0, 90)), true); assert_eq!( - CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), - true - ); - assert_eq!( - CommandReturn::get_success_u32(&servo.command(GET_ANGLE, 0, 0)), + CommandReturn::get_success_u32(&servo.command(3, 0, 0)), Some(90) ); } @@ -29,27 +26,27 @@ fn kernel_integration() { let kernel = fake::Kernel::new(); let servo = Servo::<1>::new(); kernel.add_driver(&servo); - let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); + let value = fake::Syscalls::command(DRIVER_NUM, 0, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_COUNT, 0, 0)), + CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, 1, 0, 0)), Some(1) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 1, 90).get_failure(), + fake::Syscalls::command(DRIVER_NUM, 2, 1, 90).get_failure(), Some(ErrorCode::NoDevice) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 181).get_failure(), + fake::Syscalls::command(DRIVER_NUM, 2, 0, 181).get_failure(), Some(ErrorCode::Fail) ); - assert!(fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 90).is_success()); + assert!(fake::Syscalls::command(DRIVER_NUM, 2, 0, 90).is_success()); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 0, 0).get_success_u32(), + fake::Syscalls::command(DRIVER_NUM, 3, 0, 0).get_success_u32(), Some(90) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 2, 0).get_failure(), + fake::Syscalls::command(DRIVER_NUM, 3, 2, 0).get_failure(), Some(ErrorCode::NoDevice) ); } From c49c30523fb88418b1a850e5e95fd3059fd94cf0 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Wed, 9 Oct 2024 12:25:19 +0300 Subject: [PATCH 14/18] replacing command id number with constants --- unittest/src/fake/servo/mod.rs | 16 ++++++++-------- unittest/src/fake/servo/tests.rs | 25 ++++++++++++++----------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/unittest/src/fake/servo/mod.rs b/unittest/src/fake/servo/mod.rs index e7e12258..aa9c1562 100644 --- a/unittest/src/fake/servo/mod.rs +++ b/unittest/src/fake/servo/mod.rs @@ -24,9 +24,9 @@ impl crate::fake::SyscallDriver for Servo { fn command(&self, command_num: u32, servo_index: u32, angle: u32) -> CommandReturn { match command_num { - 0 => crate::command_return::success(), - 1 => crate::command_return::success_u32(NUM_SERVO as u32), - 2 => { + EXISTS => crate::command_return::success(), + SERVO_COUNT => crate::command_return::success_u32(NUM_SERVO as u32), + SET_ANGLE => { if servo_index >= NUM_SERVO as u32 { crate::command_return::failure(ErrorCode::NoDevice) } else if angle <= 180 { @@ -37,7 +37,7 @@ impl crate::fake::SyscallDriver for Servo { } } // Return the current angle. - 3 => { + GET_ANGLE => { if servo_index >= NUM_SERVO as u32 { crate::command_return::failure(ErrorCode::NoDevice) } else { @@ -60,7 +60,7 @@ mod tests; const DRIVER_NUM: u32 = 0x90009; // Command numbers -// const EXISTS: u32 = 0; -// const SERVO_COUNT: u32 = 1; -// const SET_ANGLE: u32 = 2; -// const GET_ANGLE: u32 = 3; +const EXISTS: u32 = 0; +const SERVO_COUNT: u32 = 1; +const SET_ANGLE: u32 = 2; +const GET_ANGLE: u32 = 3; diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index c1f9004c..95cec333 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -7,15 +7,18 @@ use libtock_platform::CommandReturn; fn command() { use fake::SyscallDriver; let servo = Servo::<1>::new(); - let value = servo.command(0, 0, 0); + let value = servo.command(EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&servo.command(1, 0, 0)), + CommandReturn::get_success_u32(&servo.command(SERVO_COUNT, 0, 0)), Some(1) ); - assert_eq!(CommandReturn::is_success(&servo.command(2, 0, 90)), true); assert_eq!( - CommandReturn::get_success_u32(&servo.command(3, 0, 0)), + CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), + true + ); + assert_eq!( + CommandReturn::get_success_u32(&servo.command(GET_ANGLE, 0, 0)), Some(90) ); } @@ -26,27 +29,27 @@ fn kernel_integration() { let kernel = fake::Kernel::new(); let servo = Servo::<1>::new(); kernel.add_driver(&servo); - let value = fake::Syscalls::command(DRIVER_NUM, 0, 0, 0); + let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); assert_eq!(CommandReturn::is_success(&value), true); assert_eq!( - CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, 1, 0, 0)), + CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_COUNT, 0, 0)), Some(1) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, 2, 1, 90).get_failure(), + fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 1, 90).get_failure(), Some(ErrorCode::NoDevice) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, 2, 0, 181).get_failure(), + fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 181).get_failure(), Some(ErrorCode::Fail) ); - assert!(fake::Syscalls::command(DRIVER_NUM, 2, 0, 90).is_success()); + assert!(fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 90).is_success()); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, 3, 0, 0).get_success_u32(), + fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 0, 0).get_success_u32(), Some(90) ); assert_eq!( - fake::Syscalls::command(DRIVER_NUM, 3, 2, 0).get_failure(), + fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 2, 0).get_failure(), Some(ErrorCode::NoDevice) ); } From 2c9c1efffe722a251c711c52c7f3896a2f4243d9 Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Thu, 10 Oct 2024 16:47:05 +0300 Subject: [PATCH 15/18] update --- apis/interface/servo/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index 21920930..bcd85887 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -8,7 +8,7 @@ impl Servo { /// Check whether the driver exists. pub fn exists() -> Result<(), ErrorCode> { let val = S::command(DRIVER_NUM, EXISTS, 0, 0).is_success(); - if val == true { + if val { Ok(()) } else { Err(ErrorCode::Fail) From 1f37cbb15ff114317241534e57e208a83356d73a Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Thu, 10 Oct 2024 18:00:56 +0300 Subject: [PATCH 16/18] update --- apis/interface/servo/src/lib.rs | 4 ++-- unittest/src/fake/servo/tests.rs | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index bcd85887..825c241b 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -25,11 +25,11 @@ impl Servo { /// - `Ok(())`: The attempt at changing the angle was successful. /// - `FAIL`: Cannot change the angle. /// - `INVAL`: The value exceeds u16, indicating it's incorrect - /// since servomotors can only have a maximum of 360 degrees. + /// since servomotors can only have a maximum of 360 degrees. /// - `NODEVICE`: The index exceeds the number of servomotors provided. /// # Arguments /// - `angle` - the variable that receives the angle - /// (in degrees from 0 to 180) from the servo driver. + /// (in degrees from 0 to 180) from the servo driver. /// - `index` - the variable that receives the index of the servomotor. pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> { S::command(DRIVER_NUM, SET_ANGLE, index, angle).to_result() diff --git a/unittest/src/fake/servo/tests.rs b/unittest/src/fake/servo/tests.rs index 95cec333..b05f6a00 100644 --- a/unittest/src/fake/servo/tests.rs +++ b/unittest/src/fake/servo/tests.rs @@ -8,15 +8,12 @@ fn command() { use fake::SyscallDriver; let servo = Servo::<1>::new(); let value = servo.command(EXISTS, 0, 0); - assert_eq!(CommandReturn::is_success(&value), true); + assert!(CommandReturn::is_success(&value)); assert_eq!( CommandReturn::get_success_u32(&servo.command(SERVO_COUNT, 0, 0)), Some(1) ); - assert_eq!( - CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)), - true - ); + assert!(CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)),); assert_eq!( CommandReturn::get_success_u32(&servo.command(GET_ANGLE, 0, 0)), Some(90) @@ -30,7 +27,7 @@ fn kernel_integration() { let servo = Servo::<1>::new(); kernel.add_driver(&servo); let value = fake::Syscalls::command(DRIVER_NUM, EXISTS, 0, 0); - assert_eq!(CommandReturn::is_success(&value), true); + assert!(CommandReturn::is_success(&value)); assert_eq!( CommandReturn::get_success_u32(&fake::Syscalls::command(DRIVER_NUM, SERVO_COUNT, 0, 0)), Some(1) From 9a9fce7ed1c7562631c15f6c6f92419372e96a3a Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Tue, 15 Oct 2024 12:21:24 +0300 Subject: [PATCH 17/18] comments update --- apis/interface/servo/src/lib.rs | 24 +++++++++++++++--------- apis/interface/servo/src/tests.rs | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/apis/interface/servo/src/lib.rs b/apis/interface/servo/src/lib.rs index 825c241b..abc65f4e 100644 --- a/apis/interface/servo/src/lib.rs +++ b/apis/interface/servo/src/lib.rs @@ -15,34 +15,40 @@ impl Servo { } } /// Returns the number of the servomotors available. - pub fn servo_count() -> Result { + pub fn count() -> Result { S::command(DRIVER_NUM, SERVO_COUNT, 0, 0).to_result() } /// Changes the angle of the servo. - /// Return values: /// + /// # Arguments + /// - `angle` - the variable that receives the angle + /// (in degrees from 0 to 180) from the servo driver. + /// - `index` - the variable that receives the index of the servomotor. + /// + /// # Return values: /// - `Ok(())`: The attempt at changing the angle was successful. + /// + /// # Errors: /// - `FAIL`: Cannot change the angle. /// - `INVAL`: The value exceeds u16, indicating it's incorrect /// since servomotors can only have a maximum of 360 degrees. /// - `NODEVICE`: The index exceeds the number of servomotors provided. - /// # Arguments - /// - `angle` - the variable that receives the angle - /// (in degrees from 0 to 180) from the servo driver. - /// - `index` - the variable that receives the index of the servomotor. pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> { S::command(DRIVER_NUM, SET_ANGLE, index, angle).to_result() } /// Returns the angle of the servo. - /// Return values: /// + /// # Arguments + /// - `index` - the variable that receives the index of the servomotor. + /// + /// # Return values: /// - `angle`: The value, in angles from 0 to 360, of the servo. + /// + /// # Errors: /// - `NOSUPPORT`: The servo cannot return its angle. /// - `NODEVICE`: The index exceeds the number of servomotors provided. - /// # Arguments - /// - `index` - the variable that receives the index of the servomotor. pub fn get_angle(index: u32) -> Result { S::command(DRIVER_NUM, GET_ANGLE, index, 0).to_result() } diff --git a/apis/interface/servo/src/tests.rs b/apis/interface/servo/src/tests.rs index 33a778bc..79a256ed 100644 --- a/apis/interface/servo/src/tests.rs +++ b/apis/interface/servo/src/tests.rs @@ -16,11 +16,11 @@ fn exists() { assert_eq!(Servo::exists(), Ok(())); } #[test] -fn servo_count() { +fn count() { let kernel = fake::Kernel::new(); let driver = fake::Servo::<2>::new(); kernel.add_driver(&driver); - assert_eq!(Servo::servo_count(), Ok(2)); + assert_eq!(Servo::count(), Ok(2)); } #[test] fn set_angle() { From 813ebe9471cf27b00094ce69169ae6750c44fd1d Mon Sep 17 00:00:00 2001 From: inesmaria08 Date: Tue, 15 Oct 2024 12:25:33 +0300 Subject: [PATCH 18/18] update --- examples/servo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/servo.rs b/examples/servo.rs index 039fdb2c..83c44ab1 100644 --- a/examples/servo.rs +++ b/examples/servo.rs @@ -16,12 +16,12 @@ fn main() { writeln!(Console::writer(), "The driver could not be found").unwrap(); return; } - let servo_count = Servo::servo_count().unwrap(); + let count = Servo::count().unwrap(); writeln!( Console::writer(), "The number of available servomotors is {:?}", - servo_count + count ) .unwrap();