mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
Updated TSL2561 driver to latest i2c changes
Signed-off-by: Erik Agsjö <erik.agsjo@gmail.com>
This commit is contained in:
parent
91ac234094
commit
d8661ec444
@ -107,81 +107,110 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TSL2561Driver is the gobot driver for the Adafruit Digital Luminosity/Lux/Light Sensor
|
// TSL2561Driver is the gobot driver for the Adafruit Digital Luminosity/Lux/Light Sensor
|
||||||
|
//
|
||||||
// Datasheet: http://www.adafruit.com/datasheets/TSL2561.pdf
|
// Datasheet: http://www.adafruit.com/datasheets/TSL2561.pdf
|
||||||
|
//
|
||||||
// Ported from the Adafruit driver at https://github.com/adafruit/Adafruit_TSL2561 by
|
// Ported from the Adafruit driver at https://github.com/adafruit/Adafruit_TSL2561 by
|
||||||
// K. Townsend
|
// K. Townsend
|
||||||
type TSL2561Driver struct {
|
type TSL2561Driver struct {
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector Connector
|
||||||
connection I2cConnection
|
connection Connection
|
||||||
deviceAddress int
|
Config
|
||||||
i2cBus int
|
|
||||||
autoGain bool
|
autoGain bool
|
||||||
gain TSL2561Gain
|
gain TSL2561Gain
|
||||||
integrationTime TSL2561IntegrationTime
|
integrationTime TSL2561IntegrationTime
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTSL2561Driver creates a new driver with an I2c connector and optional options.
|
// NewTSL2561Driver creates a new driver for the TSL2561 device.
|
||||||
// Settings are provided with a map like this:
|
//
|
||||||
// options := map[string]int {address: TSL2561AddressLow}
|
// Params:
|
||||||
// d := NewTSL2561Driver(i2ccon, settings)
|
// conn Connector - the Adaptor to use with this Driver
|
||||||
func NewTSL2561Driver(c I2cConnector, options ...map[string]int) *TSL2561Driver {
|
//
|
||||||
integrationTime := TSL2561IntegrationTime402MS
|
// Optional params:
|
||||||
gain := TSL2561Gain1X
|
// i2c.WithBus(int): bus to use with this driver
|
||||||
autoGain := false
|
// i2c.WithAddress(int): address to use with this driver
|
||||||
bus := 1
|
// i2c.WithTSL2561Gain1X: sets the gain to 1X
|
||||||
address := TSL2561AddressFloat
|
// i2c.WithTSL2561Gain16X: sets the gain to 16X
|
||||||
|
// i2c.WithTSL2561AutoGain: turns on auto gain
|
||||||
|
// i2c.WithTSL2561IntegrationTime13MS: sets integration time to 13ms
|
||||||
|
// i2c.WithTSL2561IntegrationTime101MS: sets integration time to 101ms
|
||||||
|
// i2c.WithTSL2561IntegrationTime402MS: sets integration time to 402ms
|
||||||
|
//
|
||||||
|
func NewTSL2561Driver(conn Connector, options ...func(Config)) *TSL2561Driver {
|
||||||
|
driver := &TSL2561Driver{
|
||||||
|
name: gobot.DefaultName("TSL2561"),
|
||||||
|
connector: conn,
|
||||||
|
Config: NewConfig(),
|
||||||
|
integrationTime: TSL2561IntegrationTime402MS,
|
||||||
|
gain: TSL2561Gain1X,
|
||||||
|
autoGain: false,
|
||||||
|
}
|
||||||
|
|
||||||
if len(options) > 0 {
|
for _, option := range options {
|
||||||
for k, v := range options[0] {
|
option(driver)
|
||||||
switch k {
|
}
|
||||||
case "bus":
|
|
||||||
bus = v
|
return driver
|
||||||
case "address":
|
}
|
||||||
switch v {
|
|
||||||
case TSL2561AddressLow, TSL2561AddressFloat, TSL2561AddressHigh:
|
// WithTSL2561Gain1X option sets the TSL2561Driver gain to 1X
|
||||||
address = v
|
func WithTSL2561Gain1X(c Config) {
|
||||||
default:
|
d, ok := c.(*TSL2561Driver)
|
||||||
panic(fmt.Sprintf("Invalid address %v passed to NewTSL2561Driver", v))
|
if ok {
|
||||||
}
|
d.gain = TSL2561Gain1X
|
||||||
case "integrationTime":
|
} else {
|
||||||
switch TSL2561IntegrationTime(v) {
|
panic("Trying to set Gain for non-TSL2561Driver")
|
||||||
case TSL2561IntegrationTime13MS, TSL2561IntegrationTime101MS, TSL2561IntegrationTime402MS:
|
}
|
||||||
integrationTime = TSL2561IntegrationTime(v)
|
}
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("Invalid integrationTime %v passed to NewTSL2561Driver", v))
|
// WithTSL2561Gain16X option sets the TSL2561Driver gain to 16X
|
||||||
}
|
func WithTSL2561Gain16X(c Config) {
|
||||||
case "gain":
|
d, ok := c.(*TSL2561Driver)
|
||||||
switch TSL2561Gain(v) {
|
if ok {
|
||||||
case TSL2561Gain1X, TSL2561Gain16X:
|
d.gain = TSL2561Gain16X
|
||||||
gain = TSL2561Gain(v)
|
} else {
|
||||||
default:
|
panic("Trying to set Gain for non-TSL2561Driver")
|
||||||
panic(fmt.Sprintf("Invalid gain value '%v' passed to NewTSL2561Driver", v))
|
}
|
||||||
}
|
}
|
||||||
case "autoGain":
|
|
||||||
switch {
|
// WithTSL2561AutoGain option turns on TSL2561Driver auto gain
|
||||||
case v == 0:
|
func WithTSL2561AutoGain(c Config) {
|
||||||
autoGain = false
|
d, ok := c.(*TSL2561Driver)
|
||||||
case v == 1:
|
if ok {
|
||||||
autoGain = true
|
d.autoGain = true
|
||||||
default:
|
} else {
|
||||||
panic(fmt.Sprintf("Invalid autoGain value '%v' passwd to NewTSL2561Driver", v))
|
panic("Trying to set Auto Gain for non-TSL2561Driver")
|
||||||
}
|
}
|
||||||
default:
|
}
|
||||||
panic(fmt.Sprintf("Unknown option '%v' passed to NewTSL2561Driver", k))
|
|
||||||
}
|
func withTSL2561IntegrationTime(iTime TSL2561IntegrationTime) func(Config) {
|
||||||
|
return func(c Config) {
|
||||||
|
d, ok := c.(*TSL2561Driver)
|
||||||
|
if ok {
|
||||||
|
d.integrationTime = iTime
|
||||||
|
} else {
|
||||||
|
panic("Trying to set integration time for non-TSL2561Driver")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &TSL2561Driver{
|
// WithTSL2561IntegrationTime13MS option sets the TSL2561Driver integration time
|
||||||
name: "TSL2561",
|
// to 13ms
|
||||||
connector: c,
|
func WithTSL2561IntegrationTime13MS(c Config) {
|
||||||
deviceAddress: address,
|
withTSL2561IntegrationTime(TSL2561IntegrationTime13MS)(c)
|
||||||
i2cBus: bus,
|
}
|
||||||
integrationTime: integrationTime,
|
|
||||||
gain: gain,
|
// WithTSL2561IntegrationTime101MS option sets the TSL2561Driver integration time
|
||||||
autoGain: autoGain,
|
// 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the device.
|
// Name returns the name of the device.
|
||||||
@ -201,8 +230,10 @@ func (d *TSL2561Driver) Connection() gobot.Connection {
|
|||||||
|
|
||||||
// Start initializes the device.
|
// Start initializes the device.
|
||||||
func (d *TSL2561Driver) Start() (err error) {
|
func (d *TSL2561Driver) Start() (err error) {
|
||||||
|
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
|
||||||
|
address := d.GetAddressOrDefault(TSL2561AddressFloat)
|
||||||
|
|
||||||
if d.connection, err = d.connector.I2cGetConnection(d.deviceAddress, d.i2cBus); err != nil {
|
if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package i2c
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gobot.io/x/gobot"
|
"gobot.io/x/gobot"
|
||||||
@ -27,7 +28,7 @@ func idReader(b []byte) (int, error) {
|
|||||||
func TestTSL2561Driver(t *testing.T) {
|
func TestTSL2561Driver(t *testing.T) {
|
||||||
d, adaptor := initTestTSL2561Driver()
|
d, adaptor := initTestTSL2561Driver()
|
||||||
|
|
||||||
gobottest.Assert(t, d.Name(), "TSL2561")
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "TSL2561"), true)
|
||||||
|
|
||||||
adaptor.i2cReadImpl = idReader
|
adaptor.i2cReadImpl = idReader
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ func TestTSL2561Driver(t *testing.T) {
|
|||||||
gobottest.Assert(t, d.Halt(), nil)
|
gobottest.Assert(t, d.Halt(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRead16(t *testing.T) {
|
func TestTSL2561DriverRead16(t *testing.T) {
|
||||||
d, adaptor := initTestTSL2561Driver()
|
d, adaptor := initTestTSL2561Driver()
|
||||||
|
|
||||||
adaptor.i2cReadImpl = idReader
|
adaptor.i2cReadImpl = idReader
|
||||||
@ -57,48 +58,25 @@ func TestRead16(t *testing.T) {
|
|||||||
gobottest.Assert(t, val, uint16(0xAEEA))
|
gobottest.Assert(t, val, uint16(0xAEEA))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadOption(t *testing.T) {
|
func TestTSL2561DriverValidOptions(t *testing.T) {
|
||||||
adaptor := newI2cTestAdaptor()
|
adaptor := newI2cTestAdaptor()
|
||||||
options := map[string]int{
|
|
||||||
"hej": 12,
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
device := NewTSL2561Driver(adaptor,
|
||||||
x := recover()
|
WithTSL2561IntegrationTime101MS,
|
||||||
gobottest.Refute(t, x, nil)
|
WithAddress(TSL2561AddressLow),
|
||||||
}()
|
WithTSL2561Gain16X,
|
||||||
|
WithTSL2561AutoGain)
|
||||||
device := NewTSL2561Driver(adaptor, options)
|
|
||||||
|
|
||||||
gobottest.Refute(t, device, nil)
|
gobottest.Refute(t, device, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadOptionValue(t *testing.T) {
|
func TestTSL2561DriverSetName(t *testing.T) {
|
||||||
adaptor := newI2cTestAdaptor()
|
d, _ := initTestTSL2561Driver()
|
||||||
options := map[string]int{
|
d.SetName("TESTME")
|
||||||
"integrationTime": 47,
|
gobottest.Assert(t, d.Name(), "TESTME")
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
x := recover()
|
|
||||||
gobottest.Refute(t, x, nil)
|
|
||||||
}()
|
|
||||||
|
|
||||||
device := NewTSL2561Driver(adaptor, options)
|
|
||||||
|
|
||||||
gobottest.Refute(t, device, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidOptions(t *testing.T) {
|
func TestTSL2561DriverOptions(t *testing.T) {
|
||||||
adaptor := newI2cTestAdaptor()
|
d := NewTSL2561Driver(newI2cTestAdaptor(), WithBus(2))
|
||||||
options := map[string]int{
|
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
|
||||||
"integrationTime": int(TSL2561IntegrationTime101MS),
|
|
||||||
"address": TSL2561AddressLow,
|
|
||||||
"gain": TSL2561Gain16X,
|
|
||||||
"autoGain": 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
device := NewTSL2561Driver(adaptor, options)
|
|
||||||
|
|
||||||
gobottest.Refute(t, device, nil)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user