Today, we gonna implement a quick tool available into RaspberryPi with Java Device IO API. i2cdetect is a simple i2c device scanner for your RaspberryPi, it browse all i2c address to find which i2c device is connected. This tutorial show how to recompile DeviceIO API and program too.

i2cdetect with the Java Device I/O API implementation .

Requisites

  • One RaspberryPi
    • tested w/ RPi B+
    • should works with all version
  • an I2C Device

Compile Device IO

Download Device IO API and compile it.

rbpi> sudo apt-get install mercurial oracle-java-8
rbpi> hg clone http://hg.openjdk.java.net/dio/dev jdkdio
rbpi> cd jdkdio

and we need to change line below into the Makefile, to disable some low level log information.

diff -r ad12ae44e850 Makefile
--- a/Makefile	Mon Feb 29 19:06:11 2016 +0300
+++ b/Makefile	Tue Mar 15 16:06:07 2016 +0000
@@ -172,7 +172,7 @@
 JAVAC := $(JAVA_HOME)/bin/javac
 JAR := $(JAVA_HOME)/bin/jar
 MKDIR := mkdir -p
-TARGET_C_FLAGS = $(USER_C_FLAGS) -fPIC -Wno-psabi -DJAVACALL_REPORT_LEVEL=0 -DENABLE_DEVICEACCESS -c -MMD -MF $(NATIVE_OUT_DIR)/$(@).d
+TARGET_C_FLAGS = $(USER_C_FLAGS) -fPIC -Wno-psabi -DJAVACALL_REPORT_LEVEL=4 -DENABLE_DEVICEACCESS -c -MMD -MF $(NATIVE_OUT_DIR)/$(@).d
 TARGET_INCLUDES = $(foreach d,$(DIO_INCLUDE_DIRS),-I$(d)) -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
 TARGET_CXX_FLAGS = $(USER_CXX_FLAGS) -fPIC -Wno-psabi -DJAVACALL_REPORT_LEVEL=4 -DENABLE_DEVICEACCESS -c
 TARGET_LD_FLAGS= $(USER_LD_FLAGS) -Xlinker -z -Xlinker defs -Xlinker -O1 \

You can now compile DIO Api Library with

rbpi> export JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt
rbpi> export PI_TOOLS=/usr
rbpi> make

Compile program

Now, we compile i2cdetect implementation as usual.

rbpi> wget https://gist.githubusercontent.com/gautric/a239adfd06b51099c6a8/raw/93abe18ce83b311a97300c718732088bce370ae2/I2CDetect.java
rbpi> javac -cp /home/pi/jdkdio/build/jar/dio.jar I2CDetect.java

Output result

If everything looks good you could execute I2CDetect class with the following command. I’ve bought Sense Hat to plug it on top of my RaspberryPi. You should get this kind of output when running program.

pi@rbpiv2-1:~ $ sudo java -Djava.library.path=/home/pi/jdkdio/build/so \
 -Djava.security.policy=/opt/eclipse/kura/kura/jdk.dio.policy \
 -cp .:/home/pi/jdkdio/build/jar/dio.jar \
 I2CDetect

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- 1c -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- UU -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- 5c -- -- 5f
60: -- -- -- -- -- -- -- -- -- -- 6a -- -- -- -- --
70: -- -- -- -- -- -- -- --     

Conclusion

This sample is a very quick hack for a Java implementation of i2cdetect. I have to investigate some stuff about DIO exception management to clarify this code.

Please feel free to send me your feedback see Apropos.