Skip to content

Commit 1eb2d17

Browse files
authored
Fix dac.DAC implementation of MAX581X
The MAX5813, MAX5814 and MAX5815 didn't implement the dac.DAC interface. This commit also brings test coverage for the microchip package to 100%. Issues: #21
1 parent e67f230 commit 1eb2d17

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

i2c/max/max518x.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,21 @@ type max581x struct {
9595

9696
// SetVoltage set output voltage of channel. Using the Vref the input code is
9797
// calculated and then SetInputCode is called.
98-
func (m *max581x) SetVoltage(v float64, channel int) error {
98+
func (m max581x) SetVoltage(v float64, channel int) error {
9999
code := v * (math.Pow(2, float64(m.resolution)) - 1) / m.vref
100100
return m.SetInputCode(int(code), channel)
101101
}
102102

103103
// SetInputCode writes the digital input code to the DAC using the CODEn_LOADn
104104
// command.
105-
func (m *max581x) SetInputCode(code, channel int) error {
105+
func (m max581x) SetInputCode(code, channel int) error {
106106
if channel < 0 || channel > 3 {
107107
return fmt.Errorf("%d is not a valid channel", channel)
108108
}
109109

110110
max := int(math.Pow(2, float64(m.resolution)))
111111
if code < 0 || code >= max {
112-
return fmt.Errorf("digital input code %d is out of range of 0 <= code < %d ", code, max)
112+
return fmt.Errorf("digital input code %d is out of range of 0 <= code < %d", code, int(max))
113113
}
114114

115115
// The requests is 3 bytes long. Byte 1 is the command, byte 2 and 3

i2c/max/max518x_test.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/advancedclimatesystems/io/dac"
78
"github.com/advancedclimatesystems/io/iotest"
89
"github.com/stretchr/testify/assert"
910
"golang.org/x/exp/io/i2c"
1011
)
1112

12-
func TestNewMax581x(t *testing.T) {
13+
func TestDACinterface(t *testing.T) {
14+
assert.Implements(t, (*dac.DAC)(nil), new(MAX5813))
15+
assert.Implements(t, (*dac.DAC)(nil), new(MAX5814))
16+
assert.Implements(t, (*dac.DAC)(nil), new(MAX5815))
17+
}
18+
19+
func TestNewMAX581x(t *testing.T) {
1320
conn, _ := i2c.Open(iotest.NewI2CDriver(iotest.NewI2CConn()), 0x1)
1421

1522
max5813, _ := NewMAX5813(conn, 3)
@@ -100,6 +107,75 @@ func TestMAX581xSetVoltage(t *testing.T) {
100107
}
101108
}
102109

110+
func TestMAX581xConn(t *testing.T) {
111+
c, _ := i2c.Open(iotest.NewI2CDriver(iotest.NewI2CConn()), 0x1)
112+
dac, _ := NewMAX5813(c, 2.048)
113+
assert.Equal(t, c, dac.Conn())
114+
}
115+
116+
// TestMAX581xSetInputCodeWithInvalidChannel calls dac.SetInputCode with a
117+
// input code that lies outside the range of the DAC.
118+
func TestMAX581xSetInputCodeWithInvalidCode(t *testing.T) {
119+
var tests = []struct {
120+
dac dac.DAC
121+
code int
122+
}{
123+
{MAX5813{}, -1},
124+
{MAX5813{}, 256},
125+
{MAX5814{}, -1},
126+
{MAX5814{}, 1024},
127+
{MAX5815{}, -1},
128+
{MAX5815{}, 4096},
129+
}
130+
131+
for _, test := range tests {
132+
assert.EqualError(
133+
t,
134+
test.dac.SetInputCode(test.code, 1),
135+
fmt.Sprintf("digital input code %d is out of range of 0 <= code < 1", test.code))
136+
}
137+
}
138+
139+
// TestMAX581xSetInputCodeWithInvalidChannel calls dac.SetInputCode with a
140+
// channel that isn't in the range of the DAC.
141+
func TestMAX581xSetInputCodeWithInvalidChannel(t *testing.T) {
142+
dac := max581x{}
143+
144+
for _, channel := range []int{-1, 4} {
145+
assert.EqualError(
146+
t,
147+
dac.SetInputCode(512, channel),
148+
fmt.Sprintf("%d is not a valid channel", channel))
149+
}
150+
}
151+
152+
// TestMAX581xWithFailingConnection test if all DAC's return errors when the
153+
// connection fails.
154+
func TestMAX581xWithFailingConnection(t *testing.T) {
155+
c := iotest.NewI2CConn()
156+
c.TxFunc(func(w, _ []byte) error {
157+
return fmt.Errorf("som error occured")
158+
})
159+
conn, _ := i2c.Open(iotest.NewI2CDriver(c), 0x1)
160+
161+
_, err := NewMAX5813(conn, 2.048)
162+
assert.NotNil(t, err)
163+
164+
_, err = NewMAX5814(conn, 2.048)
165+
assert.NotNil(t, err)
166+
167+
_, err = NewMAX5815(conn, 2.048)
168+
assert.NotNil(t, err)
169+
170+
dac := max581x{
171+
conn: conn,
172+
vref: 2.048,
173+
resolution: 8,
174+
}
175+
176+
assert.NotNil(t, dac.SetInputCode(512, 1))
177+
}
178+
103179
func ExampleMAX5813() {
104180
d, err := i2c.Open(&i2c.Devfs{
105181
Dev: "/dev/i2c-0",

0 commit comments

Comments
 (0)