001package jmri.jmrix.anyma;
002
003import jmri.implementation.AbstractVariableLight;
004import jmri.util.MathUtil;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * AnymaDMX_UsbLight.java
010 * <p>
011 * Implementation of the Light Object for anyma dmx
012 *
013 * @author George Warner Copyright (c) 2017-2018
014 * @since 4.9.6
015 */
016public class AnymaDMX_UsbLight extends AbstractVariableLight {
017
018    private AnymaDMX_SystemConnectionMemo _memo = null;
019
020    /**
021     * Create a Light object, with only system name.
022     *
023     * @param systemName the system name (previously validated)
024     * @param memo       the system memo
025     */
026    public AnymaDMX_UsbLight(String systemName, AnymaDMX_SystemConnectionMemo memo) {
027        super(systemName);
028        log.debug("*    UsbLight constructor called");
029        _memo = memo;
030        // Initialize the Light
031        initializeLight();
032    }
033
034    /**
035     * Create a Light object, with both system and user names.
036     *
037     * @param systemName the system name (previously validated)
038     * @param userName   the user name
039     * @param memo       the system memo
040     */
041    public AnymaDMX_UsbLight(String systemName,
042            String userName, AnymaDMX_SystemConnectionMemo memo) {
043        super(systemName, userName);
044        log.debug("*    UsbLight constructor called");
045        _memo = memo;
046        initializeLight();
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() {
055        log.debug("*    UsbLight.initializeLight() called");
056        // Extract the Channel from the name
057        mChannel = _memo.getChannelFromSystemName(getSystemName());
058        // Set initial state
059        setState(OFF);
060    }
061
062    /**
063     * System dependent instance variables
064     */
065    private int mChannel = 0;                // channel within the node (1-512)
066
067    /**
068     * {@inheritDoc}
069     */
070     @Override
071    protected void doNewState(int oldState, int newState) {
072        log.debug("*    UsbLight.doNewState({}, {}) called", oldState, newState);
073        AnymaDMX_TrafficController trafficController = _memo.getTrafficController();
074        if (trafficController != null) {
075            if (newState == ON) {
076                trafficController.setChannelValue(mChannel, (byte) 0xFF);
077            } else if (newState == OFF) {
078                trafficController.setChannelValue(mChannel, (byte) 0x00);
079            } else {
080                log.warn("illegal state requested for Light: {}", getSystemName());
081            }
082        }
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088     @Override
089     protected void sendIntensity(double intensity) {
090        log.debug("*    sendIntensity({})", "" + intensity);
091        AnymaDMX_TrafficController trafficController = _memo.getTrafficController();
092        if (trafficController != null) {
093            byte value = (byte) MathUtil.pin(intensity * 255.0, 0.0, 255.0);
094            trafficController.setChannelValue(mChannel, value);
095        }
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101     @Override
102     protected void sendOnOffCommand(int newState) {
103        log.debug("*    sendOnOffCommand({})", newState);
104        AnymaDMX_TrafficController trafficController = _memo.getTrafficController();
105        if (trafficController != null) {
106            if (newState == ON) {
107                trafficController.setChannelValue(mChannel, (byte) 0xFF);
108            } else if (newState == OFF) {
109                trafficController.setChannelValue(mChannel, (byte) 0x00);
110            } else {
111                log.warn("illegal state requested for Light: {}", getSystemName());
112            }
113        }
114    }
115
116    /**
117     * {@inheritDoc}
118     */
119     @Override
120     protected int getNumberOfSteps() {
121        return 256;
122    }
123
124    private final static Logger log
125            = LoggerFactory.getLogger(AnymaDMX_UsbLight.class);
126}