001package jmri.jmrix.acela;
002
003import jmri.util.StringUtil;
004
005/**
006 * Contains the data payload of an Acela packet.
007 *
008 * @author Bob Jacobsen Copyright (C) 2001,2003
009 * @author Bob Coleman Copyright (C) 2007, 2008 Based on CMRI serial example,
010 * modified to establish Acela support.
011 */
012public class AcelaMessage extends jmri.jmrix.AbstractMRMessage {
013    // is this logically an abstract class?
014
015    final static int POLL_TIMEOUT = 250;
016
017    public AcelaMessage() {
018        super();
019    }
020
021    // create a new one
022    public AcelaMessage(int i) {
023        super(i);
024    }
025
026    // copy one
027    public AcelaMessage(AcelaMessage m) {
028        super(m);
029    }
030
031    /**
032     * This ctor interprets the String as the exact sequence to send,
033     * byte-for-byte.
034     *
035     * @param m string form of message.
036     */
037    public AcelaMessage(String m) {
038        super(m);
039    }
040
041    /**
042     * This ctor interprets the byte array as a sequence of characters to send.
043     * @deprecated 5.13.5, unused, requires further development.
044     * @param a Array of bytes to send
045     */
046    @Deprecated( since="5.13.5", forRemoval=true)
047    public AcelaMessage(byte[] a) {
048        // super(String.valueOf(a)); // Spotbug toString on array
049        // requires further development to produce correct values for hardware type.
050        super(StringUtil.hexStringFromBytes(a).replaceAll("\\s", ""));
051    }
052
053    @Override
054    public String toString() {
055        StringBuilder s = new StringBuilder();
056        for (int i = 0; i < getNumDataElements(); i++) {
057            if (i != 0) {
058                s.append(" ");
059            }
060            s.append(StringUtil.twoHexFromInt(getElement(i)));
061        }
062        return s.toString();
063    }
064
065    // static methods to return a formatted message
066    // used within AcelaTrafficController to initialize Acela system
067    static public AcelaMessage getAcelaVersionMsg() {
068        AcelaMessage m = new AcelaMessage(1);
069        m.setBinary(true);
070        m.setElement(0, 0x19);
071        return m;
072    }
073
074    static public AcelaMessage getAcelaResetMsg() {
075        // create an Acela message and add initialization bytes
076        AcelaMessage m = new AcelaMessage(1);
077        m.setBinary(true);
078        m.setElement(0, 0x15);  //  Acela command to reset Acela network
079        return m;
080    }
081
082    static public AcelaMessage getAcelaOnlineMsg() {
083        // create an Acela message and add initialization bytes
084        AcelaMessage m = new AcelaMessage(1);
085        m.setBinary(true);
086        m.setElement(0, 0x16);  //  Acela command to put Acela network ONLINE
087        return m;
088    }
089
090    static public AcelaMessage getAcelaPollNodesMsg() {
091        // create an Acela message and add initialization bytes
092        AcelaMessage m = new AcelaMessage(1);
093        m.setBinary(true);
094        m.setElement(0, 0x18);  // Acela command to poll Acela network nodes
095        return m;
096    }
097
098    static public AcelaMessage getAcelaPollSensorsMsg() {
099        // create an Acela message and add initialization bytes
100        AcelaMessage m = new AcelaMessage(1);
101        m.setBinary(true);
102        m.setElement(0, 0x14);  // Acela command to poll all sensors
103        return m;
104    }
105
106    static public AcelaMessage getAcelaConfigSensorMsg() {
107        // create an Acela message and add initialization bytes
108        AcelaMessage m = new AcelaMessage(4);
109        m.setBinary(true);
110        m.setElement(0, 0x10);  // Acela command to configure one sensor
111        m.setElement(1, 0x00);  // Address
112        m.setElement(2, 0x00);  // Address
113        m.setElement(3, 0x25);  // ending bits[2,1] == 10 means IR
114        // ending bit[0] == 1 means invert output
115        // bits [15,3] == sensitivity so 0010 0 is low
116        return m;
117    }
118
119}