fixed failing test and documentation

pull/5/head
Carlos Cardoso 5 years ago
parent c7460ffd04
commit a1d03c42b1

@ -1,12 +1,9 @@
// Copyright 2018 The Periph Authors. All rights reserved. // Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0 // Use of this source code is governed under the Apache License, Version 2.0
// that caan be found in the LICENSE file. // that caan be found in the LICENSE file.
// Package ep0099 // Package ep0099 controls a EP-0099 Raspberry Pi HAT with 4 relays via I2C.
// //
// Datasheet // Datasheet
// https://wiki.52pi.com/index.php/DockerPi_4_Channel_Relay_SKU:_EP-0099 // https://wiki.52pi.com/index.php/DockerPi_4_Channel_Relay_SKU:_EP-0099
//
// Examples
// https://github.com/geeekpi/dockerpi
package ep0099 package ep0099

@ -1,4 +1,4 @@
// Copyright 2018 The Periph Authors. All rights reserved. // Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0 // Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
@ -10,14 +10,14 @@ import (
"periph.io/x/conn/v3/i2c" "periph.io/x/conn/v3/i2c"
) )
var InvalidAddressError = errors.New("Invalid EP-0099 address") var errInvalidAddress = errors.New("Invalid EP-0099 address")
var InvalidChannelError = errors.New("Invalid EP-0099 channel") var errInvalidChannel = errors.New("Invalid EP-0099 channel")
type State byte type State byte
const ( const (
StateOff State = 0x00 StateOff State = 0x00
StateOn State = 0x01 StateOn State = 0xFF
) )
type Dev struct { type Dev struct {
@ -107,12 +107,12 @@ func isValidAddress(address uint16) error {
} }
} }
return InvalidAddressError return errInvalidAddress
} }
func (d *Dev) isValidChannel(channel uint8) error { func (d *Dev) isValidChannel(channel uint8) error {
if _, exists := d.state[channel]; !exists { if _, exists := d.state[channel]; !exists {
return InvalidChannelError return errInvalidChannel
} }
return nil return nil
} }

@ -1,4 +1,4 @@
// Copyright 2018 The Periph Authors. All rights reserved. // Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0 // Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
@ -36,7 +36,7 @@ func TestNewReturnsInvalidAddress(t *testing.T) {
bus := initTestBus() bus := initTestBus()
_, err := New(bus, 0x00) _, err := New(bus, 0x00)
if !errors.Is(err, InvalidAddressError) { if !errors.Is(err, errInvalidAddress) {
t.Fatal("New should return address validation error, got: ", err) t.Fatal("New should return address validation error, got: ", err)
} }
} }
@ -81,7 +81,7 @@ func TestOn(t *testing.T) {
t.Fatal("Should not return error, got ", err) t.Fatal("Should not return error, got ", err)
} }
checkBusWrites(t, bus, 0, []byte{3, byte(StateOn)}) checkBusHasWrite(t, bus, []byte{3, byte(StateOn)})
checkChannelState(t, dev, 3, StateOn) checkChannelState(t, dev, 3, StateOn)
} }
@ -97,7 +97,7 @@ func TestOff(t *testing.T) {
t.Fatal("Should not return error, got ", err) t.Fatal("Should not return error, got ", err)
} }
checkBusWrites(t, bus, 0, []byte{4, byte(StateOff)}) checkBusHasWrite(t, bus, []byte{4, byte(StateOff)})
checkChannelState(t, dev, 4, StateOff) checkChannelState(t, dev, 4, StateOff)
} }
@ -105,11 +105,11 @@ func TestReturnErrorForInvalidChannel(t *testing.T) {
bus := initTestBus() bus := initTestBus()
dev, _ := New(bus, testDefaultValidAddress) dev, _ := New(bus, testDefaultValidAddress)
if err := dev.On(98); err != InvalidChannelError { if err := dev.On(98); err != errInvalidChannel {
t.Fatal("On should return invalid channel error, got ", err) t.Fatal("On should return invalid channel error, got ", err)
} }
if err := dev.Off(98); err != InvalidChannelError { 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)
} }
} }
@ -132,22 +132,25 @@ func checkChannelState(t *testing.T, dev *Dev, channel uint8, state State) {
} }
} }
func checkBusWrites(t *testing.T, bus *i2ctest.Record, index int, data []byte) { func checkBusHasWrite(t *testing.T, bus *i2ctest.Record, data []byte) {
if !bytes.Equal(bus.Ops[index].W, data) { for _, op := range bus.Ops {
t.Fatal("Expected data ", data, " to be written, got ", bus.Ops[index].W) if bytes.Equal(op.W, data) {
return
}
} }
t.Fatal("Expected data ", data, " to be written but it never did")
} }
func checkDevReset(t *testing.T, dev *Dev, bus *i2ctest.Record) { func checkDevReset(t *testing.T, dev *Dev, bus *i2ctest.Record) {
checkBusWrites(t, bus, 0, []byte{1, byte(StateOff)}) checkBusHasWrite(t, bus, []byte{1, byte(StateOff)})
checkChannelState(t, dev, 1, StateOff) checkChannelState(t, dev, 1, StateOff)
checkBusWrites(t, bus, 1, []byte{2, byte(StateOff)}) checkBusHasWrite(t, bus, []byte{2, byte(StateOff)})
checkChannelState(t, dev, 2, StateOff) checkChannelState(t, dev, 2, StateOff)
checkBusWrites(t, bus, 2, []byte{3, byte(StateOff)}) checkBusHasWrite(t, bus, []byte{3, byte(StateOff)})
checkChannelState(t, dev, 3, StateOff) checkChannelState(t, dev, 3, StateOff)
checkBusWrites(t, bus, 3, []byte{4, byte(StateOff)}) checkBusHasWrite(t, bus, []byte{4, byte(StateOff)})
checkChannelState(t, dev, 4, StateOff) checkChannelState(t, dev, 4, StateOff)
} }

@ -1,4 +1,4 @@
// Copyright 2018 The Periph Authors. All rights reserved. // Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0 // Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file. // that can be found in the LICENSE file.

Loading…
Cancel
Save