2017-01-16 00:59:54 +01:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*TSL2561Driver)(nil)
|
|
|
|
|
|
|
|
func initTestTSL2561Driver() (*TSL2561Driver, *i2cTestAdaptor) {
|
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
return NewTSL2561Driver(adaptor), adaptor
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:34:33 +01:00
|
|
|
func idReader(b []byte) (int, error) {
|
2017-01-20 00:27:24 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2017-02-06 18:34:33 +01:00
|
|
|
// Mock device responding 0xA
|
2017-01-20 00:27:24 +01:00
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(0x0A))
|
2017-02-06 18:34:33 +01:00
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
2017-01-20 00:27:24 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 00:59:54 +01:00
|
|
|
func TestTSL2561Driver(t *testing.T) {
|
|
|
|
d, adaptor := initTestTSL2561Driver()
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Name(), "TSL2561")
|
|
|
|
|
2017-01-20 00:27:24 +01:00
|
|
|
adaptor.i2cReadImpl = idReader
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead16(t *testing.T) {
|
|
|
|
d, adaptor := initTestTSL2561Driver()
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = idReader
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
|
2017-02-06 18:34:33 +01:00
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
2017-01-16 00:59:54 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2017-01-20 00:27:24 +01:00
|
|
|
// send low
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(0xEA))
|
|
|
|
// send high
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(0xAE))
|
2017-02-06 18:34:33 +01:00
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
2017-01-16 00:59:54 +01:00
|
|
|
}
|
2017-02-06 18:34:33 +01:00
|
|
|
val, err := d.connection.ReadWordData(1)
|
2017-01-20 00:27:24 +01:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, val, uint16(0xAEEA))
|
2017-01-16 00:59:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadOption(t *testing.T) {
|
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
options := map[string]int{
|
|
|
|
"hej": 12,
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
x := recover()
|
|
|
|
gobottest.Refute(t, x, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
device := NewTSL2561Driver(adaptor, options)
|
|
|
|
|
|
|
|
gobottest.Refute(t, device, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadOptionValue(t *testing.T) {
|
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
options := map[string]int{
|
|
|
|
"integrationTime": 47,
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
x := recover()
|
|
|
|
gobottest.Refute(t, x, nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
device := NewTSL2561Driver(adaptor, options)
|
|
|
|
|
|
|
|
gobottest.Refute(t, device, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidOptions(t *testing.T) {
|
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
options := map[string]int{
|
2017-01-20 00:27:24 +01:00
|
|
|
"integrationTime": int(TSL2561IntegrationTime101MS),
|
2017-01-16 00:59:54 +01:00
|
|
|
"address": TSL2561AddressLow,
|
|
|
|
"gain": TSL2561Gain16X,
|
|
|
|
"autoGain": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
device := NewTSL2561Driver(adaptor, options)
|
|
|
|
|
|
|
|
gobottest.Refute(t, device, nil)
|
|
|
|
}
|