Skip to content

Commit

Permalink
Added ability to make a relay driver inverted (#674)
Browse files Browse the repository at this point in the history
* gpio: Added ability to make a relay driver inverted
  • Loading branch information
andrewebdev authored and deadprogram committed Jun 25, 2019
1 parent 36730e3 commit c7d6ea7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
21 changes: 19 additions & 2 deletions drivers/gpio/relay_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type RelayDriver struct {
name string
connection DigitalWriter
high bool
Inverted bool
gobot.Commander
}

Expand All @@ -23,6 +24,7 @@ func NewRelayDriver(a DigitalWriter, pin string) *RelayDriver {
pin: pin,
connection: a,
high: false,
Inverted: false,
Commander: gobot.NewCommander(),
}

Expand Down Expand Up @@ -63,6 +65,9 @@ func (l *RelayDriver) Connection() gobot.Connection {

// State return true if the relay is On and false if the relay is Off
func (l *RelayDriver) State() bool {
if l.Inverted {
return !l.high
}
return l.high
}

Expand All @@ -71,7 +76,13 @@ func (l *RelayDriver) On() (err error) {
if err = l.connection.DigitalWrite(l.Pin(), 1); err != nil {
return
}
l.high = true

if l.Inverted {
l.high = false
} else {
l.high = true
}

return
}

Expand All @@ -80,7 +91,13 @@ func (l *RelayDriver) Off() (err error) {
if err = l.connection.DigitalWrite(l.Pin(), 0); err != nil {
return
}
l.high = false

if l.Inverted {
l.high = true
} else {
l.high = false
}

return
}

Expand Down
31 changes: 31 additions & 0 deletions drivers/gpio/relay_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

var _ gobot.Driver = (*RelayDriver)(nil)

// Helper to return low/high value for testing
func (l *RelayDriver) High() bool { return l.high }

func initTestRelayDriver() *RelayDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
Expand Down Expand Up @@ -52,6 +55,17 @@ func TestRelayDriverToggle(t *testing.T) {
gobottest.Assert(t, d.State(), false)
}

func TestRelayDriverToggleInverted(t *testing.T) {
d := initTestRelayDriver()
d.Inverted = true
d.Off()
gobottest.Assert(t, d.State(), false)
d.Toggle()
gobottest.Assert(t, d.State(), true)
d.Toggle()
gobottest.Assert(t, d.State(), false)
}

func TestRelayDriverCommands(t *testing.T) {
d := initTestRelayDriver()
gobottest.Assert(t, d.Command("Off")(nil), nil)
Expand All @@ -63,3 +77,20 @@ func TestRelayDriverCommands(t *testing.T) {
gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.State(), false)
}

func TestRelayDriverCommandsInverted(t *testing.T) {
d := initTestRelayDriver()
d.Inverted = true

gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)

gobottest.Assert(t, d.Command("On")(nil), nil)
gobottest.Assert(t, d.High(), false)
gobottest.Assert(t, d.State(), true)

gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)
}

0 comments on commit c7d6ea7

Please sign in to comment.