gobot.io/x/gobot/v2@v2.1.0/adaptor.go (about) 1 package gobot 2 3 import ( 4 "io" 5 "time" 6 ) 7 8 // DigitalPinOptioner is the interface to provide the possibility to change pin behavior for the next usage 9 type DigitalPinOptioner interface { 10 // SetLabel change the pins label 11 SetLabel(string) (changed bool) 12 // SetDirectionOutput sets the pins direction to output with the given initial value 13 SetDirectionOutput(initialState int) (changed bool) 14 // SetDirectionInput sets the pins direction to input 15 SetDirectionInput() (changed bool) 16 // SetActiveLow initializes the pin with inverse reaction (applies on input and output). 17 SetActiveLow() (changed bool) 18 // SetBias initializes the pin with the given bias (applies on input and output). 19 SetBias(bias int) (changed bool) 20 // SetDrive initializes the output pin with the given drive option. 21 SetDrive(drive int) (changed bool) 22 // SetDebounce initializes the input pin with the given debounce period. 23 SetDebounce(period time.Duration) (changed bool) 24 // SetEventHandlerForEdge initializes the input pin for edge detection and to call the event handler on specified edge. 25 // lineOffset is within the GPIO chip (needs to transformed to the pin id), timestamp is the detection time, 26 // detectedEdge contains the direction of the pin changes, seqno is the sequence number for this event in the sequence 27 // of events for all the lines in this line request, lseqno is the same but for this line 28 SetEventHandlerForEdge(handler func(lineOffset int, timestamp time.Duration, detectedEdge string, seqno uint32, 29 lseqno uint32), edge int) (changed bool) 30 } 31 32 // DigitalPinOptionApplier is the interface to apply options to change pin behavior immediately 33 type DigitalPinOptionApplier interface { 34 // ApplyOptions apply all given options to the pin immediately 35 ApplyOptions(...func(DigitalPinOptioner) bool) error 36 } 37 38 // DigitalPinner is the interface for system gpio interactions 39 type DigitalPinner interface { 40 // Export exports the pin for use by the adaptor 41 Export() error 42 // Unexport releases the pin from the adaptor, so it is free for the operating system 43 Unexport() error 44 // Read reads the current value of the pin 45 Read() (int, error) 46 // Write writes to the pin 47 Write(int) error 48 // DigitalPinOptionApplier is the interface to change pin behavior immediately 49 DigitalPinOptionApplier 50 } 51 52 // DigitalPinValuer is the interface to get pin behavior for the next usage. The interface is and should be rarely used. 53 type DigitalPinValuer interface { 54 // DirectionBehavior gets the direction behavior when the pin is used the next time. 55 // This means its possibly not in this direction type at the moment. 56 DirectionBehavior() string 57 } 58 59 // DigitalPinnerProvider is the interface that an Adaptor should implement to allow clients to obtain 60 // access to any DigitalPin's available on that board. If the pin is initially acquired, it is an input. 61 // Pin direction and other options can be changed afterwards by pin.ApplyOptions() at any time. 62 type DigitalPinnerProvider interface { 63 DigitalPin(id string) (DigitalPinner, error) 64 } 65 66 // PWMPinner is the interface for system PWM interactions 67 type PWMPinner interface { 68 // Export exports the PWM pin for use by the operating system 69 Export() error 70 // Unexport releases the PWM pin from the operating system 71 Unexport() error 72 // Enabled returns the enabled state of the PWM pin 73 Enabled() (bool, error) 74 // SetEnabled enables/disables the PWM pin 75 SetEnabled(bool) error 76 // Polarity returns true if the polarity of the PWM pin is normal, otherwise false 77 Polarity() (bool, error) 78 // SetPolarity sets the polarity of the PWM pin to normal if called with true and to inverted if called with false 79 SetPolarity(normal bool) error 80 // Period returns the current PWM period in nanoseconds for pin 81 Period() (uint32, error) 82 // SetPeriod sets the current PWM period in nanoseconds for pin 83 SetPeriod(uint32) error 84 // DutyCycle returns the duty cycle in nanoseconds for the PWM pin 85 DutyCycle() (uint32, error) 86 // SetDutyCycle writes the duty cycle in nanoseconds to the PWM pin 87 SetDutyCycle(uint32) error 88 } 89 90 // PWMPinnerProvider is the interface that an Adaptor should implement to allow 91 // clients to obtain access to any PWMPin's available on that board. 92 type PWMPinnerProvider interface { 93 PWMPin(id string) (PWMPinner, error) 94 } 95 96 // I2cSystemDevicer is the interface to a i2c bus at system level, according to I2C/SMBus specification. 97 // Some functions are not in the interface yet: 98 // * Process Call (WriteWordDataReadWordData) 99 // * Block Write - Block Read (WriteBlockDataReadBlockData) 100 // * Host Notify - WriteWordData() can be used instead 101 // 102 // see: https://docs.kernel.org/i2c/smbus-protocol.html#key-to-symbols 103 // 104 // S: Start condition; Sr: Repeated start condition, used to switch from write to read mode. 105 // P: Stop condition; Rd/Wr (1 bit): Read/Write bit. Rd equals 1, Wr equals 0. 106 // A, NA (1 bit): Acknowledge (ACK) and Not Acknowledge (NACK) bit 107 // Addr (7 bits): I2C 7 bit address. (10 bit I2C address not yet supported by gobot). 108 // Comm (8 bits): Command byte, a data byte which often selects a register on the device. 109 // Data (8 bits): A plain data byte. DataLow and DataHigh represent the low and high byte of a 16 bit word. 110 // Count (8 bits): A data byte containing the length of a block operation. 111 // [..]: Data sent by I2C device, as opposed to data sent by the host adapter. 112 // 113 type I2cSystemDevicer interface { 114 // ReadByte must be implemented as the sequence: 115 // "S Addr Rd [A] [Data] NA P" 116 ReadByte(address int) (byte, error) 117 118 // ReadByteData must be implemented as the sequence: 119 // "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Data] NA P" 120 ReadByteData(address int, reg uint8) (uint8, error) 121 122 // ReadWordData must be implemented as the sequence: 123 // "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [DataLow] A [DataHigh] NA P" 124 ReadWordData(address int, reg uint8) (uint16, error) 125 126 // ReadBlockData must be implemented as the sequence: 127 // "S Addr Wr [A] Comm [A] Sr Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P" 128 ReadBlockData(address int, reg uint8, data []byte) error 129 130 // WriteByte must be implemented as the sequence: 131 // "S Addr Wr [A] Data [A] P" 132 WriteByte(address int, val byte) error 133 134 // WriteByteData must be implemented as the sequence: 135 // "S Addr Wr [A] Comm [A] Data [A] P" 136 WriteByteData(address int, reg uint8, val uint8) error 137 138 // WriteBlockData must be implemented as the sequence: 139 // "S Addr Wr [A] Comm [A] Count [A] Data [A] Data [A] ... [A] Data [A] P" 140 WriteBlockData(address int, reg uint8, data []byte) error 141 142 // WriteWordData must be implemented as the sequence: 143 // "S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P" 144 WriteWordData(address int, reg uint8, val uint16) error 145 146 // WriteBytes writes the given data starting from the current register of bus device. 147 WriteBytes(address int, data []byte) error 148 149 // Read implements direct read operations. 150 Read(address int, b []byte) (int, error) 151 152 // Write implements direct write operations. 153 Write(address int, b []byte) (n int, err error) 154 155 // Close closes the character device file. 156 Close() error 157 } 158 159 // SpiSystemDevicer is the interface to a SPI bus at system level. 160 type SpiSystemDevicer interface { 161 TxRx(tx []byte, rx []byte) error 162 // Close the SPI connection. 163 Close() error 164 } 165 166 // BusOperations are functions provided by a bus device, e.g. SPI, i2c. 167 type BusOperations interface { 168 // ReadByteData reads a byte from the given register of bus device. 169 ReadByteData(reg uint8) (uint8, error) 170 // ReadBlockData fills the given buffer with reads starting from the given register of bus device. 171 ReadBlockData(reg uint8, data []byte) error 172 // WriteByteData writes the given byte value to the given register of bus device. 173 WriteByteData(reg uint8, val uint8) error 174 // WriteBlockData writes the given data starting from the given register of bus device. 175 WriteBlockData(reg uint8, data []byte) error 176 // WriteByte writes the given byte value to the current register of bus device. 177 WriteByte(val byte) error 178 // WriteBytes writes the given data starting from the current register of bus device. 179 WriteBytes(data []byte) error 180 } 181 182 // I2cOperations represents the i2c methods according to I2C/SMBus specification. 183 type I2cOperations interface { 184 io.ReadWriteCloser 185 BusOperations 186 // ReadByte reads a byte from the current register of an i2c device. 187 ReadByte() (byte, error) 188 // ReadWordData reads a 16 bit value starting from the given register of an i2c device. 189 ReadWordData(reg uint8) (uint16, error) 190 // WriteWordData writes the given 16 bit value starting from the given register of an i2c device. 191 WriteWordData(reg uint8, val uint16) error 192 } 193 194 // SpiOperations are the wrappers around the actual functions used by the SPI device interface 195 type SpiOperations interface { 196 BusOperations 197 // ReadCommandData uses the SPI device TX to send/receive data. 198 ReadCommandData(command []byte, data []byte) error 199 // Close the connection. 200 Close() error 201 } 202 203 // Adaptor is the interface that describes an adaptor in gobot 204 type Adaptor interface { 205 // Name returns the label for the Adaptor 206 Name() string 207 // SetName sets the label for the Adaptor 208 SetName(string) 209 // Connect initiates the Adaptor 210 Connect() error 211 // Finalize terminates the Adaptor 212 Finalize() error 213 } 214 215 // Porter is the interface that describes an adaptor's port 216 type Porter interface { 217 Port() string 218 }