1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::cell::Cell;
use super::SensorPort;
use crate::{Attribute, Device, Driver, Ev3Error, Ev3Result};
#[derive(Debug, Clone, Device)]
pub struct LightSensor {
driver: Driver,
reflect_scale: Cell<Option<f32>>,
ambient_scale: Cell<Option<f32>>,
}
impl LightSensor {
fn new(driver: Driver) -> Self {
Self {
driver,
reflect_scale: Cell::new(None),
ambient_scale: Cell::new(None),
}
}
findable!(
"lego-sensor",
"lego-nxt-light",
SensorPort,
"LightSensor",
"in"
);
pub const MODE_REFLECT: &'static str = "REFLECT";
pub const MODE_AMBIENT: &'static str = "AMBIENT";
sensor!();
pub fn set_mode_reflect(&self) -> Ev3Result<()> {
self.set_mode(Self::MODE_REFLECT)
}
pub fn set_mode_ambient(&self) -> Ev3Result<()> {
self.set_mode(Self::MODE_AMBIENT)
}
pub fn get_light_intensity(&self) -> Ev3Result<i32> {
self.get_value0()
}
pub fn get_reflected_light_intensity(&self) -> Ev3Result<f32> {
let scale_field = self.reflect_scale.get();
let scale = match scale_field {
Some(s) => s,
None => {
let decimals = self.get_decimals()?;
let s = 10f32.powi(-decimals);
self.reflect_scale.set(Some(s));
s
}
};
Ok((self.get_value0()? as f32) * scale)
}
pub fn get_ambient_light_intensity(&self) -> Ev3Result<f32> {
let scale_field = self.ambient_scale.get();
let scale = match scale_field {
Some(s) => s,
None => {
let decimals = self.get_decimals()?;
let s = 10f32.powi(-decimals);
self.ambient_scale.set(Some(s));
s
}
};
Ok((self.get_value0()? as f32) * scale)
}
}