LUX.pm0000664000175000000120000000351214515043662010703 0ustar toddwheel# # http://www.toolz.com - software@toolz.com # # $Id: LUX.pm,v 1.1 2023/10/04 21:44:53 todd Exp $ # # This module is made to work with the TSL2561 Lux sensor. # Datasheet at https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf # Hardware configuration: https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf # # readVal returns an array, element 0 is IR + visible, element 1 is visible # light. # See the documentation for RPi::I2C for info on how to determine how your # I2C bus is defined and required prerequisites. The Raspberry Pi defaults are assumed. # # There are no license restrictions on this code. # use warnings; package RPi::I2C::LUX; our $VERSION = '1.1'; require Exporter; our @ISA = qw(Exporter); @EXPORT = qw(readVal); use Time::HiRes; # TSL2561 Address $DEVADDR = 0x39; # default address on i2c bus sub new { my ($class,%args) = @_; my $key; # defaults my %params = ( devaddr => $DEVADDR, busno => 1, # 0, 1 version => "$VERSION", ); # default overrides foreach $key (keys %args) { $params{$key} = $args{$key}; } $params{bus} = RPi::I2C->new ($params{devaddr}); foreach $key (keys %params) { $self->{$key} = $params{$key}; } bless $self, $class; # Select control register, 0x00(00) with command register, 0x80(128) # 0x03(03) Power ON mode $self->{bus}->write_byte(0x03,0x00|0x80); # Select timing register, 0x01(01) with command register, 0x80(128) # 0x02(02) Nominal integration time = 402ms $self->{bus}->write_byte(0x02,0x01|0x80); # integration time sleep(0.5); return $self; } sub readVal { my ($self,$byte) = @_; my @data = (0,0); my @data1 = (0,0); @data = $self->{bus}->read_block(2,0x0C | 0x80); @data1 = $self->{bus}->read_block(2,0x0E | 0x80); my $ch0 = ($data[1] * 256) + $data[0]; my $ch1 = ($data1[1] * 256) + $data1[0]; return ($ch0,$ch0-$ch1); # IR+visible, visible } 1; README0000664000175000000120000000310514515046314010550 0ustar toddwheelNAME RPi::I2C::LUX - Interface vi I2C to the TSL2561 lux sensor SYNOPSIS use RPi::I2C::LUX # all option values have defaults my $device = RPi::I2C::LUX->new( 'devaddr' => , 'busno' => , ); @vals = $device->readVal DESCRIPTION Interface to read from a TSL2561 lux sensor YOU SHOULD KNOW RPi::I2C is prerequisite and its documentation specifies requirements for enabling the I2C bus and determining the bus address to use. Datasheet at https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf Hardware configuration: https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf METHODS new([options]) Instantiates an object representing the attached sensor. Parameters Every option has a default value, and the defaults may be sufficient for most applications. my $device = RPi::I2C::LUX->new( 'devaddr' => , default 0x39 'busno' => , default 1, for /dev/i2c-1 ); readVal Reads the sensor and returns an array. Element 0 is infared+visible. Element 1 is visible light. EXAMPLE #!/usr/bin/perl use RPi::I2C; use RPi::I2C::LUX; my $lux = RPi::I2C::LUX->new; my @vals = $lux->readVal; print "IR+visible=$vals[0], visible=$vals[1]\n"; AUTHOR Todd Merriman, , d.b.a. Software Toolz, Atlanta, Georgia LICENSE AND COPYRIGHT Copyright (C) 2023 by Todd Merriman d.b.a. Software Toolz This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.