Documentation
¶
Overview ¶
Package system provides generic access to Linux gpio, i2c and filesystem.
It is intended to be used while implementing support for a single board Linux computer
Index ¶
- Constants
- func WithDigitalPinCdevAccess() systemUseDigitalPinSysfsOption
- func WithDigitalPinDebug() systemDigitalPinDebugOption
- func WithDigitalPinSysfsAccess() systemUseDigitalPinSysfsOption
- func WithPinActiveLow() func(gobot.DigitalPinOptioner) bool
- func WithPinDebounce(period time.Duration) func(gobot.DigitalPinOptioner) bool
- func WithPinDirectionInput() func(gobot.DigitalPinOptioner) bool
- func WithPinDirectionOutput(initial int) func(gobot.DigitalPinOptioner) bool
- func WithPinEventOnBothEdges(...) func(gobot.DigitalPinOptioner) bool
- func WithPinEventOnFallingEdge(...) func(gobot.DigitalPinOptioner) bool
- func WithPinEventOnRisingEdge(...) func(gobot.DigitalPinOptioner) bool
- func WithPinLabel(label string) func(gobot.DigitalPinOptioner) bool
- func WithPinOpenDrain() func(gobot.DigitalPinOptioner) bool
- func WithPinOpenSource() func(gobot.DigitalPinOptioner) bool
- func WithPinPollForEdgeDetection(pollInterval time.Duration, pollQuitChan chan struct{}) func(gobot.DigitalPinOptioner) bool
- func WithPinPullDown() func(gobot.DigitalPinOptioner) bool
- func WithPinPullUp() func(gobot.DigitalPinOptioner) bool
- func WithSpiDebug() systemSpiDebugOption
- func WithSpiGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, ncsPin, sdoPin, sdiPin string) systemUseSpiGpioOption
- func WithSystemAccesserDebug() systemAccesserDebugOption
- type Accesser
- func (a *Accesser) AddAnalogSupport()
- func (a *Accesser) AddDigitalPinSupport(options ...AccesserOptionApplier)
- func (a *Accesser) AddI2CSupport()
- func (a *Accesser) AddOneWireSupport()
- func (a *Accesser) AddPWMSupport()
- func (a *Accesser) AddSPISupport(options ...AccesserOptionApplier)
- func (a *Accesser) Find(baseDir string, pattern string) ([]string, error)
- func (a *Accesser) HasDigitalPinCdevAccess() bool
- func (a *Accesser) HasDigitalPinSysfsAccess() bool
- func (a *Accesser) HasSpiGpioAccess() bool
- func (a *Accesser) HasSpiPeriphioAccess() bool
- func (a *Accesser) NewAnalogPin(path string, w bool, readBufLen uint16) gobot.AnalogPinner
- func (a *Accesser) NewDigitalPin(chip string, pin int, options ...func(gobot.DigitalPinOptioner) bool) gobot.DigitalPinner
- func (a *Accesser) NewI2cDevice(location string) (*i2cDevice, error)
- func (a *Accesser) NewOneWireDevice(familyCode byte, serialNumber uint64) (gobot.OneWireSystemDevicer, error)
- func (a *Accesser) NewPWMPin(path string, pin int, polNormIdent string, polInvIdent string) gobot.PWMPinner
- func (a *Accesser) NewSpiDevice(busNum, chipNum, mode, bits int, maxSpeed int64) (gobot.SpiSystemDevicer, error)
- func (a *Accesser) OpenFile(name string, flag int, perm os.FileMode) (File, error)
- func (a *Accesser) ReadFile(name string) ([]byte, error)
- func (a *Accesser) Stat(name string) (os.FileInfo, error)
- func (a *Accesser) UseMockDigitalPinAccess() *mockDigitalPinAccess
- func (a *Accesser) UseMockFilesystem(files []string) *MockFilesystem
- func (a *Accesser) UseMockSpi() *MockSpiAccess
- func (a *Accesser) UseMockSyscall() *mockSyscall
- type AccesserOptionApplier
- type File
- type MockFile
- func (f *MockFile) Close() error
- func (f *MockFile) Fd() uintptr
- func (f *MockFile) Read(b []byte) (int, error)
- func (f *MockFile) ReadAt(b []byte, off int64) (int, error)
- func (f *MockFile) Seek(offset int64, whence int) (int64, error)
- func (f *MockFile) Sync() error
- func (f *MockFile) Write(b []byte) (int, error)
- func (f *MockFile) WriteString(s string) (int, error)
- type MockFilesystem
- type MockSpiAccess
- type SyscallErrno
Constants ¶
const ( // IN gpio direction IN = "in" // OUT gpio direction OUT = "out" // HIGH gpio level HIGH = 1 // LOW gpio level LOW = 0 )
const ( // DigitalPinEventRisingEdge indicates an inactive to active event. DigitalPinEventRisingEdge = "rising edge" // DigitalPinEventFallingEdge indicates an active to inactive event. DigitalPinEventFallingEdge = "falling edge" )
const ( // From /usr/include/linux/i2c-dev.h: // ioctl signals I2C_TARGET = 0x0703 I2C_FUNCS = 0x0705 I2C_SMBUS = 0x0720 // Read/write markers I2C_SMBUS_READ = 1 I2C_SMBUS_WRITE = 0 // From /usr/include/linux/i2c.h: // Adapter functionality I2C_FUNC_SMBUS_READ_BYTE = 0x00020000 I2C_FUNC_SMBUS_WRITE_BYTE = 0x00040000 I2C_FUNC_SMBUS_READ_BYTE_DATA = 0x00080000 I2C_FUNC_SMBUS_WRITE_BYTE_DATA = 0x00100000 I2C_FUNC_SMBUS_READ_WORD_DATA = 0x00200000 I2C_FUNC_SMBUS_WRITE_WORD_DATA = 0x00400000 I2C_FUNC_SMBUS_READ_BLOCK_DATA = 0x01000000 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = 0x02000000 I2C_FUNC_SMBUS_READ_I2C_BLOCK = 0x04000000 // I2C-like block transfer with 1-byte reg. addr. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK = 0x08000000 // I2C-like block transfer with 1-byte reg. addr. // Transaction types I2C_SMBUS_BYTE = 1 I2C_SMBUS_BYTE_DATA = 2 I2C_SMBUS_WORD_DATA = 3 I2C_SMBUS_PROC_CALL = 4 I2C_SMBUS_BLOCK_DATA = 5 I2C_SMBUS_I2C_BLOCK_BROKEN = 6 I2C_SMBUS_BLOCK_PROC_CALL = 7 /* SMBus 2.0 */ I2C_SMBUS_I2C_BLOCK_DATA = 8 /* SMBus 2.0 */ )
const ( Syscall_SYS_IOCTL = unix.SYS_IOCTL Syscall_EINVAL = unix.EINVAL Syscall_EBUSY = unix.EBUSY Syscall_EFAULT = unix.EFAULT )
wrapping for used constants of unix package
Variables ¶
This section is empty.
Functions ¶
func WithDigitalPinCdevAccess ¶ added in v2.5.0
func WithDigitalPinCdevAccess() systemUseDigitalPinSysfsOption
WithDigitalPinCdevAccess can be used to change the default sysfs implementation for digital pins in old platforms to test the character device Kernel ABI. The access is provided by the go-gpiocdev package.
func WithDigitalPinDebug ¶ added in v2.5.0
func WithDigitalPinDebug() systemDigitalPinDebugOption
WithDigitalPinDebug can be used to switch on debug messages for digital pins.
func WithDigitalPinSysfsAccess ¶ added in v2.5.0
func WithDigitalPinSysfsAccess() systemUseDigitalPinSysfsOption
WithDigitalPinSysfsAccess can be used to change the default character device implementation for digital pins to the legacy sysfs Kernel ABI.
func WithPinActiveLow ¶
func WithPinActiveLow() func(gobot.DigitalPinOptioner) bool
WithPinActiveLow initializes the pin with inverse reaction (applies on input and output).
func WithPinDebounce ¶
WithPinDebounce initializes the input pin to be debounced.
func WithPinDirectionInput ¶
func WithPinDirectionInput() func(gobot.DigitalPinOptioner) bool
WithPinDirectionInput initializes the pin as input.
func WithPinDirectionOutput ¶
WithPinDirectionOutput initializes the pin as output instead of the default "input".
func WithPinEventOnBothEdges ¶
func WithPinEventOnBothEdges(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32), ) func(gobot.DigitalPinOptioner) bool
WithPinEventOnBothEdges initializes the input pin for edge detection and call the event handler on all edges.
func WithPinEventOnFallingEdge ¶
func WithPinEventOnFallingEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32), ) func(gobot.DigitalPinOptioner) bool
WithPinEventOnFallingEdge initializes the input pin for edge detection and call the event handler on falling edges.
func WithPinEventOnRisingEdge ¶
func WithPinEventOnRisingEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, lseqno uint32), ) func(gobot.DigitalPinOptioner) bool
WithPinEventOnRisingEdge initializes the input pin for edge detection and call the event handler on rising edges.
func WithPinLabel ¶
WithPinLabel use a pin label, which will replace the default label "gobotio#".
func WithPinOpenDrain ¶
func WithPinOpenDrain() func(gobot.DigitalPinOptioner) bool
WithPinOpenDrain initializes the output pin to be driven with open drain/collector.
func WithPinOpenSource ¶
func WithPinOpenSource() func(gobot.DigitalPinOptioner) bool
WithPinOpenSource initializes the output pin to be driven with open source/emitter.
func WithPinPollForEdgeDetection ¶ added in v2.2.0
func WithPinPollForEdgeDetection( pollInterval time.Duration, pollQuitChan chan struct{}, ) func(gobot.DigitalPinOptioner) bool
WithPinPollForEdgeDetection initializes a discrete input pin polling function to use for edge detection.
func WithPinPullDown ¶
func WithPinPullDown() func(gobot.DigitalPinOptioner) bool
WithPinPullDown initializes the pin to be pulled down (high impedance to GND, applies on input and output). This is working since Kernel 5.5.
func WithPinPullUp ¶
func WithPinPullUp() func(gobot.DigitalPinOptioner) bool
WithPinPullUp initializes the pin to be pulled up (high impedance to VDD, applies on input and output). This is working since Kernel 5.5.
func WithSpiDebug ¶ added in v2.5.0
func WithSpiDebug() systemSpiDebugOption
WithSpiDebug can be used to switch on debug messages for SPI.
func WithSpiGpioAccess ¶
func WithSpiGpioAccess(p gobot.DigitalPinnerProvider, sclkPin, ncsPin, sdoPin, sdiPin string) systemUseSpiGpioOption
WithSpiGpioAccess can be used to switch the default SPI implementation to GPIO usage.
func WithSystemAccesserDebug ¶ added in v2.5.0
func WithSystemAccesserDebug() systemAccesserDebugOption
WithSystemAccesserDebug can be used to switch on debug messages.
Types ¶
type Accesser ¶
type Accesser struct {
// contains filtered or unexported fields
}
Accesser provides access to system calls, filesystem, implementation for digital pin and SPI
func NewAccesser ¶
func NewAccesser(options ...AccesserOptionApplier) *Accesser
NewAccesser returns a accesser to native system call, native file system and the chosen digital pin access. Digital pin accesser can be empty or "sysfs", otherwise it will be automatically chosen.
func (*Accesser) AddAnalogSupport ¶ added in v2.5.0
func (a *Accesser) AddAnalogSupport()
AddAnalogSupport adds the support to access the analog features of the system, usually by sysfs.
func (*Accesser) AddDigitalPinSupport ¶ added in v2.5.0
func (a *Accesser) AddDigitalPinSupport(options ...AccesserOptionApplier)
AddDigitalPinSupport adds the support to access the GPIO features of the system. Usually by character device or sysfs Kernel API. Related options can be applied here.
func (*Accesser) AddI2CSupport ¶ added in v2.5.0
func (a *Accesser) AddI2CSupport()
AddI2CSupport adds the support to access the I2C features of the system, usually by syscall with character device.
func (*Accesser) AddOneWireSupport ¶ added in v2.5.0
func (a *Accesser) AddOneWireSupport()
AddOneWireSupport adds the support to access the one wire features of the system, usually by sysfs.
func (*Accesser) AddPWMSupport ¶ added in v2.5.0
func (a *Accesser) AddPWMSupport()
AddPWMSupport adds the support to access the PWM features of the system, usually by sysfs.
func (*Accesser) AddSPISupport ¶ added in v2.5.0
func (a *Accesser) AddSPISupport(options ...AccesserOptionApplier)
AddSPISupport adds the support to access the SPI features of the system, usually by character device or GPIOs. Related options can be applied here.
func (*Accesser) HasDigitalPinCdevAccess ¶ added in v2.5.0
HasDigitalPinCdevAccess returns whether the used digital pin accesser is a sysfs one. If no digital pin accesser is defined, returns false.
func (*Accesser) HasDigitalPinSysfsAccess ¶ added in v2.5.0
HasDigitalPinSysfsAccess returns whether the used digital pin accesser is a sysfs one. If no digital pin accesser is defined, returns false.
func (*Accesser) HasSpiGpioAccess ¶ added in v2.5.0
HasSpiGpioAccess returns whether the used SPI accesser is GPIO based. If SPI accesser is defined, returns false.
func (*Accesser) HasSpiPeriphioAccess ¶ added in v2.5.0
HasSpiPeriphioAccess returns whether the used SPI accesser is periphio based. If SPI accesser is defined, returns false.
func (*Accesser) NewAnalogPin ¶ added in v2.3.0
func (*Accesser) NewDigitalPin ¶
func (a *Accesser) NewDigitalPin(chip string, pin int, options ...func(gobot.DigitalPinOptioner) bool, ) gobot.DigitalPinner
NewDigitalPin returns a new system digital pin, according to the given pin number.
func (*Accesser) NewI2cDevice ¶
NewI2cDevice returns a Linux Kernel access by ioctrl to the given i2c bus location (character device). Important note for "os.ModeExclusive": this is undefined without create the file for character devices, this means a second open will not return an error e.g. due to a busy resource. If this is not wanted, e.g. to minimize count of open fd's this needs to be prevented at caller side by implementing a caching mechanism. Furthermore this behavior can lead to problems with multiple devices on the same bus because the cycle SetAddress()...Read()/Write() etc. can be interrupted when using multiple instances for the same location.
func (*Accesser) NewOneWireDevice ¶ added in v2.5.0
func (a *Accesser) NewOneWireDevice(familyCode byte, serialNumber uint64) (gobot.OneWireSystemDevicer, error)
NewOneWireDevice returns a new 1-wire device with the given parameters. note: this is a basic implementation without using the possibilities of bus controller it depends on automatic device search, see https://www.kernel.org/doc/Documentation/w1/w1.generic
func (*Accesser) NewPWMPin ¶
func (a *Accesser) NewPWMPin(path string, pin int, polNormIdent string, polInvIdent string) gobot.PWMPinner
NewPWMPin returns a new system PWM pin, according to the given pin number.
func (*Accesser) NewSpiDevice ¶
func (a *Accesser) NewSpiDevice(busNum, chipNum, mode, bits int, maxSpeed int64) (gobot.SpiSystemDevicer, error)
NewSpiDevice returns a new connection to SPI with the given parameters.
func (*Accesser) ReadFile ¶
ReadFile reads the named file and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
func (*Accesser) Stat ¶
Stat returns a generic FileInfo, if the file with given name exists. It uses the native or the mocked file system.
func (*Accesser) UseMockDigitalPinAccess ¶ added in v2.5.0
func (a *Accesser) UseMockDigitalPinAccess() *mockDigitalPinAccess
UseMockDigitalPinAccess sets the digital pin handler accesser to the chosen one. Used only for tests.
func (*Accesser) UseMockFilesystem ¶
func (a *Accesser) UseMockFilesystem(files []string) *MockFilesystem
UseMockFilesystem sets the filesystem implementation of the accesser to the mocked one. Used only for tests.
func (*Accesser) UseMockSpi ¶
func (a *Accesser) UseMockSpi() *MockSpiAccess
UseMockSpi sets the SPI implementation of the accesser to the mocked one. Used only for tests.
func (*Accesser) UseMockSyscall ¶
func (a *Accesser) UseMockSyscall() *mockSyscall
UseMockSyscall sets the Syscall implementation of the accesser to the mocked one. Used only for tests.
type AccesserOptionApplier ¶ added in v2.5.0
type AccesserOptionApplier interface {
// contains filtered or unexported methods
}
accesserOptionApplier is the interface for system options. This provides the possibility for change the systems behavior by the caller/user when creating the system access, e.g. by "NewAccesser().Add..". The interface needs to be implemented by each configurable option type.
type File ¶
type File interface { Write(b []byte) (n int, err error) WriteString(s string) (ret int, err error) Sync() error Read(b []byte) (n int, err error) ReadAt(b []byte, off int64) (n int, err error) Seek(offset int64, whence int) (ret int64, err error) Fd() uintptr Close() error }
A File represents basic IO interactions with the underlying file system
type MockFile ¶
type MockFile struct { Contents string Seq int // When this file was last written or read. Opened bool Closed bool // contains filtered or unexported fields }
A MockFile represents a mock file that contains a single string. Any write overwrites, and any read returns from the start.
type MockFilesystem ¶
type MockFilesystem struct { Seq int // Increases with each write or read. Files map[string]*MockFile WithReadError bool WithWriteError bool WithCloseError bool // contains filtered or unexported fields }
MockFilesystem represents a filesystem of mock files.
func (*MockFilesystem) Add ¶
func (fs *MockFilesystem) Add(name string) *MockFile
Add adds a new file to fs.Files given a name, and returns the newly created file
type MockSpiAccess ¶
type MockSpiAccess struct { CreateError bool // contains filtered or unexported fields }
func (*MockSpiAccess) Reset ¶
func (spi *MockSpiAccess) Reset()
Reset resets the last written values.
func (*MockSpiAccess) SetCloseError ¶
func (spi *MockSpiAccess) SetCloseError(val bool)
SetCloseError can be used to simulate a error on Close().
func (*MockSpiAccess) SetReadError ¶
func (spi *MockSpiAccess) SetReadError(val bool)
SetReadError can be used to simulate a read error.
func (*MockSpiAccess) SetSimRead ¶
func (spi *MockSpiAccess) SetSimRead(data []byte)
SetSimRead is used to set the byte stream for next read.
func (*MockSpiAccess) SetWriteError ¶
func (spi *MockSpiAccess) SetWriteError(val bool)
SetWriteError can be used to simulate a write error.
func (*MockSpiAccess) Written ¶
func (spi *MockSpiAccess) Written() []byte
Written returns the byte stream which was last written.
type SyscallErrno ¶ added in v2.1.1
SyscallErrno wraps the "unix.Errno"
func (SyscallErrno) Error ¶ added in v2.1.1
func (e SyscallErrno) Error() string
Error implements the error interface. It wraps the "unix.Errno.Error()".
Source Files
¶
- analogpin_sysfs.go
- digitalpin_cdev.go
- digitalpin_mock.go
- digitalpin_poll.go
- digitalpin_sysfs.go
- digitalpinaccess.go
- digitalpinoptions.go
- doc.go
- fs.go
- fs_mock.go
- i2c_device.go
- onewiredevice_sysfs.go
- pwmpin_sysfs.go
- spi_gpio.go
- spi_mock.go
- spi_periphio.go
- spiaccess.go
- syscall.go
- syscall_mock.go
- sysfsfileaccess.go
- system.go
- systemoptions.go