'Previously be sure that you include the Pi4J jars needed in your aditional libraries folder:
'pi4j-core.jar
'pi4j-device.jar
'pi4j-gpio-extension.jar
'pi4j-service.jar
'You can get them from the Pi4J project page
'I create a bas module for the I2C bus and add it to my app:
Sub Process_Globals
Public i2cbus As JavaObject
Public i2cdevice As JavaObject
End Sub
'Routine to Initialize the bus and to get the device
Public Sub Init_I2C(i2caddress As Int)
Dim factory As JavaObject
'From here I create calls to the library for I2c bus from Pi4J jar not present in actual jPi4J wrapper:
Try
Log("----------------------")
i2cbus = factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(1))
i2cdevice = i2cbus.RunMethodJO("getDevice", Array As Object(i2caddress))
Catch
Log(LastException.Message)
End Try
End Sub
Public Sub CloseBusI2C (bus As JavaObject)
bus.RunMethod("close", Null)
End Sub
Public Sub WriteI2C(device As JavaObject, b As Byte)
device.RunMethod("write", Array As Object(b))
End Sub
Public Sub ReadI2C(device As JavaObject) As Int
Return device.RunMethod("read", Null)
End Sub
Public Sub Write2I2C(device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int)
device.RunMethod("write", Array As Object(Buffer, Offset, Size))
End Sub
Public Sub Read2I2C (device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int) As Int
Return device.RunMethod("read", Array As Object(Buffer, Offset, Size))
End Sub
'Finally, this is the button click event inside the web app page made with ABMaterial from the template
Sub btn1_Clicked(Target As String)
If page.BlockEvent("btn1_clicked") Then Return
Dim myTexts, myReturns As List
Dim Bufferi2cout(256) As Byte 'BUFFER FOR WRITING
Dim Bufferi2cin(256) As Byte 'BUFFER FOR READING
Private bc As ByteConverter 'utility for showing data in Hex format
I2cController.Init_I2C(0x50) 'Device Address for my M24M01 Eeprom memory
'Initialize the write buffer with some arbitrary data for test:
For a=2 To 129
Bufferi2cout(a)=a-2
Next
Bufferi2cout(0)=0 'Testing address to access (2 bytes needed for M24m01 chip)
Bufferi2cout(1)=0
'write the address(2 bytes) + 128 bytes of data
I2cController.Write2I2C(I2cController.i2cdevice,Bufferi2cout,0,130)
Log("Write: ")
Log("Data: " & bc.HexFromBytes(Bufferi2cout)) 'Show data in Hex format
'Now I read the 128 bytes from the same address to confirm that it works:
I2cController.Write2I2C(I2cController.i2cdevice,Bufferi2cout,0,2) 'write the address
I2cController.Read2I2C(I2cController.i2cdevice,Bufferi2cin,0,128) 'read the data
Log("Read: ")
Log("Data: " & bc.HexFromBytes(Bufferi2cin)) 'Show data in Hex format to compare
page.UnblockEvent("btn1_Clicked")
End Sub