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