From fe358bfcf7df2d482657de5bb60e5d950bba05c6 Mon Sep 17 00:00:00 2001 From: hama Date: Fri, 15 Jul 2016 00:57:18 +0200 Subject: [PATCH] GpioSettings: InputData --- .../de/republib/pinet/gui/GPIOSettings.java | 171 ++++++++++++++++-- src/main/java/de/republib/util/DynBytes.java | 30 ++- .../java/de/republib/util/DynBytesTest.java | 24 ++- 3 files changed, 195 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/republib/pinet/gui/GPIOSettings.java b/src/main/java/de/republib/pinet/gui/GPIOSettings.java index 812fd18..66fc633 100644 --- a/src/main/java/de/republib/pinet/gui/GPIOSettings.java +++ b/src/main/java/de/republib/pinet/gui/GPIOSettings.java @@ -4,6 +4,7 @@ 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; @@ -14,6 +15,9 @@ import javax.swing.JLabel; 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; @@ -31,8 +35,11 @@ public class GPIOSettings extends JPanel { * */ 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; /** @@ -41,17 +48,35 @@ public class GPIOSettings extends JPanel { * @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); } } @@ -60,7 +85,7 @@ class GPIOTable extends JPanel implements ActionListener { * */ 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")), @@ -68,13 +93,14 @@ class GPIOTable extends JPanel implements ActionListener { 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); @@ -131,10 +157,18 @@ class GPIOTable extends JPanel implements ActionListener { 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) { @@ -142,6 +176,13 @@ class GPIOTable extends JPanel implements ActionListener { return rc; } + /** + * @return the parent + */ + public GPIOSettings getGpioSettings() { + return this.gpioSettings; + } + /** * @return the pinButtons */ @@ -170,6 +211,36 @@ class GPIOTable extends JPanel implements ActionListener { } } +class InputData extends JPanel { + private static final long serialVersionUID = 1L; + // private final Logger logger = LoggerFactory.getLogger(OutputData.class); + private JComboBox 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. * @@ -181,8 +252,8 @@ class OutputData extends JPanel implements ActionListener { * */ private static final long serialVersionUID = 1L; - private final static String[] modes = { I18N.tr("Blink") }; + // private final Logger logger = LoggerFactory.getLogger(OutputData.class); private JComboBox comboMode; private JTextField textCount; private JTextField textHigh; @@ -208,21 +279,33 @@ class OutputData extends JPanel implements ActionListener { 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) { + } } @@ -254,8 +337,12 @@ class PinButton extends JButton { * */ 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; @@ -284,6 +371,13 @@ class PinButton extends JButton { return this.index; } + /** + * @return the mode + */ + public int getMode() { + return this.mode; + } + /** * @return the pinNumber */ @@ -297,6 +391,14 @@ class PinButton extends JButton { public String getTitle() { return this.title; } + + /** + * @param mode + * the mode to set + */ + public void setMode(int mode) { + this.mode = mode; + } } /** @@ -305,17 +407,19 @@ class PinButton extends JButton { * @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 comboMode; private final String[] modes = { I18N.tr("undefined"), I18N.tr("input"), I18N.tr("output") }; private PinButton currentPin = null; + private final GPIOTable parent; /** * Constructor. @@ -325,6 +429,7 @@ class PinData extends JPanel { */ 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:"))); @@ -333,10 +438,31 @@ class PinData extends JPanel { 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. * @@ -350,7 +476,16 @@ class PinData extends JPanel { 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); + } } /** diff --git a/src/main/java/de/republib/util/DynBytes.java b/src/main/java/de/republib/util/DynBytes.java index eb3edcd..2c03111 100644 --- a/src/main/java/de/republib/util/DynBytes.java +++ b/src/main/java/de/republib/util/DynBytes.java @@ -220,16 +220,26 @@ public class DynBytes { if (from2 < 0) { from2 = 0; } - final int maxLength = Math.max(value.length - from, this.length - from2); - if (length > maxLength) { - length = maxLength; - } - while (rc == 0 && length > 0 && from < value.length && from2 < this.length) { - rc = this.buffer[from2++] - value[from++]; - length--; - } - if (rc == 0 && length != 0) { - rc = from < value.length ? -1 : from2 < this.length ? 1 : -1; + if (from >= value.length) { + // string from value is empty + // Is other string empty? + if (from2 < this.length) { + rc = -1; + } + } else if (from2 >= this.length) { + rc = 1; + } else { + final int maxLength = Math.max(value.length - from, this.length - from2); + if (length > maxLength) { + length = maxLength; + } + while (rc == 0 && length > 0 && from < value.length && from2 < this.length) { + rc = this.buffer[from2++] - value[from++]; + length--; + } + if (rc == 0 && length != 0) { + rc = from < value.length ? -1 : from2 < this.length ? 1 : -1; + } } return rc; } diff --git a/src/test/java/de/republib/util/DynBytesTest.java b/src/test/java/de/republib/util/DynBytesTest.java index e0dadd0..01bb1d9 100644 --- a/src/test/java/de/republib/util/DynBytesTest.java +++ b/src/test/java/de/republib/util/DynBytesTest.java @@ -125,9 +125,13 @@ public class DynBytesTest { 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 @@ -238,6 +242,22 @@ public class DynBytesTest { 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'); -- 2.39.5