001package jmri.jmrix.maple;
002
003import jmri.implementation.AbstractLight;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * Implementation of the Light Object for C/MRI
009 * <p>
010 * Based in part on SerialTurnout.java
011 *
012 * @author Dave Duchamp Copyright (C) 2004, 2009, 2010
013 */
014public class SerialLight extends AbstractLight {
015
016    private MapleSystemConnectionMemo _memo = null;
017
018    /**
019     * Create a Light object, with only system name.
020     * <p>
021     * 'systemName' has already been validated in SerialLightManager
022     *
023     * @param systemName the system name for this Light
024     * @param memo system connection.
025     */
026    public SerialLight(String systemName, MapleSystemConnectionMemo memo) {
027        super(systemName);
028        _memo = memo;
029        // Initialize the Light
030        initializeLight(systemName);
031    }
032
033    /**
034     * Create a Light object, with both system and user names.
035     * <p>
036     * 'systemName' has already been validated in SerialLightManager
037     *
038     * @param systemName the system name for this Light
039     * @param userName   the user name for this Light
040     * @param memo system connection.
041     */
042    public SerialLight(String systemName, String userName, MapleSystemConnectionMemo memo) {
043        super(systemName, userName);
044        _memo = memo;
045        // Initialize the Light
046        initializeLight(systemName);
047    }
048
049    /**
050     * Sets up system dependent instance variables and sets system independent
051     * instance variables to default values Note: most instance variables are in
052     * AbstractLight.java
053     */
054    private void initializeLight(String systemName) {
055        // Extract the Bit from the name
056        mBit = SerialAddress.getBitFromSystemName(systemName, _memo.getSystemPrefix());
057        // Set initial state
058        setState(OFF);
059    }
060
061    /**
062     * System dependent instance variables
063     */
064    int mBit = 0; // bit within the node
065
066    /**
067     * Set the current state of this Light This routine requests the hardware to
068     * change. If this is really a change in state of this bit (tested in
069     * SerialNode), a Transmit packet will be sent before this Node is next
070     * polled.
071     */
072    @Override
073    protected void doNewState(int oldState, int newState) {
074        if (newState == ON) {
075            _memo.getTrafficController().outputBits().setOutputBit(mBit, false);
076        } else if (newState == OFF) {
077            _memo.getTrafficController().outputBits().setOutputBit(mBit, true);
078        } else {
079            log.warn("illegal state requested for Light: {}", getSystemName());
080        }
081    }
082
083    private final static Logger log = LoggerFactory.getLogger(SerialLight.class);
084
085}