mirror of
https://github.com/kidoman/embd
synced 2025-01-02 10:01: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
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var gpioDriverInitialized bool
|
||||||
var gpioDriverInstance GPIODriver
|
var gpioDriverInstance GPIODriver
|
||||||
|
|
||||||
// InitGPIO initializes the GPIO driver.
|
// InitGPIO initializes the GPIO driver.
|
||||||
func InitGPIO() error {
|
func InitGPIO() error {
|
||||||
|
if gpioDriverInitialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
desc, err := DescribeHost()
|
desc, err := DescribeHost()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -133,6 +138,7 @@ func InitGPIO() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gpioDriverInstance = desc.GPIODriver()
|
gpioDriverInstance = desc.GPIODriver()
|
||||||
|
gpioDriverInitialized = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -145,6 +151,10 @@ func CloseGPIO() error {
|
|||||||
// NewDigitalPin returns a DigitalPin interface which allows control over
|
// NewDigitalPin returns a DigitalPin interface which allows control over
|
||||||
// the digital GPIO pin.
|
// the digital GPIO pin.
|
||||||
func NewDigitalPin(key interface{}) (DigitalPin, error) {
|
func NewDigitalPin(key interface{}) (DigitalPin, error) {
|
||||||
|
if err := InitGPIO(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return gpioDriverInstance.DigitalPin(key)
|
return gpioDriverInstance.DigitalPin(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,6 +222,10 @@ func PullDown(key interface{}) error {
|
|||||||
// NewAnalogPin returns a AnalogPin interface which allows control over
|
// NewAnalogPin returns a AnalogPin interface which allows control over
|
||||||
// the analog GPIO pin.
|
// the analog GPIO pin.
|
||||||
func NewAnalogPin(key interface{}) (AnalogPin, error) {
|
func NewAnalogPin(key interface{}) (AnalogPin, error) {
|
||||||
|
if err := InitGPIO(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return gpioDriverInstance.AnalogPin(key)
|
return gpioDriverInstance.AnalogPin(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,5 +242,9 @@ func AnalogRead(key interface{}) (int, error) {
|
|||||||
// NewPWMPin returns a PWMPin interface which allows PWM signal
|
// NewPWMPin returns a PWMPin interface which allows PWM signal
|
||||||
// generation over a the PWM pin.
|
// generation over a the PWM pin.
|
||||||
func NewPWMPin(key interface{}) (PWMPin, error) {
|
func NewPWMPin(key interface{}) (PWMPin, error) {
|
||||||
|
if err := InitGPIO(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return gpioDriverInstance.PWMPin(key)
|
return gpioDriverInstance.PWMPin(key)
|
||||||
}
|
}
|
||||||
|
10
i2c.go
10
i2c.go
@ -38,10 +38,15 @@ type I2CDriver interface {
|
|||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var i2cDriverInitialized bool
|
||||||
var i2cDriverInstance I2CDriver
|
var i2cDriverInstance I2CDriver
|
||||||
|
|
||||||
// InitI2C initializes the I2C driver.
|
// InitI2C initializes the I2C driver.
|
||||||
func InitI2C() error {
|
func InitI2C() error {
|
||||||
|
if i2cDriverInitialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
desc, err := DescribeHost()
|
desc, err := DescribeHost()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -52,6 +57,7 @@ func InitI2C() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
i2cDriverInstance = desc.I2CDriver()
|
i2cDriverInstance = desc.I2CDriver()
|
||||||
|
i2cDriverInitialized = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -63,5 +69,9 @@ func CloseI2C() error {
|
|||||||
|
|
||||||
// NewI2CBus returns a I2CBus.
|
// NewI2CBus returns a I2CBus.
|
||||||
func NewI2CBus(l byte) I2CBus {
|
func NewI2CBus(l byte) I2CBus {
|
||||||
|
if err := InitI2C(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
return i2cDriverInstance.Bus(l)
|
return i2cDriverInstance.Bus(l)
|
||||||
}
|
}
|
||||||
|
10
led.go
10
led.go
@ -25,10 +25,15 @@ type LEDDriver interface {
|
|||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ledDriverInitialized bool
|
||||||
var ledDriverInstance LEDDriver
|
var ledDriverInstance LEDDriver
|
||||||
|
|
||||||
// InitLED initializes the LED driver.
|
// InitLED initializes the LED driver.
|
||||||
func InitLED() error {
|
func InitLED() error {
|
||||||
|
if ledDriverInitialized {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
desc, err := DescribeHost()
|
desc, err := DescribeHost()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -39,6 +44,7 @@ func InitLED() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ledDriverInstance = desc.LEDDriver()
|
ledDriverInstance = desc.LEDDriver()
|
||||||
|
ledDriverInitialized = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -50,6 +56,10 @@ func CloseLED() error {
|
|||||||
|
|
||||||
// NewLED returns a LED interface which allows control over the LED.
|
// NewLED returns a LED interface which allows control over the LED.
|
||||||
func NewLED(key interface{}) (LED, error) {
|
func NewLED(key interface{}) (LED, error) {
|
||||||
|
if err := InitLED(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return ledDriverInstance.LED(key)
|
return ledDriverInstance.LED(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user