gobot.io/x/gobot@v1.16.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 // Config is the interface which describes how a Driver can specify 12 // optional SPI params such as which SPI bus it wants to use. 13 type Config interface { 14 // WithBus sets which bus to use 15 WithBus(bus int) 16 17 // GetBusOrDefault gets which bus to use 18 GetBusOrDefault(def int) int 19 20 // WithChip sets which chip to use 21 WithChip(chip int) 22 23 // GetChipOrDefault gets which chip to use 24 GetChipOrDefault(def int) int 25 26 // WithMode sets which mode to use 27 WithMode(mode int) 28 29 // GetModeOrDefault gets which mode to use 30 GetModeOrDefault(def int) int 31 32 // WithBIts sets how many bits to use 33 WithBits(bits int) 34 35 // GetBitsOrDefault gets how many bits to use 36 GetBitsOrDefault(def int) int 37 38 // WithSpeed sets which speed to use (in Hz) 39 WithSpeed(speed int64) 40 41 // GetSpeedOrDefault gets which speed to use (in Hz) 42 GetSpeedOrDefault(def int64) int64 43 } 44 45 // NewConfig returns a new SPI Config. 46 func NewConfig() Config { 47 return &spiConfig{ 48 bus: NotInitialized, 49 chip: NotInitialized, 50 mode: NotInitialized, 51 bits: NotInitialized, 52 speed: NotInitialized} 53 } 54 55 // WithBus sets preferred bus to use. 56 func (s *spiConfig) WithBus(bus int) { 57 s.bus = bus 58 } 59 60 // GetBusOrDefault returns which bus to use, either the one set using WithBus(), 61 // or the default value which is passed in as the one param. 62 func (s *spiConfig) GetBusOrDefault(d int) int { 63 if s.bus == NotInitialized { 64 return d 65 } 66 return s.bus 67 } 68 69 // WithBus sets which bus to use as a optional param. 70 func WithBus(bus int) func(Config) { 71 return func(s Config) { 72 s.WithBus(bus) 73 } 74 } 75 76 // WithChip sets preferred chip to use. 77 func (s *spiConfig) WithChip(chip int) { 78 s.chip = chip 79 } 80 81 // GetChipOrDefault returns which chip to use, either the one set using WithChip(), 82 // or the default value which is passed in as the one param. 83 func (s *spiConfig) GetChipOrDefault(d int) int { 84 if s.chip == NotInitialized { 85 return d 86 } 87 return s.chip 88 } 89 90 // WithChip sets which chip to use as a optional param. 91 func WithChip(chip int) func(Config) { 92 return func(s Config) { 93 s.WithChip(chip) 94 } 95 } 96 97 // WithMode sets SPI mode to use. 98 func (s *spiConfig) WithMode(mode int) { 99 s.mode = mode 100 } 101 102 // GetModeOrDefault returns which mode to use, either the one set using WithChip(), 103 // or the default value which is passed in as the one param. 104 func (s *spiConfig) GetModeOrDefault(d int) int { 105 if s.mode == NotInitialized { 106 return d 107 } 108 return s.mode 109 } 110 111 // WithMode sets which mode to use as a optional param. 112 func WithMode(mode int) func(Config) { 113 return func(s Config) { 114 s.WithMode(mode) 115 } 116 } 117 118 // WithBits sets how many SPI bits to use. 119 func (s *spiConfig) WithBits(bits int) { 120 s.bits = bits 121 } 122 123 // GetBitsOrDefault returns how many to use, either the one set using WithBits(), 124 // or the default value which is passed in as the one param. 125 func (s *spiConfig) GetBitsOrDefault(d int) int { 126 if s.bits == NotInitialized { 127 return d 128 } 129 return s.bits 130 } 131 132 // WithBits sets how many bits to use as a optional param. 133 func WithBits(bits int) func(Config) { 134 return func(s Config) { 135 s.WithBits(bits) 136 } 137 } 138 139 // WithSpeed sets which SPI speed to use. 140 func (s *spiConfig) WithSpeed(speed int64) { 141 s.speed = speed 142 } 143 144 // GetSpeedOrDefault returns what speed to use, either the one set using WithSpeed(), 145 // or the default value which is passed in as the one param. 146 func (s *spiConfig) GetSpeedOrDefault(d int64) int64 { 147 if s.speed == NotInitialized { 148 return d 149 } 150 return s.speed 151 } 152 153 // WithSpeed sets what speed to use as a optional param. 154 func WithSpeed(speed int64) func(Config) { 155 return func(s Config) { 156 s.WithSpeed(speed) 157 } 158 }