use super::{Sensor, SensorPort};
use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result};
#[derive(Debug, Clone, Device, Sensor)]
pub struct CompassSensor {
driver: Driver,
origin: i32, }
impl CompassSensor {
fn new(driver: Driver) -> Self {
Self { driver, origin: 0 }
}
findable!(
"lego-sensor",
["ht-nxt-compass"],
SensorPort,
"Compass",
"in"
);
pub const COMMAND_START_CALIBRATION: &'static str = "BEGIN-CAL";
pub const COMMAND_STOP_CALIBRATION: &'static str = "END-CAL";
pub fn get_rotation(&self) -> Ev3Result<i32> {
self.get_value0()
}
pub fn set_zero(&mut self) -> Ev3Result<()> {
self.origin = self.get_rotation()?;
Ok(())
}
pub fn get_relative_rotation(&self) -> Ev3Result<i32> {
let pos = self.get_rotation()?;
let mut rel_rot = pos - self.origin;
if rel_rot < 0 {
rel_rot += 360;
}
Ok(rel_rot)
}
pub fn start_calibration(&self) -> Ev3Result<()> {
self.set_command(Self::COMMAND_START_CALIBRATION)
}
pub fn stop_calibration(&self) -> Ev3Result<()> {
self.set_command(Self::COMMAND_STOP_CALIBRATION)
}
}