1
0
Fork 0
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:
Karan Misra 2014-04-11 09:19:03 +05:30
parent 9ac0872493
commit 94f99476c2
3 changed files with 38 additions and 0 deletions

10
i2c.go
View file

@ -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)
}