Clean up button driver
This commit is contained in:
parent
7050410217
commit
cad4c4abb0
|
@ -13,18 +13,24 @@ type ButtonDriver struct {
|
||||||
Active bool
|
Active bool
|
||||||
pin string
|
pin string
|
||||||
name string
|
name string
|
||||||
|
interval time.Duration
|
||||||
connection gobot.Connection
|
connection gobot.Connection
|
||||||
gobot.Eventer
|
gobot.Eventer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
|
// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
|
||||||
func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
|
func NewButtonDriver(a DigitalReader, name string, pin string, v ...time.Duration) *ButtonDriver {
|
||||||
b := &ButtonDriver{
|
b := &ButtonDriver{
|
||||||
name: name,
|
name: name,
|
||||||
connection: a.(gobot.Adaptor),
|
connection: a,
|
||||||
pin: pin,
|
pin: pin,
|
||||||
Active: false,
|
Active: false,
|
||||||
Eventer: gobot.NewEventer(),
|
Eventer: gobot.NewEventer(),
|
||||||
|
interval: 10 * time.Millisecond,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(v) > 0 {
|
||||||
|
b.interval = v[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
b.AddEvent("push")
|
b.AddEvent("push")
|
||||||
|
@ -56,8 +62,7 @@ func (b *ButtonDriver) Start() (errs []error) {
|
||||||
state = newValue
|
state = newValue
|
||||||
b.update(newValue)
|
b.update(newValue)
|
||||||
}
|
}
|
||||||
//<-time.After(b.Interval())
|
<-time.After(b.interval)
|
||||||
<-time.After(100 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return
|
return
|
||||||
|
@ -69,17 +74,6 @@ func (b *ButtonDriver) Halt() (errs []error) { return }
|
||||||
func (b *ButtonDriver) Name() string { return b.name }
|
func (b *ButtonDriver) Name() string { return b.name }
|
||||||
func (b *ButtonDriver) Pin() string { return b.pin }
|
func (b *ButtonDriver) Pin() string { return b.pin }
|
||||||
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection }
|
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection }
|
||||||
func (b *ButtonDriver) String() string { return "ButtonDriver" }
|
|
||||||
func (b *ButtonDriver) ToJSON() *gobot.JSONDevice {
|
|
||||||
return &gobot.JSONDevice{
|
|
||||||
Name: b.Name(),
|
|
||||||
Driver: b.String(),
|
|
||||||
Connection: b.Connection().Name(),
|
|
||||||
//Commands: l.Commands(),
|
|
||||||
//Commands: l.Commands(),
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *ButtonDriver) readState() (val int, err error) {
|
func (b *ButtonDriver) readState() (val int, err error) {
|
||||||
return b.adaptor().DigitalRead(b.Pin())
|
return b.adaptor().DigitalRead(b.Pin())
|
||||||
|
|
Loading…
Reference in New Issue