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 1 commit
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
27 changes: 25 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,
andrewebdev marked this conversation as resolved.
Show resolved Hide resolved
Commander: gobot.NewCommander(),
}

Expand Down Expand Up @@ -53,6 +55,12 @@ func (l *RelayDriver) Name() string { return l.name }
// SetName sets the RelayDrivers name
func (l *RelayDriver) SetName(n string) { l.name = n }

// Inverted returns the RelayDrivers inverted state
andrewebdev marked this conversation as resolved.
Show resolved Hide resolved
func (l *RelayDriver) Inverted() bool { return l.inverted }

// SetInverted sets the RelayDrivers inverted state
func (l *RelayDriver) SetInverted(inverted bool) { l.inverted = inverted }

// Pin returns the RelayDrivers name
func (l *RelayDriver) Pin() string { return l.pin }

Expand All @@ -63,6 +71,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 +82,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 +97,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
38 changes: 38 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 All @@ -33,6 +36,13 @@ func TestRelayDriverSetName(t *testing.T) {
gobottest.Assert(t, g.Name(), "mybot")
}

func TestRelayDriverSetInverted(t *testing.T) {
d := initTestRelayDriver()
gobottest.Assert(t, d.Inverted(), false)
d.SetInverted(true)
gobottest.Assert(t, d.Inverted(), true)
}

func TestRelayDriverStart(t *testing.T) {
d := initTestRelayDriver()
gobottest.Assert(t, d.Start(), nil)
Expand All @@ -52,6 +62,17 @@ func TestRelayDriverToggle(t *testing.T) {
gobottest.Assert(t, d.State(), false)
}

func TestRelayDriverToggleInverted(t *testing.T) {
d := initTestRelayDriver()
d.SetInverted(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 +84,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.SetInverted(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)
}