2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
import "gobot.io/x/gobot"
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// LedDriver 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
|
|
|
}
|
|
|
|
|
2016-09-25 19:36:18 +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
|
2016-09-25 19:36:18 +08:00
|
|
|
func NewLedDriver(a DigitalWriter, pin string) *LedDriver {
|
2014-06-12 08:41:04 +08:00
|
|
|
l := &LedDriver{
|
2017-02-02 22:37:09 +08:00
|
|
|
name: gobot.DefaultName("LED"),
|
2014-11-23 04:21:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Start implements the Driver interface
|
2016-11-07 21:55:21 +08:00
|
|
|
func (l *LedDriver) Start() (err error) { return }
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Halt implements the Driver interface
|
2016-11-07 21:55:21 +08:00
|
|
|
func (l *LedDriver) Halt() (err error) { return }
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Name returns the LedDrivers name
|
2014-12-17 05:42:48 +08:00
|
|
|
func (l *LedDriver) Name() string { return l.name }
|
2015-01-03 02:42:53 +08:00
|
|
|
|
2016-09-25 19:36:18 +08:00
|
|
|
// SetName sets the LedDrivers name
|
|
|
|
func (l *LedDriver) SetName(n string) { l.name = n }
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// 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-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
|
|
|
}
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// On sets the led to a high state.
|
2014-11-17 05:45:18 +08:00
|
|
|
func (l *LedDriver) On() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
if err = l.connection.DigitalWrite(l.Pin(), 1); err != nil {
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Off sets the led to a low state.
|
2014-11-17 05:45:18 +08:00
|
|
|
func (l *LedDriver) Off() (err error) {
|
2014-12-17 05:42:48 +08:00
|
|
|
if err = l.connection.DigitalWrite(l.Pin(), 0); err != nil {
|
2014-11-17 05:45:18 +08:00
|
|
|
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
|
|
|
}
|