2
2
#include < algorithm>
3
3
4
4
namespace pimoroni {
5
- lookup::lookup (std::initializer_list<uint16_t > values) : lut(values) {
6
- }
7
-
8
- uint8_t lookup::index (uint16_t value) {
9
- auto it = find (lut.begin (), lut.end (), value);
10
-
11
- if (it == lut.end ())
12
- return 0 ;
13
-
14
- return it - lut.begin ();
15
- }
16
-
17
- uint16_t lookup::value (uint8_t index) {
18
- return lut[index];
19
- }
20
-
21
- pimoroni::lookup LTR559::lookup_led_current ({5 , 10 , 20 , 50 , 100 });
22
- pimoroni::lookup LTR559::lookup_led_duty_cycle ({25 , 50 , 75 , 100 });
23
- pimoroni::lookup LTR559::lookup_led_pulse_freq ({30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 });
24
- pimoroni::lookup LTR559::lookup_proximity_meas_rate ({10 , 50 , 70 , 100 , 200 , 500 , 1000 , 2000 });
25
- pimoroni::lookup LTR559::lookup_light_integration_time ({100 , 50 , 200 , 400 , 150 , 250 , 300 , 350 });
26
- pimoroni::lookup LTR559::lookup_light_repeat_rate ({50 , 100 , 200 , 500 , 1000 , 2000 });
27
- pimoroni::lookup LTR559::lookup_light_gain ({1 , 2 , 4 , 8 , 0 , 0 , 48 , 96 });
28
-
29
5
bool LTR559::init () {
30
6
if (interrupt != PIN_UNUSED) {
31
7
gpio_set_function (interrupt, GPIO_FUNC_SIO);
@@ -129,7 +105,7 @@ namespace pimoroni {
129
105
i2c->read_bytes (address, LTR559_ALS_DATA_CH1, (uint8_t *)&als, 4 );
130
106
data.als0 = als[1 ];
131
107
data.als1 = als[0 ];
132
- data.gain = this -> lookup_light_gain . value (( status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK) ;
108
+ data.gain = lookup_light_gain[( status >> LTR559_ALS_PS_STATUS_ALS_GAIN_SHIFT) & LTR559_ALS_PS_STATUS_ALS_GAIN_MASK] ;
133
109
134
110
data.ratio = 101 .0f ;
135
111
@@ -163,12 +139,12 @@ namespace pimoroni {
163
139
}
164
140
165
141
void LTR559::proximity_led (uint8_t current, uint8_t duty_cycle, uint8_t pulse_freq, uint8_t num_pulses) {
166
- current = lookup_led_current. index (current);
142
+ current = lookup< lookup_led_current> (current);
167
143
168
- duty_cycle = lookup_led_duty_cycle. index (duty_cycle);
144
+ duty_cycle = lookup< lookup_led_duty_cycle> (duty_cycle);
169
145
duty_cycle <<= LTR559_PS_LED_DUTY_CYCLE_SHIFT;
170
146
171
- pulse_freq = lookup_led_pulse_freq. index (pulse_freq);
147
+ pulse_freq = lookup< lookup_led_pulse_freq> (pulse_freq);
172
148
pulse_freq <<= LTR559_PS_LED_PULSE_FREQ_SHIFT;
173
149
174
150
uint8_t buf = current | duty_cycle | pulse_freq;
@@ -180,7 +156,7 @@ namespace pimoroni {
180
156
181
157
void LTR559::light_control (bool active, uint8_t gain) {
182
158
uint8_t buf = 0 ;
183
- gain = lookup_light_gain. index (gain);
159
+ gain = lookup< lookup_light_gain> (gain);
184
160
buf |= gain << LTR559_ALS_CONTROL_GAIN_SHIFT;
185
161
186
162
if (active)
@@ -223,16 +199,16 @@ namespace pimoroni {
223
199
224
200
void LTR559::light_measurement_rate (uint16_t integration_time, uint16_t rate) {
225
201
data.integration_time = integration_time;
226
- integration_time = lookup_light_integration_time. index (integration_time);
227
- rate = lookup_light_repeat_rate. index (rate);
202
+ integration_time = lookup< lookup_light_integration_time> (integration_time);
203
+ rate = lookup< lookup_light_repeat_rate> (rate);
228
204
uint8_t buf = 0 ;
229
205
buf |= rate;
230
206
buf |= integration_time << LTR559_ALS_MEAS_RATE_INTEGRATION_TIME_SHIFT;
231
207
i2c->write_bytes (address, LTR559_ALS_MEAS_RATE, &buf, 1 );
232
208
}
233
209
234
210
void LTR559::proximity_measurement_rate (uint16_t rate) {
235
- uint8_t buf = lookup_proximity_meas_rate. index (rate);
211
+ uint8_t buf = lookup< lookup_proximity_meas_rate> (rate);
236
212
i2c->write_bytes (address, LTR559_PS_MEAS_RATE, &buf, 1 );
237
213
}
238
214
0 commit comments