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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use crate::correction::{correct_high_lux, get_lux_raw_conversion_factor};
use crate::{
calculate_raw_threshold_value, Config, Error, FaultCount, Gain, IntegrationTime,
InterruptStatus, PowerSavingMode, Veml7700, DEVICE_ADDRESS,
};
use embedded_hal::blocking::i2c;
struct Register;
impl Register {
const ALS_CONF: u8 = 0x00;
const ALS_WH: u8 = 0x01;
const ALS_WL: u8 = 0x02;
const PSM: u8 = 0x03;
const ALS: u8 = 0x04;
const WHITE: u8 = 0x05;
const ALS_INT: u8 = 0x06;
}
struct BitFlags;
impl BitFlags {
const ALS_SD: u16 = 0x01;
const ALS_INT_EN: u16 = 0x02;
const PSM_EN: u16 = 0x01;
const INT_TH_LOW: u16 = 1 << 15;
const INT_TH_HIGH: u16 = 1 << 14;
}
impl Config {
fn with_high(self, mask: u16) -> Self {
Config {
bits: self.bits | mask,
}
}
fn with_low(self, mask: u16) -> Self {
Config {
bits: self.bits & !mask,
}
}
}
impl<I2C, E> Veml7700<I2C>
where
I2C: i2c::Write<Error = E>,
{
pub fn new(i2c: I2C) -> Self {
Veml7700 {
i2c,
config: Config {
bits: BitFlags::ALS_SD,
},
gain: Gain::One,
it: IntegrationTime::_100ms,
}
}
pub fn destroy(self) -> I2C {
self.i2c
}
}
impl<I2C, E> Veml7700<I2C>
where
I2C: i2c::Write<Error = E>,
{
pub fn enable(&mut self) -> Result<(), Error<E>> {
let config = self.config.with_low(BitFlags::ALS_SD);
self.set_config(config)
}
pub fn disable(&mut self) -> Result<(), Error<E>> {
let config = self.config.with_high(BitFlags::ALS_SD);
self.set_config(config)
}
pub fn set_integration_time(&mut self, it: IntegrationTime) -> Result<(), Error<E>> {
let mask = match it {
IntegrationTime::_25ms => 0b1100,
IntegrationTime::_50ms => 0b1000,
IntegrationTime::_100ms => 0b0000,
IntegrationTime::_200ms => 0b0001,
IntegrationTime::_400ms => 0b0010,
IntegrationTime::_800ms => 0b0011,
};
let config = self.config.bits & !(0b1111 << 6) | (mask << 6);
self.set_config(Config { bits: config })?;
self.it = it;
Ok(())
}
pub fn set_gain(&mut self, gain: Gain) -> Result<(), Error<E>> {
let mask = match gain {
Gain::One => 0,
Gain::Two => 1,
Gain::OneEighth => 2,
Gain::OneQuarter => 3,
};
let config = self.config.bits & !(0b11 << 11) | mask << 11;
self.set_config(Config { bits: config })?;
self.gain = gain;
Ok(())
}
pub fn set_fault_count(&mut self, fc: FaultCount) -> Result<(), Error<E>> {
let mask = match fc {
FaultCount::One => 0,
FaultCount::Two => 1,
FaultCount::Four => 2,
FaultCount::Eight => 3,
};
let config = self.config.bits & !(0b11 << 4) | mask << 4;
self.set_config(Config { bits: config })
}
pub fn enable_interrupts(&mut self) -> Result<(), Error<E>> {
let config = self.config.with_high(BitFlags::ALS_INT_EN);
self.set_config(config)
}
pub fn disable_interrupts(&mut self) -> Result<(), Error<E>> {
let config = self.config.with_low(BitFlags::ALS_INT_EN);
self.set_config(config)
}
pub fn set_high_threshold_raw(&mut self, threshold: u16) -> Result<(), Error<E>> {
self.write_register(Register::ALS_WH, threshold)
}
pub fn set_low_threshold_raw(&mut self, threshold: u16) -> Result<(), Error<E>> {
self.write_register(Register::ALS_WL, threshold)
}
pub fn set_high_threshold_lux(&mut self, lux: f32) -> Result<(), Error<E>> {
let raw = self.calculate_raw_threshold_value(lux);
self.write_register(Register::ALS_WH, raw)
}
pub fn set_low_threshold_lux(&mut self, lux: f32) -> Result<(), Error<E>> {
let raw = self.calculate_raw_threshold_value(lux);
self.write_register(Register::ALS_WL, raw)
}
pub fn calculate_raw_threshold_value(&self, lux: f32) -> u16 {
calculate_raw_threshold_value(self.it, self.gain, lux)
}
pub fn enable_power_saving(&mut self, psm: PowerSavingMode) -> Result<(), Error<E>> {
let mask = match psm {
PowerSavingMode::One => 0,
PowerSavingMode::Two => 1,
PowerSavingMode::Three => 2,
PowerSavingMode::Four => 3,
};
let value = BitFlags::PSM_EN | mask << 1;
self.write_register(Register::PSM, value)
}
pub fn disable_power_saving(&mut self) -> Result<(), Error<E>> {
self.write_register(Register::PSM, 0)
}
fn set_config(&mut self, config: Config) -> Result<(), Error<E>> {
self.write_register(Register::ALS_CONF, config.bits)?;
self.config = config;
Ok(())
}
fn write_register(&mut self, register: u8, value: u16) -> Result<(), Error<E>> {
self.i2c
.write(DEVICE_ADDRESS, &[register, value as u8, (value >> 8) as u8])
.map_err(Error::I2C)
}
}
impl<I2C, E> Veml7700<I2C>
where
I2C: i2c::WriteRead<Error = E>,
{
pub fn read_interrupt_status(&mut self) -> Result<InterruptStatus, Error<E>> {
let data = self.read_register(Register::ALS_INT)?;
Ok(InterruptStatus {
was_too_low: (data & BitFlags::INT_TH_LOW) != 0,
was_too_high: (data & BitFlags::INT_TH_HIGH) != 0,
})
}
pub fn read_raw(&mut self) -> Result<u16, Error<E>> {
self.read_register(Register::ALS)
}
pub fn read_lux(&mut self) -> Result<f32, Error<E>> {
let raw = self.read_register(Register::ALS)?;
Ok(self.convert_raw_als_to_lux(raw))
}
pub fn convert_raw_als_to_lux(&self, raw_als: u16) -> f32 {
convert_raw_als_to_lux(self.it, self.gain, raw_als)
}
pub fn read_white(&mut self) -> Result<u16, Error<E>> {
self.read_register(Register::WHITE)
}
fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> {
let mut data = [0; 2];
self.i2c
.write_read(DEVICE_ADDRESS, &[register], &mut data)
.map_err(Error::I2C)
.and(Ok(u16::from(data[0]) | u16::from(data[1]) << 8))
}
}
pub fn convert_raw_als_to_lux(it: IntegrationTime, gain: Gain, raw_als: u16) -> f32 {
let factor = get_lux_raw_conversion_factor(it, gain);
let lux = f32::from(raw_als) * f32::from(factor);
if (gain == Gain::OneQuarter || gain == Gain::OneEighth) && lux > 1000.0 {
correct_high_lux(lux) as f32
} else {
lux as f32
}
}