### Bricklet Listener JavaObject

An example of creating a TinkerForge JavaObject listener.
As adviced by TinkerForge, using listeners for recurring events is always preferred compared to using getters.
Using a listener, means that the app keeps on running till it is stopped.
This also means that the connection with the Master Brick is kept alive while running.
Note Inline Java
This howto was been with older versions of B4J which did not support Inline Java.
In the meantime, Inline Java is supported which makes it rather easy to implement a listener (see How Bricklet Listener).
TinkerForge provides examples of creating interrupt listener.
This is an example using Java, to listen to the IO16 Bricklet Port a, Pin2.
On that port and pin, a switch could be connected. Depending on the state of the switch an action can be triggered.
The port must configured as Direction = Input with Value = Pull-Up.
B4x solution
This solution has been developed after several iterations find a way to implement a listener.
There might be better ways, but the outlined solution has proven working fine, both for B4A and B4J (Note B4i not tested).
The solution makes use of the TinkerForge Java API Library which is accessed in B4x using the JavaObject library.
Solution Principle
1. Define a Class Module dedicated for a TinkerForge Bricklet
Within the Class Module
2. Set the Bricklet Callback Period
3. Create a Bricklet Listener Event
4. Add the Bricklet Listener
5. Handle the Listener Event
6. Call a sub in the main module to action the Listener Events
Within the Main Module (or any other module)
1. Define the Listener Action with the arguments from the Listener Event as defined in the Class module
Example
Extract from the example Java Code by TinkerForge
io16.addInterruptListener(new BrickletIO16.InterruptListener() {
Public void interrupt(Char port, Short interruptMask, Short valueMask) {
System.out.println("Interrupt on port: " + port);
System.out.println("Interrupt by: " + Integer.toBinaryString(interruptMask));
System.out.println("Value: " + Integer.toBinaryString(valueMask));
}
});
// Enable interrupt on pin 2 of port a
io16.setPortInterrupt('a', (short)(1 << 2));
Converting above Java Code into B4J
Note that the code below only focusses on handling the interrupt listener.
...
' Define the JavaObjects to connect to the Bricklet
Dim joBricklet As JavaObject
' Init the JavaObject = Packagename.Class
joBricklet.InitializeNewInstance("com.tinkerforge.BrickletIO16", Array(Parameter.IO16BrickletUID, joIPConnection))
...
' Set the port configuration as inputE with example
Dim p1 As Char = port ' Port = b
Dim p2 As Short = pin ' Pin = 7 for pin B7
Dim p3 As Char = "i" ' Always i
Dim p4 As Boolean = True ' Pullup
joBricklet.RunMethod("setPortConfiguration", Array As Object(p1,p2,p3,p4))
' Create a new JavaObject which creates the listener event
Dim joBrickletIL As JavaObject = joBricklet
' The listener executes the sub switchtouch_Event
Dim e As Object = joBrickletIL.CreateEvent("com.tinkerforge.BrickletIO16.InterruptListener","switchtouch",False)
' Add the listener
joBricklet.RunMethod("addInterruptListener", Array(e))
' Set the port as interrupt
joBricklet.RunMethod("setPortInterrupt", Array As Object(p1,p2))
...
Define the event listener
'Methodname = interrupt
'Arguments (3): 0 = Port, 1 = short interruptMask, 2 = short valueMask
'Examples: b, 128, 127 or b, 128, 255
'Examples: b, 10000000, 11111111 or b, 10000000, 0111111
`Private Sub switchtouch_Event(MethodName As String, Args() As Object) As Object
Log("Switchtouch ::: Start - " & MethodName)
Log("Port = " & Args(0) & ", by " & Args(1) & ", Value: " & Args(2))
Log("Port = " & Args(0) & ", by " & ToBinaryString8(Args(1)) & ", Value: " & ToBinaryString(Args(2)))
' valueMask - if bit 8 is 1 then switch is pressed
Return True
End Sub`
Helper Subs
' Converts a number to a binary string
' Parameter: number to convert
' Returns the number as a binary string with length 8
`Private Sub ToBinaryString8(number As Int) As String
Dim result As String = ""
Dim joI As JavaObject
joI.InitializeStatic("java.lang.Integer")
result = joI.RunMethod("toBinaryString", Array(number))
Dim n As Int = result.Length
For i = n To 7
result = "0" & result
Next
Return result
End Sub`

Changed: , Category: TinkerForge, File: T6C264M1961.htm, ID: 1961