package de.republib.pinet.gui;
import java.awt.Component;
+import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JTextField;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import de.republib.pinet.GpioClient;
import de.republib.pinet.PinNumber;
import de.republib.util.Announcer;
*
*/
private static final long serialVersionUID = 1L;
+ // private final Logger logger =
+ // LoggerFactory.getLogger(GPIOSettings.class);
private GPIOTable gpioTable;
private OutputData panelOutputData;
+ private InputData panelInputData;
private final ControlCenter center;
/**
* @return
*/
public GPIOSettings(ControlCenter center) {
+ super(new FlowLayout(FlowLayout.LEADING));
this.center = center;
}
+ /**
+ * @return the panelInputData
+ */
+ public InputData getPanelInputData() {
+ return this.panelInputData;
+ }
+
+ /**
+ * @return the panelOutputData
+ */
+ public OutputData getPanelOutputData() {
+ return this.panelOutputData;
+ }
+
public void populate() {
- this.gpioTable = new GPIOTable();
+ this.gpioTable = new GPIOTable(this);
add(this.gpioTable);
final PinData panelPinData = new PinData(this.gpioTable);
this.gpioTable.setPanelPinData(panelPinData);
add(panelPinData);
add(this.panelOutputData = new OutputData(this.center, panelPinData));
+ this.panelOutputData.setVisible(false);
+ add(this.panelInputData = new InputData(this.center, panelPinData));
+ this.panelOutputData.setVisible(false);
}
}
*
*/
private static final long serialVersionUID = 1L;
-
+ // private final Logger logger = LoggerFactory.getLogger(GPIOTable.class);
private final PinButton[] pinButtons = new PinButton[40];
private final JLabel[] columnNames = { new JLabel(I18N.tr("Name")), new JLabel(I18N.tr("Pin")),
private final Object[][] cells;
PinData panelPinData;
+ GPIOSettings gpioSettings;
/**
* Constructor.
*/
- public GPIOTable() {
+ public GPIOTable(GPIOSettings parent) {
super(new GridLayout(21, 4));
- this.panelPinData = this.panelPinData;
+ this.gpioSettings = parent;
this.pinButtons[0] = new PinButton(0, I18N.tr("+3.3V"), null);
this.pinButtons[1] = new PinButton(1, I18N.tr("+5V"), null);
this.pinButtons[2] = new PinButton(2, "SDA1", PinNumber.PIN_RPi2_03);
populate();
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
@Override
public void actionPerformed(ActionEvent e) {
- final PinButton button = (PinButton) e.getSource();
- this.panelPinData.fillData(button);
+ if (e.getSource() instanceof PinButton) {
+ final PinButton button = (PinButton) e.getSource();
+ this.panelPinData.fillData(button);
+ }
}
public Component getCell(int row, int column) {
return rc;
}
+ /**
+ * @return the parent
+ */
+ public GPIOSettings getGpioSettings() {
+ return this.gpioSettings;
+ }
+
/**
* @return the pinButtons
*/
}
}
+class InputData extends JPanel {
+ private static final long serialVersionUID = 1L;
+ // private final Logger logger = LoggerFactory.getLogger(OutputData.class);
+ private JComboBox<String> comboChannel;
+ private JTextField textChannel;
+ private final ControlCenter center;
+ private final PinData pinData;
+
+ /**
+ * Constructor.
+ */
+ public InputData(ControlCenter center, PinData pinData) {
+ super(new GridLayout(0, 2));
+ this.center = center;
+ this.pinData = pinData;
+ add(new JLabel(I18N.tr("Channel:")));
+ add(this.textChannel = new JTextField("0"));
+ add(new JLabel(I18N.tr("Interval:")));
+ add(this.textChannel = new JTextField("0"));
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ add(new JLabel());
+ }
+}
+
/**
* Shows and manages the panel controlling the output behaviour of a pin.
*
*
*/
private static final long serialVersionUID = 1L;
-
private final static String[] modes = { I18N.tr("Blink") };
+ // private final Logger logger = LoggerFactory.getLogger(OutputData.class);
private JComboBox<String> comboMode;
private JTextField textCount;
private JTextField textHigh;
add(this.textLow = new JTextField("500"));
add(new JLabel(""));
add(this.buttonRun = new JButton(I18N.tr("Start")));
+ this.comboMode.addActionListener(this);
this.buttonRun.addActionListener(this);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
@Override
public void actionPerformed(ActionEvent e) {
- final GpioClient client = this.center.getClient();
- final PinButton current = this.pinData.getCurrentPin();
- final int count = numberOf(this.textCount, 10);
- final int high = numberOf(this.textHigh, 500);
- final int low = numberOf(this.textLow, 500);
- this.center.say(Announcer.LOG, String.format(I18N.tr("blinking started on pin %d"), current.getPinNumber()));
- final DynBytes answer = client.blink(current.getPinNumber(), count, high, low);
- if (answer.startsWith("OK")) {
+ if (e.getSource() == this.buttonRun) {
+ final GpioClient client = this.center.getClient();
+ final PinButton current = this.pinData.getCurrentPin();
+ final int count = numberOf(this.textCount, 10);
+ final int high = numberOf(this.textHigh, 500);
+ final int low = numberOf(this.textLow, 500);
this.center.say(Announcer.LOG,
- String.format(I18N.tr("pin %d blinks %d times"), current.getPinNumber(), count));
+ String.format(I18N.tr("blinking started on pin %d"), current.getPinNumber()));
+ final DynBytes answer = client.blink(current.getPinNumber(), count, high, low);
+ if (answer.startsWith("OK")) {
+ this.center.say(Announcer.LOG,
+ String.format(I18N.tr("pin %d blinks %d times"), current.getPinNumber(), count));
+ }
+ } else if (e.getSource() == this.comboMode) {
+
}
}
*
*/
private static final long serialVersionUID = 1L;
+ public static final int MODE_UNDEF = 0;
+ public static final int MODE_INPUT = 1;
+ public static final int MODE_OUTPUT = 2;
private final PinNumber pinNumber;
private final String title;
+ private int mode = PinButton.MODE_UNDEF;
int index;
return this.index;
}
+ /**
+ * @return the mode
+ */
+ public int getMode() {
+ return this.mode;
+ }
+
/**
* @return the pinNumber
*/
public String getTitle() {
return this.title;
}
+
+ /**
+ * @param mode
+ * the mode to set
+ */
+ public void setMode(int mode) {
+ this.mode = mode;
+ }
}
/**
* @author hm
*
*/
-class PinData extends JPanel {
+class PinData extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
+ private final Logger logger = LoggerFactory.getLogger(PinData.class);
private JLabel labelPin;
private JLabel labelInternalNo;
private JLabel labelTitle;
private JComboBox<String> comboMode;
private final String[] modes = { I18N.tr("undefined"), I18N.tr("input"), I18N.tr("output") };
private PinButton currentPin = null;
+ private final GPIOTable parent;
/**
* Constructor.
*/
public PinData(GPIOTable table) {
super(new GridLayout(0, 2));
+ this.parent = table;
add(new JLabel(I18N.tr("Pin No:")));
add(this.labelPin = new JLabel());
add(new JLabel(I18N.tr("GPIO number:")));
add(this.labelTitle = new JLabel());
add(new JLabel(I18N.tr("Mode:")));
add(this.comboMode = new JComboBox<>(this.modes));
+ this.comboMode.addActionListener(this);
add(new JLabel());
fillData(table.getPinButton(11));
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
+ @Override
+ public void actionPerformed(ActionEvent event) {
+ if (event.getSource() == this.comboMode) {
+ final int index = this.comboMode.getSelectedIndex();
+ if (this.currentPin.getMode() != index) {
+ this.logger.debug("actionPerformed(); changing mode: " + index);
+ this.currentPin.setMode(index);
+ this.parent.getGpioSettings().getPanelOutputData().setVisible(index == PinButton.MODE_OUTPUT);
+ this.parent.getGpioSettings().getPanelInputData().setVisible(index == PinButton.MODE_INPUT);
+ }
+ }
+
+ }
+
/**
* Fills the data of the panel from the current pin.
*
this.labelInternalNo.setText(number == null ? "-" : String.valueOf(number.getNumber()));
this.labelTitle.setText(button.getTitle());
this.comboMode.setVisible(number != null);
- this.comboMode.setSelectedIndex(0);
+ final int index = button.getMode();
+ this.comboMode.setSelectedIndex(index);
+ final OutputData output = this.parent.getGpioSettings().getPanelOutputData();
+ if (output != null) {
+ output.setVisible(index == PinButton.MODE_OUTPUT);
+ }
+ final InputData input = this.parent.getGpioSettings().getPanelInputData();
+ if (input != null) {
+ input.setVisible(index == PinButton.MODE_INPUT);
+ }
}
/**
Assert.assertEquals(buffer.compare(buffer2, 3, 4, 2), 0);
// to small in value:
- Assert.assertEquals(buffer.compare(buffer2, -1, 0, 2), -1);
+ Assert.assertEquals(buffer.compare(buffer2, -1, 0, 4), 0);
+ Assert.assertEquals(buffer.compare(buffer4, -1, 0, 4), -1);
+ Assert.assertEquals(buffer.compare(buffer3, -1, 0, 4), 1);
// too small in buffer:
- Assert.assertEquals(buffer.compare(buffer2, 4, -1, 2), 1);
+ Assert.assertEquals(buffer.compare(buffer2, 0, -1, 4), 0);
+ Assert.assertEquals(buffer.compare(buffer4, 0, -2, 4), -1);
+ Assert.assertEquals(buffer.compare(buffer3, 0, -3, 4), 1);
}
@org.testng.annotations.Test
Assert.assertEquals(buffer.longAsLittleEndian(0, 4, -5), -1);
}
+ @org.testng.annotations.Test
+ public void testStartsWith() throws Exception {
+ final DynBytes buffer = new DynBytes(16, 16);
+ buffer.append("Ächzend öfter üben!");
+ Assert.assertTrue(buffer.startsWith("Ächzend"));
+ Assert.assertTrue(buffer.startsWith("Ächzend öfter üben!"));
+ Assert.assertFalse(buffer.startsWith("Ächzend öfter üben."));
+ Assert.assertFalse(buffer.startsWith("Ächzend öfter üben!!"));
+ buffer.setLength(0).append((byte) 0x61).append((byte) 0x62);
+ Assert.assertTrue(buffer.startsWith("a"));
+ Assert.assertTrue(buffer.startsWith("ab"));
+ Assert.assertFalse(buffer.startsWith("abc"));
+ Assert.assertFalse(buffer.startsWith("A"));
+ Assert.assertFalse(buffer.startsWith("aB"));
+ }
+
@org.testng.annotations.Test
public void testToHex() {
Assert.assertEquals(DynBytes.toHex(0), '0');