001package jmri; 002 003import java.util.EnumSet; 004 005/** 006 * DCC Speed Step Mode. 007 * 008 * <hr> 009 * This file is part of JMRI. 010 * <p> 011 * JMRI is free software; you can redistribute it and/or modify it under the 012 * terms of version 2 of the GNU General Public License as published by the Free 013 * Software Foundation. See the "COPYING" file for a copy of this license. 014 * <p> 015 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 016 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 017 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 018 * 019 * @author Austin Hendrix Copyright (C) 2019 020 * with edits/additions by 021 * @author Timothy Jump Copyright (C) 2025 022 */ 023@javax.annotation.concurrent.Immutable 024public enum SpeedStepMode { 025 // NOTE: keep these up to date with xml/schema/locomotive-config.xsd 026 UNKNOWN("unknown", 1, 0.0f, "SpeedStepUnknown"), 027 // NMRA DCC standard speed step modes. 028 NMRA_DCC_128("128", 126, "SpeedStep128"), // Remember there are only 126 non-stop values in 128 speed. 029 NMRA_DCC_28("28", 28, "SpeedStep28"), 030 NMRA_DCC_27("27", 27, "SpeedStep27"), 031 NMRA_DCC_14("14", 14, "SpeedStep14"), 032 // Non-DCC speed step modes. 033 MOTOROLA_28("motorola_28", 28, "SpeedStep28Motorola"), // Motorola 28 speed step mode. 034 TMCC1_32("tmcc1_32", 32, "SpeedStep32TMCC1"), // Lionel TMCC 1, 32 speed step mode. 035 TMCC2_32("tmcc2_32", 32, "SpeedStep32TMCC2"), // Lionel TMCC 2 Legacy, 32 speed step mode. 036 TMCC1_100("tmcc1_100", 100, "SpeedStep100TMCC1"), // Lionel TMCC ERR, 100 speed step mode. 037 TMCC2_200("tmcc2_200", 200, "SpeedStep200TMCC2"), // Lionel TMCC 2 Legacy, 200 speed step mode. 038 INCREMENTAL("incremental", 1, 1.0f, "SpeedStepIncremental"); 039 040 SpeedStepMode(String name, int numSteps, String description) { 041 this(name, numSteps, 1.0f / numSteps, description); 042 } 043 044 SpeedStepMode(String name, int numSteps, float increment, String description) { 045 this.name = name; 046 this.numSteps = numSteps; 047 this.increment = increment; 048 this.description = Bundle.getMessage(description); 049 } 050 051 public final String name; 052 053 /** 054 * The Number of steps, e.g. 126 for DCC 128 055 */ 056 public final int numSteps; 057 058 /** 059 * The increment between steps, e.g. 1 / 126 for DCC 128 060 */ 061 public final float increment; 062 public final String description; 063 064 /** 065 * Get a locale friendly Step Mode Description. 066 * For just "128" use name() 067 * @return e.g. "128 SS" 068 */ 069 @Override 070 public String toString() { 071 return description; 072 } 073 074 /** 075 * Convert a human-readable string to a DCC speed step mode. 076 * 077 * @param name string version of speed step mode; example "128" 078 * @return matching SpeedStepMode 079 * @throws IllegalArgumentException if name does not correspond to a valid speed step mode. 080 */ 081 static public SpeedStepMode getByName(String name) { 082 for (SpeedStepMode s : SpeedStepMode.values()) { 083 if (s.name.equals(name)) { 084 return s; 085 } 086 } 087 throw new IllegalArgumentException("Invalid speed step mode: " + name); 088 } 089 090 /** 091 * Convert a localized name string to a DCC speed step mode. 092 * 093 * @param name localized string version of speed step mode; example "128" 094 * @return matching SpeedStepMode 095 * @throws IllegalArgumentException if name does not correspond to a valid speed step mode. 096 */ 097 static public SpeedStepMode getByDescription(String name) { 098 for (SpeedStepMode s : SpeedStepMode.values()) { 099 if (s.description.equals(name)) { 100 return s; 101 } 102 } 103 throw new IllegalArgumentException("Invalid speed step mode: " + name); 104 } 105 106 static public EnumSet<SpeedStepMode> getCompatibleModes( 107 EnumSet<SpeedStepMode> command_station_modes, 108 EnumSet<SpeedStepMode> decoder_modes) { 109 EnumSet<SpeedStepMode> result = command_station_modes.clone(); 110 result.retainAll(decoder_modes); 111 return result; 112 } 113 114 static public SpeedStepMode bestCompatibleMode( 115 EnumSet<SpeedStepMode> command_station_modes, 116 EnumSet<SpeedStepMode> decoder_modes) { 117 EnumSet<SpeedStepMode> result = getCompatibleModes(command_station_modes, decoder_modes); 118 return bestMode(result); 119 } 120 121 static public SpeedStepMode bestMode(EnumSet<SpeedStepMode> modes) { 122 if(modes.contains(NMRA_DCC_128)) { 123 return NMRA_DCC_128; 124 } else if(modes.contains(TMCC1_32)) { 125 return TMCC1_32; 126 } else if(modes.contains(NMRA_DCC_28)) { 127 return NMRA_DCC_28; 128 } else if(modes.contains(MOTOROLA_28)) { 129 return MOTOROLA_28; 130 } else if(modes.contains(NMRA_DCC_27)) { 131 return NMRA_DCC_27; 132 } else if(modes.contains(NMRA_DCC_14)) { 133 return NMRA_DCC_14; 134 } 135 return UNKNOWN; 136 } 137 138 static public EnumSet<SpeedStepMode> getCompatibleModesForProtocol(LocoAddress.Protocol protocol) { 139 switch (protocol) { 140 case DCC: 141 case DCC_LONG: 142 case DCC_SHORT: 143 return EnumSet.of( 144 // NMRA Speed step modes. 145 SpeedStepMode.NMRA_DCC_128, 146 SpeedStepMode.NMRA_DCC_28, 147 SpeedStepMode.NMRA_DCC_27, 148 SpeedStepMode.NMRA_DCC_14, 149 // Incremental speed step mode, used by LENZ XPA 150 // XpressNet Phone Adapter. 151 SpeedStepMode.INCREMENTAL, 152 // TMCC1 mode, since some NMRA decoder models are used 153 // for TMCC1 locomotives. 154 SpeedStepMode.TMCC1_32); 155 case MFX: 156 return EnumSet.of( 157 // NMRA Speed step modes. 158 SpeedStepMode.NMRA_DCC_128, 159 SpeedStepMode.NMRA_DCC_28, 160 SpeedStepMode.NMRA_DCC_27, 161 SpeedStepMode.NMRA_DCC_14, 162 // Incremental speed step mode, used by LENZ XPA 163 // XpressNet Phone Adapter. 164 SpeedStepMode.INCREMENTAL, 165 // MFX decoders also support Motorola speed step mode. 166 SpeedStepMode.MOTOROLA_28); 167 case MOTOROLA: 168 return EnumSet.of(SpeedStepMode.MOTOROLA_28); 169 case SELECTRIX: 170 case M4: 171 case OPENLCB: 172 case LGB: 173 // No compatible speed step modes for these protocols. 174 // NOTE: these protocols only appear to be used in conjunction 175 // with ECOS. 176 break; 177 default: 178 // Unhandled case; no compatible speed step mode. 179 break; 180 } 181 return EnumSet.noneOf(SpeedStepMode.class); 182 } 183}