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     *
044     * @param a Array of bytes to send
045     */
046    public AcelaMessage(byte[] a) {
047        super(String.valueOf(a));
048    }
049
050    @Override
051    public String toString() {
052        StringBuilder s = new StringBuilder();
053        for (int i = 0; i < getNumDataElements(); i++) {
054            if (i != 0) {
055                s.append(" ");
056            }
057            s.append(StringUtil.twoHexFromInt(getElement(i)));
058        }
059        return s.toString();
060    }
061
062    // static methods to return a formatted message
063    // used within AcelaTrafficController to initialize Acela system
064    static public AcelaMessage getAcelaVersionMsg() {
065        AcelaMessage m = new AcelaMessage(1);
066        m.setBinary(true);
067        m.setElement(0, 0x19);
068        return m;
069    }
070
071    static public AcelaMessage getAcelaResetMsg() {
072        // create an Acela message and add initialization bytes
073        AcelaMessage m = new AcelaMessage(1);
074        m.setBinary(true);
075        m.setElement(0, 0x15);  //  Acela command to reset Acela network
076        return m;
077    }
078
079    static public AcelaMessage getAcelaOnlineMsg() {
080        // create an Acela message and add initialization bytes
081        AcelaMessage m = new AcelaMessage(1);
082        m.setBinary(true);
083        m.setElement(0, 0x16);  //  Acela command to put Acela network ONLINE
084        return m;
085    }
086
087    static public AcelaMessage getAcelaPollNodesMsg() {
088        // create an Acela message and add initialization bytes
089        AcelaMessage m = new AcelaMessage(1);
090        m.setBinary(true);
091        m.setElement(0, 0x18);  // Acela command to poll Acela network nodes
092        return m;
093    }
094
095    static public AcelaMessage getAcelaPollSensorsMsg() {
096        // create an Acela message and add initialization bytes
097        AcelaMessage m = new AcelaMessage(1);
098        m.setBinary(true);
099        m.setElement(0, 0x14);  // Acela command to poll all sensors
100        return m;
101    }
102
103    static public AcelaMessage getAcelaConfigSensorMsg() {
104        // create an Acela message and add initialization bytes
105        AcelaMessage m = new AcelaMessage(4);
106        m.setBinary(true);
107        m.setElement(0, 0x10);  // Acela command to configure one sensor
108        m.setElement(1, 0x00);  // Address
109        m.setElement(2, 0x00);  // Address
110        m.setElement(3, 0x25);  // ending bits[2,1] == 10 means IR
111        // ending bit[0] == 1 means invert output
112        // bits [15,3] == sensitivity so 0010 0 is low
113        return m;
114    }
115
116}