001package jmri.jmrix.zimo;
002
003import jmri.Turnout;
004import jmri.implementation.AbstractTurnout;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * New Zimo Binary implementation of the Turnout interface
010 *
011 * @author Kevin Dickerson Copyright (C) 2014
012 */
013public class Mx1Turnout extends AbstractTurnout /*implements Mx1TrafficListener*/ {
014
015    // Private data member to keep track of what turnout we control.
016    int _number;
017    private final Mx1TrafficController tc;
018
019    /**
020     * Mx1 turnouts use any address allowed as an accessory decoder address on
021     * the particular command station.
022     *
023     * @param number the address
024     * @param tc     the controller for traffic to the layout
025     * @param p      the system connection prefix
026     */
027    public Mx1Turnout(int number, Mx1TrafficController tc, String p) {
028        super(p + "T" + number);
029        _number = number;
030        this.tc = tc;
031        //tc.addMx1Listener(Mx1Interface.TURNOUTS, null);
032    }
033
034    public int getNumber() {
035        return _number;
036    }
037
038    /**
039     * {@inheritDoc}
040     * Sends a formatted DCC packet
041     */
042    @Override
043    protected void forwardCommandChangeToLayout(int newState) {
044        // sort out states
045        if ((newState & Turnout.CLOSED) != 0) {
046            // first look for the double case, which we can't handle
047            if ((newState & Turnout.THROWN) != 0) {
048                // this is the disaster case!
049                log.error("Cannot command both CLOSED and THROWN {}", newState); // NOI18N
050            } else {
051                // send a CLOSED command
052                forwardToCommandStation(jmri.Turnout.THROWN);
053            }
054        } else {
055            // send a THROWN command
056            forwardToCommandStation(jmri.Turnout.CLOSED);
057        }
058    }
059
060    void forwardToCommandStation(int state) {
061        Mx1Message m = Mx1Message.getSwitchMsg(_number, state, true);
062        tc.sendMx1Message(m, null);
063    }
064
065    @Override
066    protected void turnoutPushbuttonLockout(boolean pushButtonLockout) {
067    }
068
069    private final static Logger log = LoggerFactory.getLogger(Mx1Turnout.class);
070
071}