Hallo liebe Forenmitglieder
Nach langem Ausprobieren, bin ich nun am Ende mit meinem Latein (welches leider nicht besonders gross ist, wenn es um OOP geht).
Konkret ist mein Problem: Ich möchte aus einer Klasse, auf eine Methode einer anderen Klasse zugreifen. In dieser Methode soll dann auf eine Instanz des "GpioController" der pi4j Bibliothek etwas geschrieben werden. Nur leider kann ich diese Instanz nicht statisch machen.
Hat von euch jemand eine Idde oder einen Lösungsansatz, der funktionieren könnte?
Das Projekt habe ich als Anhang angefügt und hier noch den Code der beiden Klassen:
Döchte ich mir dieser Zeile
die Methode "setDO(...)" aufrufen, was allerdings nicht geht, da die Instanz "gpio" nicht statisch ist. Gibt es eine andere Möglichkeit das hinzubekommen?
Einen hilfreichen Ansatz wäre erfreulich.
Nach langem Ausprobieren, bin ich nun am Ende mit meinem Latein (welches leider nicht besonders gross ist, wenn es um OOP geht).
Konkret ist mein Problem: Ich möchte aus einer Klasse, auf eine Methode einer anderen Klasse zugreifen. In dieser Methode soll dann auf eine Instanz des "GpioController" der pi4j Bibliothek etwas geschrieben werden. Nur leider kann ich diese Instanz nicht statisch machen.
Hat von euch jemand eine Idde oder einen Lösungsansatz, der funktionieren könnte?
Das Projekt habe ich als Anhang angefügt und hier noch den Code der beiden Klassen:
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gh_beta2;
import com.pi4j.io.i2c.I2CFactory;
import java.awt.Color;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class JFrame extends javax.swing.JFrame {
/**
* Creates new form JFrame
* @throws com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException
* @throws java.io.IOException
*/
public JFrame() throws I2CFactory.UnsupportedBusNumberException, IOException {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton_DO_0 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton_DO_0.setText("OFF");
jButton_DO_0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_DO_0ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(jButton_DO_0, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
.addGap(88, 88, 88))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(jButton_DO_0)
.addContainerGap(106, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(67, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton_DO_0ActionPerformed(java.awt.event.ActionEvent evt) {
if("OFF".equals(jButton_DO_0.getText())){
jButton_DO_0.setText("ON");
jButton_DO_0.setBackground(Color.green);
IO.setDO(0, "high");
}// end if
else{
jButton_DO_0.setText("OFF");
jButton_DO_0.setBackground(null);
IO.setDO(0, "low");
}// end else
}
/**
* @param args the command line arguments
* @throws java.lang.InterruptedException
* @throws java.lang.reflect.InvocationTargetException
* @throws com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException
* @throws java.io.IOException
*/
public static void main(String args[]) throws InterruptedException, InvocationTargetException, I2CFactory.UnsupportedBusNumberException, IOException {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeAndWait(() -> {
try {
new JFrame().setVisible(true);
} catch (I2CFactory.UnsupportedBusNumberException | IOException ex) {
Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);
}
});
IO io = new IO();
}
// Variables declaration - do not modify
public static javax.swing.JButton jButton_DO_0;
public javax.swing.JPanel jPanel1;
// End of variables declaration
}
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gh_beta2;
import com.pi4j.gpio.extension.mcp.MCP23008GpioProvider;
import com.pi4j.gpio.extension.mcp.MCP23008Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
/**
*
* @author Administrator
*/
class IO {
public IO() throws I2CFactory.UnsupportedBusNumberException, IOException{
}// end constuctor
GpioController gpio = GpioFactory.getInstance();
final MCP23008GpioProvider DO_8Bit = new MCP23008GpioProvider(I2CBus.BUS_1, 0x21);
// provision gpio output pins and make sure they are all LOW at startup
GpioPinDigitalOutput DO[] = {
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_00, "DO_0", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_01, "DO_1", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_02, "DO_2", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_03, "DO_3", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_04, "DO_4", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_05, "DO_5", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_06, "DO_6", PinState.LOW),
gpio.provisionDigitalOutputPin(DO_8Bit, MCP23008Pin.GPIO_07, "DO_7", PinState.LOW)
};
static void setDO(int DO_x, String state) {
int DO_x_intern = DO_x;
String state_intern = state;
if(DO_x_intern == 0 & "high".equals(state_intern)){
gpio.setState(PinState.HIGH, DO[0]);
}// end if
if(DO_x_intern == 0 & "low".equals(state_intern)){
gpio.setState(PinState.LOW, DO[0]);
}// end if
if(DO_x_intern == 1 & "high".equals(state_intern)){
gpio.setState(PinState.HIGH, DO[1]);
}// end if
if(DO_x_intern == 1 & "low".equals(state_intern)){
gpio.setState(PinState.LOW, DO[1]);
}// end if
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}// end class
Döchte ich mir dieser Zeile
Java:
IO.setDO(0, "high");
Einen hilfreichen Ansatz wäre erfreulich.