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