2013-10-23 07:45:31 +08:00
|
|
|
package gobot
|
2013-10-24 13:00:03 +08:00
|
|
|
|
2023-01-28 19:22:32 +08:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
2022-12-17 18:56:11 +08:00
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// DigitalPinOptioner is the interface to provide the possibility to change pin behavior for the next usage
|
|
|
|
type DigitalPinOptioner interface {
|
|
|
|
// SetLabel change the pins label
|
|
|
|
SetLabel(string) (changed bool)
|
|
|
|
// SetDirectionOutput sets the pins direction to output with the given initial value
|
|
|
|
SetDirectionOutput(initialState int) (changed bool)
|
|
|
|
// SetDirectionInput sets the pins direction to input
|
|
|
|
SetDirectionInput() (changed bool)
|
2023-01-28 19:22:32 +08:00
|
|
|
// SetActiveLow initializes the pin with inverse reaction (applies on input and output).
|
|
|
|
SetActiveLow() (changed bool)
|
|
|
|
// SetBias initializes the pin with the given bias (applies on input and output).
|
|
|
|
SetBias(bias int) (changed bool)
|
|
|
|
// SetDrive initializes the output pin with the given drive option.
|
|
|
|
SetDrive(drive int) (changed bool)
|
|
|
|
// SetDebounce initializes the input pin with the given debounce period.
|
|
|
|
SetDebounce(period time.Duration) (changed bool)
|
|
|
|
// SetEventHandlerForEdge initializes the input pin for edge detection and to call the event handler on specified edge.
|
|
|
|
// lineOffset is within the GPIO chip (needs to transformed to the pin id), timestamp is the detection time,
|
|
|
|
// detectedEdge contains the direction of the pin changes, seqno is the sequence number for this event in the sequence
|
|
|
|
// of events for all the lines in this line request, lseqno is the same but for this line
|
|
|
|
SetEventHandlerForEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32,
|
|
|
|
lseqno uint32), edge int) (changed bool)
|
2022-11-27 23:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalPinOptionApplier is the interface to apply options to change pin behavior immediately
|
|
|
|
type DigitalPinOptionApplier interface {
|
|
|
|
// ApplyOptions apply all given options to the pin immediately
|
|
|
|
ApplyOptions(...func(DigitalPinOptioner) bool) error
|
|
|
|
}
|
|
|
|
|
2022-11-21 02:22:26 +08:00
|
|
|
// DigitalPinner is the interface for system gpio interactions
|
|
|
|
type DigitalPinner interface {
|
2022-11-27 23:06:09 +08:00
|
|
|
// Export exports the pin for use by the adaptor
|
2022-11-21 02:22:26 +08:00
|
|
|
Export() error
|
2022-11-27 23:06:09 +08:00
|
|
|
// Unexport releases the pin from the adaptor, so it is free for the operating system
|
2022-11-21 02:22:26 +08:00
|
|
|
Unexport() error
|
|
|
|
// Read reads the current value of the pin
|
|
|
|
Read() (int, error)
|
|
|
|
// Write writes to the pin
|
|
|
|
Write(int) error
|
2022-11-27 23:06:09 +08:00
|
|
|
// DigitalPinOptionApplier is the interface to change pin behavior immediately
|
|
|
|
DigitalPinOptionApplier
|
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalPinValuer is the interface to get pin behavior for the next usage. The interface is and should be rarely used.
|
|
|
|
type DigitalPinValuer interface {
|
|
|
|
// DirectionBehavior gets the direction behavior when the pin is used the next time.
|
|
|
|
// This means its possibly not in this direction type at the moment.
|
|
|
|
DirectionBehavior() string
|
2022-11-21 02:22:26 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:06:09 +08:00
|
|
|
// DigitalPinnerProvider is the interface that an Adaptor should implement to allow clients to obtain
|
|
|
|
// access to any DigitalPin's available on that board. If the pin is initially acquired, it is an input.
|
|
|
|
// Pin direction and other options can be changed afterwards by pin.ApplyOptions() at any time.
|
2022-11-21 02:22:26 +08:00
|
|
|
type DigitalPinnerProvider interface {
|
2022-11-27 23:06:09 +08:00
|
|
|
DigitalPin(id string) (DigitalPinner, error)
|
2022-11-21 02:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PWMPinner is the interface for system PWM interactions
|
|
|
|
type PWMPinner interface {
|
2022-12-06 01:28:57 +08:00
|
|
|
// Export exports the PWM pin for use by the operating system
|
2022-11-21 02:22:26 +08:00
|
|
|
Export() error
|
2022-12-06 01:28:57 +08:00
|
|
|
// Unexport releases the PWM pin from the operating system
|
2022-11-21 02:22:26 +08:00
|
|
|
Unexport() error
|
2022-12-06 01:28:57 +08:00
|
|
|
// Enabled returns the enabled state of the PWM pin
|
|
|
|
Enabled() (bool, error)
|
|
|
|
// SetEnabled enables/disables the PWM pin
|
|
|
|
SetEnabled(bool) error
|
|
|
|
// Polarity returns true if the polarity of the PWM pin is normal, otherwise false
|
|
|
|
Polarity() (bool, error)
|
|
|
|
// SetPolarity sets the polarity of the PWM pin to normal if called with true and to inverted if called with false
|
|
|
|
SetPolarity(normal bool) error
|
|
|
|
// Period returns the current PWM period in nanoseconds for pin
|
|
|
|
Period() (uint32, error)
|
|
|
|
// SetPeriod sets the current PWM period in nanoseconds for pin
|
|
|
|
SetPeriod(uint32) error
|
|
|
|
// DutyCycle returns the duty cycle in nanoseconds for the PWM pin
|
|
|
|
DutyCycle() (uint32, error)
|
|
|
|
// SetDutyCycle writes the duty cycle in nanoseconds to the PWM pin
|
|
|
|
SetDutyCycle(uint32) error
|
2022-11-21 02:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PWMPinnerProvider is the interface that an Adaptor should implement to allow
|
|
|
|
// clients to obtain access to any PWMPin's available on that board.
|
|
|
|
type PWMPinnerProvider interface {
|
2022-11-27 23:06:09 +08:00
|
|
|
PWMPin(id string) (PWMPinner, error)
|
2022-11-21 02:22:26 +08:00
|
|
|
}
|
|
|
|
|
2023-02-04 20:22:22 +08:00
|
|
|
// I2cSystemDevicer is the interface to a i2c bus at system level, according to I2C/SMBus specification.
|
|
|
|
// Some functions are not in the interface yet:
|
|
|
|
// * Process Call (WriteWordDataReadWordData)
|
|
|
|
// * Block Write - Block Read (WriteBlockDataReadBlockData)
|
|
|
|
// * Host Notify - WriteWordData() can be used instead
|
|
|
|
//
|
|
|
|
// see: https://docs.kernel.org/i2c/smbus-protocol.html#key-to-symbols
|
|
|
|
//
|
|
|
|
// S: Start condition; Sr: Repeated start condition, used to switch from write to read mode.
|
|
|
|
// P: Stop condition; Rd/Wr (1 bit): Read/Write bit. Rd equals 1, Wr equals 0.
|
|
|
|
// A, NA (1 bit): Acknowledge (ACK) and Not Acknowledge (NACK) bit
|
|
|
|
// Addr (7 bits): I2C 7 bit address. (10 bit I2C address not yet supported by gobot).
|
|
|
|
// Comm (8 bits): Command byte, a data byte which often selects a register on the device.
|
|
|
|
// Data (8 bits): A plain data byte. DataLow and DataHigh represent the low and high byte of a 16 bit word.
|
|
|
|
// Count (8 bits): A data byte containing the length of a block operation.
|
|
|
|
// [..]: Data sent by I2C device, as opposed to data sent by the host adapter.
|
|
|
|
//
|
2023-01-06 02:04:32 +08:00
|
|
|
type I2cSystemDevicer interface {
|
2023-02-04 20:22:22 +08:00
|
|
|
// ReadByte must be implemented as the sequence:
|
|
|
|
// "S Addr Rd [A] [Data] NA P"
|
|
|
|
ReadByte(address int) (byte, error)
|
|
|
|
|
|
|
|
// ReadByteData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Data] NA P"
|
|
|
|
ReadByteData(address int, reg uint8) (uint8, error)
|
|
|
|
|
|
|
|
// ReadWordData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [DataLow] A [DataHigh] NA P"
|
|
|
|
ReadWordData(address int, reg uint8) (uint16, error)
|
|
|
|
|
|
|
|
// ReadBlockData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P"
|
|
|
|
ReadBlockData(address int, reg uint8, data []byte) error
|
|
|
|
|
|
|
|
// WriteByte must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Data [A] P"
|
|
|
|
WriteByte(address int, val byte) error
|
|
|
|
|
|
|
|
// WriteByteData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] Data [A] P"
|
|
|
|
WriteByteData(address int, reg uint8, val uint8) error
|
|
|
|
|
|
|
|
// WriteBlockData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] Count [A] Data [A] Data [A] ... [A] Data [A] P"
|
|
|
|
WriteBlockData(address int, reg uint8, data []byte) error
|
|
|
|
|
|
|
|
// WriteWordData must be implemented as the sequence:
|
|
|
|
// "S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P"
|
|
|
|
WriteWordData(address int, reg uint8, val uint16) error
|
|
|
|
|
|
|
|
// WriteBytes writes the given data starting from the current register of bus device.
|
|
|
|
WriteBytes(address int, data []byte) error
|
|
|
|
|
|
|
|
// Read implements direct read operations.
|
|
|
|
Read(address int, b []byte) (int, error)
|
|
|
|
|
|
|
|
// Write implements direct write operations.
|
|
|
|
Write(address int, b []byte) (n int, err error)
|
|
|
|
|
|
|
|
// Close closes the character device file.
|
|
|
|
Close() error
|
2023-01-06 02:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpiSystemDevicer is the interface to a SPI bus at system level.
|
|
|
|
type SpiSystemDevicer interface {
|
|
|
|
TxRx(tx []byte, rx []byte) error
|
|
|
|
// Close the SPI connection.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2022-12-17 18:56:11 +08:00
|
|
|
// BusOperations are functions provided by a bus device, e.g. SPI, i2c.
|
|
|
|
type BusOperations interface {
|
2023-01-06 02:04:32 +08:00
|
|
|
// ReadByteData reads a byte from the given register of bus device.
|
|
|
|
ReadByteData(reg uint8) (uint8, error)
|
|
|
|
// ReadBlockData fills the given buffer with reads starting from the given register of bus device.
|
|
|
|
ReadBlockData(reg uint8, data []byte) error
|
|
|
|
// WriteByteData writes the given byte value to the given register of bus device.
|
|
|
|
WriteByteData(reg uint8, val uint8) error
|
2022-12-17 18:56:11 +08:00
|
|
|
// WriteBlockData writes the given data starting from the given register of bus device.
|
|
|
|
WriteBlockData(reg uint8, data []byte) error
|
2023-01-06 02:04:32 +08:00
|
|
|
// WriteByte writes the given byte value to the current register of bus device.
|
2022-12-17 18:56:11 +08:00
|
|
|
WriteByte(val byte) error
|
2023-01-06 02:04:32 +08:00
|
|
|
// WriteBytes writes the given data starting from the current register of bus device.
|
2022-12-17 18:56:11 +08:00
|
|
|
WriteBytes(data []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2cOperations represents the i2c methods according to I2C/SMBus specification.
|
|
|
|
type I2cOperations interface {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
BusOperations
|
2023-02-04 20:22:22 +08:00
|
|
|
// ReadByte reads a byte from the current register of an i2c device.
|
2022-12-17 18:56:11 +08:00
|
|
|
ReadByte() (byte, error)
|
2023-02-04 20:22:22 +08:00
|
|
|
// ReadWordData reads a 16 bit value starting from the given register of an i2c device.
|
2022-12-17 18:56:11 +08:00
|
|
|
ReadWordData(reg uint8) (uint16, error)
|
2023-02-04 20:22:22 +08:00
|
|
|
// WriteWordData writes the given 16 bit value starting from the given register of an i2c device.
|
2022-12-17 18:56:11 +08:00
|
|
|
WriteWordData(reg uint8, val uint16) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpiOperations are the wrappers around the actual functions used by the SPI device interface
|
|
|
|
type SpiOperations interface {
|
|
|
|
BusOperations
|
|
|
|
// ReadCommandData uses the SPI device TX to send/receive data.
|
|
|
|
ReadCommandData(command []byte, data []byte) error
|
2023-01-06 02:04:32 +08:00
|
|
|
// Close the connection.
|
|
|
|
Close() error
|
2022-12-17 18:56:11 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Adaptor is the interface that describes an adaptor in gobot
|
2014-11-21 10:00:32 +08:00
|
|
|
type Adaptor interface {
|
2014-12-31 21:15:52 +08:00
|
|
|
// Name returns the label for the Adaptor
|
2014-07-04 10:52:31 +08:00
|
|
|
Name() string
|
2016-09-25 17:46:55 +08:00
|
|
|
// SetName sets the label for the Adaptor
|
2022-12-06 01:28:57 +08:00
|
|
|
SetName(string)
|
2014-12-31 21:15:52 +08:00
|
|
|
// Connect initiates the Adaptor
|
2016-11-07 21:55:21 +08:00
|
|
|
Connect() error
|
2014-12-31 21:15:52 +08:00
|
|
|
// Finalize terminates the Adaptor
|
2016-11-07 21:55:21 +08:00
|
|
|
Finalize() error
|
2014-11-22 11:35:01 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Porter is the interface that describes an adaptor's port
|
2014-11-22 11:35:01 +08:00
|
|
|
type Porter interface {
|
2014-11-21 10:00:32 +08:00
|
|
|
Port() string
|
2014-06-14 01:46:58 +08:00
|
|
|
}
|