001package jmri.jmrix.loconet.sdf;
002
003/**
004 * Implement the SKIP_ON_TRIGGER macro from the Digitrax sound definition
005 * language
006 *
007 * @author Bob Jacobsen Copyright (C) 2007, 2008
008 */
009public class SkipOnTrigger extends SdfMacro {
010
011    public SkipOnTrigger(int byte1, int byte2) {
012        this.byte1 = byte1;
013        this.byte2 = byte2;
014        this.logic = byte1 & 0x03;
015        this.trigger = byte2;
016    }
017
018    @Override
019    public String name() {
020        return "SKIP_ON_TRIGGER"; // NOI18N
021    }
022
023    int byte1, byte2;
024
025    int logic;
026    int trigger;
027
028    @Override
029    public int length() {
030        return 2;
031    }
032
033    String logicVal() {
034        return decodeFlags(logic, trigLogicCodes, trigLogicMasks, trigLogicNames);
035    }
036
037    String triggerVal() {
038        String trigName = jmri.util.StringUtil.getNameFromState(trigger, triggerCodes, triggerNames);
039        if (trigName != null) {
040            return trigName;
041        }
042        return "(trigger = 0x" + jmri.util.StringUtil.twoHexFromInt(trigger) + ")"; // NOI18N
043    }
044
045    static public SdfMacro match(SdfBuffer buff) {
046        if ((buff.getAtIndex() & 0xFC) != 0x04) {
047            return null;
048        }
049        int byte1 = buff.getAtIndexAndInc();
050        int byte2 = buff.getAtIndexAndInc();
051        return new SkipOnTrigger(byte1, byte2);
052    }
053
054    /**
055     * Store into a buffer.
056     */
057    @Override
058    public void loadByteArray(SdfBuffer buffer) {
059        // data
060        buffer.setAtIndexAndInc(byte1);
061        buffer.setAtIndexAndInc(byte2);
062
063        // store children
064        super.loadByteArray(buffer);
065    }
066
067    @Override
068    public String toString() {
069        return "Skip on Trigger\n"; // NOI18N
070    }
071
072    @Override
073    public String oneInstructionString() {
074        return name() + ' ' + logicVal() + ", " + triggerVal() + '\n'; // NOI18N
075    }
076
077    @Override
078    public String allInstructionString(String indent) {
079        return indent + oneInstructionString();
080    }
081}