Skip to content

Commit

Permalink
gpio: Add ButtonDriver.DefaultState to allow for 'reverse' buttons (o…
Browse files Browse the repository at this point in the history
…nes that go from HIGH to LOW)

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 22, 2017
1 parent 40b6367 commit 9f1e5fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
32 changes: 17 additions & 15 deletions drivers/gpio/button_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (

// ButtonDriver Represents a digital Button
type ButtonDriver struct {
Active bool
pin string
name string
halt chan bool
interval time.Duration
connection DigitalReader
Active bool
DefaultState int
pin string
name string
halt chan bool
interval time.Duration
connection DigitalReader
gobot.Eventer
}

Expand All @@ -24,13 +25,14 @@ type ButtonDriver struct {
// time.Duration: Interval at which the ButtonDriver is polled for new information
func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDriver {
b := &ButtonDriver{
name: gobot.DefaultName("Button"),
connection: a,
pin: pin,
Active: false,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
name: gobot.DefaultName("Button"),
connection: a,
pin: pin,
Active: false,
DefaultState: 0,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
}

if len(v) > 0 {
Expand All @@ -51,7 +53,7 @@ func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDri
// Release int - On button release
// Error error - On button error
func (b *ButtonDriver) Start() (err error) {
state := 0
state := b.DefaultState
go func() {
for {
newValue, err := b.connection.DigitalRead(b.Pin())
Expand Down Expand Up @@ -90,7 +92,7 @@ func (b *ButtonDriver) Pin() string { return b.pin }
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }

func (b *ButtonDriver) update(newValue int) {
if newValue == 1 {
if newValue != b.DefaultState {
b.Active = true
b.Publish(ButtonPush, newValue)
} else {
Expand Down
41 changes: 41 additions & 0 deletions drivers/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,47 @@ func TestButtonDriverStart(t *testing.T) {
}
}

func TestButtonDriverDefaultState(t *testing.T) {
sem := make(chan bool, 0)
a := newGpioTestAdaptor()
d := NewButtonDriver(a, "1")
d.DefaultState = 1

d.Once(ButtonPush, func(data interface{}) {
gobottest.Assert(t, d.Active, true)
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 0
return
})

gobottest.Assert(t, d.Start(), nil)

select {
case <-sem:
case <-time.After(buttonTestDelay * time.Millisecond):
t.Errorf("Button Event \"Push\" was not published")
}

d.Once(ButtonRelease, func(data interface{}) {
gobottest.Assert(t, d.Active, false)
sem <- true
})

a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
})

select {
case <-sem:
case <-time.After(buttonTestDelay * time.Millisecond):
t.Errorf("Button Event \"Release\" was not published")
}
}

func TestButtonDriverDefaultName(t *testing.T) {
g := initTestButtonDriver()
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Button"), true)
Expand Down

0 comments on commit 9f1e5fe

Please sign in to comment.