ev3dev_lang_rust/sensors/
compass_sensor.rs1use super::{Sensor, SensorPort};
4use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result};
5
6#[derive(Debug, Clone, Device, Sensor)]
8pub struct CompassSensor {
9 driver: Driver,
10 origin: i32, }
12
13impl CompassSensor {
14 fn new(driver: Driver) -> Self {
15 Self { driver, origin: 0 }
16 }
17
18 findable!(
19 "lego-sensor",
20 ["ht-nxt-compass"],
21 SensorPort,
22 "Compass",
23 "in"
24 );
25
26 pub const COMMAND_START_CALIBRATION: &'static str = "BEGIN-CAL";
28
29 pub const COMMAND_STOP_CALIBRATION: &'static str = "END-CAL";
31
32 pub fn get_rotation(&self) -> Ev3Result<i32> {
36 self.get_value0()
37 }
38
39 pub fn set_zero(&mut self) -> Ev3Result<()> {
41 self.origin = self.get_rotation()?;
42 Ok(())
43 }
44
45 pub fn get_relative_rotation(&self) -> Ev3Result<i32> {
47 let pos = self.get_rotation()?;
48 let mut rel_rot = pos - self.origin;
49 if rel_rot < 0 {
50 rel_rot += 360;
51 }
52 Ok(rel_rot)
53 }
54
55 pub fn start_calibration(&self) -> Ev3Result<()> {
63 self.set_command(Self::COMMAND_START_CALIBRATION)
64 }
65
66 pub fn stop_calibration(&self) -> Ev3Result<()> {
68 self.set_command(Self::COMMAND_STOP_CALIBRATION)
69 }
70}