gobot.io/x/gobot/v2@v2.1.0/system/system_options.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gobot.io/x/gobot/v2"
     7  )
     8  
     9  // Optioner is the interface for system options. This provides the possibility for change the systems behavior by the
    10  // caller/user when creating the system access, e.g. by "NewAccesser()".
    11  type Optioner interface {
    12  	setDigitalPinToGpiodAccess()
    13  	setSpiToGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, nssPin, mosiPin, misoPin string)
    14  }
    15  
    16  // WithDigitalPinGpiodAccess can be used to change the default sysfs implementation for digital pins to the character
    17  // device Kernel ABI. The access is provided by the gpiod package.
    18  func WithDigitalPinGpiodAccess() func(Optioner) {
    19  	return func(s Optioner) {
    20  		s.setDigitalPinToGpiodAccess()
    21  	}
    22  }
    23  
    24  // WithSpiGpioAccess can be used to switch the default SPI implementation to GPIO usage.
    25  func WithSpiGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, nssPin, mosiPin, misoPin string) func(Optioner) {
    26  	return func(s Optioner) {
    27  		s.setSpiToGpioAccess(p, sclkPin, nssPin, mosiPin, misoPin)
    28  	}
    29  }
    30  
    31  func (a *Accesser) setDigitalPinToGpiodAccess() {
    32  	dpa := &gpiodDigitalPinAccess{fs: a.fs}
    33  	if dpa.isSupported() {
    34  		a.digitalPinAccess = dpa
    35  		if systemDebug {
    36  			fmt.Printf("use gpiod driver for digital pins with this chips: %v\n", dpa.chips)
    37  		}
    38  		return
    39  	}
    40  	if systemDebug {
    41  		fmt.Println("gpiod driver not supported, fallback to sysfs")
    42  	}
    43  
    44  }
    45  
    46  func (a *Accesser) setSpiToGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, nssPin, mosiPin, misoPin string) {
    47  	cfg := spiGpioConfig{
    48  		pinProvider: p,
    49  		sclkPinID:   sclkPin,
    50  		nssPinID:    nssPin,
    51  		mosiPinID:   mosiPin,
    52  		misoPinID:   misoPin,
    53  	}
    54  	gsa := &gpioSpiAccess{cfg: cfg}
    55  	if gsa.isSupported() {
    56  		a.spiAccess = gsa
    57  		if systemDebug {
    58  			fmt.Printf("use gpio driver for SPI with this config: %s\n", gsa.cfg.String())
    59  		}
    60  		return
    61  	}
    62  	if systemDebug {
    63  		fmt.Println("gpio driver not supported for SPI, fallback to periphio")
    64  	}
    65  }