-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcb8809
commit 372e94b
Showing
6 changed files
with
156 additions
and
9 deletions.
There are no files selected for viewing
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 @@ | ||
cargo-features = ["edition2024"] | ||
[package] | ||
name = "libtock_servo" | ||
version = "0.1.0" | ||
|
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,32 @@ | ||
use libtock_platform::ErrorCode; | ||
use libtock_unittest::fake; | ||
|
||
type Servo = super::Servo<fake::Syscalls>; | ||
|
||
#[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)); | ||
} |
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,64 @@ | ||
use std::cell::Cell; | ||
|
||
use crate::DriverInfo; | ||
use libtock_platform::{CommandReturn, ErrorCode}; | ||
|
||
pub struct Servo<const NUM_SERVO: usize> { | ||
servo: [Cell<u16>; NUM_SERVO], | ||
} | ||
|
||
impl<const NUM_SERVO: usize> Servo<NUM_SERVO> { | ||
pub fn new() -> std::rc::Rc<Servo<NUM_SERVO>> { | ||
#[allow(clippy::declare_interior_mutable_const)] | ||
const ANGLE: Cell<u16> = Cell::new(0); | ||
std::rc::Rc::new(Servo { | ||
servo: [ANGLE; NUM_SERVO], | ||
}) | ||
} | ||
} | ||
|
||
impl<const NUM_SERVO: usize> crate::fake::SyscallDriver for Servo<NUM_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; |
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,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) | ||
); | ||
} |