001package jmri.jmrix.loconet.usb_dcs240Plus;
002
003import jmri.jmrix.loconet.LnCommandStationType;
004import jmri.jmrix.loconet.LnPacketizer;
005import jmri.jmrix.loconet.LocoNetMessage;
006import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
007import jmri.jmrix.loconet.locobuffer.LocoBufferAdapter;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Update the code in jmri.jmrix.loconet.locobuffer so that it refers to the
013 * option settings for the Digitrax DCS240Plus's USB interface
014 * <p>
015 * Based on PR3Adapter.java
016 *
017 * @author Bob Jacobsen Copyright (C) 2004, 2005, 2006, 2008
018 * @author B. Milhaupt Copyright (C) 2019
019 */
020public class UsbDcs240PlusAdapter extends LocoBufferAdapter {
021
022    public UsbDcs240PlusAdapter() {
023        super(new UsbDcs240PlusSystemConnectionMemo());
024
025        options.remove(option2Name);
026        options.put(option2Name, new Option(Bundle.getMessage("CommandStationTypeLabel"), commandStationOptions(), false));
027        setOptionState("TranspondingPresent", "Yes"); // set the default value
028    }
029
030    @Override
031    protected void reportOpen(String portName) {
032        log.info("Connecting USB DCS240Plus via {} {}", portName, currentSerialPort);
033    }
034
035    /**
036     * Always on flow control
037     */
038    @Override
039    protected void setLocalFlowControl() {
040        FlowControl flow = FlowControl.RTSCTS;
041        setFlowControl(currentSerialPort, flow);
042    }
043
044    /**
045     * Set up all of the other objects to operate with a DCS240Plus USB interface connected to this
046     * port. This overrides the version in loconet.locobuffer, but it has to
047     * duplicate much of the functionality there, so the code is basically
048     * copied.
049     */
050    @Override
051    public void configure() {
052        setCommandStationType(getOptionState(option2Name));
053        setTurnoutHandling(getOptionState(option3Name));
054
055        if (commandStationType == LnCommandStationType.COMMAND_STATION_USB_DCS240PLUS_ALONE) {
056            // DCS240Plus USB in standalone programmer case:
057            // connect to a packetizing traffic controller
058            // that does echoing
059            //
060            // Note - already created a LocoNetSystemConnectionMemo, so re-use
061            // it when creating a PR2 Packetizer.  (If create a new one, will
062            // end up with two "LocoNet" menus...)
063            jmri.jmrix.loconet.pr2.LnPr2Packetizer packets =
064                    new jmri.jmrix.loconet.pr2.LnPr2Packetizer(this.getSystemConnectionMemo());
065            packets.connectPort(this);
066
067            // set traffic controller and configure command station and mangers
068            this.getSystemConnectionMemo().setLnTrafficController(packets);
069            // DCS204+ has transponding built in
070            setOptionState("TranspondingPresent", "Yes");
071            mTranspondingAvailable = true;
072
073            // do the common manager config
074            this.getSystemConnectionMemo().configureCommandStation(commandStationType,
075                    mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);  // never transponding!
076            this.getSystemConnectionMemo().configureManagersPR2();
077
078            // start operation
079            packets.startThreads();
080
081            // set mode
082            LocoNetMessage msg = new LocoNetMessage(6);
083            msg.setOpCode(0xD3);
084            msg.setElement(1, 0x10);
085            msg.setElement(2, 1);  // set PR2
086            msg.setElement(3, 0);
087            msg.setElement(4, 0);
088            packets.sendLocoNetMessage(msg);
089
090        } else {
091            // MS100 modes - connecting to a separate command station
092            // set transponding option - DCS240+ has transponding built in
093            setOptionState("TranspondingPresent", "Yes");
094            setTranspondingAvailable(getOptionState("TranspondingPresent"));
095            
096            setInterrogateOnStart(getOptionState("InterrogateOnStart"));
097            setLoconetProtocolAutoDetect(getOptionState("LoconetProtocolAutoDetect"));
098            // connect to a packetizing traffic controller
099            LnPacketizer packets = getPacketizer(getOptionState(option4Name));
100            packets.connectPort(this);
101
102            // set traffic controller and configure command station and mangers
103            this.getSystemConnectionMemo().setLnTrafficController(packets);
104            // do the common manager config
105            this.getSystemConnectionMemo().configureCommandStation(commandStationType,
106                    mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);
107
108            this.getSystemConnectionMemo().configureManagersMS100();
109
110            // start operation
111            packets.startThreads();
112
113            // set mode
114            LocoNetMessage msg = new LocoNetMessage(6);
115            msg.setOpCode(0xD3);
116            msg.setElement(1, 0x10);
117            msg.setElement(2, 0);  // set MS100, no power
118            msg.setElement(3, 0);
119            msg.setElement(4, 0);
120            packets.sendLocoNetMessage(msg);
121        }
122    }
123
124    /**
125     * {@inheritDoc}
126     *
127     * @return String[] containing the single valid baud rate, "57,600".
128     */
129    @Override
130    public String[] validBaudRates() {
131        return new String[]{"57,600 baud"}; // NOI18N
132    }
133
134    /**
135     * {@inheritDoc}
136     *
137     * @return int[] containing the single valid baud rate, 57600.
138     */
139    @Override
140    public int[] validBaudNumbers() {
141        return new int[]{57600};
142    }
143
144    @Override
145    public int defaultBaudIndex() {
146        return 0;
147    }
148
149    // Option 1 does flow control, inherited from LocoBufferAdapter
150
151    /**
152     * The DCS240Plus USB interface can be used as a "Standalone Programmer", or with various LocoNet
153     * command stations, or as an interface to a "Standalone LocoNet".  Provide those
154     * options.
155     *
156     * @return an array of strings containing the various command station names and
157     *      name(s) of modes without command stations
158     */
159    public String[] commandStationOptions() {
160        String[] retval = new String[commandStationNames.length + 1];
161        retval[0] = LnCommandStationType.COMMAND_STATION_DCS240PLUS.getName();
162        retval[1] = LnCommandStationType.COMMAND_STATION_USB_DCS240PLUS_ALONE.getName();
163        int count = 2;
164        for (String commandStationName : commandStationNames) {
165            if (!commandStationName.equals(LnCommandStationType.COMMAND_STATION_DCS240PLUS.getName())) {
166            // include all but COMMAND_STATION_DCS240Plus, which was forced  to
167            // the front of the list (above)
168                retval[count++] = commandStationName;
169        }
170    }
171        // Note: Standalone loconet does not make sense for DCS240Plus USB interface.
172        return retval;
173    }
174
175    @Override
176    public UsbDcs240PlusSystemConnectionMemo getSystemConnectionMemo() {
177        LocoNetSystemConnectionMemo m = super.getSystemConnectionMemo();
178        if (m instanceof UsbDcs240PlusSystemConnectionMemo) {
179            return (UsbDcs240PlusSystemConnectionMemo) m;
180        }
181        log.error("Cannot cast the system connection memo to a UsbDcs240PlusSystemConnection Memo.");
182        return null;
183    }
184
185    private final static Logger log = LoggerFactory.getLogger(UsbDcs240PlusAdapter.class);
186}