/
I2CUtils.java
49 lines (38 loc) · 1.67 KB
/
I2CUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.emoseman.utils;
public class I2CUtils
{
static
{
try
{
// System.out.println("java.library.path: " + System.getProperty("java.library.path"));
System.loadLibrary("i2c_native");
}
catch (UnsatisfiedLinkError e)
{
System.err.println("Failed to load library: " + e.getMessage());
System.exit(1);
}
}
public static void main(final String[] args)
throws Exception
{
int result = writeByteToI2CDevice("/dev/i2c-3", 0x1E, 0x00, 0x58);
System.out.println("write test: " + String.format("%02X", result));
result = writeByteToI2CDevice("/dev/i2c-3", 0x1E, 0x01, 0x20);
System.out.println("write test: " + String.format("%02X", result));
result = writeByteToI2CDevice("/dev/i2c-3", 0x1E, 0x02, 0x00);
System.out.println("write test: " + String.format("%02X", result));
result = readByteFromI2CDevice("/dev/i2c-3", 0x1E, 0x00);
System.out.println("read test: " + String.format("%02X", result));
result = readByteFromI2CDevice("/dev/i2c-3", 0x1E, 0x01);
System.out.println("read test: " + String.format("%02X", result));
result = readByteFromI2CDevice("/dev/i2c-3", 0x1E, 0x02);
System.out.println("read test: " + String.format("%02X", result));
}
public static native byte readByteFromI2CDevice(String path, int address, int register_address);
public static native short readWordFromI2CDevice(String path, int address, int register_address);
public static native byte writeByteToI2CDevice(String path, int address, int register_address, int data);
public static native short writeWordToI2CDevice(String path, int address, int register_address, int data);
}
// vim:ts=2:sw=2:expandtab