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 */
016public class SerialThrottleManager extends AbstractThrottleManager {
017
018    private final TmccSystemConnectionMemo _memo;
019
020    /**
021     * Create a throttle manager.
022     *
023     * @param memo the memo for the connection this tm will use
024     */
025    public SerialThrottleManager(TmccSystemConnectionMemo memo) {
026        super(memo);
027        _memo = memo;
028        userName = "Lionel TMCC";
029    }
030
031    /**
032     * {@inheritDoc}
033     */
034    @Override
035    public void dispose() { // no listener on tc to be removed
036    }
037
038    @Override
039    public void requestThrottleSetup(LocoAddress a, boolean control) {
040        if (a instanceof DccLocoAddress ) {
041            // the protocol doesn't require an interaction with the command
042            // station for this, so immediately trigger the callback.
043            DccLocoAddress address = (DccLocoAddress) a;
044            log.debug("new TMCC throttle for {}", address);
045            notifyThrottleKnown(new SerialThrottle(_memo, address), address);
046        } else {
047            log.error("{} is not a DccLocoAddress",a);
048            failedThrottleRequest(a, "LocoAddress " +a+ " is not a DccLocoAddress");
049        }
050    }
051
052    /**
053     * Address 1 and above can be long.
054     */
055    @Override
056    public boolean canBeLongAddress(int address) {
057        return (address >= 1);
058    }
059
060    /**
061     * The full range of short addresses are available.
062     */
063    @Override
064    public boolean canBeShortAddress(int address) {
065        return (address <= 127);
066    }
067
068    /**
069     * Are there any ambiguous addresses (short vs long) on this system?
070     */
071    @Override
072    public boolean addressTypeUnique() {
073        return false;
074    }
075
076    /**
077     * What speed modes are supported by this system? value should be xor of
078     * possible modes specifed by the DccThrottle interface
079     */
080    @Override
081    public EnumSet<SpeedStepMode> supportedSpeedModes() {
082        return EnumSet.of(SpeedStepMode.TMCC_32, SpeedStepMode.TMCC_200);
083    }
084
085    private final static Logger log = LoggerFactory.getLogger(SerialThrottleManager.class);
086
087}