hybridgroup.gobot/platforms/gpio/led_driver.go

108 lines
2.6 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import "github.com/hybridgroup/gobot"
2014-04-28 10:34:16 +08:00
var _ gobot.Driver = (*LedDriver)(nil)
2014-09-28 02:34:13 +08:00
// Represents a digital Led
2014-04-28 10:34:16 +08:00
type LedDriver struct {
pin string
name string
2014-11-30 03:02:10 +08:00
connection DigitalWriter
high bool
gobot.Commander
2014-04-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08: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-11-30 03:02:10 +08:00
func NewLedDriver(a DigitalWriter, name string, pin string) *LedDriver {
2014-06-12 08:41:04 +08:00
l := &LedDriver{
name: name,
pin: pin,
2014-11-30 03:02:10 +08:00
connection: a,
high: false,
Commander: gobot.NewCommander(),
2014-04-28 10:34:16 +08:00
}
2014-06-12 08:41:04 +08:00
2014-07-10 07:51:00 +08:00
l.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
level := byte(params["level"].(float64))
return l.Brightness(level)
2014-06-12 08:41:04 +08:00
})
2014-07-10 07:51:00 +08:00
l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
return l.Toggle()
2014-06-12 08:41:04 +08:00
})
2014-07-10 07:51:00 +08:00
l.AddCommand("On", func(params map[string]interface{}) interface{} {
return l.On()
2014-06-12 08:41:04 +08:00
})
2014-07-10 07:51:00 +08:00
l.AddCommand("Off", func(params map[string]interface{}) interface{} {
return l.Off()
2014-06-12 08:41:04 +08:00
})
return l
2014-04-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08:00
// Start starts the LedDriver. Returns true on successful start of the driver
func (l *LedDriver) Start() (errs []error) { return }
2014-04-28 10:34:16 +08:00
2014-09-28 02:34:13 +08:00
// Halt halts the LedDriver. Returns true on successful halt of the driver
func (l *LedDriver) Halt() (errs []error) { return }
2014-04-28 10:34:16 +08:00
func (l *LedDriver) Name() string { return l.name }
func (l *LedDriver) Pin() string { return l.pin }
2014-11-30 03:02:10 +08:00
func (l *LedDriver) Connection() gobot.Connection { return l.connection.(gobot.Connection) }
2014-09-28 02:34:13 +08: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-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08:00
// On sets the led to a high state. Returns true on success
func (l *LedDriver) On() (err error) {
err = l.changeState(1)
if err != nil {
return
}
2014-09-28 02:34:13 +08:00
l.high = true
return
2014-04-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08:00
// Off sets the led to a low state. Returns true on success
func (l *LedDriver) Off() (err error) {
err = l.changeState(0)
if err != nil {
return
}
2014-09-28 02:34:13 +08:00
l.high = false
return
2014-04-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08:00
// Toggle sets the led to the opposite of it's current state
func (l *LedDriver) Toggle() (err error) {
2014-09-28 02:34:13 +08:00
if l.State() {
err = l.Off()
2014-04-28 10:34:16 +08:00
} else {
err = l.On()
2014-04-28 10:34:16 +08:00
}
return
2014-04-28 10:34:16 +08:00
}
2014-09-28 02:34:13 +08:00
// Brightness sets the led to the specified level of brightness
func (l *LedDriver) Brightness(level byte) (err error) {
2014-11-30 03:02:10 +08:00
if writer, ok := l.connection.(PwmWriter); ok {
return writer.PwmWrite(l.Pin(), level)
}
return ErrPwmWriteUnsupported
2014-04-28 10:34:16 +08:00
}
func (l *LedDriver) changeState(level byte) (err error) {
2014-11-30 03:02:10 +08:00
return l.connection.DigitalWrite(l.Pin(), level)
2014-04-28 10:34:16 +08:00
}