mirror of
https://github.com/kidoman/embd
synced 2024-12-30 16:41:37 +01:00
allow lazy initialisation of the drivers
this allows brevity in the sample codes (and should not be misutilized in real world applications)
This commit is contained in:
parent
9ac0872493
commit
94f99476c2
18
gpio.go
18
gpio.go
@ -119,10 +119,15 @@ type GPIODriver interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
var gpioDriverInitialized bool
|
||||
var gpioDriverInstance GPIODriver
|
||||
|
||||
// InitGPIO initializes the GPIO driver.
|
||||
func InitGPIO() error {
|
||||
if gpioDriverInitialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
desc, err := DescribeHost()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -133,6 +138,7 @@ func InitGPIO() error {
|
||||
}
|
||||
|
||||
gpioDriverInstance = desc.GPIODriver()
|
||||
gpioDriverInitialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -145,6 +151,10 @@ func CloseGPIO() error {
|
||||
// NewDigitalPin returns a DigitalPin interface which allows control over
|
||||
// the digital GPIO pin.
|
||||
func NewDigitalPin(key interface{}) (DigitalPin, error) {
|
||||
if err := InitGPIO(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return gpioDriverInstance.DigitalPin(key)
|
||||
}
|
||||
|
||||
@ -212,6 +222,10 @@ func PullDown(key interface{}) error {
|
||||
// NewAnalogPin returns a AnalogPin interface which allows control over
|
||||
// the analog GPIO pin.
|
||||
func NewAnalogPin(key interface{}) (AnalogPin, error) {
|
||||
if err := InitGPIO(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return gpioDriverInstance.AnalogPin(key)
|
||||
}
|
||||
|
||||
@ -228,5 +242,9 @@ func AnalogRead(key interface{}) (int, error) {
|
||||
// NewPWMPin returns a PWMPin interface which allows PWM signal
|
||||
// generation over a the PWM pin.
|
||||
func NewPWMPin(key interface{}) (PWMPin, error) {
|
||||
if err := InitGPIO(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return gpioDriverInstance.PWMPin(key)
|
||||
}
|
||||
|
10
i2c.go
10
i2c.go
@ -38,10 +38,15 @@ type I2CDriver interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
var i2cDriverInitialized bool
|
||||
var i2cDriverInstance I2CDriver
|
||||
|
||||
// InitI2C initializes the I2C driver.
|
||||
func InitI2C() error {
|
||||
if i2cDriverInitialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
desc, err := DescribeHost()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -52,6 +57,7 @@ func InitI2C() error {
|
||||
}
|
||||
|
||||
i2cDriverInstance = desc.I2CDriver()
|
||||
i2cDriverInitialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -63,5 +69,9 @@ func CloseI2C() error {
|
||||
|
||||
// NewI2CBus returns a I2CBus.
|
||||
func NewI2CBus(l byte) I2CBus {
|
||||
if err := InitI2C(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return i2cDriverInstance.Bus(l)
|
||||
}
|
||||
|
10
led.go
10
led.go
@ -25,10 +25,15 @@ type LEDDriver interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
var ledDriverInitialized bool
|
||||
var ledDriverInstance LEDDriver
|
||||
|
||||
// InitLED initializes the LED driver.
|
||||
func InitLED() error {
|
||||
if ledDriverInitialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
desc, err := DescribeHost()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -39,6 +44,7 @@ func InitLED() error {
|
||||
}
|
||||
|
||||
ledDriverInstance = desc.LEDDriver()
|
||||
ledDriverInitialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -50,6 +56,10 @@ func CloseLED() error {
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user