001package jmri.jmrix.loconet;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * A specialization of the LocoNet Throttle for Intellibox-II foibles.
008 *
009 * @author Bob Jacobsen Copyright (C) 2014
010 */
011public class Ib2Throttle extends LocoNetThrottle {
012
013    /**
014     * Constructor.
015     *
016     * @param memo system connection.
017     * @param slot The LocoNetSlot this throttle will talk on.
018     */
019    public Ib2Throttle(LocoNetSystemConnectionMemo memo, LocoNetSlot slot) {
020        super(memo, slot);
021        log.debug("Ib2Throttle created");
022    }
023
024    @Override
025    protected void sendFunctionGroup3() {
026        // Special LocoNet message for Uhlenbrock Intellibox-II implementation
027        // Intellibox-I uses another implementation for these functions
028        // Functions F9 to F12
029        int new_IB2_F9_F12 = ((getF12() ? LnConstants.RE_IB2_F12_MASK : 0)
030                | (getF11() ? LnConstants.RE_IB2_F11_MASK : 0)
031                | (getF10() ? LnConstants.RE_IB2_F10_MASK : 0)
032                | (getF9() ? LnConstants.RE_IB2_F9_MASK : 0));
033        LocoNetMessage msg = new LocoNetMessage(4);
034        msg.setOpCode(LnConstants.RE_OPC_IB2_F9_F12);
035        msg.setElement(1, slot.getSlot());
036        msg.setElement(2, new_IB2_F9_F12);
037        network.sendLocoNetMessage(msg);
038    }
039
040    @Override
041    protected void sendFunctionGroup4() {
042        // Special LocoNet message for Uhlenbrock (IB-I and IB-II) implementation
043        // Functions F13 to F19
044        int new_IB2_F13_F19 = ((getF19() ? LnConstants.RE_IB2_F19_MASK : 0)
045                | (getF18() ? LnConstants.RE_IB2_F18_MASK : 0)
046                | (getF17() ? LnConstants.RE_IB2_F17_MASK : 0)
047                | (getF16() ? LnConstants.RE_IB2_F16_MASK : 0)
048                | (getF15() ? LnConstants.RE_IB2_F15_MASK : 0)
049                | (getF14() ? LnConstants.RE_IB2_F14_MASK : 0)
050                | (getF13() ? LnConstants.RE_IB2_F13_MASK : 0));
051        LocoNetMessage msg = new LocoNetMessage(6);
052        msg.setOpCode(LnConstants.RE_OPC_IB2_SPECIAL);
053        msg.setElement(1, LnConstants.RE_IB2_SPECIAL_FUNCS_TOKEN);
054        msg.setElement(2, slot.getSlot());
055        msg.setElement(3, LnConstants.RE_IB2_SPECIAL_F13_F19_TOKEN);
056        msg.setElement(4, new_IB2_F13_F19);
057        network.sendLocoNetMessage(msg);
058
059        // Function F20 (and F28) 
060        // F12 is also controlled from this message though IB-II uses RE_OPC_IB2_F9_F12 OPS code for F12 - needed to avoid overridding F12 value
061        int new_IB2_F20_F28 = ((getF12() ? LnConstants.RE_IB2_SPECIAL_F12_MASK : 0)
062                | (getF20() ? LnConstants.RE_IB2_SPECIAL_F20_MASK : 0)
063                | (getF28() ? LnConstants.RE_IB2_SPECIAL_F28_MASK : 0));
064        LocoNetMessage msg2 = new LocoNetMessage(6);
065        msg2.setOpCode(LnConstants.RE_OPC_IB2_SPECIAL);
066        msg2.setElement(1, LnConstants.RE_IB2_SPECIAL_FUNCS_TOKEN);
067        msg2.setElement(2, slot.getSlot());
068        msg2.setElement(3, LnConstants.RE_IB2_SPECIAL_F20_F28_TOKEN);
069        msg2.setElement(4, new_IB2_F20_F28);
070        network.sendLocoNetMessage(msg2);
071    }
072
073    @Override
074    protected void sendFunctionGroup5() {
075        // Functions F21 to F27
076        int new_IB2_F21_F27 = ((getF27() ? LnConstants.RE_IB2_F27_MASK : 0)
077                | (getF26() ? LnConstants.RE_IB2_F26_MASK : 0)
078                | (getF25() ? LnConstants.RE_IB2_F25_MASK : 0)
079                | (getF24() ? LnConstants.RE_IB2_F24_MASK : 0)
080                | (getF23() ? LnConstants.RE_IB2_F23_MASK : 0)
081                | (getF22() ? LnConstants.RE_IB2_F22_MASK : 0)
082                | (getF21() ? LnConstants.RE_IB2_F21_MASK : 0));
083        LocoNetMessage msg = new LocoNetMessage(6);
084        msg.setOpCode(LnConstants.RE_OPC_IB2_SPECIAL);
085        msg.setElement(1, LnConstants.RE_IB2_SPECIAL_FUNCS_TOKEN);
086        msg.setElement(2, slot.getSlot());
087        msg.setElement(3, LnConstants.RE_IB2_SPECIAL_F21_F27_TOKEN);
088        msg.setElement(4, new_IB2_F21_F27);
089        network.sendLocoNetMessage(msg);
090
091        // Function F28 (and F20)
092        // F12 is also controlled from this message though IB-II uses RE_OPC_IB2_F9_F12 OPS code for F12 - needed to avoid overridding F12 value
093        int new_IB2_F20_F28 = ((getF12() ? LnConstants.RE_IB2_SPECIAL_F12_MASK : 0)
094                | (getF20() ? LnConstants.RE_IB2_SPECIAL_F20_MASK : 0)
095                | (getF28() ? LnConstants.RE_IB2_SPECIAL_F28_MASK : 0));
096        LocoNetMessage msg2 = new LocoNetMessage(6);
097        msg2.setOpCode(LnConstants.RE_OPC_IB2_SPECIAL);
098        msg2.setElement(1, LnConstants.RE_IB2_SPECIAL_FUNCS_TOKEN);
099        msg2.setElement(2, slot.getSlot());
100        msg2.setElement(3, LnConstants.RE_IB2_SPECIAL_F20_F28_TOKEN);
101        msg2.setElement(4, new_IB2_F20_F28);
102        network.sendLocoNetMessage(msg2);
103    }
104
105    // initialize logging
106    private final static Logger log = LoggerFactory.getLogger(Ib2Throttle.class);
107
108}