mirror of
https://github.com/kidoman/embd
synced 2025-07-03 11:57:38 +02: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
3 changed files with 38 additions and 0 deletions
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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue