Jump to content

[Java] Problem mit Listener


Recommended Posts

Hallo,

 

wie einige Forumsteilnehmer vor mir habe ich auch Probleme mit Listenern. Leider habe ich nicht die richtige Lösung im Forum gefunden.

Hier das Problem:

Für die Erfassung der Tasten am Bricklet LCD20x4 habe ich wie in den Beispielen den ButtonPressedListener und ButtonReleasedListener hinzugefügt. Um auch mehrere Tastendrücke zugleich erfassen zu können (soll für SET oder RESET Anweisungen genutzt werden) addiere ich über eine Variabale außerhalb der Listener die Tastensignale bei Pressed auf und ziehe diese bei Released wieder ab.

 

Genau das ist das Problem: ich kann nicht mit Variabalen außerhalb der Klassen kommunizieren. Wie könnte so etwas gehen?

 

Ich bin für jeden Hinweis dankbar.

 

Grüße

Urmel

 

 

Hier der Code

 

package net.tinkerforgeTest;

 

import com.tinkerforge.BrickletDualRelay;

import com.tinkerforge.BrickletHumidity;

import com.tinkerforge.BrickletLCD20x4;

import com.tinkerforge.IPConnection;

 

public class ExampleSimple {

private static final String HOST = "localhost";

private static final int PORT = 4223;

private static final String UIDDualRelay = "kuE"; // Change to your UID

private static final String UIDHumidity = "nED"; // Change to your UID

private static final String UIDDisplay = "ocE"; // Change to your UID

 

 

 

// Note: To make the example code cleaner we do not handle exceptions. Exceptions you

//      might normally want to catch are described in the documentation

public static void main(String args[]) throws Exception {

 

final short buttonState = 0; // der Tastenstatus außerhalb der Listener

IPConnection ipcon = new IPConnection(); // Create IP connection

BrickletDualRelay dr = new BrickletDualRelay(UIDDualRelay, ipcon); // Create device object

BrickletHumidity hum = new BrickletHumidity(UIDHumidity, ipcon); // Create device object

final BrickletLCD20x4 lcd = new BrickletLCD20x4(UIDDisplay, ipcon); // Create device object

 

ipcon.connect(HOST, PORT); // Connect to brickd

// Don't use device before ipcon is connected

 

// Turn backlight on

lcd.backlightOn();

 

// Write "Hello World"

lcd.writeLine((short)0, (short)0, "Hello World");

 

 

// Turn relays alternating on/off for 10 times with 1 second delay

for(int i = 0; i < 6; i++) {

Thread.sleep(200);

if(i % 2 == 1) {

dr.setState(true, false);

} else {

dr.setState(false, true);

}

}

 

 

// Get current humidity (unit is %RH/10)

for(int j = 0; j < 2; j++){

Thread.sleep(1000);

int humidity = hum.getHumidity(); // Can throw com.tinkerforge.TimeoutException

System.out.println("Relative Humidity: " + humidity/10.0 + " %RH");

lcd.clearDisplay();

lcd.writeLine((short)0, (short)0, "RH: " + humidity/10.0 + " %RH");

}

 

// Add and implement listener for pressed and released events

lcd.addButtonPressedListener(new BrickletLCD20x4.ButtonPressedListener() {

public void buttonPressed(short button) {

                        // von hier kein Zugriff auf buttonState

System.out.println("Pressed: " + button);

switch(button){

        case 0:

        buttonState = buttonState + 1; // kein Zugriff

            break;

        case 1:

        buttonState = buttonState + 2;

            break;

        case 2:

        buttonState = buttonState + 4;

            break;

        case 3:

        buttonState = buttonState + 8;

            break; 

}

}

});

 

lcd.addButtonReleasedListener(new BrickletLCD20x4.ButtonReleasedListener() {

public void buttonReleased(short button) {

                        // von hier kein Zugriff auf buttonState

System.out.println("Pressed: " + button);

switch(button){

        case 0:

        buttonState = buttonState - 1;

            break;

        case 1:

        buttonState = buttonState - 2;

            break;

        case 2:

        buttonState = buttonState - 4;

            break;

        case 3:

        buttonState = buttonState - 8;

            break; 

}

}

});

 

 

 

lcd.backlightOff();

ipcon.disconnect();

System.out.println("Ende");

}

}

 

 

Link to comment
Share on other sites

Hallo,

 

versuch mal die Variable als Klassenvariable anzulegen, d.h. zwischen

public class ExampleSimple {
   private static final String HOST = "localhost";
   private static final int PORT = 4223;
   private static final String UIDDualRelay = "kuE"; // Change to your UID
   private static final String UIDHumidity = "nED"; // Change to your UID
   private static final String UIDDisplay = "ocE"; // Change to your UID

und

public static void main(String args[]) throws Exception {

also da, wo du auch die Konstanten definierst.

Link to comment
Share on other sites

Hallo,

 

hast du auch das "final" entfernt?

Ich mache das auch und es geht problemlos.

Beispiel: IPConnection direkt nach der Klassendefinition

private IPConnection ipcon = null;

 

und dann in einer Methode (die bei mir init() heißt):

ipcon.addConnectedListener(new IPConnection.ConnectedListener() {			
		@Override
		public void connected(short disconnectReason) {
			try {
				System.out.println("Connected CB called.");
				ipcon.enumerate();
			} catch (NotConnectedException exc) {
				// Should never happen
				System.out.println("Connected called but not connected.");
				System.out.println(exc.getMessage());
			}			
		}
	});

 

Ich rufe in meinen Listenern auch Methoden der umgebenden Klasse auf.

Link to comment
Share on other sites

Hallo Equinox,

 

der Tip die Variable als Klassenvariable zu deklarieren und das final zu entfernen war goldrichtig.

--> Es Funktioniert.

 

Grüße & Vielen Dank

 

Urmel

 

PS wann kommt der RED Brick ?

 

 

 

public class ExampleSimple {

private static final String HOST = "localhost";

private static final int PORT = 4223;

private static final String UIDDualRelay = "kuE"; // Change to your UID

private static final String UIDHumidity = "nED"; // Change to your UID

private static final String UIDDisplay = "ocE"; // Change to your UID

private static int buttonState = 0;

 

 

// Note: To make the example code cleaner we do not handle exceptions. Exceptions you

//      might normally want to catch are described in the documentation

public static void main(String args[]) throws Exception {

IPConnection ipcon = new IPConnection(); // Create IP connection

BrickletDualRelay dr = new BrickletDualRelay(UIDDualRelay, ipcon); // Create device object

BrickletHumidity hum = new BrickletHumidity(UIDHumidity, ipcon); // Create device object

BrickletLCD20x4 lcd = new BrickletLCD20x4(UIDDisplay, ipcon); // Create device object

 

...

...

...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...