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

109 lines
2.5 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import "github.com/hybridgroup/gobot"
2014-04-27 19:34:16 -07:00
var _ gobot.Driver = (*LedDriver)(nil)
// LedDriver represents a digital Led
2014-04-27 19:34:16 -07:00
type LedDriver struct {
pin string
name string
2014-11-29 11:02:10 -08:00
connection DigitalWriter
high bool
gobot.Commander
2014-04-27 19:34:16 -07:00
}
// NewLedDriver return a new LedDriver given a DigitalWriter, name and pin.
2014-09-27 11:34:13 -07:00
//
// Adds the following API Commands:
// "Brightness" - See LedDriver.Brightness
// "Toggle" - See LedDriver.Toggle
// "On" - See LedDriver.On
// "Off" - See LedDriver.Off
2014-11-29 11:02:10 -08:00
func NewLedDriver(a DigitalWriter, name string, pin string) *LedDriver {
2014-06-11 17:41:04 -07:00
l := &LedDriver{
name: name,
pin: pin,
2014-11-29 11:02:10 -08:00
connection: a,
high: false,
Commander: gobot.NewCommander(),
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))
return l.Brightness(level)
2014-06-11 17:41:04 -07:00
})
2014-07-09 16:51:00 -07:00
l.AddCommand("Toggle", func(params map[string]interface{}) interface{} {
return l.Toggle()
2014-06-11 17:41:04 -07:00
})
2014-07-09 16:51:00 -07:00
l.AddCommand("On", func(params map[string]interface{}) interface{} {
return l.On()
2014-06-11 17:41:04 -07:00
})
2014-07-09 16:51:00 -07:00
l.AddCommand("Off", func(params map[string]interface{}) interface{} {
return l.Off()
2014-06-11 17:41:04 -07:00
})
return l
2014-04-27 19:34:16 -07:00
}
// Start implements the Driver interface
func (l *LedDriver) Start() (errs []error) { return }
2014-04-27 19:34:16 -07:00
// Halt implements the Driver interface
func (l *LedDriver) Halt() (errs []error) { return }
2014-04-27 19:34:16 -07:00
// Name returns the LedDrivers name
2014-12-16 13:42:48 -08:00
func (l *LedDriver) Name() string { return l.name }
// Pin returns the LedDrivers name
func (l *LedDriver) Pin() string { return l.pin }
// Connection returns the LedDrivers Connection
2014-12-16 13:42:48 -08:00
func (l *LedDriver) Connection() gobot.Connection {
return l.connection.(gobot.Connection)
}
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
}
// On sets the led to a high state.
func (l *LedDriver) On() (err error) {
2014-12-16 13:42:48 -08:00
if err = l.connection.DigitalWrite(l.Pin(), 1); err != nil {
return
}
2014-09-27 11:34:13 -07:00
l.high = true
return
2014-04-27 19:34:16 -07:00
}
// Off sets the led to a low state.
func (l *LedDriver) Off() (err error) {
2014-12-16 13:42:48 -08:00
if err = l.connection.DigitalWrite(l.Pin(), 0); err != nil {
return
}
2014-09-27 11:34:13 -07:00
l.high = false
return
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// Toggle sets the led to the opposite of it's current state
func (l *LedDriver) Toggle() (err error) {
2014-09-27 11:34:13 -07:00
if l.State() {
err = l.Off()
2014-04-27 19:34:16 -07:00
} else {
err = l.On()
2014-04-27 19:34:16 -07:00
}
return
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
// Brightness sets the led to the specified level of brightness
func (l *LedDriver) Brightness(level byte) (err error) {
2014-11-29 11:02:10 -08:00
if writer, ok := l.connection.(PwmWriter); ok {
return writer.PwmWrite(l.Pin(), level)
}
return ErrPwmWriteUnsupported
2014-04-27 19:34:16 -07:00
}