001package jmri.jmrix.loconet.usb_dcs240; 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 DCS240'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 UsbDcs240Adapter extends LocoBufferAdapter { 021 022 public UsbDcs240Adapter() { 023 super(new UsbDcs240SystemConnectionMemo()); 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 DCS240 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 DCS240 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 if (commandStationType == LnCommandStationType.COMMAND_STATION_USB_DCS240_ALONE) { 055 // DCS240 USB in standalone programmer case: 056 // connect to a packetizing traffic controller 057 // that does echoing 058 // 059 // Note - already created a LocoNetSystemConnectionMemo, so re-use 060 // it when creating a PR2 Packetizer. (If create a new one, will 061 // end up with two "LocoNet" menus...) 062 jmri.jmrix.loconet.pr2.LnPr2Packetizer packets = 063 new jmri.jmrix.loconet.pr2.LnPr2Packetizer(this.getSystemConnectionMemo()); 064 packets.connectPort(this); 065 066 // set traffic controller and configure command station and mangers 067 this.getSystemConnectionMemo().setLnTrafficController(packets); 068 // do the common manager config 069 this.getSystemConnectionMemo().configureCommandStation(commandStationType, 070 mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect); // never transponding! 071 this.getSystemConnectionMemo().configureManagersPR2(); 072 073 // start operation 074 packets.startThreads(); 075 076 // set mode 077 LocoNetMessage msg = new LocoNetMessage(6); 078 msg.setOpCode(0xD3); 079 msg.setElement(1, 0x10); 080 msg.setElement(2, 1); // set PR2 081 msg.setElement(3, 0); 082 msg.setElement(4, 0); 083 packets.sendLocoNetMessage(msg); 084 085 } else { 086 // MS100 modes - connecting to a separate command station 087 // get transponding option 088 setTranspondingAvailable(getOptionState("TranspondingPresent")); 089 setInterrogateOnStart(getOptionState("InterrogateOnStart")); 090 setLoconetProtocolAutoDetect(getOptionState("LoconetProtocolAutoDetect")); 091 // connect to a packetizing traffic controller 092 LnPacketizer packets = getPacketizer(getOptionState(option4Name)); 093 packets.connectPort(this); 094 095 // set traffic controller and configure command station and mangers 096 this.getSystemConnectionMemo().setLnTrafficController(packets); 097 // do the common manager config 098 this.getSystemConnectionMemo().configureCommandStation(commandStationType, 099 mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect); 100 101 this.getSystemConnectionMemo().configureManagersMS100(); 102 103 // start operation 104 packets.startThreads(); 105 106 // set mode 107 LocoNetMessage msg = new LocoNetMessage(6); 108 msg.setOpCode(0xD3); 109 msg.setElement(1, 0x10); 110 msg.setElement(2, 0); // set MS100, no power 111 msg.setElement(3, 0); 112 msg.setElement(4, 0); 113 packets.sendLocoNetMessage(msg); 114 } 115 } 116 117 /** 118 * {@inheritDoc} 119 * 120 * @return String[] containing the single valid baud rate, "57,600". 121 */ 122 @Override 123 public String[] validBaudRates() { 124 return new String[]{"57,600 baud"}; // NOI18N 125 } 126 127 /** 128 * {@inheritDoc} 129 * 130 * @return int[] containing the single valid baud rate, 57600. 131 */ 132 @Override 133 public int[] validBaudNumbers() { 134 return new int[]{57600}; 135 } 136 137 @Override 138 public int defaultBaudIndex() { 139 return 0; 140 } 141 142 // Option 1 does flow control, inherited from LocoBufferAdapter 143 144 /** 145 * The DCS240 USB interface can be used as a "Standalone Programmer", or with various LocoNet 146 * command stations, or as an interface to a "Standalone LocoNet". Provide those 147 * options. 148 * 149 * @return an array of strings containing the various command station names and 150 * name(s) of modes without command stations 151 */ 152 public String[] commandStationOptions() { 153 String[] retval = new String[commandStationNames.length + 1]; 154 retval[0] = LnCommandStationType.COMMAND_STATION_DCS240.getName(); 155 retval[1] = LnCommandStationType.COMMAND_STATION_USB_DCS240_ALONE.getName(); 156 int count = 2; 157 for (String commandStationName : commandStationNames) { 158 if (!commandStationName.equals(LnCommandStationType.COMMAND_STATION_DCS240.getName())) { 159 // include all but COMMAND_STATION_DCS240, which was forced to 160 // the front of the list (above) 161 retval[count++] = commandStationName; 162 } 163 } 164 // Note: Standalone loconet does not make sense for DCS240 USB interface. 165 return retval; 166 } 167 168 @Override 169 public UsbDcs240SystemConnectionMemo getSystemConnectionMemo() { 170 LocoNetSystemConnectionMemo m = super.getSystemConnectionMemo(); 171 if (m instanceof UsbDcs240SystemConnectionMemo) { 172 return (UsbDcs240SystemConnectionMemo) m; 173 } 174 log.error("Cannot cast the system connection memo to a UsbDcs240SystemConnection Memo."); 175 return null; 176 } 177 178 private final static Logger log = LoggerFactory.getLogger(UsbDcs240Adapter.class); 179}