Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to make a relay driver inverted #674

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}