diff --git a/src/org/openlcb/DatagramMessage.java b/src/org/openlcb/DatagramMessage.java index a4c101d5..ea969f54 100644 --- a/src/org/openlcb/DatagramMessage.java +++ b/src/org/openlcb/DatagramMessage.java @@ -91,11 +91,7 @@ public String toString() { int n = getData().length; value.append("("+n+") "); boolean first = true; - for (int i = 0; i> 2)+1 ) & 0x3F; - int AAA = ( (from) >> 8) & 0x7; - long event = 0x0101020000FF0000L | (AAA << 9) | (aaaaaa << 3) | (DD << 1); + // See JMRI OlcnAddress line 111 for Event ID coding + if (from >= 2045) from = from-2045; + else from = from + 3; + long event = 0x0101020000FF0000L | (from<<1); + event |= onOffBox.getSelectedIndex(); EventID id = new EventID(String.format("%016X", event)); @@ -278,6 +280,59 @@ public void actionPerformed(ActionEvent e) { return menuItem; } + public static JMenuItem makeDccSensorEventMenuItem(JTextComponent textfield) { + JMenuItem menuItem = new JMenuItem("Insert DCC sensor events ..."); + menuItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JDialog dialog = new JDialog(); + dialog.setTitle("Select DCC Sensor Address"); + + JPanel innerPanel = new JPanel(new FlowLayout()); + + JTextField number = new JTextField(12); + number.setText("1"); + innerPanel.add(number); + + JComboBox onOffBox = new JComboBox( + new String[]{ + "Inactive/Off", + "Active/On" + }); + innerPanel.add(onOffBox); + + JButton setButton = new JButton("Set"); + innerPanel.add(setButton); + setButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int from = Integer.parseInt(number.getText().trim()); + + // See JMRI OlcnAddress line 126 for Event ID coding + from = 0xFFF & (from - 1); // 1 based name to 0 based network, 12 bit value + + long eventActive = 0x0101020000FB0000L | from; // active/on + long eventInactive = 0x0101020000FA0000L | from; // inactive/off + + long event = onOffBox.getSelectedIndex() == 0 ? eventInactive : eventActive; + + EventID id = new EventID(String.format("%016X", event)); + textfield.setText(id.toShortString()); + dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); + } + }); + + dialog.add(innerPanel); + dialog.setModal(true); + dialog.pack(); + dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); + } + }); + return menuItem; + } + private static class EventIdInserter extends JMenuItem { public EventIdInserter(String name, String value, JTextComponent target) { super(name);