001package jmri.jmrix.loconet.sdf;
002
003/**
004 * Implement generic four-byte macros from the Digitrax sound definition
005 * language
006 *
007 * @author Bob Jacobsen Copyright (C) 2007
008 */
009public class FourByteMacro extends SdfMacro {
010
011    public FourByteMacro(int byte1, int byte2, int byte3, int byte4) {
012        bytes[0] = (byte) (byte1 & 0xFF);
013        bytes[1] = (byte) (byte2 & 0xFF);
014        bytes[2] = (byte) (byte3 & 0xFF);
015        bytes[3] = (byte) (byte4 & 0xFF);
016    }
017
018    @Override
019    public String name() {
020        return "Four Byte Macro"; // NOI18N
021    }
022
023    byte[] bytes = new byte[4];
024
025    @Override
026    public int length() {
027        return 4;
028    }
029
030    static public SdfMacro match(SdfBuffer buff) {
031        // course match
032        if ((buff.getAtIndex() & 0xFF) < 0xE0) {
033            return null;
034        }
035        return new FourByteMacro(buff.getAtIndexAndInc(),
036                buff.getAtIndexAndInc(),
037                buff.getAtIndexAndInc(),
038                buff.getAtIndexAndInc());
039    }
040
041    /**
042     * Store into a buffer.
043     */
044    @Override
045    public void loadByteArray(SdfBuffer buffer) {
046        // data
047        buffer.setAtIndexAndInc(bytes[0]);
048        buffer.setAtIndexAndInc(bytes[1]);
049        buffer.setAtIndexAndInc(bytes[2]);
050        buffer.setAtIndexAndInc(bytes[3]);
051
052        // store children
053        super.loadByteArray(buffer);
054    }
055
056    @Override
057    public String toString() {
058        return name() + ' ' + jmri.util.StringUtil.hexStringFromBytes(bytes) + '\n';
059    }
060
061    @Override
062    public String oneInstructionString() {
063        return name() + ' ' + jmri.util.StringUtil.hexStringFromBytes(bytes) + '\n';
064    }
065
066    @Override
067    public String allInstructionString(String indent) {
068        return indent + oneInstructionString();
069    }
070}