diff --git a/Chapter05/build.rs b/Chapter05/build.rs index 3ef607f..1208988 100644 --- a/Chapter05/build.rs +++ b/Chapter05/build.rs @@ -11,14 +11,12 @@ fn main() { Command::new("ar").args(&["crus", "libmotor1.a", "motor1.o"]) .current_dir(&Path::new(&out_dir)) .status().unwrap(); - Command::new("gcc").args(&["src/motor2.c", "-c", "-fPIC", "-o"]) .arg(&format!("{}/motor2.o", out_dir)) .status().unwrap(); Command::new("ar").args(&["crus", "libmotor2.a", "motor2.o"]) .current_dir(&Path::new(&out_dir)) .status().unwrap(); - Command::new("gcc").args(&["src/motor3.c", "-c", "-fPIC", "-o"]) .arg(&format!("{}/motor3.o", out_dir)) .status().unwrap(); @@ -26,8 +24,30 @@ fn main() { .current_dir(&Path::new(&out_dir)) .status().unwrap(); + Command::new("gcc").args(&["src/elevator1.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/elevator1.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libelevator1.a", "elevator1.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + Command::new("gcc").args(&["src/elevator2.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/elevator2.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libelevator2.a", "elevator2.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + Command::new("gcc").args(&["src/elevator3.c", "-c", "-fPIC", "-o"]) + .arg(&format!("{}/elevator3.o", out_dir)) + .status().unwrap(); + Command::new("ar").args(&["crus", "libelevator3.a", "elevator3.o"]) + .current_dir(&Path::new(&out_dir)) + .status().unwrap(); + println!("cargo:rustc-link-search=native={}", out_dir); println!("cargo:rustc-link-lib=static=motor1"); println!("cargo:rustc-link-lib=static=motor2"); println!("cargo:rustc-link-lib=static=motor3"); + println!("cargo:rustc-link-lib=static=elevator1"); + println!("cargo:rustc-link-lib=static=elevator2"); + println!("cargo:rustc-link-lib=static=elevator3"); } diff --git a/Chapter05/src/elevator1.c b/Chapter05/src/elevator1.c new file mode 100644 index 0000000..f2f0ccb --- /dev/null +++ b/Chapter05/src/elevator1.c @@ -0,0 +1,6 @@ + +int elevator1_poll_floor_request() +{ + //real implementation would interface with hardware here + return 0; +} diff --git a/Chapter05/src/elevator2.c b/Chapter05/src/elevator2.c new file mode 100644 index 0000000..f2f0ccb --- /dev/null +++ b/Chapter05/src/elevator2.c @@ -0,0 +1,6 @@ + +int elevator1_poll_floor_request() +{ + //real implementation would interface with hardware here + return 0; +} diff --git a/Chapter05/src/elevator3.c b/Chapter05/src/elevator3.c new file mode 100644 index 0000000..f2f0ccb --- /dev/null +++ b/Chapter05/src/elevator3.c @@ -0,0 +1,6 @@ + +int elevator1_poll_floor_request() +{ + //real implementation would interface with hardware here + return 0; +} diff --git a/Chapter05/src/elevator_drivers.rs b/Chapter05/src/elevator_drivers.rs index e69de29..3fb8c97 100644 --- a/Chapter05/src/elevator_drivers.rs +++ b/Chapter05/src/elevator_drivers.rs @@ -0,0 +1,69 @@ +use libc::c_int; + +#[link(name = "elevator1")] +extern { + pub fn elevator1_poll_floor_request() -> c_int; +} + +#[link(name = "elevator2")] +extern { + pub fn elevator2_poll_floor_request() -> c_int; +} + +#[link(name = "elevator3")] +extern { + pub fn elevator3_poll_floor_request() -> c_int; +} + +pub trait ElevatorDriver +{ + fn poll_floor_request(&self) -> Option; +} + +pub struct ElevatorDriver1; +impl ElevatorDriver for ElevatorDriver1 +{ + fn poll_floor_request(&self) -> Option + { + unsafe { + let req = elevator1_poll_floor_request(); + if req > 0 { + Some(req as u64) + } else { + None + } + } + } +} + +pub struct ElevatorDriver2; +impl ElevatorDriver for ElevatorDriver2 +{ + fn poll_floor_request(&self) -> Option + { + unsafe { + let req = elevator2_poll_floor_request(); + if req > 0 { + Some(req as u64) + } else { + None + } + } + } +} + +pub struct ElevatorDriver3; +impl ElevatorDriver for ElevatorDriver3 +{ + fn poll_floor_request(&self) -> Option + { + unsafe { + let req = elevator3_poll_floor_request(); + if req > 0 { + Some(req as u64) + } else { + None + } + } + } +} diff --git a/Chapter05/src/motor_controllers.rs b/Chapter05/src/motor_controllers.rs index c107f13..c62189d 100644 --- a/Chapter05/src/motor_controllers.rs +++ b/Chapter05/src/motor_controllers.rs @@ -25,13 +25,13 @@ pub enum MotorInput pub trait MotorDriver { - fn adjust_motor(input: MotorInput); + fn adjust_motor(&self, input: MotorInput); } struct Motor1; impl MotorDriver for Motor1 { - fn adjust_motor(input: MotorInput) + fn adjust_motor(&self, input: MotorInput) { if let MotorInput::Motor1 { target_force: target_force } = input { unsafe { @@ -44,9 +44,9 @@ impl MotorDriver for Motor1 struct Motor2; impl MotorDriver for Motor2 { - fn adjust_motor(input: MotorInput) + fn adjust_motor(&self, input: MotorInput) { - if let MotorInput::Motor1 { target_force: target_force } = input { + if let MotorInput::Motor2 { target_force: target_force } = input { unsafe { motor2_adjust_motor(target_force); } @@ -57,12 +57,90 @@ impl MotorDriver for Motor2 struct Motor3; impl MotorDriver for Motor3 { - fn adjust_motor(input: MotorInput) + fn adjust_motor(&self, input: MotorInput) { - if let MotorInput::Motor1 { target_force: target_force } = input { + if let MotorInput::Motor3 { target_force: target_force } = input { unsafe { motor3_adjust_motor(target_force); } } } } + +pub trait MotorController +{ + fn adjust_motor(&self, f: f64); + fn max_force(&self) -> f64; +} + +pub struct MotorController1 +{ + motor: Motor1 +} +pub fn newMotorController1() -> Box +{ + Box::new(MotorController1 { + motor: Motor1 + }) +} +impl MotorController for MotorController1 +{ + fn adjust_motor(&self, f: f64) + { + self.motor.adjust_motor(MotorInput::Motor1 { + target_force: f + }) + } + fn max_force(&self) -> f64 + { + 50000.0 + } +} + +pub struct MotorController2 +{ + motor: Motor2 +} +pub fn newMotorController2() -> Box +{ + Box::new(MotorController2 { + motor: Motor2 + }) +} +impl MotorController for MotorController2 +{ + fn adjust_motor(&self, f: f64) + { + self.motor.adjust_motor(MotorInput::Motor2 { + target_force: f + }) + } + fn max_force(&self) -> f64 + { + 100000.0 + } +} + +pub struct MotorController3 +{ + motor: Motor3 +} +pub fn newMotorController3() -> Box +{ + Box::new(MotorController3 { + motor: Motor3 + }) +} +impl MotorController for MotorController3 +{ + fn adjust_motor(&self, f: f64) + { + self.motor.adjust_motor(MotorInput::Motor3 { + target_force: f + }) + } + fn max_force(&self) -> f64 + { + 90000.0 + } +}