blib/lib/Device/Chip/TSL256x.pm

filter: Statementstmt 65.9% 29 / 44 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 68.4% 39 / 57 CC 1 SCAR 2.1
CC
1
Cov
66%
I2C_options
2.0
_read
2.0
_write
2.0
CRAP 1.2
« Build ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2016-2021 -- leonerd@leonerd.org.uk
5  
6 8 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Object::Pad 0.57;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Device::Chip::TSL256x 0.07;
10 class Device::Chip::TSL256x
11 1 ▶ :isa(Device::Chip);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Device::Chip::Sensor -declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use Data::Bitfield qw( bitfield enumfield );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use constant PROTOCOL => "I2C";
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =encoding UTF-8
22  
23 =head1 NAME
24  
25 C<Device::Chip::TSL256x> - chip driver for F<TSL256x>
26  
27 =head1 SYNOPSIS
28  
29 use Device::Chip::TSL256x;
30 use Future::AsyncAwait;
31  
32 my $chip = Device::Chip::TSL256x->new;
33 await $chip->mount( Device::Chip::Adapter::...->new );
34  
35 await $chip->power(1);
36  
37 sleep 1; # Wait for one integration cycle
38  
39 printf "Current ambient light level is %.2f lux\n",
40 scalar await $chip->read_lux;
41  
42 =head1 DESCRIPTION
43  
44 This L<Device::Chip> subclass provides specific communication to a F<TAOS>
45 F<TSL2560> or F<TSL2561> attached to a computer via an I²C adapter.
46  
47 The reader is presumed to be familiar with the general operation of this chip;
48 the documentation here will not attempt to explain or define chip-specific
49 concepts or features, only the use of this module to access them.
50  
51 =cut
52  
53 method I2C_options
54 {
55 return (
56 0 ▶ addr => 0x39,
Subroutine I2C_options: not called CC=1 SCAR=6.9
57 max_bitrate => 100E3,
58 );
59 }
60  
61 use constant {
62 # Mask bits for the command byte
63 8 CMD_MASK => 1<<7,
64 CMD_CLEAR => 1<<6,
65 CMD_WORD => 1<<5,
66 CMD_BLOCK => 1<<4,
67 8 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 use constant {
70 8 REG_CONTROL => 0x00,
71 REG_TIMING => 0x01,
72 REG_THRESHLOW => 0x02, # 16bit
73 REG_THRESHHIGH => 0x04, # 16bit
74 REG_INTERRUPT => 0x06,
75 REG_ID => 0x0A,
76 REG_DATA0 => 0x0C, # 16bit
77 REG_DATA1 => 0x0E, # 16bit
78 8 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 bitfield { format => "bytes-LE" }, CONFIG =>
81 POWER => enumfield( 0, qw( OFF . . ON )),
82 GAIN => enumfield( 1*8+4, qw( 1 16 )),
83 INTEG => enumfield( 1*8+0, qw( 13ms 101ms 402ms ));
84  
85 0 ▶async method _read ( $addr, $len )
Subroutine _read: not called CC=1 SCAR=6.9
86 0 {
87 0 return await $self->protocol->write_then_read(
88 ( pack "C", CMD_MASK | ( $addr & 0x0f ) ), $len
89 );
90 }
91  
92 0 ▶async method _write ( $addr, $data )
Subroutine _write: not called CC=1 SCAR=6.9
93 0 {
94 0 await $self->protocol->write(
95 pack "C a*", CMD_MASK | ( $addr & 0x0f ), $data
96 );
97 }
98  
99 =head1 ACCESSORS
100  
101 The following methods documented in an C<await> expression return L<Future>
102 instances.
103  
104 =cut
105  
106 =head2 read_config
107  
108 $config = await $chip->read_config;
109  
110 Returns a C<HASH> reference of the contents of control and timing registers,
111 using fields named from the data sheet.
112  
113 POWER => OFF | ON
114 GAIN => 1 | 16
115 INTEG => 13ms | 101ms | 402ms
116  
117 Additionally, the following keys are provided calculated from those, as a
118 convenience.
119  
120 integ_msec => 13.7 | 101 | 402
121  
122 =head2 change_config
123  
124 await $chip->change_config( %changes );
125  
126 Writes updates to the control and timing registers.
127  
128 Note that this method will ignore the C<integ_msec> convenience value.
129  
130 Note that these two methods use a cache of configuration bytes to make
131 subsequent modifications more efficient. This cache will not respect the
132 "one-shot" nature of the C<Manual> bit.
133  
134 =cut
135  
136 has $_CONTROLbyte;
137 has $_TIMINGbyte;
138  
139 my %INTEG_to_msec = (
140 '13ms' => 13.7,
141 '101ms' => 101,
142 '402ms' => 402,
143 );
144  
145 async method read_config ()
146 {
147 my %config = unpack_CONFIG( pack "a1 a1",
148 $_CONTROLbyte //= await $self->_read( REG_CONTROL, 1 ),
149 $_TIMINGbyte //= await $self->_read( REG_TIMING, 1 ),
150 );
151  
152 $config{integ_msec} = $INTEG_to_msec{ $config{INTEG} };
153  
154 return \%config;
155 }
156  
157 async method change_config ( %changes )
158 {
159 my $config = await $self->read_config;
160  
161 $config->{$_} = $changes{$_} for keys %changes;
162  
163 delete $config->{integ_msec};
164  
165 my ( $CONTROL, $TIMING ) = unpack "a1 a1", pack_CONFIG( %$config );
166  
167 if( $CONTROL ne $_CONTROLbyte ) {
168 await $self->_write( REG_CONTROL, $_CONTROLbyte = $CONTROL );
169 }
170 if( $TIMING ne $_TIMINGbyte ) {
171 await $self->_write( REG_TIMING, $_TIMINGbyte = $TIMING );
172 }
173 }
174  
175 async method initialize_sensors ()
176 {
177 await $self->power( 1 );
178  
179 $self->enable_agc( 1 );
180  
181 # Wait for one integration cycle
182 await $self->protocol->sleep( ( await $self->read_config )->{integ_msec} / 1000 );
183 }
184  
185 =head2 enable_agc
186  
187 $chip->enable_agc( $agc )
188  
189 Accessor for the internal gain-control algorithm. If enabled, the C<GAIN>
190 configuration will be automatically controlled to switch between high- and
191 low-level settings.
192  
193 =cut
194  
195 has $_agc_enabled;
196  
197 method enable_agc ( $agc )
198 {
199 $_agc_enabled = $agc;
200 }
201  
202 =head2 read_id
203  
204 $id = await $chip->read_id;
205  
206 Returns the chip's ID register value.
207  
208 =cut
209  
210 async method read_id ()
211 {
212 return unpack "C", await $self->_read( REG_ID, 1 );
213 }
214  
215 =head2 read_data0
216  
217 =head2 read_data1
218  
219 $data0 = await $chip->read_data0;
220  
221 $data1 = await $chip->read_data1;
222  
223 Reads the current values of the ADC channels.
224  
225 =cut
226  
227 async method read_data0 ()
228 {
229 return unpack "S<", await $self->_read( REG_DATA0, 2 );
230 }
231  
232 async method read_data1 ()
233 {
234 return unpack "S<", await $self->_read( REG_DATA1, 2 );
235 }
236  
237 =head2 read_data
238  
239 ( $data0, $data1 ) = await $chip->read_data;
240  
241 Read the current values of both ADC channels in a single I²C transaction.
242  
243 =cut
244  
245 async method read_data ()
246 {
247 return unpack "S< S<", await $self->_read( REG_DATA0, 4 );
248 }
249  
250 =head1 METHODS
251  
252 =cut
253  
254 =head2 power
255  
256 await $chip->power( $on );
257  
258 Enables or disables the main power control bits in the C<CONTROL> register.
259  
260 =cut
261  
262 async method power ( $on )
263 {
264 await $self->_write( REG_CONTROL, $_CONTROLbyte = ( $on ? "\x03" : "\x00" ) );
265 }
266  
267 declare_sensor light =>
268 method => "read_lux",
269 units => "lux",
270 precision => 2;
271  
272 =head2 read_lux
273  
274 $lux = await $chip->read_lux;
275  
276 ( $lux, $data0, $data1 ) = await $chip->read_lux;
277  
278 Reads the two data registers then performs the appropriate scaling
279 calculations to return a floating-point number that approximates the light
280 level in Lux.
281  
282 Currently this conversion code presumes the contants for the T, FN and CL
283 chip types.
284  
285 In list context, also returns the raw C<$data0> and C<$data1> channel values.
286 The controlling code may wish to use these to adjust the gain if required.
287  
288 =cut
289  
290 has $_smallcount;
291  
292 async method read_lux ()
293 {
294 my ( $data0, $data1, $config ) = await Future->needs_all(
295 $self->read_data,
296 $self->read_config,
297 );
298  
299 my $gain = $config->{GAIN};
300 my $msec = $config->{integ_msec};
301  
302 my $ch0 = $data0 * ( 16 / $gain ) * ( 402 / $msec );
303 my $ch1 = $data1 * ( 16 / $gain ) * ( 402 / $msec );
304  
305 my $lux = 0;
306  
307 if( $ch0 != 0 ) {
308 my $ratio = $ch1 / $ch0;
309  
310 # TODO: take account of differing package types.
311  
312 if( $ratio <= 0.52 ) {
313 $lux = 0.0304 * $ch0 - 0.062 * $ch0 * ( $ratio ** 1.4 );
314 }
315 elsif( $ratio <= 0.65 ) {
316 $lux = 0.0224 * $ch0 - 0.031 * $ch1;
317 }
318 elsif( $ratio <= 0.80 ) {
319 $lux = 0.0128 * $ch0 - 0.0153 * $ch1;
320 }
321 elsif( $ratio <= 1.30 ) {
322 $lux = 0.00146 * $ch0 - 0.00112 * $ch1;
323 }
324  
325 my $saturation = ( $msec == 402 ) ? 0xFFFF :
326 ( $msec == 101 ) ? 0x9139 : 0x13B7;
327  
328 # Detect sensor saturation
329 if( $data0 == $saturation or $data1 == $saturation ) {
330 # The sensor saturates at well under 50klux
331 $lux = 50_000;
332 }
333 }
334  
335 if( $_agc_enabled ) {
336 if( $gain == 1 and $data0 < 0x0800 and $data1 < 0x0800 ) {
337 $_smallcount++;
338 await $self->change_config( GAIN => 16 ) if $_smallcount >= 4;
339 }
340 else {
341 $_smallcount = 0;
342 }
343  
344 if( $gain == 16 and ( $data0 > 0x8000 or $data1 > 0x8000 ) ) {
345 await $self->change_config( GAIN => 1 );
346 }
347 }
348  
349 return $lux if !wantarray;
350 return $lux, $data0, $data1;
351 }
352  
353 =head1 AUTHOR
354  
355 Paul Evans <leonerd@leonerd.org.uk>
356  
357 =cut
358  
359 0x55AA;
« Build ↑ Summary