001package jmri.jmrix.powerline.cp290;
002
003/**
004 * Constants and functions specific to the CP290 interface
005 *
006 * @author Bob Jacobsen Copyright (C) 2008
007 * @author Ken Cameron, (C) 2009, add sensors from poll replies
008 */
009public class Constants {
010
011    public static final int CMD_ON = 0x02;
012    public static final int CMD_OFF = 0x03;
013    public static final int CMD_DIM_R = 0x04;
014    public static final int CMD_DIM_S = 0x05;
015
016    /**
017     * Translate Function to Text
018     * @param cmd   cmd value
019     * @param level dim value
020     * @return      formated command as text
021     */
022    public static String commandToText(int cmd, int level) {
023        String cmdText = "";
024        switch (cmd) {
025            case CMD_ON:
026                cmdText = "ON";
027                break;
028            case CMD_OFF:
029                cmdText = "OFF";
030                break;
031            case CMD_DIM_R:
032                cmdText = "recvDIM";
033                cmdText = cmdText + " ";
034                break;
035            case CMD_DIM_S:
036                cmdText = "sendDIM";
037                int v2 = (level * 100) / 16;
038                cmdText = cmdText + " " + v2 + "%";
039                break;
040            default:
041                cmdText = "Unk Cmd: " + cmd;
042                break;
043        }
044        return (cmdText);
045    }
046
047    /**
048     * Translate House Code to text
049     * @param hC housecode value
050     * @return   housecode as text
051     */
052    public static String houseCodeToText(int hC) {
053        String hCode = "";
054        switch (hC) {
055            case 0x06:
056                hCode = "A";
057                break;
058            case 0x0E:
059                hCode = "B";
060                break;
061            case 0x02:
062                hCode = "C";
063                break;
064            case 0x0A:
065                hCode = "D";
066                break;
067            case 0x01:
068                hCode = "E";
069                break;
070            case 0x09:
071                hCode = "F";
072                break;
073            case 0x05:
074                hCode = "G";
075                break;
076            case 0x0D:
077                hCode = "H";
078                break;
079            case 0x07:
080                hCode = "I";
081                break;
082            case 0x0F:
083                hCode = "J";
084                break;
085            case 0x03:
086                hCode = "K";
087                break;
088            case 0x0B:
089                hCode = "L";
090                break;
091            case 0x00:
092                hCode = "M";
093                break;
094            case 0x08:
095                hCode = "N";
096                break;
097            case 0x04:
098                hCode = "O";
099                break;
100            case 0x0C:
101                hCode = "P";
102                break;
103            default:
104                hCode = "Unk hC:" + hC;
105                break;
106        }
107        return hCode;
108    }
109
110    /**
111     * Translate Device Bits to Text
112     * @param hByte high byte
113     * @param lByte low byte
114     * @return      text version of bytes
115     */
116    public static String deviceToText(int hByte, int lByte) {
117        int mask = 0x01;
118        int x = lByte;
119        StringBuilder dev = new StringBuilder();
120        for (int i = 8; i > 0; i--) {
121            if ((x & mask) != 0) {
122                dev.append(" " + i);
123            }
124            mask = mask << 1;
125        }
126        mask = 0x01;
127        x = hByte;
128        for (int i = 16; i > 8; i--) {
129            if ((x & mask) != 0) {
130                dev.append(" " + i);
131            }
132            mask = mask << 1;
133        }
134        return dev.toString();
135    }
136
137    /**
138     * Translate status to text
139     * @param s status value
140     * @return  status value as text
141     */
142    public static String statusToText(int s) {
143        String stat = "";
144        switch (s) {
145            case 0:
146                stat = "Interface Powered Off";
147                break;
148            case 1:
149                stat = "Cmd Ok";
150                break;
151            default:
152                stat = "Unk Status: " + s;
153                break;
154        }
155        return (stat);
156    }
157
158    /**
159     * Format a message nicely
160     * @param m message
161     * @return  message contents as text
162     */
163    public static String toMonitorString(jmri.jmrix.Message m) {
164        // check for valid length
165        String val = "???";
166        int len = m.getNumDataElements();
167        boolean goodSync = true;
168        boolean goodCheckSum = true;
169        int sum = 0;
170        String cmd;
171        String stat;
172        String hCode;
173        String bCode;
174        String dev;
175        switch (len) {
176            case 7:
177                for (int i = 0; i < 6; i++) {
178                    if ((m.getElement(i) & 0xFF) != 0xFF) {
179                        goodSync = false;
180                    }
181                }
182                val = statusToText(m.getElement(6));
183                break;
184            case 12:
185                for (int i = 0; i < 6; i++) {
186                    if ((m.getElement(i) & 0xFF) != 0xFF) {
187                        goodSync = false;
188                    }
189                }
190                for (int i = 7; i < 11; i++) {
191                    sum = (sum + (m.getElement(i) & 0xFF)) & 0xFF;
192                }
193                stat = statusToText(m.getElement(6));
194                cmd = commandToText(m.getElement(7) & 0x0F, -1);
195                hCode = houseCodeToText((m.getElement(7) >> 4) & 0x0F);
196                dev = deviceToText(m.getElement(8), m.getElement(9));
197                bCode = houseCodeToText((m.getElement(10) >> 4) & 0x0F);
198                if (sum != (m.getElement(11) & 0xFF)) {
199                    goodCheckSum = false;
200                }
201                val = "Cmd Echo: " + cmd + " stat: " + stat + " House: " + hCode + " Device:" + dev + " base: " + bCode;
202                if (!goodSync) {
203                    val = val + " BAD SYNC";
204                }
205                if (!goodCheckSum) {
206                    val = val + " BAD CHECKSUM: " + (m.getElement(11) & 0xFF) + " vs " + sum;
207                }
208                break;
209            case 22:
210                for (int i = 0; i < 16; i++) {
211                    if ((m.getElement(i) & 0xFF) != 0xFF) {
212                        goodSync = false;
213                    }
214                }
215                for (int i = 17; i < 21; i++) {
216                    sum = (sum + (m.getElement(i) & 0xFF)) & 0xFF;
217                }
218                cmd = commandToText((m.getElement(17) & 0x0F), ((m.getElement(17) & 0xF0) >> 4));
219                hCode = houseCodeToText((m.getElement(18) >> 4) & 0x0F);
220                dev = deviceToText(m.getElement(19), m.getElement(20));
221                if (sum != (m.getElement(21) & 0xFF)) {
222                    goodCheckSum = false;
223                }
224                val = cmd + " House: " + hCode + " Device:" + dev;
225                if (!goodSync) {
226                    val = val + " BAD SYNC";
227                }
228                if (!goodCheckSum) {
229                    val = val + " BAD CHECKSUM: " + (m.getElement(21) & 0xFF) + " vs " + sum;
230                }
231                break;
232            default:
233                val = "UNK " + m.toString();
234                break;
235        }
236        return val;
237    }
238
239}