2014-03-23 07:21:22 +05:30
|
|
|
// LED support.
|
|
|
|
|
2014-03-23 06:25:32 +05:30
|
|
|
package embd
|
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// The LED interface is used to control a led on the prototyping board.
|
2014-03-23 06:25:32 +05:30
|
|
|
type LED interface {
|
2014-03-23 14:09:31 +05:30
|
|
|
// On switches the LED on.
|
2014-03-23 06:25:32 +05:30
|
|
|
On() error
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// Off switches the LED off.
|
2014-03-23 06:25:32 +05:30
|
|
|
Off() error
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Toggle toggles the LED.
|
2014-03-23 06:25:32 +05:30
|
|
|
Toggle() error
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Close releases resources associated with the LED.
|
2014-03-23 06:25:32 +05:30
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// LEDDriver interface interacts with the host descriptors to allow us
|
|
|
|
// control of the LEDs.
|
2014-03-23 06:25:32 +05:30
|
|
|
type LEDDriver interface {
|
|
|
|
LED(key interface{}) (LED, error)
|
|
|
|
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-04-11 09:19:03 +05:30
|
|
|
var ledDriverInitialized bool
|
2014-03-23 06:25:32 +05:30
|
|
|
var ledDriverInstance LEDDriver
|
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// InitLED initializes the LED driver.
|
2014-03-23 06:25:32 +05:30
|
|
|
func InitLED() error {
|
2014-04-11 09:19:03 +05:30
|
|
|
if ledDriverInitialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 06:25:32 +05:30
|
|
|
desc, err := DescribeHost()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if desc.LEDDriver == nil {
|
2014-03-23 06:41:51 +05:30
|
|
|
return ErrFeatureNotSupported
|
2014-03-23 06:25:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ledDriverInstance = desc.LEDDriver()
|
2014-04-11 09:19:03 +05:30
|
|
|
ledDriverInitialized = true
|
2014-03-23 06:25:32 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// CloseLED releases resources associated with the LED driver.
|
2014-03-23 06:25:32 +05:30
|
|
|
func CloseLED() error {
|
|
|
|
return ledDriverInstance.Close()
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// NewLED returns a LED interface which allows control over the LED.
|
2014-03-23 06:25:32 +05:30
|
|
|
func NewLED(key interface{}) (LED, error) {
|
2014-04-11 09:19:03 +05:30
|
|
|
if err := InitLED(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-23 06:25:32 +05:30
|
|
|
return ledDriverInstance.LED(key)
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// LEDOn switches the LED on.
|
2014-03-23 06:25:32 +05:30
|
|
|
func LEDOn(key interface{}) error {
|
|
|
|
led, err := NewLED(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return led.On()
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// LEDOff switches the LED off.
|
2014-03-23 06:25:32 +05:30
|
|
|
func LEDOff(key interface{}) error {
|
|
|
|
led, err := NewLED(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return led.Off()
|
|
|
|
}
|
2014-03-23 06:32:57 +05:30
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// LEDToggle toggles the LED.
|
2014-03-23 06:32:57 +05:30
|
|
|
func LEDToggle(key interface{}) error {
|
|
|
|
led, err := NewLED(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return led.Toggle()
|
|
|
|
}
|