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