1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-04 22:17:39 +08:00
hybridgroup.gobot/drivers/spi/spi_config.go
2017-09-12 00:51:01 -06:00

42 lines
928 B
Go

package spi
type spiConfig struct {
bus int
}
// Config is the interface which describes how a Driver can specify
// optional SPI params such as which SPI bus it wants to use.
type Config interface {
// WithBus sets which bus to use
WithBus(bus int)
// GetBusOrDefault gets which bus to use
GetBusOrDefault(def int) int
}
// NewConfig returns a new SPI Config.
func NewConfig() Config {
return &spiConfig{bus: BusNotInitialized}
}
// WithBus sets preferred bus to use.
func (s *spiConfig) WithBus(bus int) {
s.bus = bus
}
// GetBusOrDefault returns which bus to use, either the one set using WithBus(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetBusOrDefault(d int) int {
if s.bus == BusNotInitialized {
return d
}
return s.bus
}
// WithBus sets which bus to use as a optional param.
func WithBus(bus int) func(Config) {
return func(s Config) {
s.WithBus(bus)
}
}