1
1
package com .pi4j .plugin .ffm .providers .i2c ;
2
2
3
+ import com .pi4j .exception .Pi4JException ;
3
4
import com .pi4j .io .i2c .I2CBase ;
4
5
import com .pi4j .io .i2c .I2CConfig ;
5
6
import com .pi4j .io .i2c .I2CProvider ;
@@ -19,7 +20,7 @@ public class I2CSMBus extends I2CBase<I2CBusFFM> {
19
20
*
20
21
* @param provider a {@link I2CProvider} object.
21
22
* @param config a {@link I2CConfig} object.
22
- * @param i2CBus a {@link I2CBus } object.
23
+ * @param i2CBus a {@link I2CBusFFM } object.
23
24
*/
24
25
public I2CSMBus (I2CProvider provider , I2CConfig config , I2CBusFFM i2CBus ) {
25
26
super (provider , config , i2CBus );
@@ -37,12 +38,10 @@ private int writeInternal(int register, byte[] data) {
37
38
return i2CBus .execute (this , (i2cFileDescriptor ) -> {
38
39
if (data .length == 1 ) {
39
40
return SMBUS .writeByteData (i2cFileDescriptor , (byte ) register , data [0 ]);
41
+ } else if (i2CBus .hasFunctionality (I2CFunctionality .I2C_FUNC_SMBUS_WRITE_BLOCK_DATA )) {
42
+ return SMBUS .writeBlockData (i2cFileDescriptor , (byte ) register , data );
40
43
} else {
41
- return 0 ;
42
- // var buffer = new byte[data.length + 1];
43
- // buffer[0] = (byte) register;
44
- // System.arraycopy(data, 0, buffer, 1, data.length);
45
- // return FILE.write(i2cFileDescriptor, buffer);
44
+ throw new Pi4JException ("No support any of I2C device write mode." );
46
45
}
47
46
});
48
47
}
@@ -57,12 +56,12 @@ private int writeInternal(int register, byte[] data) {
57
56
private byte [] readInternal (int register , int size ) {
58
57
i2CBus .selectDevice (config .device ());
59
58
return i2CBus .execute (this , (i2cFileDescriptor ) -> {
60
- //FILE.write(i2cFileDescriptor, new byte[]{(byte) register});
61
- //return FILE.read(i2cFileDescriptor, new byte[size], size);
62
59
if (size == 1 ) {
63
60
return new byte [] {SMBUS .readByteData (i2cFileDescriptor , (byte ) register )};
61
+ } else if (i2CBus .hasFunctionality (I2CFunctionality .I2C_FUNC_SMBUS_READ_BLOCK_DATA )) {
62
+ return SMBUS .readBlockData (i2cFileDescriptor , (byte ) register , new byte [size ]);
64
63
} else {
65
- return new byte [ 0 ] ;
64
+ throw new Pi4JException ( "No support any of I2C device read mode." ) ;
66
65
}
67
66
});
68
67
}
@@ -71,13 +70,15 @@ private byte[] readInternal(int register, int size) {
71
70
@ Override
72
71
public int read () {
73
72
i2CBus .selectDevice (config .device ());
73
+ // this is needed, because we are receiving raw bytes, which we have to convert to proper int
74
74
return Byte .toUnsignedInt (i2CBus .execute (this , SMBUS ::readByte ));
75
75
}
76
76
77
77
@ Override
78
- public int read (byte [] buffer , int offset , int length ) {
79
- //TODO: how to implement? That is not a part os standard
80
- return 0 ;
78
+ public int read (byte [] data , int offset , int length ) {
79
+ var buffer = new byte [data .length - 1 ];
80
+ System .arraycopy (data , 1 , buffer , 0 , length );
81
+ return readRegister (data [0 ], buffer , offset , length );
81
82
}
82
83
83
84
@ Override
@@ -88,19 +89,30 @@ public int write(byte b) {
88
89
89
90
@ Override
90
91
public int write (byte [] data , int offset , int length ) {
91
- //TODO: how to implement? That is not a part os standard
92
- return 0 ;
92
+ var buffer = new byte [data .length - 1 ];
93
+ System .arraycopy (data , 1 , buffer , 0 , length );
94
+ return writeRegister (data [0 ], buffer , offset , length );
93
95
}
94
96
95
97
@ Override
96
98
public int readRegister (int register ) {
99
+ i2CBus .selectDevice (config .device ());
100
+ // this is needed, because we are receiving raw bytes, which we have to convert to proper int
97
101
return Byte .toUnsignedInt (readInternal (register , 1 )[0 ]);
98
102
}
99
103
100
104
@ Override
101
- public int readRegister (byte [] register , byte [] buffer , int offset , int length ) {
102
- //TODO: how to implement? That is not a part os standard
103
- return 0 ;
105
+ public int readRegister (byte [] register , byte [] data , int offset , int length ) {
106
+ Objects .checkFromIndexSize (offset , length , data .length );
107
+ // Prepare the data for multibyte register
108
+ // Sequence:
109
+ // - set address to first byte, all other bytes send with data.
110
+ // - read the byte without address (internal chip cursor will point to the correct address)
111
+ var byteRegister = register [0 ];
112
+ var readData = new byte [data .length + register .length - 1 ];
113
+ System .arraycopy (Arrays .copyOfRange (register , 1 , register .length ), 0 , readData , 0 , register .length - 1 );
114
+ System .arraycopy (data , 0 , readData , register .length - 1 , data .length );
115
+ return readRegister (byteRegister , readData );
104
116
}
105
117
106
118
@ Override
0 commit comments