hybridgroup.gobot/drivers/gpio/led_driver.go

110 lines
2.5 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import "gobot.io/x/gobot"
2014-04-28 10:34:16 +08:00
// LedDriver 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
}
// NewLedDriver return a new LedDriver given a DigitalWriter and pin.
2014-09-28 02:34:13 +08:00
//
// Adds the following API Commands:
// "Brightness" - See LedDriver.Brightness
// "Toggle" - See LedDriver.Toggle
// "On" - See LedDriver.On
// "Off" - See LedDriver.Off
func NewLedDriver(a DigitalWriter, pin string) *LedDriver {
2014-06-12 08:41:04 +08:00
l := &LedDriver{
name: gobot.DefaultName("LED"),
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
}
// Start implements the Driver interface
func (l *LedDriver) Start() (err error) { return }
2014-04-28 10:34:16 +08:00
// Halt implements the Driver interface
func (l *LedDriver) Halt() (err error) { return }
2014-04-28 10:34:16 +08:00
// Name returns the LedDrivers name
2014-12-17 05:42:48 +08:00
func (l *LedDriver) Name() string { return l.name }
// SetName sets the LedDrivers name
func (l *LedDriver) SetName(n string) { l.name = n }
// Pin returns the LedDrivers name
func (l *LedDriver) Pin() string { return l.pin }
// Connection returns the LedDrivers Connection
2014-12-17 05:42:48 +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
}
// On sets the led to a high state.
func (l *LedDriver) On() (err error) {
2014-12-17 05:42:48 +08:00
if err = l.connection.DigitalWrite(l.Pin(), 1); err != nil {
return
}
2014-09-28 02:34:13 +08:00
l.high = true
return
2014-04-28 10:34:16 +08:00
}
// Off sets the led to a low state.
func (l *LedDriver) Off() (err error) {
2014-12-17 05:42:48 +08:00
if err = l.connection.DigitalWrite(l.Pin(), 0); 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
}