B4J Question Again...B4j and I2C[SOLVED]

koffie

Member
Licensed User
Longtime User
Instead of using Python on my Raspi I want to try B4J as a host for my Android.
Python has loads of libraries/modules for I2C. In my case I use the python module for a MCP23017 which is a I2C port extender. I connect the MCP to the SCl and SDA of my Raspi, install the python modules and voila....16 extra input/output ports at my disposal.

I did look in the forum and googled but did not find anything...but can I use I2C (MCP23017) with B4J ie. is there a library for I2C ? or am I bound to Python on my Raspi ?

thanks,
 

koffie

Member
Licensed User
Longtime User
Okay...after some more clicking ;) I did find something about JPi4J and 'untested code' for the I2C.
Can somebody help me in a few steps in how to get the I2C (the above MCP23017) working after I have a functioning B4J IDE on my W7 and want to make a jar for the Raspi ?
 
Upvote 0

koffie

Member
Licensed User
Longtime User
It's no matter of getting errors...I wish. That would be progress. It's not knowing what to do.

I do know how to use B4A.
I do know how to (kind of) use B4J
I did add JPi4J as a library to B4J

I do not know what to do with the (quote) 'You can use this untested code to access I2C API:' (unquote) part

Without knowing what code like
B4X:
Return factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", ArrayAsObject(BusNumber))

actually does....I can copy and paste your example.

But...
- is the 'untested' part of the API... a piece of code already downloaded with the JPI4J library ?
- Should I do or download more code from pi4j.com to access the i2c part ?

It's probably the noob knowledge when it comes to Java in general...I guess.

Hope I made myself clear.
 
Upvote 0

koffie

Member
Licensed User
Longtime User
Erel,

Got the Hardware working under Python to check hardware and 'drivers'

I did copy and paste all the subs and followed the code you mentioned.

Question:

I use
B4X:
 Write (device,120)
like in your example.
Although I don't get any errors...nothing happens.

For the MCP23017 i need 2- bytes to write to the device. something like
B4X:
Write (device,15,255)

The above bytes are 15 for the particular port and 255 for all bits high.

How should I do this ?
 
Upvote 0

koffie

Member
Licensed User
Longtime User
Got a few years of programming skills...but feel a complete noob..

Writing works..but...how do I get an INPUTvalue from the device 0x20 ?
I tried read and read2. but there is no returnvalue from register 0x12.
B4X:
 Read2 (device,Array As Byte(0x12),0,1)


Which variable is filled in your example ?
B4X:
Sub Read2 (Device As JavaObject, Buffer() AsByte, Offset AsInt, Size AsInt) AsIntReturn Device.RunMethod("read", ArrayAsObject(Buffer, Offset, Size))End Sub



last question when it comes to this....promise :)

@ positrom2: must say that the writing bit works a charm (a bit slow) but workable.If I can't figure out the the input part...i'll have a look at the ELV part.
 
Upvote 0

koffie

Member
Licensed User
Longtime User
Finally got it working..... Thanks to Erel.:)

An example for the MCP23017. This was my road to result.

first take care of having a working I2C. You could try and use http://www.instructables.com/id/Raspberry-Pi-I2C-Python/ or use the instructions from Adafruit.

Copy the code found in post #3


The below part is a small and simple example for using Write and Read for the MCP23017 with some added text.
B4X:
Sub AppStart (Args() As String)
Dim bus As JavaObject = GetBus(0)' Raspberry rev 1
Dim device As JavaObject = GetDevice(bus, 32)' all addressbits on the MCP23017 are high (0x20)
Dim i As Boolean
i= True
Dim buffer(20) As Byte ' see below
write2(device, Array As Byte(01,00),0,2) ' all bits on GPB (0x01) are output (0x00)
Do While i= True
Write2(device, Array As Byte(00, 8), 0, 2) ' GPIOA bit 3 set to INPUT. Try to set this command outside While loop.
' and debug buffer0 to buffer20. You see The state of these will shift.
Read2(device, buffer,0,20)' read first 20 bytes from several registers....setting offset to something else then 0 will result in java exception errors !
' in case of MCP23017 you need to ' read'  register 0x12 for the inputstate of GPA0-GPA7
' it was/is not possible to only read the state of 0x12. Therefore I load 20 registers.
If buffer(17) = -9 Then ' I need register 18 (0x12) to read state of GPA. During debug I see that i need register 17....
' maybe because of offset. Well...It works.
' have to do something about the -9 though. :-)
Log (" SET")
Write2(device, Array As Byte(0x15,0x01),0,2)' output GPB0 high
Else
Write2( device,Array As Byte (0x15,0x00),0,2)' output GPB- low
End If
Loop ' endless loop
End Sub

A lot of trial and error and I can not explain everything, but it works.
Hope it is of some use for one of you....
 
Upvote 0
Top