tsl2591

package module
v0.0.0-...-14a517a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 7 Imported by: 2

README

This is a temporary fork of https://github.com/jimnelson2/tsl2591. Please create issues and PR's directly on that repo.

tsl2591

This is a Go module for the TSL2591 lux sensor, available from http://www.adafruit.com/products/1980 among other places.

Why this exists

We already have http://github.com/mstahl/tsl2591, but I wanted to make a few changes

  • Use modules
  • Switch from the use of the deprecated golang.org/x/exp/io/i2c import to the recommended periph.io/x/periph
  • I had a small bit of trouble working with the original, so I figured I'd have better success re-writing from scratch as that would force me to learn a bit more. That being said, a significant amount of code in this module has been copied from other sources as noted in the package header, i.e. Adafruit's original cpp and python implementations, as well as mstahl's work.

Note this module does NOT provide FULL control of the tsl2581, i.e. interrupts and alerts have not been exposed. That being said, it does all I need.

  • enable/disable
  • set gain
  • set timing
  • read visible, IR, and full spectrum
  • calculate lux

If anyone would like to implement missing functionality, or discovers problems with what's here - I'm sure there are problems somewhere - please submit and issue or even better, a PR.

Example Usage

Import this module and retrieve a lux value, e.g.

import ("github.com/jimnelson2/tsl2591")

// connect the the tsl2591
tsl, err := tsl2591.NewTSL2591(&tsl2591.Opts{
	Gain:   tsl2591.GainMed,
	Timing: tsl2591.Integrationtime600MS,
})
if err != nil {
	panic(err)
}

// read lux
lux, _ := tsl.Lux()

Sample code

Sample code is here, intended for use on a Raspberry Pi Zero.

Compile the code - in this case for Raspberry Pi Zero

env GOOS=linux GOARCH=arm GOARM=5 go build -o tsltest cmd/tsl2591/tsl2591.go

And when executed on a RPi with a connected TSL2591, output like the following will be printed every second.

Total Light: 12.451616 lux
Infrared light: 1921
Visible light: 125894656
Full spectrum (IR + visible) light: 125898232
Total Light: 12.426864 lux
Infrared light: 1920
Visible light: 125829120
Full spectrum (IR + visible) light: 125832693

Acknowledgements

As noted above and in the code - substantial work from the following is included from

Documentation

Overview

Package tsl2591 interacts with TSL2591 lux sensors

Heavily inspired by https://github.com/mstahl/tsl2591 and https://github.com/adafruit/Adafruit_TSL2591_Library/ as well as https://github.com/adafruit/Adafruit_TSL2591_Library/blob/master/Adafruit_TSL2591.cpp

Index

Constants

View Source
const (
	// FullSpectrum is channel 0
	FullSpectrum byte = 0

	// Infrared is channel 1
	Infrared byte = 1

	// Visible is FullSpectrum minus Infrared, i.e. channel 0 - channel 1
	Visible byte = 2

	// Addr is the default I2C address for the TSL2591
	Addr uint16 = 0x29

	// Device ID of the TSL2591 chip
	DeviceID byte = 0x50

	// CommandBits is 1010 0000 - sets bits 7 and 5 to indicate 'command normal'
	CommandBit byte = 0xa0

	// ClearInt command for 'Clear ALS and no persist ALS interrupt'
	ClearInt byte = 0xe7

	// TestInt command for 'Interrupt set - forces an interrupt'
	TestInt byte = 0xe4

	// WordBit to read/write word rather than byte
	WordBit byte = 0x20

	// BlockBit to block read/write
	BlockBit byte = 0x10

	// EnablePowerOff to set 'enable' register to disabled
	EnablePowerOff byte = 0x00

	// EnablePowerOn to set 'enable' register to enabled
	EnablePowerOn byte = 0x01

	// EnableAEN commands the ALS function. 1 enables, 0 disables
	EnableAEN byte = 0x02

	// EnableAIEN permits ALS interrupts to be generated, subject to the persist filter
	EnableAIEN byte = 0x10

	// EnableNPIEN commands that NP Threshold conditions will generate an interrupt, bypassing the persist filter
	EnableNPIEN byte = 0x80

	// LuxDF is the Lux cooefficient
	LuxDF float64 = 408.0

	// LuxCoefB is the channel0 coefficient
	LuxCoefB float64 = 1.64

	// LuxCoefC is channel1 coefficient A
	LuxCoefC float64 = 0.59

	// LuxCoefD is channel2 coefficient B
	LuxCoefD float64 = 0.86

	// MaxCount100ms sensor count
	MaxCount100ms uint16 = 0x8fff

	// MaxCount sensor count
	MaxCount uint16 = 0xffff
)

General purpose constants

View Source
const (
	// RegisterEnable is the enable register
	RegisterEnable byte = 0x00

	// RegisterControl is the control register
	RegisterControl byte = 0x01

	// RegisterThresholdAILTL is the ALS low threshold lower byte
	RegisterThresholdAILTL byte = 0x04

	// RegisterThresholdAILTH is the ALS low threshold upper byte
	RegisterThresholdAILTH byte = 0x05

	// RegisterThresholdAIHTL is the ALS high threshold lower byte
	RegisterThresholdAIHTL byte = 0x06

	// RegisterThresholdAIHTH is the ALS high threshold upper byte
	RegisterThresholdAIHTH byte = 0x07

	// RegisterThresholdNPAILTL is the no-persist ALS low threshold lower byte
	RegisterThresholdNPAILTL byte = 0x08

	// RegisterThresholdNPAILTH is the no-persist ALS low threshold higher byte
	RegisterThresholdNPAILTH byte = 0x09

	// RegisterThresholdNPAIHTL is the no-persist ALS high threshold lower byte
	RegisterThresholdNPAIHTL byte = 0x0a

	// RegisterThresholdNPAIHTH is the no-persist ALS high threshold higher byte
	RegisterThresholdNPAIHTH byte = 0x0b

	// RegisterPersistFilter is the interrupt persistence filter
	RegisterPersistFilter byte = 0x0c

	// RegisterPackagePID is for package identification
	RegisterPackagePID byte = 0x11

	// RegisterDeviceID is for device identification
	RegisterDeviceID byte = 0x12

	// RegisterDeviceStatus is for internal status
	RegisterDeviceStatus byte = 0x13

	// RegisterChan0Low is channel 0 data, low byte
	RegisterChan0Low byte = 0x14

	// RegisterChan0High is channel 0 data, high byte
	RegisterChan0High byte = 0x15

	// RegisterChan1Low is channel 1 data, low byte
	RegisterChan1Low byte = 0x16

	// RegisterChan1High is channel 1 data, high byte
	RegisterChan1High byte = 0x17
)

Register maps

Variables

View Source
var ErrOverflow = errors.New("overflow reading light channels")

Functions

This section is empty.

Types

type Gain

type Gain byte
const (
	// GainLow is low gain (1x)
	GainLow Gain = 0x00

	// GainMed is medium gain (25x)
	GainMed Gain = 0x10

	// GainHigh is high gain (428x)
	GainHigh Gain = 0x20

	// GainMax is max gain (9876x)
	GainMax Gain = 0x30
)

Constants for adjusting the sensor gain

type IntegrationTime

type IntegrationTime byte
const (
	// IntegrationTime100MS is 100 millis
	IntegrationTime100MS IntegrationTime = 0x00

	// IntegrationTime200MS is 200 millis
	IntegrationTime200MS IntegrationTime = 0x01

	// IntegrationTime300MS is 300 millis
	IntegrationTime300MS IntegrationTime = 0x02

	// IntegrationTime400MS is 400 millis
	IntegrationTime400MS IntegrationTime = 0x03

	// IntegrationTime500MS is 500 millis
	IntegrationTime500MS IntegrationTime = 0x04

	// IntegrationTime600MS is 600 millis
	IntegrationTime600MS IntegrationTime = 0x05
)

Constants for sensor integration timing

type Opts

type Opts struct {
	// Bus name, alias or its number.
	// See https://pkg.go.dev/periph.io/x/conn/v3/i2c/i2creg#Open for more info.
	Bus    string
	Gain   Gain
	Timing IntegrationTime
}

Opts holds various configuration options for the sensor

func DefaultOptions

func DefaultOptions() *Opts

type Persist

type Persist byte
const (
	// PersistEvery is every ALS cycle generates an interrupt
	PersistEvery Persist = 0x00

	// PersistAny for any value outside of threshold range
	PersistAny Persist = 0x01

	// Persist2 for 2 consecutive values out of range
	Persist2 Persist = 0x02

	// Persist3 for 3 consecutive values out of range
	Persist3 Persist = 0x03

	// Persist5 for 5 consecutive values out of range
	Persist5 Persist = 0x04

	// Persist10 for 10 consecutive values out of range
	Persist10 Persist = 0x05

	// Persist15 for 15 consecutive values out of range
	Persist15 Persist = 0x06

	// Persist20 for 20 consecutive values out of range
	Persist20 Persist = 0x07

	// Persist25 for 25 consecutive values out of range
	Persist25 Persist = 0x08

	// Persist30 for 30 consecutive values out of range
	Persist30 Persist = 0x09

	// Persist35 for 35 consecutive values out of range
	Persist35 Persist = 0x0a

	// Persist40 for 40 consecutive values out of range
	Persist40 Persist = 0x0b

	// Persist45 for 45 consecutive values out of range
	Persist45 Persist = 0x0c

	// Persist50 for 50 consecutive values out of range
	Persist50 Persist = 0x0d

	// Persist55 for 55 consecutive values out of range
	Persist55 Persist = 0x0e

	// Persist60 for 60 consecutive values out of range
	Persist60 Persist = 0x0f
)

Constants for adjusting the persistence filter

type TSL2591

type TSL2591 struct {
	// contains filtered or unexported fields
}

TSL2591 holds board setup detail

func NewTSL2591

func NewTSL2591(opts *Opts) (*TSL2591, error)

NewTSL2591 sets up a TSL2591 chip via the I2C protocol, sets its gain and timing attributes, and returns an error if any occurred in that process or if the TSL2591 was not found

func (*TSL2591) Disable

func (tsl *TSL2591) Disable() error

Disable disables the TSL2591 chip

func (*TSL2591) Enable

func (tsl *TSL2591) Enable() error

Enable enables the TSL2591 chip

func (*TSL2591) FullSpectrum

func (tsl *TSL2591) FullSpectrum() (uint32, error)

FullSpectrum returns the full spectrum value

func (*TSL2591) Infrared

func (tsl *TSL2591) Infrared() (uint16, error)

Infrared returns infrared value

func (*TSL2591) Lux

func (tsl *TSL2591) Lux() (float64, error)

Lux calculates a lux value from both the infrared and visible channels

func (*TSL2591) RawLuminosity

func (tsl *TSL2591) RawLuminosity() (uint16, uint16, error)

RawLuminosity reads from the sensor

func (*TSL2591) SetGain

func (tsl *TSL2591) SetGain(gain Gain) error

SetGain sets TSL2591 gain

func (*TSL2591) SetTiming

func (tsl *TSL2591) SetTiming(timing IntegrationTime) error

SetTiming sets TSL2591 timing. Chip is enabled, timing set, then disabled

func (*TSL2591) Visible

func (tsl *TSL2591) Visible() (uint32, error)

Visible returns visible value

type UnexpectedDeviceIDError

type UnexpectedDeviceIDError struct {
	Expected byte
	Actual   byte
}

func (UnexpectedDeviceIDError) Error

func (e UnexpectedDeviceIDError) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL