2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
import "github.com/hybridgroup/gobot"
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2014-11-23 04:21:28 +08:00
|
|
|
var _ gobot.Driver = (*LedDriver)(nil)
|
2014-11-17 04:25:48 +08:00
|
|
|
|
2014-09-28 02:34:13 +08:00
|
|
|
// Represents a digital Led
|
2014-04-28 10:34:16 +08:00
|
|
|
type LedDriver struct {
|
2014-11-23 04:21:28 +08:00
|
|
|
pin string
|
|
|
|
name string
|
2014-11-30 03:02:10 +08:00
|
|
|
connection DigitalWriter
|
2014-11-23 04:21:28 +08:00
|
|
|
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{
|
2014-11-23 04:21:28 +08:00
|
|
|
name: name,
|
|
|
|
pin: pin,
|
2014-11-30 03:02:10 +08:00
|
|
|
connection: a,
|
2014-11-23 04:21:28 +08:00
|
|
|
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))
|
2014-11-17 05:45:18 +08:00
|
|
|
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{} {
|
2014-11-17 05:45:18 +08:00
|
|
|
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{} {
|
2014-11-17 05:45:18 +08:00
|
|
|
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{} {
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
2014-11-20 15:21:19 +08:00
|
|
|
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
|
2014-11-20 15:21:19 +08:00
|
|
|
func (l *LedDriver) Halt() (errs []error) { return }
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2014-11-23 04:21:28 +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-11-23 04:21:28 +08:00
|
|
|
|
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
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
2014-11-17 05:45:18 +08:00
|
|
|
func (l *LedDriver) Toggle() (err error) {
|
2014-09-28 02:34:13 +08:00
|
|
|
if l.State() {
|
2014-11-17 05:45:18 +08:00
|
|
|
err = l.Off()
|
2014-04-28 10:34:16 +08:00
|
|
|
} else {
|
2014-11-17 05:45:18 +08:00
|
|
|
err = l.On()
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
2014-11-17 05:45:18 +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
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-17 05:45:18 +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
|
|
|
}
|