1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

103 lines
2.2 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
"github.com/hybridgroup/gobot"
)
var _ gobot.DriverInterface = (*LedDriver)(nil)
2014-09-27 11:34:13 -07:00
// Represents a digital Led
2014-04-27 19:34:16 -07:00
type LedDriver struct {
gobot.Driver
2014-09-27 11:34:13 -07:00
high bool
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// NewLedDriver return a new LedDriver given a PwmDigitalWriter, name and pin.
//
// Adds the following API Commands:
// "Brightness" - See LedDriver.Brightness
// "Toggle" - See LedDriver.Toggle
// "On" - See LedDriver.On
// "Off" - See LedDriver.Off
2014-07-09 16:51:00 -07:00
func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
2014-06-11 17:41:04 -07:00
l := &LedDriver{
2014-06-23 20:33:59 -07:00
Driver: *gobot.NewDriver(
name,
"LedDriver",
pin,
2014-06-23 20:33:59 -07:00
a.(gobot.AdaptorInterface),
),
2014-09-27 11:34:13 -07:00
high: false,
2014-04-27 19:34:16 -07:00
}
2014-06-11 17:41:04 -07:00
2014-07-09 16:51:00 -07:00
l.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
level := byte(params["level"].(float64))
l.Brightness(level)
return nil
})
2014-07-09 16:51:00 -07:00
l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
l.Toggle()
return nil
})
2014-07-09 16:51:00 -07:00
l.AddCommand("On", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
l.On()
return nil
})
2014-07-09 16:51:00 -07:00
l.AddCommand("Off", func(params map[string]interface{}) interface{} {
2014-06-11 17:41:04 -07:00
l.Off()
return nil
})
return l
2014-04-27 19:34:16 -07:00
}
func (l *LedDriver) adaptor() PwmDigitalWriter {
2014-07-09 16:51:00 -07:00
return l.Adaptor().(PwmDigitalWriter)
}
2014-09-27 11:34:13 -07:00
// Start starts the LedDriver. Returns true on successful start of the driver
func (l *LedDriver) Start() error { return nil }
2014-04-27 19:34:16 -07:00
2014-09-27 11:34:13 -07:00
// Halt halts the LedDriver. Returns true on successful halt of the driver
func (l *LedDriver) Halt() error { return nil }
2014-04-27 19:34:16 -07:00
2014-09-27 11:34:13 -07:00
// State return true if the led is On and false if the led is Off
func (l *LedDriver) State() bool {
return l.high
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// On sets the led to a high state. Returns true on success
2014-04-27 19:34:16 -07:00
func (l *LedDriver) On() bool {
l.changeState(1)
2014-09-27 11:34:13 -07:00
l.high = true
2014-04-27 19:34:16 -07:00
return true
}
2014-09-27 11:34:13 -07:00
// Off sets the led to a low state. Returns true on success
2014-04-27 19:34:16 -07:00
func (l *LedDriver) Off() bool {
l.changeState(0)
2014-09-27 11:34:13 -07:00
l.high = false
2014-04-27 19:34:16 -07:00
return true
}
2014-09-27 11:34:13 -07:00
// Toggle sets the led to the opposite of it's current state
2014-04-27 19:34:16 -07:00
func (l *LedDriver) Toggle() {
2014-09-27 11:34:13 -07:00
if l.State() {
2014-04-27 19:34:16 -07:00
l.Off()
} else {
l.On()
}
}
2014-09-27 11:34:13 -07:00
// Brightness sets the led to the specified level of brightness
2014-04-27 19:34:16 -07:00
func (l *LedDriver) Brightness(level byte) {
l.adaptor().PwmWrite(l.Pin(), level)
2014-04-27 19:34:16 -07:00
}
func (l *LedDriver) changeState(level byte) {
l.adaptor().DigitalWrite(l.Pin(), level)
2014-04-27 19:34:16 -07:00
}