2017-01-16 07:59:54 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-03-21 16:28:46 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
2017-01-16 07:59:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TSL2561AddressLow - the address of the device when address pin is low
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561AddressLow = 0x29
|
2017-01-16 07:59:54 +08:00
|
|
|
// TSL2561AddressFloat - the address of the device when address pin is floating
|
|
|
|
TSL2561AddressFloat = 0x39
|
|
|
|
// TSL2561AddressHigh - the address of the device when address pin is high
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561AddressHigh = 0x49
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
tsl2561CommandBit = 0x80 // Must be 1
|
|
|
|
tsl2561ClearBit = 0x40 // Clears any pending interrupt (write 1 to clear)
|
|
|
|
tsl2561WordBit = 0x20 // 1 = read/write word (rather than byte)
|
|
|
|
tsl2561BlockBit = 0x10 // 1 = using block read/write
|
|
|
|
|
|
|
|
tsl2561ControlPowerOn = 0x03
|
|
|
|
tsl2561ControlPowerOff = 0x00
|
|
|
|
|
|
|
|
tsl2561LuxLuxScale = 14 // Scale by 2^14
|
|
|
|
tsl2561LuxRatioScale = 9 // Scale ratio by 2^9
|
|
|
|
tsl2561LuxChScale = 10 // Scale channel values by 2^10
|
|
|
|
tsl2561LuxCHScaleTInt0 = 0x7517 // 322/11 * 2^tsl2561LUXCHSCALE
|
|
|
|
tsl2561LuxChScaleTInt1 = 0x0FE7 // 322/81 * 2^tsl2561LUXCHSCALE
|
|
|
|
|
|
|
|
// T, FN and CL package values
|
|
|
|
tsl2561LuxK1T = 0x0040 // 0.125 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB1T = 0x01f2 // 0.0304 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM1T = 0x01be // 0.0272 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK2T = 0x0080 // 0.250 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB2T = 0x0214 // 0.0325 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM2T = 0x02d1 // 0.0440 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK3T = 0x00c0 // 0.375 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB3T = 0x023f // 0.0351 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM3T = 0x037b // 0.0544 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK4T = 0x0100 // 0.50 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB4T = 0x0270 // 0.0381 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM4T = 0x03fe // 0.0624 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK5T = 0x0138 // 0.61 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB5T = 0x016f // 0.0224 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM5T = 0x01fc // 0.0310 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK6T = 0x019a // 0.80 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB6T = 0x00d2 // 0.0128 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM6T = 0x00fb // 0.0153 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK7T = 0x029a // 1.3 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB7T = 0x0018 // 0.00146 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM7T = 0x0012 // 0.00112 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxK8T = 0x029a // 1.3 * 2^RATIO_SCALE
|
|
|
|
tsl2561LuxB8T = 0x0000 // 0.000 * 2^LUX_SCALE
|
|
|
|
tsl2561LuxM8T = 0x0000 // 0.000 * 2^LUX_SCALE
|
|
|
|
|
|
|
|
// Auto-gain thresholds
|
|
|
|
tsl2561AgcTHi13MS = 4850 // Max value at Ti 13ms = 5047
|
|
|
|
tsl2561AgcTLo13MS = 100
|
|
|
|
tsl2561AgcTHi101MS = 36000 // Max value at Ti 101ms = 37177
|
|
|
|
tsl2561AgcTLo101MS = 200
|
|
|
|
tsl2561AgcTHi402MS = 63000 // Max value at Ti 402ms = 65535
|
|
|
|
tsl2561AgcTLo402MS = 500
|
|
|
|
|
|
|
|
// Clipping thresholds
|
|
|
|
tsl2561Clipping13MS = 4900
|
|
|
|
tsl2561Clipping101MS = 37000
|
|
|
|
tsl2561Clipping402MS = 65000
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-20 07:27:24 +08:00
|
|
|
tsl2561RegisterControl = 0x00
|
|
|
|
tsl2561RegisterTiming = 0x01
|
|
|
|
tsl2561RegisterThreshholdLLow = 0x02
|
|
|
|
tsl2561RegisterThreshholdLHigh = 0x03
|
|
|
|
tsl2561RegisterThreshholdHLow = 0x04
|
|
|
|
tsl2561RegisterThreshholdHHigh = 0x05
|
|
|
|
tsl2561RegisterInterrupt = 0x06
|
|
|
|
tsl2561RegisterCRC = 0x08
|
|
|
|
tsl2561RegisterID = 0x0A
|
|
|
|
tsl2561RegisterChan0Low = 0x0C
|
|
|
|
tsl2561RegisterChan0High = 0x0D
|
|
|
|
tsl2561RegisterChan1Low = 0x0E
|
|
|
|
tsl2561RegisterChan1High = 0x0F
|
2017-01-16 07:59:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TSL2561IntegrationTime is the type of all valid integration time settings
|
|
|
|
type TSL2561IntegrationTime int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TSL2561IntegrationTime13MS integration time 13ms
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561IntegrationTime13MS TSL2561IntegrationTime = iota // 13.7ms
|
2017-01-16 07:59:54 +08:00
|
|
|
// TSL2561IntegrationTime101MS integration time 101ms
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561IntegrationTime101MS // 101ms
|
2017-01-16 07:59:54 +08:00
|
|
|
// TSL2561IntegrationTime402MS integration time 402ms
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561IntegrationTime402MS // 402ms
|
2017-01-16 07:59:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TSL2561Gain is the type of all valid gain settings
|
|
|
|
type TSL2561Gain int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TSL2561Gain1X gain == 1x
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561Gain1X TSL2561Gain = 0x00 // No gain
|
2017-01-16 07:59:54 +08:00
|
|
|
// TSL2561Gain16X gain == 16x
|
2017-01-20 07:27:24 +08:00
|
|
|
TSL2561Gain16X = 0x10 // 16x gain
|
2017-01-16 07:59:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// TSL2561Driver is the gobot driver for the Adafruit Digital Luminosity/Lux/Light Sensor
|
2017-02-15 07:01:47 +08:00
|
|
|
//
|
2017-01-16 07:59:54 +08:00
|
|
|
// Datasheet: http://www.adafruit.com/datasheets/TSL2561.pdf
|
2017-02-15 07:01:47 +08:00
|
|
|
//
|
2017-01-16 07:59:54 +08:00
|
|
|
// Ported from the Adafruit driver at https://github.com/adafruit/Adafruit_TSL2561 by
|
|
|
|
// K. Townsend
|
|
|
|
type TSL2561Driver struct {
|
2017-02-15 07:01:47 +08:00
|
|
|
name string
|
|
|
|
connector Connector
|
|
|
|
connection Connection
|
|
|
|
Config
|
2017-01-16 07:59:54 +08:00
|
|
|
autoGain bool
|
|
|
|
gain TSL2561Gain
|
|
|
|
integrationTime TSL2561IntegrationTime
|
|
|
|
}
|
|
|
|
|
2017-02-15 07:01:47 +08:00
|
|
|
// NewTSL2561Driver creates a new driver for the TSL2561 device.
|
|
|
|
//
|
|
|
|
// Params:
|
|
|
|
// conn Connector - the Adaptor to use with this Driver
|
|
|
|
//
|
|
|
|
// Optional params:
|
2017-02-22 05:47:21 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
|
|
|
// i2c.WithTSL2561Gain1X: sets the gain to 1X
|
|
|
|
// i2c.WithTSL2561Gain16X: sets the gain to 16X
|
2017-02-15 07:01:47 +08:00
|
|
|
// i2c.WithTSL2561AutoGain: turns on auto gain
|
|
|
|
// i2c.WithTSL2561IntegrationTime13MS: sets integration time to 13ms
|
2017-02-22 05:47:21 +08:00
|
|
|
// i2c.WithTSL2561IntegrationTime101MS: sets integration time to 101ms
|
|
|
|
// i2c.WithTSL2561IntegrationTime402MS: sets integration time to 402ms
|
2017-02-15 07:01:47 +08:00
|
|
|
//
|
|
|
|
func NewTSL2561Driver(conn Connector, options ...func(Config)) *TSL2561Driver {
|
|
|
|
driver := &TSL2561Driver{
|
|
|
|
name: gobot.DefaultName("TSL2561"),
|
|
|
|
connector: conn,
|
|
|
|
Config: NewConfig(),
|
|
|
|
integrationTime: TSL2561IntegrationTime402MS,
|
|
|
|
gain: TSL2561Gain1X,
|
|
|
|
autoGain: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
option(driver)
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 07:01:47 +08:00
|
|
|
return driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561Gain1X option sets the TSL2561Driver gain to 1X
|
|
|
|
func WithTSL2561Gain1X(c Config) {
|
|
|
|
d, ok := c.(*TSL2561Driver)
|
|
|
|
if ok {
|
|
|
|
d.gain = TSL2561Gain1X
|
2017-04-11 18:50:32 +08:00
|
|
|
return
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
2017-04-11 18:50:32 +08:00
|
|
|
// TODO: return errors.New("Trying to set Gain for non-TSL2561Driver")
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561Gain16X option sets the TSL2561Driver gain to 16X
|
|
|
|
func WithTSL2561Gain16X(c Config) {
|
|
|
|
d, ok := c.(*TSL2561Driver)
|
|
|
|
if ok {
|
|
|
|
d.gain = TSL2561Gain16X
|
2017-04-11 18:50:32 +08:00
|
|
|
return
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
2017-04-11 18:50:32 +08:00
|
|
|
// TODO: return errors.New("Trying to set Gain for non-TSL2561Driver")
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561AutoGain option turns on TSL2561Driver auto gain
|
|
|
|
func WithTSL2561AutoGain(c Config) {
|
|
|
|
d, ok := c.(*TSL2561Driver)
|
|
|
|
if ok {
|
|
|
|
d.autoGain = true
|
2017-04-11 18:50:32 +08:00
|
|
|
return
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
2017-04-11 18:50:32 +08:00
|
|
|
// TODO: return errors.New("Trying to set Auto Gain for non-TSL2561Driver")
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 07:01:47 +08:00
|
|
|
func withTSL2561IntegrationTime(iTime TSL2561IntegrationTime) func(Config) {
|
|
|
|
return func(c Config) {
|
|
|
|
d, ok := c.(*TSL2561Driver)
|
|
|
|
if ok {
|
|
|
|
d.integrationTime = iTime
|
2017-04-11 18:50:32 +08:00
|
|
|
return
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
2017-04-11 18:50:32 +08:00
|
|
|
// TODO: return errors.New("Trying to set integration time for non-TSL2561Driver")
|
2017-02-15 07:01:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561IntegrationTime13MS option sets the TSL2561Driver integration time
|
|
|
|
// to 13ms
|
|
|
|
func WithTSL2561IntegrationTime13MS(c Config) {
|
|
|
|
withTSL2561IntegrationTime(TSL2561IntegrationTime13MS)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561IntegrationTime101MS option sets the TSL2561Driver integration time
|
|
|
|
// to 101ms
|
|
|
|
func WithTSL2561IntegrationTime101MS(c Config) {
|
|
|
|
withTSL2561IntegrationTime(TSL2561IntegrationTime101MS)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTSL2561IntegrationTime402MS option sets the TSL2561Driver integration time
|
|
|
|
// to 402ms
|
|
|
|
func WithTSL2561IntegrationTime402MS(c Config) {
|
|
|
|
withTSL2561IntegrationTime(TSL2561IntegrationTime402MS)(c)
|
|
|
|
}
|
|
|
|
|
2017-01-16 07:59:54 +08:00
|
|
|
// Name returns the name of the device.
|
|
|
|
func (d *TSL2561Driver) Name() string {
|
|
|
|
return d.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets the name of the device.
|
|
|
|
func (d *TSL2561Driver) SetName(name string) {
|
|
|
|
d.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection returns the connection of the device.
|
|
|
|
func (d *TSL2561Driver) Connection() gobot.Connection {
|
2017-01-20 07:27:24 +08:00
|
|
|
return d.connector.(gobot.Connection)
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start initializes the device.
|
2017-01-20 07:27:24 +08:00
|
|
|
func (d *TSL2561Driver) Start() (err error) {
|
2017-02-15 07:01:47 +08:00
|
|
|
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
|
|
|
|
address := d.GetAddressOrDefault(TSL2561AddressFloat)
|
2017-01-20 07:27:24 +08:00
|
|
|
|
2017-02-15 07:01:47 +08:00
|
|
|
if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
|
2017-01-20 07:27:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.enable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-21 16:28:46 +08:00
|
|
|
var initialized byte
|
|
|
|
if initialized, err = d.connection.ReadByteData(tsl2561RegisterID); err != nil {
|
2017-01-16 07:59:54 +08:00
|
|
|
return err
|
|
|
|
} else if (initialized & 0x0A) == 0 {
|
2017-01-20 07:27:24 +08:00
|
|
|
return fmt.Errorf("TSL2561 device not found (0x%X)", initialized)
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 07:27:24 +08:00
|
|
|
if err = d.SetIntegrationTime(d.integrationTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.SetGain(d.gain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.disable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt stops the device
|
|
|
|
func (d *TSL2561Driver) Halt() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIntegrationTime sets integrations time for the TSL2561
|
|
|
|
func (d *TSL2561Driver) SetIntegrationTime(time TSL2561IntegrationTime) error {
|
|
|
|
if err := d.enable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeGainVal := uint8(time) | uint8(d.gain)
|
2017-01-20 07:27:24 +08:00
|
|
|
if err := d.connection.WriteByteData(tsl2561CommandBit|tsl2561RegisterTiming, timeGainVal); err != nil {
|
2017-01-16 07:59:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.integrationTime = time
|
|
|
|
|
|
|
|
return d.disable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGain adjusts the TSL2561 gain (sensitivity to light)
|
|
|
|
func (d *TSL2561Driver) SetGain(gain TSL2561Gain) error {
|
|
|
|
if err := d.enable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeGainVal := uint8(d.integrationTime) | uint8(gain)
|
2017-01-20 07:27:24 +08:00
|
|
|
if err := d.connection.WriteByteData(tsl2561CommandBit|tsl2561RegisterTiming, timeGainVal); err != nil {
|
2017-01-16 07:59:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.gain = gain
|
|
|
|
|
|
|
|
return d.disable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLuminocity gets the broadband and IR only values from the TSL2561,
|
|
|
|
// adjusting gain if auto-gain is enabled
|
|
|
|
func (d *TSL2561Driver) GetLuminocity() (broadband uint16, ir uint16, err error) {
|
|
|
|
// if auto gain disabled get a single reading and continue
|
|
|
|
if !d.autoGain {
|
|
|
|
broadband, ir, err = d.getData()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
agcCheck := false
|
2017-04-11 20:17:04 +08:00
|
|
|
hi, lo := d.getHiLo()
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
// Read data until we find a valid range
|
|
|
|
valid := false
|
|
|
|
for {
|
|
|
|
broadband, ir, err = d.getData()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run an auto-gain check if we haven't already done so
|
|
|
|
if !agcCheck {
|
|
|
|
if (broadband < lo) && (d.gain == TSL2561Gain1X) {
|
|
|
|
// increase gain and try again
|
|
|
|
err = d.SetGain(TSL2561Gain16X)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
agcCheck = true
|
|
|
|
} else if (broadband > hi) && (d.gain == TSL2561Gain16X) {
|
|
|
|
// drop gain and try again
|
|
|
|
err = d.SetGain(TSL2561Gain1X)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
agcCheck = true
|
|
|
|
} else {
|
|
|
|
// Reading is either valid, or we're already at the chips
|
|
|
|
// limits
|
|
|
|
valid = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we've already adjusted the gain once, just return the new results.
|
|
|
|
// This avoids endless loops where a value is at one extreme pre-gain,
|
|
|
|
// and the the other extreme post-gain
|
|
|
|
valid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateLux converts raw sensor values to the standard SI Lux equivalent.
|
|
|
|
// Returns 65536 if the sensor is saturated.
|
|
|
|
func (d *TSL2561Driver) CalculateLux(broadband uint16, ir uint16) (lux uint32) {
|
|
|
|
var channel1 uint32
|
|
|
|
var channel0 uint32
|
|
|
|
|
|
|
|
// Set cliplevel and scaling based on integration time
|
2017-04-11 20:17:04 +08:00
|
|
|
clipThreshold, chScale := d.getClipScaling()
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
// Saturated sensor
|
|
|
|
if (broadband > clipThreshold) || (ir > clipThreshold) {
|
|
|
|
return 65536
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust scale for gain
|
|
|
|
if d.gain == TSL2561Gain1X {
|
|
|
|
chScale = chScale * 16
|
|
|
|
}
|
|
|
|
|
|
|
|
channel0 = (uint32(broadband) * chScale) >> tsl2561LuxChScale
|
|
|
|
channel1 = (uint32(ir) * chScale) >> tsl2561LuxChScale
|
|
|
|
|
|
|
|
// Find the ratio of the channel values (channel1 / channel0)
|
|
|
|
var ratio1 uint32
|
|
|
|
if channel0 != 0 {
|
|
|
|
ratio1 = (channel1 << (tsl2561LuxRatioScale + 1)) / channel0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round the ratio value
|
|
|
|
ratio := (ratio1 + 1) / 2
|
|
|
|
|
2017-04-11 20:17:04 +08:00
|
|
|
b, m := d.getBM(ratio)
|
2017-01-16 07:59:54 +08:00
|
|
|
temp := (channel0 * b) - (channel1 * m)
|
|
|
|
|
|
|
|
// Negative lux not allowed
|
|
|
|
if temp < 0 {
|
|
|
|
temp = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round lsb (2^(LUX_SCALE+1))
|
|
|
|
temp += (1 << (tsl2561LuxLuxScale - 1))
|
|
|
|
|
|
|
|
// Strip off fractional portion
|
|
|
|
lux = temp >> tsl2561LuxLuxScale
|
|
|
|
|
|
|
|
return lux
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TSL2561Driver) enable() (err error) {
|
2017-01-20 07:27:24 +08:00
|
|
|
err = d.connection.WriteByteData(uint8(tsl2561CommandBit|tsl2561RegisterControl), tsl2561ControlPowerOn)
|
2017-01-16 07:59:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TSL2561Driver) disable() (err error) {
|
2017-01-20 07:27:24 +08:00
|
|
|
err = d.connection.WriteByteData(uint8(tsl2561CommandBit|tsl2561RegisterControl), tsl2561ControlPowerOff)
|
2017-01-16 07:59:54 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TSL2561Driver) getData() (broadband uint16, ir uint16, err error) {
|
2017-01-20 07:27:24 +08:00
|
|
|
if err = d.enable(); err != nil {
|
|
|
|
return
|
2017-01-16 07:59:54 +08:00
|
|
|
}
|
|
|
|
|
2017-04-12 22:44:15 +08:00
|
|
|
d.waitForADC()
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
// Reads a two byte value from channel 0 (visible + infrared)
|
2017-01-22 07:43:54 +08:00
|
|
|
broadband, err = d.connection.ReadWordData(tsl2561CommandBit | tsl2561WordBit | tsl2561RegisterChan0Low)
|
2017-01-20 07:27:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
// Reads a two byte value from channel 1 (infrared)
|
2017-01-22 07:43:54 +08:00
|
|
|
ir, err = d.connection.ReadWordData(tsl2561CommandBit | tsl2561WordBit | tsl2561RegisterChan1Low)
|
2017-01-20 07:27:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-16 07:59:54 +08:00
|
|
|
|
2017-01-20 07:27:24 +08:00
|
|
|
err = d.disable()
|
2017-01-16 07:59:54 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-04-11 20:17:04 +08:00
|
|
|
|
|
|
|
func (d *TSL2561Driver) getHiLo() (hi, lo uint16) {
|
|
|
|
switch d.integrationTime {
|
|
|
|
case TSL2561IntegrationTime13MS:
|
|
|
|
hi = tsl2561AgcTHi13MS
|
|
|
|
lo = tsl2561AgcTLo13MS
|
|
|
|
case TSL2561IntegrationTime101MS:
|
|
|
|
hi = tsl2561AgcTHi101MS
|
|
|
|
lo = tsl2561AgcTLo101MS
|
|
|
|
case TSL2561IntegrationTime402MS:
|
|
|
|
hi = tsl2561AgcTHi402MS
|
|
|
|
lo = tsl2561AgcTLo402MS
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TSL2561Driver) getClipScaling() (clipThreshold uint16, chScale uint32) {
|
|
|
|
switch d.integrationTime {
|
|
|
|
case TSL2561IntegrationTime13MS:
|
|
|
|
clipThreshold = tsl2561Clipping13MS
|
|
|
|
chScale = tsl2561LuxCHScaleTInt0
|
|
|
|
case TSL2561IntegrationTime101MS:
|
|
|
|
clipThreshold = tsl2561Clipping101MS
|
|
|
|
chScale = tsl2561LuxChScaleTInt1
|
|
|
|
case TSL2561IntegrationTime402MS:
|
|
|
|
clipThreshold = tsl2561Clipping402MS
|
|
|
|
chScale = (1 << tsl2561LuxChScale)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *TSL2561Driver) getBM(ratio uint32) (b uint32, m uint32) {
|
|
|
|
switch {
|
|
|
|
case (ratio >= 0) && (ratio <= tsl2561LuxK1T):
|
|
|
|
b = tsl2561LuxB1T
|
|
|
|
m = tsl2561LuxM1T
|
|
|
|
case (ratio <= tsl2561LuxK2T):
|
|
|
|
b = tsl2561LuxB2T
|
|
|
|
m = tsl2561LuxM2T
|
|
|
|
case (ratio <= tsl2561LuxK3T):
|
|
|
|
b = tsl2561LuxB3T
|
|
|
|
m = tsl2561LuxM3T
|
|
|
|
case (ratio <= tsl2561LuxK4T):
|
|
|
|
b = tsl2561LuxB4T
|
|
|
|
m = tsl2561LuxM4T
|
|
|
|
case (ratio <= tsl2561LuxK5T):
|
|
|
|
b = tsl2561LuxB5T
|
|
|
|
m = tsl2561LuxM5T
|
|
|
|
case (ratio <= tsl2561LuxK6T):
|
|
|
|
b = tsl2561LuxB6T
|
|
|
|
m = tsl2561LuxM6T
|
|
|
|
case (ratio <= tsl2561LuxK7T):
|
|
|
|
b = tsl2561LuxB7T
|
|
|
|
m = tsl2561LuxM7T
|
2017-04-11 20:51:03 +08:00
|
|
|
case (ratio > tsl2561LuxK8T): // TODO: there is a gap here...
|
2017-04-11 20:17:04 +08:00
|
|
|
b = tsl2561LuxB8T
|
|
|
|
m = tsl2561LuxM8T
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-04-12 22:44:15 +08:00
|
|
|
|
|
|
|
func (d *TSL2561Driver) waitForADC() {
|
|
|
|
switch d.integrationTime {
|
|
|
|
case TSL2561IntegrationTime13MS:
|
|
|
|
time.Sleep(15 * time.Millisecond)
|
|
|
|
case TSL2561IntegrationTime101MS:
|
|
|
|
time.Sleep(120 * time.Millisecond)
|
|
|
|
case TSL2561IntegrationTime402MS:
|
|
|
|
time.Sleep(450 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|