gobot.io/x/gobot/v2@v2.1.0/drivers/spi/spi_config.go (about)

     1  package spi
     2  
     3  type spiConfig struct {
     4  	bus   int
     5  	chip  int
     6  	mode  int
     7  	bits  int
     8  	speed int64
     9  }
    10  
    11  // NewConfig returns a new SPI Config.
    12  func NewConfig() Config {
    13  	return &spiConfig{
    14  		bus:   NotInitialized,
    15  		chip:  NotInitialized,
    16  		mode:  NotInitialized,
    17  		bits:  NotInitialized,
    18  		speed: NotInitialized}
    19  }
    20  
    21  // WithBusNumber sets which bus to use as a optional param.
    22  func WithBusNumber(busNum int) func(Config) {
    23  	return func(s Config) {
    24  		s.SetBusNumber(busNum)
    25  	}
    26  }
    27  
    28  // WithChipNumber sets which chip to use as a optional param.
    29  func WithChipNumber(chipNum int) func(Config) {
    30  	return func(s Config) {
    31  		s.SetChipNumber(chipNum)
    32  	}
    33  }
    34  
    35  // WithMode sets which mode to use as a optional param.
    36  func WithMode(mode int) func(Config) {
    37  	return func(s Config) {
    38  		s.SetMode(mode)
    39  	}
    40  }
    41  
    42  // WithBitCount sets how many bits to use as a optional param.
    43  func WithBitCount(bitCount int) func(Config) {
    44  	return func(s Config) {
    45  		s.SetBitCount(bitCount)
    46  	}
    47  }
    48  
    49  // WithSpeed sets what speed to use as a optional param.
    50  func WithSpeed(speed int64) func(Config) {
    51  	return func(s Config) {
    52  		s.SetSpeed(speed)
    53  	}
    54  }
    55  
    56  // SetBusNumber sets preferred bus to use.
    57  func (s *spiConfig) SetBusNumber(bus int) {
    58  	s.bus = bus
    59  }
    60  
    61  // GetBusNumberOrDefault returns which bus to use, either the one set using WithBus(),
    62  // or the default value which is passed in as the one param.
    63  func (s *spiConfig) GetBusNumberOrDefault(d int) int {
    64  	if s.bus == NotInitialized {
    65  		return d
    66  	}
    67  	return s.bus
    68  }
    69  
    70  // SetChipNumber sets preferred chip to use.
    71  func (s *spiConfig) SetChipNumber(chip int) {
    72  	s.chip = chip
    73  }
    74  
    75  // GetChipNumberOrDefault returns which chip to use, either the one set using WithChip(),
    76  // or the default value which is passed in as the one param.
    77  func (s *spiConfig) GetChipNumberOrDefault(d int) int {
    78  	if s.chip == NotInitialized {
    79  		return d
    80  	}
    81  	return s.chip
    82  }
    83  
    84  // SetMode sets SPI mode to use.
    85  func (s *spiConfig) SetMode(mode int) {
    86  	s.mode = mode
    87  }
    88  
    89  // GetModeOrDefault returns which mode to use, either the one set using WithChip(),
    90  // or the default value which is passed in as the one param.
    91  func (s *spiConfig) GetModeOrDefault(d int) int {
    92  	if s.mode == NotInitialized {
    93  		return d
    94  	}
    95  	return s.mode
    96  }
    97  
    98  // SetBitCount sets how many SPI bits to use.
    99  func (s *spiConfig) SetBitCount(bits int) {
   100  	s.bits = bits
   101  }
   102  
   103  // GetBitCountOrDefault returns how many to use, either the one set using WithBits(),
   104  // or the default value which is passed in as the one param.
   105  func (s *spiConfig) GetBitCountOrDefault(d int) int {
   106  	if s.bits == NotInitialized {
   107  		return d
   108  	}
   109  	return s.bits
   110  }
   111  
   112  // SetSpeed sets which SPI speed to use.
   113  func (s *spiConfig) SetSpeed(speed int64) {
   114  	s.speed = speed
   115  }
   116  
   117  // GetSpeedOrDefault returns what speed to use, either the one set using WithSpeed(),
   118  // or the default value which is passed in as the one param.
   119  func (s *spiConfig) GetSpeedOrDefault(d int64) int64 {
   120  	if s.speed == NotInitialized {
   121  		return d
   122  	}
   123  	return s.speed
   124  }