### Sample AmbientLightv2 Listener

An example of the class to set or get the state of an IO-16 Bricklet pin (0-7) from port (a or b).
The TinkerForge API Reference for the AmbientLight 2.0 Bricklet:
http://www.tinkerforge.com/en/doc/Software/Bricklets/AmbientLightV2_Bricklet_Java.html#ambient-light-v2-bricklet-java-api
`Private Sub Class_Globals
Dim joInline As JavaObject = Me
Public Illuminance As Long
Private isConnected As Boolean = False
End Sub`
'Initializes the object.
`Public Sub Initialize
' DO NOT REMOVE - has to be called
End Sub`
`Public Sub StartListener(HOST As String, PORT As Int, UID As String, interval As Int)
joInline.RunMethod("StartListener", Array(HOST, PORT, UID, interval))
isConnected = True
End Sub`
`Public Sub StopListener
If isConnected Then joInline.RunMethod("StopListener", Null)
End Sub`
' The loglistener is called by the inline java IlluminanceListener.
' IMPORTANT: Updating the listview in the main thread has to be done via callsubdelayed.
`Private Sub LogListener(il As Long) 'ignore
Illuminance = il / 100.0
CallSubDelayed(Me, "updatelog")
End Sub`
' Update the listview defined in the main class. New entries at added at first position.
`Private Sub UpdateLog
Main.lvLog.Items.InsertAt(0, $"${DateTime.Time(DateTime.Now)}: ${Illuminance}"$)
End Sub`
If JAVA
import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletAmbientLightV2;
IPConnection ipcon;
public void StartListener(String HOST, int PORT, String UID, int interval) throws Exception {
ipcon = new IPConnection();
BrickletAmbientLightV2 al = new BrickletAmbientLightV2(UID, ipcon);
ipcon.connect(HOST, PORT);
// Set period for illuminance callback
al.setIlluminanceCallbackPeriod(interval);
// Add illuminance listener (parameter has unit Lux/100)
al.addIlluminanceListener(new BrickletAmbientLightV2.IlluminanceListener() {
public void illuminance(long illuminance) {
// System.out.println("Illuminance: " + illuminance/100.0 + " Lux");
ba.raiseEvent(this, "loglistener", illuminance);
}
});
//ipcon.disconnect();
}
public void StopListener() throws Exception {
ipcon.disconnect();
}
End If

Changed: 2018-06-07 09:24, Category: TinkerForge Inline Java, File: T6C265M1971.htm, ID: 1971