Change SBC to Jetson Orin
This commit is contained in:
147
src/main/java/SBC/I2C_Device.java
Normal file
147
src/main/java/SBC/I2C_Device.java
Normal file
@@ -0,0 +1,147 @@
|
||||
package SBC;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class I2C_Device {
|
||||
|
||||
|
||||
Linux_C_lib C = Linux_C_lib.INSTANCE;
|
||||
private @Getter int bus_handle;
|
||||
private @Getter int device_address;
|
||||
private @Getter int dev_handle;
|
||||
private @Getter boolean opened = false;
|
||||
|
||||
/**
|
||||
* Create I2C Device
|
||||
* @param bus_handle I2C Bus Handle, from opening I2C_Bus
|
||||
* @param device_address I2C Device Address
|
||||
*/
|
||||
public I2C_Device(int bus_handle, int device_address){
|
||||
opened = false;
|
||||
if (bus_handle<0 || device_address<0) return;
|
||||
this.bus_handle = bus_handle;
|
||||
this.device_address = device_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open I2C Device in Slave mode
|
||||
* @return true if successfully opened
|
||||
*/
|
||||
public boolean Open_Slave(){
|
||||
boolean sucess = false;
|
||||
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SLAVE, device_address);
|
||||
if (dev_handle>=0){
|
||||
sucess = true;
|
||||
}
|
||||
opened = sucess;
|
||||
return opened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open I2C Device in Slave mode with Force
|
||||
* @return true if successfully opened
|
||||
*/
|
||||
public boolean Open_SlaveForce(){
|
||||
boolean sucess = false;
|
||||
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SLAVE_FORCE, device_address);
|
||||
if (dev_handle>=0){
|
||||
C.ioctl(bus_handle,I2C_Flags.I2C_RETRIES, 3);
|
||||
C.ioctl(bus_handle,I2C_Flags.I2C_TIMEOUT, 1000);
|
||||
sucess = true;
|
||||
}
|
||||
opened = sucess;
|
||||
return opened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open I2C Device in Read Write mode
|
||||
* @return true if successfully opened
|
||||
*/
|
||||
public boolean Open_RDWR(){
|
||||
boolean sucess = false;
|
||||
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_RDWR, device_address);
|
||||
if (dev_handle>=0){
|
||||
sucess = true;
|
||||
}
|
||||
opened = sucess;
|
||||
return opened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open I2C Device in SMBus mode
|
||||
* @return true if successfully opened
|
||||
*/
|
||||
public boolean Open_SMBus(){
|
||||
boolean sucess = false;
|
||||
dev_handle = C.ioctl(bus_handle, I2C_Flags.I2C_SMBUS, device_address);
|
||||
if (dev_handle>=0){
|
||||
sucess = true;
|
||||
}
|
||||
opened = sucess;
|
||||
return opened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close I2C Device
|
||||
*/
|
||||
public void Close(){
|
||||
C.close(dev_handle);
|
||||
opened = false;
|
||||
dev_handle = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Bytes to I2C Device
|
||||
* @param data bytes to write
|
||||
* @return number of bytes written
|
||||
*/
|
||||
public int WriteBytes(byte[] data){
|
||||
if (bus_handle<0 || dev_handle<0 || device_address<0) return -1;
|
||||
return C.write(bus_handle, data, data.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Byte to I2C Device
|
||||
* @param register_address register address
|
||||
* @param data byte to write
|
||||
* @return number of bytes written
|
||||
*/
|
||||
public int WriteBytes(int register_address, byte data){
|
||||
byte[] cmd = new byte[2];
|
||||
cmd[0] = (byte) register_address;
|
||||
cmd[1] = data;
|
||||
return WriteBytes(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Bytes to I2C Device
|
||||
* @param register_address register address
|
||||
* @param data bytes to write
|
||||
* @return number of bytes written
|
||||
*/
|
||||
public int WriteBytes(int register_address, byte[] data){
|
||||
byte[] cmd = new byte[data.length+1];
|
||||
cmd[0] = (byte) register_address;
|
||||
System.arraycopy(data, 0, cmd, 1, data.length);
|
||||
return WriteBytes(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Bytes from I2C Device
|
||||
* @param readsize number of bytes to read
|
||||
* @return bytes read
|
||||
*/
|
||||
public byte[] ReadBytes(int readsize){
|
||||
if (bus_handle<0 || dev_handle<0 || device_address<0) return null;
|
||||
byte[] data = new byte[readsize];
|
||||
int read = C.read(bus_handle, data, readsize);
|
||||
if (read<0) return null;
|
||||
if (read<readsize){
|
||||
byte[] temp = new byte[read];
|
||||
System.arraycopy(data, 0, temp, 0, read);
|
||||
return temp;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user