@@ -13,9 +13,15 @@ import (
1313
1414type i2cBusNumberValidator func (busNumber int ) error
1515
16+ // i2cBusConfiguration contains all changeable attributes of the adaptor.
17+ type i2cBusConfiguration struct {
18+ debug bool
19+ }
20+
1621// I2cBusAdaptor is a adaptor for i2c bus, normally used for composition in platforms.
1722type I2cBusAdaptor struct {
1823 sys * system.Accesser
24+ i2cBusCfg * i2cBusConfiguration
1925 validateNumber i2cBusNumberValidator
2026 defaultBusNumber int
2127 mutex sync.Mutex
@@ -24,24 +30,49 @@ type I2cBusAdaptor struct {
2430
2531// NewI2cBusAdaptor provides the access to i2c buses of the board. The validator is used to check the bus number,
2632// which is given by user, to the abilities of the board.
27- func NewI2cBusAdaptor (sys * system.Accesser , v i2cBusNumberValidator , defaultBusNr int ) * I2cBusAdaptor {
33+ //
34+ // Options:
35+ //
36+ // "WithI2cDebug"
37+ func NewI2cBusAdaptor (
38+ sys * system.Accesser ,
39+ v i2cBusNumberValidator ,
40+ defaultBusNr int ,
41+ opts ... I2CBusOptionApplier ,
42+ ) * I2cBusAdaptor {
2843 a := I2cBusAdaptor {
2944 sys : sys ,
45+ i2cBusCfg : & i2cBusConfiguration {},
3046 validateNumber : v ,
3147 defaultBusNumber : defaultBusNr ,
3248 }
3349
50+ for _ , o := range opts {
51+ o .apply (a .i2cBusCfg )
52+ }
53+
3454 sys .AddI2CSupport ()
3555
3656 return & a
3757}
3858
59+ // WithI2cDebug can be used to switch on debugging for I2C implementation.
60+ func WithI2cDebug () i2cBusDebugOption {
61+ return i2cBusDebugOption (true )
62+ }
63+
3964// Connect prepares the connection to i2c buses.
4065func (a * I2cBusAdaptor ) Connect () error {
4166 a .mutex .Lock ()
4267 defer a .mutex .Unlock ()
4368
69+ if a .buses != nil {
70+ return fmt .Errorf ("I2C bus adaptor already connected, please call Finalize() for re-connect" )
71+ }
72+
4473 a .buses = make (map [int ]gobot.I2cSystemDevicer )
74+ a .debuglnf ("connect the I2C bus adaptor done" )
75+
4576 return nil
4677}
4778
@@ -50,15 +81,21 @@ func (a *I2cBusAdaptor) Finalize() error {
5081 a .mutex .Lock ()
5182 defer a .mutex .Unlock ()
5283
84+ a .debuglnf ("finalize the I2C bus adaptor for %d buses..." , len (a .buses ))
85+
5386 var err error
54- for _ , bus := range a .buses {
87+ for busNum , bus := range a .buses {
5588 if bus != nil {
56- if e := bus .Close (); e != nil {
89+ e := bus .Close ()
90+ if e != nil {
5791 err = multierror .Append (err , e )
5892 }
93+ a .debuglnf ("I2C bus %d closed with error: %v" , busNum , e )
5994 }
6095 }
6196 a .buses = nil
97+ a .debuglnf ("finalize the I2C bus adaptor done with error: %v" , err )
98+
6299 return err
63100}
64101
@@ -90,3 +127,7 @@ func (a *I2cBusAdaptor) GetI2cConnection(address int, busNum int) (i2c.Connectio
90127func (a * I2cBusAdaptor ) DefaultI2cBus () int {
91128 return a .defaultBusNumber
92129}
130+
131+ func (a * I2cBusAdaptor ) debuglnf (format string , p ... interface {}) {
132+ gobot .Debuglnf (a .i2cBusCfg .debug , format , p ... )
133+ }
0 commit comments