Skip to content

Commit

Permalink
Merge pull request hybridgroup#742 from stuartleeks/sl/relay-inverted
Browse files Browse the repository at this point in the history
Update value written via DigitalWrite if RelayDriver.Inverted is true
  • Loading branch information
trevrosen authored Apr 6, 2020
2 parents 5541dcd + 73eafc5 commit 6f87167
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 72 deletions.
12 changes: 6 additions & 6 deletions drivers/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand All @@ -62,7 +62,7 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 0
return
})
Expand All @@ -77,7 +77,7 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
err = errors.New("digital read error")
return
})
Expand All @@ -94,7 +94,7 @@ func TestButtonDriverStart(t *testing.T) {

d.halt <- true

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand All @@ -117,7 +117,7 @@ func TestButtonDriverDefaultState(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 0
return
})
Expand All @@ -135,7 +135,7 @@ func TestButtonDriverDefaultState(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand Down
6 changes: 3 additions & 3 deletions drivers/gpio/buzzer_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBuzzerDriverTone(t *testing.T) {
func TestBuzzerDriverOnError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func() (err error) {
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
return errors.New("write error")
})

Expand All @@ -63,7 +63,7 @@ func TestBuzzerDriverOnError(t *testing.T) {
func TestBuzzerDriverOffError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func() (err error) {
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
return errors.New("write error")
})

Expand All @@ -73,7 +73,7 @@ func TestBuzzerDriverOffError(t *testing.T) {
func TestBuzzerDriverToneError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.TestAdaptorDigitalWrite(func() (err error) {
a.TestAdaptorDigitalWrite(func(string, byte) (err error) {
return errors.New("write error")
})

Expand Down
8 changes: 4 additions & 4 deletions drivers/gpio/direct_pin_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ var _ gobot.Driver = (*DirectPinDriver)(nil)

func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalRead = func() (val int, err error) {
a.testAdaptorDigitalRead = func(string) (val int, err error) {
val = 1
return
}
a.testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorServoWrite = func() (err error) {
a.testAdaptorServoWrite = func(string, byte) (err error) {
return errors.New("write error")
}
return NewDirectPinDriver(a, "1")
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/grove_drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDigitalDriverHalt(t *testing.T) {
for _, driver := range drivers {

var callCount int32
testAdaptor.testAdaptorDigitalRead = func() (int, error) {
testAdaptor.testAdaptorDigitalRead = func(string) (int, error) {
atomic.AddInt32(&callCount, 1)
return 42, nil
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestDriverPublishesError(t *testing.T) {
for _, driver := range drivers {
sem := make(chan struct{}, 1)
// send error
returnErr := func() (val int, err error) {
returnErr := func(string) (val int, err error) {
err = errors.New("read error")
return
}
Expand Down
50 changes: 25 additions & 25 deletions drivers/gpio/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,63 @@ type gpioTestAdaptor struct {
name string
port string
mtx sync.Mutex
testAdaptorDigitalWrite func() (err error)
testAdaptorServoWrite func() (err error)
testAdaptorPwmWrite func() (err error)
testAdaptorAnalogRead func() (val int, err error)
testAdaptorDigitalRead func() (val int, err error)
testAdaptorDigitalWrite func(pin string, val byte) (err error)
testAdaptorServoWrite func(pin string, val byte) (err error)
testAdaptorPwmWrite func(pin string, val byte) (err error)
testAdaptorAnalogRead func(ping string) (val int, err error)
testAdaptorDigitalRead func(ping string) (val int, err error)
}

func (t *gpioTestAdaptor) TestAdaptorDigitalWrite(f func() (err error)) {
func (t *gpioTestAdaptor) TestAdaptorDigitalWrite(f func(pin string, val byte) (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorDigitalWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorServoWrite(f func() (err error)) {
func (t *gpioTestAdaptor) TestAdaptorServoWrite(f func(pin string, val byte) (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorServoWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorPwmWrite(f func() (err error)) {
func (t *gpioTestAdaptor) TestAdaptorPwmWrite(f func(pin string, val byte) (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorPwmWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorAnalogRead(f func() (val int, err error)) {
func (t *gpioTestAdaptor) TestAdaptorAnalogRead(f func(pin string) (val int, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorAnalogRead = f
}
func (t *gpioTestAdaptor) TestAdaptorDigitalRead(f func() (val int, err error)) {
func (t *gpioTestAdaptor) TestAdaptorDigitalRead(f func(pin string) (val int, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorDigitalRead = f
}

func (t *gpioTestAdaptor) ServoWrite(string, byte) (err error) {
func (t *gpioTestAdaptor) ServoWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorServoWrite()
return t.testAdaptorServoWrite(pin, val)
}
func (t *gpioTestAdaptor) PwmWrite(string, byte) (err error) {
func (t *gpioTestAdaptor) PwmWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorPwmWrite()
return t.testAdaptorPwmWrite(pin, val)
}
func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) {
func (t *gpioTestAdaptor) AnalogRead(pin string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorAnalogRead()
return t.testAdaptorAnalogRead(pin)
}
func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
func (t *gpioTestAdaptor) DigitalRead(pin string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorDigitalRead()
return t.testAdaptorDigitalRead(pin)
}
func (t *gpioTestAdaptor) DigitalWrite(string, byte) (err error) {
func (t *gpioTestAdaptor) DigitalWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorDigitalWrite()
return t.testAdaptorDigitalWrite(pin, val)
}
func (t *gpioTestAdaptor) Connect() (err error) { return }
func (t *gpioTestAdaptor) Finalize() (err error) { return }
Expand All @@ -86,19 +86,19 @@ func (t *gpioTestAdaptor) Port() string { return t.port }
func newGpioTestAdaptor() *gpioTestAdaptor {
return &gpioTestAdaptor{
port: "/dev/null",
testAdaptorDigitalWrite: func() (err error) {
testAdaptorDigitalWrite: func(pin string, val byte) (err error) {
return nil
},
testAdaptorServoWrite: func() (err error) {
testAdaptorServoWrite: func(pin string, val byte) (err error) {
return nil
},
testAdaptorPwmWrite: func() (err error) {
testAdaptorPwmWrite: func(pin string, val byte) (err error) {
return nil
},
testAdaptorAnalogRead: func() (val int, err error) {
testAdaptorAnalogRead: func(pin string) (val int, err error) {
return 99, nil
},
testAdaptorDigitalRead: func() (val int, err error) {
testAdaptorDigitalRead: func(pin string) (val int, err error) {
return 1, nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions drivers/gpio/led_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var _ gobot.Driver = (*LedDriver)(nil)

func initTestLedDriver() *LedDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return nil
}
a.testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return nil
}
return NewLedDriver(a, "1")
Expand All @@ -30,10 +30,10 @@ func TestLedDriver(t *testing.T) {
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)

a.testAdaptorDigitalWrite = func() (err error) {
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return errors.New("pwm error")
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestLedDriverToggle(t *testing.T) {
func TestLedDriverBrightness(t *testing.T) {
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
a.testAdaptorPwmWrite = func() (err error) {
a.testAdaptorPwmWrite = func(string, byte) (err error) {
err = errors.New("pwm error")
return
}
Expand Down
8 changes: 4 additions & 4 deletions drivers/gpio/makey_button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 0
return
})
Expand All @@ -70,7 +70,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand All @@ -86,7 +86,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
err = errors.New("digital read error")
return
})
Expand All @@ -102,7 +102,7 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand Down
6 changes: 3 additions & 3 deletions drivers/gpio/pir_motion_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
Expand All @@ -62,7 +62,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 0
return
})
Expand All @@ -77,7 +77,7 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
err = errors.New("digital read error")
return
})
Expand Down
12 changes: 10 additions & 2 deletions drivers/gpio/relay_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func (l *RelayDriver) State() bool {

// On sets the relay to a high state.
func (l *RelayDriver) On() (err error) {
if err = l.connection.DigitalWrite(l.Pin(), 1); err != nil {
newValue := byte(1)
if l.Inverted {
newValue = 0
}
if err = l.connection.DigitalWrite(l.Pin(), newValue); err != nil {
return
}

Expand All @@ -88,7 +92,11 @@ func (l *RelayDriver) On() (err error) {

// Off sets the relay to a low state.
func (l *RelayDriver) Off() (err error) {
if err = l.connection.DigitalWrite(l.Pin(), 0); err != nil {
newValue := byte(0)
if l.Inverted {
newValue = 1
}
if err = l.connection.DigitalWrite(l.Pin(), newValue); err != nil {
return
}

Expand Down
Loading

0 comments on commit 6f87167

Please sign in to comment.