001package jmri.jmrix.tmcc;
002
003import java.util.EnumSet;
004import jmri.DccLocoAddress;
005import jmri.LocoAddress;
006import jmri.SpeedStepMode;
007import jmri.jmrix.AbstractThrottleManager;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Implementation of a TMCC ThrottleManager.
013 *
014 * @author Bob Jacobsen Copyright (C) 2001, 2006
015 * with edits/additions by
016 * @author Timothy Jump Copyright (C) 2025
017 */
018public class SerialThrottleManager extends AbstractThrottleManager {
019
020    private final TmccSystemConnectionMemo _memo;
021
022    /**
023     * Create a throttle manager.
024     *
025     * @param memo the memo for the connection this tm will use
026     */
027    public SerialThrottleManager(TmccSystemConnectionMemo memo) {
028        super(memo);
029        _memo = memo;
030        userName = "Lionel TMCC";
031    }
032
033    /**
034     * {@inheritDoc}
035     */
036    @Override
037    public void dispose() { // no listener on tc to be removed
038    }
039
040    @Override
041    public void requestThrottleSetup(LocoAddress a, boolean control) {
042        if (a instanceof DccLocoAddress ) {
043            // the protocol doesn't require an interaction with the command
044            // station for this, so immediately trigger the callback.
045            DccLocoAddress address = (DccLocoAddress) a;
046            log.debug("new TMCC throttle for {}", address);
047            notifyThrottleKnown(new SerialThrottle(_memo, address), address);
048        } else {
049            log.error("{} is not a DccLocoAddress",a);
050            failedThrottleRequest(a, "LocoAddress " +a+ " is not a DccLocoAddress");
051        }
052    }
053
054    /**
055     * Address 1 and above can be long.
056     */
057    @Override
058    public boolean canBeLongAddress(int address) {
059        return (address >= 1);
060    }
061
062    /**
063     * The full range of short addresses are available.
064     */
065    @Override
066    public boolean canBeShortAddress(int address) {
067        return (address <= 127);
068    }
069
070    /**
071     * Are there any ambiguous addresses (short vs long) on this system?
072     */
073    @Override
074    public boolean addressTypeUnique() {
075        return false;
076    }
077
078    @Override
079    public String[] getAddressTypes() {
080        return new String[]{
081            LocoAddress.Protocol.TMCC1.getPeopleName(),
082            LocoAddress.Protocol.TMCC2.getPeopleName()};
083    }
084
085    @Override
086    public LocoAddress.Protocol[] getAddressProtocolTypes() {
087        return new LocoAddress.Protocol[]{
088            LocoAddress.Protocol.TMCC1,
089            LocoAddress.Protocol.TMCC2};
090    }
091
092
093    /**
094     * What speed modes are supported by this system? value should be xor of
095     * possible modes specifed by the DccThrottle interface
096     */
097    @Override
098    public EnumSet<SpeedStepMode> supportedSpeedModes() {
099        return EnumSet.of(SpeedStepMode.TMCC1_32, SpeedStepMode.TMCC2_32, SpeedStepMode.TMCC1_100, SpeedStepMode.TMCC2_200, SpeedStepMode.TMCC1TR_32, SpeedStepMode.TMCC2TR_32, SpeedStepMode.TMCC1TR_100, SpeedStepMode.TMCC2TR_200);
100    }
101
102    private final static Logger log = LoggerFactory.getLogger(SerialThrottleManager.class);
103
104}