1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/led.go

95 lines
1.6 KiB
Go
Raw Normal View History

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