001package jmri.jmrix.loconet.uhlenbrock;
002
003import java.util.Arrays;
004import jmri.jmrix.loconet.LnCommandStationType;
005import jmri.jmrix.loconet.locobuffer.LocoBufferAdapter;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Update the code in jmri.jmrix.loconet.locobuffer so that it operates
011 * correctly with the IC-COM and Intellibox II on-board USB port. Note that the
012 * jmri.jmrix.loconet.intellibox package is for the first version of Uhlenbrock
013 * Intellibox, whereas this package (jmri.jmrix.loconet.uhlenbrock) is for the
014 * Intellibox II and the IB-COM.
015 * <p>
016 * Since this is by definition connected to an Intellibox II or IB-COM, the
017 * command station prompt is suppressed.
018 *
019 * @author Alex Shepherd Copyright (C) 2004
020 * @author Bob Jacobsen Copyright (C) 2005, 2010
021 */
022public class UhlenbrockAdapter extends LocoBufferAdapter {
023
024    public UhlenbrockAdapter() {
025        super(new UhlenbrockSystemConnectionMemo());
026
027        // define command station options
028        options.remove(option2Name);
029        options.put(option2Name, new Option(Bundle.getMessage("CommandStationTypeLabel"), commandStationOptions(), false));
030        options.put("InterrogateOnStart", new Option(Bundle.getMessage("InterrogateOnStart"),
031                new String[]{Bundle.getMessage("ButtonYes"), Bundle.getMessage("ButtonNo")} )); // NOI18N
032
033        validSpeeds = new String[]{Bundle.getMessage("Baud19200"), Bundle.getMessage("Baud38400"),
034                Bundle.getMessage("Baud57600"), Bundle.getMessage("Baud115200")};
035        validSpeedValues = new int[]{19200, 38400, 57600, 115200};
036        configureBaudRate(validSpeeds[3]); // Set the default baud rate (localized)
037    }
038
039    /**
040     * Set up all of the other objects to operate with an IB-II connected to
041     * this port.
042     */
043    @Override
044    public void configure() {
045
046        setCommandStationType(getOptionState(option2Name));
047        setTurnoutHandling(getOptionState(option3Name));
048        setInterrogateOnStart(getOptionState("InterrogateOnStart"));
049        // connect to a packetizing traffic controller
050        UhlenbrockPacketizer packets = new UhlenbrockPacketizer(this.getSystemConnectionMemo());
051        packets.setLoconetUpdateSlotOnMessageCreation(Bundle.getMessage("ButtonYes").equals(getOptionState("LoconetUpdateSlotOnMessageCreation")));
052        packets.connectPort(this);
053
054        // create memo
055        this.getSystemConnectionMemo().setLnTrafficController(packets);
056        // do the common manager config
057        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
058                mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, false); //never Xp slots
059        this.getSystemConnectionMemo().configureManagers();
060
061        // start operation
062        packets.startThreads();
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public String[] validBaudRates() {
070        return Arrays.copyOf(validSpeeds, validSpeeds.length);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public int[] validBaudNumbers() {
078        return Arrays.copyOf(validSpeedValues, validSpeedValues.length);
079    }
080
081    @Override
082    public int defaultBaudIndex() {
083        return 3;
084    }
085
086    @Override
087    public boolean okToSend() {
088        return true;
089    }
090
091    @Override
092    protected void reportOpen(String portName) {
093        log.info("Connecting Uhlenbrock via {} {}", portName, currentSerialPort);
094    }
095
096    /**
097     * Always off flow control
098     */
099    @Override
100    protected void setLocalFlowControl() {
101        FlowControl flow = FlowControl.NONE;
102        setFlowControl(currentSerialPort, flow);
103    }
104
105    /**
106     * Provide just one valid command station value.
107     * @return array with single entry, name of COMMAND_STATION_IBX_TYPE_2 .
108     */
109    public String[] commandStationOptions() {
110        String[] retval = {
111            LnCommandStationType.COMMAND_STATION_IBX_TYPE_2.getName()
112        };
113        return retval;
114    }
115
116    private static final Logger log = LoggerFactory.getLogger(UhlenbrockAdapter.class);
117
118}