|
|
|
@ -8,6 +8,7 @@ import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"periph.io/x/conn/v3/i2c/i2ctest"
|
|
|
|
"periph.io/x/conn/v3/i2c/i2ctest"
|
|
|
|
@ -46,16 +47,10 @@ func TestAvailableChannels(t *testing.T) {
|
|
|
|
expected := []uint8{0x01, 0x02, 0x03, 0x04}
|
|
|
|
expected := []uint8{0x01, 0x02, 0x03, 0x04}
|
|
|
|
|
|
|
|
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
channels := dev.AvailableChannels()
|
|
|
|
list := dev.AvailableChannels()
|
|
|
|
|
|
|
|
|
|
|
|
if len(channels) != len(expected) {
|
|
|
|
if !reflect.DeepEqual(expected, list) {
|
|
|
|
t.Fatal("Available channels len should be ", len(expected), ", got ", len(channels))
|
|
|
|
t.Fatal("Available channels should be ", expected, " got ", list)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(expected); i++ {
|
|
|
|
|
|
|
|
if channels[i] != expected[i] {
|
|
|
|
|
|
|
|
t.Fatal("Available channels should be ", expected, " got ", channels)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -63,8 +58,6 @@ func TestHalt(t *testing.T) {
|
|
|
|
bus := initTestBus()
|
|
|
|
bus := initTestBus()
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
|
|
|
|
|
|
|
|
resetTestBusOps(bus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dev.Halt()
|
|
|
|
dev.Halt()
|
|
|
|
checkDevReset(t, dev, bus)
|
|
|
|
checkDevReset(t, dev, bus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -73,8 +66,6 @@ func TestOn(t *testing.T) {
|
|
|
|
bus := initTestBus()
|
|
|
|
bus := initTestBus()
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
|
|
|
|
|
|
|
|
resetTestBusOps(bus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := dev.On(3)
|
|
|
|
err := dev.On(3)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -89,8 +80,6 @@ func TestOff(t *testing.T) {
|
|
|
|
bus := initTestBus()
|
|
|
|
bus := initTestBus()
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
dev, _ := New(bus, testDefaultValidAddress)
|
|
|
|
|
|
|
|
|
|
|
|
resetTestBusOps(bus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := dev.Off(4)
|
|
|
|
err := dev.Off(4)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
@ -112,6 +101,20 @@ func TestReturnErrorForInvalidChannel(t *testing.T) {
|
|
|
|
if err := dev.Off(98); err != errInvalidChannel {
|
|
|
|
if err := dev.Off(98); err != errInvalidChannel {
|
|
|
|
t.Fatal("Off should return invalid channel error, got ", err)
|
|
|
|
t.Fatal("Off should return invalid channel error, got ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := dev.Off(98); err != errInvalidChannel {
|
|
|
|
|
|
|
|
t.Fatal("Off should return invalid channel error, got ", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestStateToString(t *testing.T) {
|
|
|
|
|
|
|
|
if s := fmt.Sprintf("%s", StateOn); s != "on" {
|
|
|
|
|
|
|
|
t.Fatal("StateOn as string should be 'on', got ", s)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if s := fmt.Sprintf("%s", StateOff); s != "off" {
|
|
|
|
|
|
|
|
t.Fatal("StateOn as string should be 'off', got ", s)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func initTestBus() *i2ctest.Record {
|
|
|
|
func initTestBus() *i2ctest.Record {
|
|
|
|
@ -121,10 +124,6 @@ func initTestBus() *i2ctest.Record {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func resetTestBusOps(bus *i2ctest.Record) {
|
|
|
|
|
|
|
|
bus.Ops = []i2ctest.IO{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkChannelState(t *testing.T, dev *Dev, channel uint8, state State) {
|
|
|
|
func checkChannelState(t *testing.T, dev *Dev, channel uint8, state State) {
|
|
|
|
if actual, _ := dev.State(channel); actual != state {
|
|
|
|
if actual, _ := dev.State(channel); actual != state {
|
|
|
|
msg := fmt.Sprintf("Channel %d should have state %s, got: %s", channel, state, actual)
|
|
|
|
msg := fmt.Sprintf("Channel %d should have state %s, got: %s", channel, state, actual)
|
|
|
|
|