001package jmri.jmrix.sprog;
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 * SPROG Command Station implementation of a ThrottleManager.
013 * <p>
014 * Updated by Andrew Crosland February 2012 to enable 28 step speed packets
015 *
016 * @author Andrew Crosland Copyright (C) 2006, 2012
017 */
018public class SprogCSThrottleManager extends AbstractThrottleManager {
019
020    /**
021     * Constructor.
022     * @param memo system connection.
023     */
024    public SprogCSThrottleManager(SprogSystemConnectionMemo memo) {
025        super(memo);
026    }
027
028    @Override
029    public void requestThrottleSetup(LocoAddress a, boolean control) {
030        if (a instanceof DccLocoAddress ) {
031            // Although DCCLocoAddress not enforced in SprogCSThrottle constructor,
032            // is required in the construction.
033            
034            // The SPROG protocol doesn't require an interaction with the command
035            // station for this, so immediately trigger the callback
036            log.debug("new SprogThrottle for {}", a);
037            notifyThrottleKnown(new SprogCSThrottle((SprogSystemConnectionMemo) adapterMemo, a), a);
038        }
039        else {
040            log.error("{} is not a DccLocoAddress",a);
041            failedThrottleRequest(a, "LocoAddress " +a+ " is not a DccLocoAddress");
042        }
043    }
044
045    /**
046     * What speed modes are supported by this system? value should be or of
047     * possible modes specified by the DccThrottle interface
048     */
049    @Override
050    public EnumSet<SpeedStepMode> supportedSpeedModes() {
051        return EnumSet.of(SpeedStepMode.NMRA_DCC_128, SpeedStepMode.NMRA_DCC_28);
052    }
053
054    /**
055     * Addresses 0-10239 can be long
056     *
057     */
058    @Override
059    public boolean canBeLongAddress(int address) {
060        return ((address >= 0) && (address <= 10239));
061    }
062
063    /**
064     * The short addresses 1-127 are available
065     *
066     */
067    @Override
068    public boolean canBeShortAddress(int address) {
069        return ((address >= 1) && (address <= 127));
070    }
071
072    /**
073     * Are there any ambiguous addresses (short vs long) on this system?
074     */
075    @Override
076    public boolean addressTypeUnique() {
077        return false;
078    }
079
080    @Override
081    public boolean disposeThrottle(jmri.DccThrottle t, jmri.ThrottleListener l) {
082        if (super.disposeThrottle(t, l)) {
083            if (t instanceof SprogCSThrottle) {
084                SprogCSThrottle lnt = (SprogCSThrottle) t;
085                lnt.throttleDispose();
086                return true;
087            }
088        }
089        return false;
090    }
091
092    private final static Logger log = LoggerFactory.getLogger(SprogCSThrottleManager.class);
093
094}