gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/i2c_config.go (about)

     1  package i2c
     2  
     3  type i2cConfig struct {
     4  	bus     int
     5  	address int
     6  }
     7  
     8  // NewConfig returns a new I2c Config.
     9  func NewConfig() Config {
    10  	return &i2cConfig{bus: BusNotInitialized, address: AddressNotInitialized}
    11  }
    12  
    13  // WithBus sets which bus to use as a optional param.
    14  func WithBus(bus int) func(Config) {
    15  	return func(i Config) {
    16  		i.SetBus(bus)
    17  	}
    18  }
    19  
    20  // WithAddress sets which address to use as a optional param.
    21  func WithAddress(address int) func(Config) {
    22  	return func(i Config) {
    23  		i.SetAddress(address)
    24  	}
    25  }
    26  
    27  // SetBus sets preferred bus to use.
    28  func (i *i2cConfig) SetBus(bus int) {
    29  	i.bus = bus
    30  }
    31  
    32  // GetBusOrDefault returns which bus to use, either the one set using WithBus(),
    33  // or the default value which is passed in as the one param.
    34  func (i *i2cConfig) GetBusOrDefault(d int) int {
    35  	if i.bus == BusNotInitialized {
    36  		return d
    37  	}
    38  
    39  	return i.bus
    40  }
    41  
    42  // SetAddress sets which address to use.
    43  func (i *i2cConfig) SetAddress(address int) {
    44  	i.address = address
    45  }
    46  
    47  // GetAddressOrDefault returns which address to use, either
    48  // the one set using WithBus(), or the default value which
    49  // is passed in as the param.
    50  func (i *i2cConfig) GetAddressOrDefault(a int) int {
    51  	if i.address == AddressNotInitialized {
    52  		return a
    53  	}
    54  
    55  	return i.address
    56  }