001package jmri.jmrix.lenz.messageformatters;
002
003
004/**
005 * Utility methods for parsing the Loco Info Reply messages.
006 *
007 * @author Paul Bender Copyright (C) 2025
008 */
009public class XNetLocoInfoReplyUtilities {
010
011    private static final String POWER_STATE_ON = "PowerStateOn";
012    private static final String POWER_STATE_OFF = "PowerStateOff";
013    private static final String SPEED_STEP_MODE_X = "SpeedStepModeX";
014
015    /**
016     * Parse the speed step and the direction information for a locomotive.
017     *
018     * @param element1 contains the speed step mode designation and
019     * availability information
020     * @param element2 contains the data byte including the step mode and
021     * availability information
022     * @return readable version of message
023     */
024    public static String parseSpeedAndDirection(int element1, int element2) {
025        String text = "";
026        int speedVal;
027        if ((element2 & 0x80) == 0x80) {
028            text += Bundle.getMessage("Forward") + ",";
029        } else {
030            text += Bundle.getMessage("Reverse") + ",";
031        }
032
033        if ((element1 & 0x04) == 0x04) {
034            // We're in 128 speed step mode
035            speedVal = element2 & 0x7f;
036            // The first speed step used is actually at 2 for 128
037            // speed step mode.
038            if (speedVal >= 1) {
039                speedVal -= 1;
040            } else {
041                speedVal = 0;
042            }
043            text += Bundle.getMessage(SPEED_STEP_MODE_X, 128) + ",";
044        } else if ((element1 & 0x02) == 0x02) {
045            // We're in 28 speed step mode
046            // We have to re-arange the bits, since bit 4 is the LSB,
047            // but other bits are in order from 0-3
048            speedVal = ((element2 & 0x0F) << 1) + ((element2 & 0x10) >> 4);
049            // The first speed step used is actually at 4 for 28
050            // speed step mode.
051            if (speedVal >= 3) {
052                speedVal -= 3;
053            } else {
054                speedVal = 0;
055            }
056            text += Bundle.getMessage(SPEED_STEP_MODE_X, 28) + ",";
057        } else if ((element1 & 0x01) == 0x01) {
058            // We're in 27 speed step mode
059            // We have to re-arange the bits, since bit 4 is the LSB,
060            // but other bits are in order from 0-3
061            speedVal = ((element2 & 0x0F) << 1) + ((element2 & 0x10) >> 4);
062            // The first speed step used is actually at 4 for 27
063            // speed step mode.
064            if (speedVal >= 3) {
065                speedVal -= 3;
066            } else {
067                speedVal = 0;
068            }
069            text += Bundle.getMessage(SPEED_STEP_MODE_X, 27) + ",";
070        } else {
071            // Assume we're in 14 speed step mode.
072            speedVal = (element2 & 0x0F);
073            if (speedVal >= 1) {
074                speedVal -= 1;
075            } else {
076                speedVal = 0;
077            }
078            text += Bundle.getMessage(SPEED_STEP_MODE_X, 14) + ",";
079        }
080
081        text += Bundle.getMessage("SpeedStepLabel") + " " + speedVal + ". ";
082
083        if ((element1 & 0x08) == 0x08) {
084            text += "" + Bundle.getMessage("XNetReplyAddressInUse");
085        } else {
086            text += "" + Bundle.getMessage("XNetReplyAddressFree");
087        }
088        return (text);
089    }
090
091    /**
092     * Parse the status of functions F0-F12.
093     *
094     * @param element3 contains the data byte including F0,F1,F2,F3,F4
095     * @param element4 contains F12,F11,F10,F9,F8,F7,F6,F5
096     * @return readable version of message
097     */
098    public static String parseFunctionStatus(int element3, int element4) {
099        String text = "";
100        if ((element3 & 0x10) != 0) {
101            text += "F0 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
102        } else {
103            text += "F0 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
104        }
105        if ((element3 & 0x01) != 0) {
106            text += "F1 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
107        } else {
108            text += "F1 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
109        }
110        if ((element3 & 0x02) != 0) {
111            text += "F2 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
112        } else {
113            text += "F2 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
114        }
115        if ((element3 & 0x04) != 0) {
116            text += "F3 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
117        } else {
118            text += "F3 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
119        }
120        if ((element3 & 0x08) != 0) {
121            text += "F4 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
122        } else {
123            text += "F4 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
124        }
125        if ((element4 & 0x01) != 0) {
126            text += "F5 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
127        } else {
128            text += "F5 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
129        }
130        if ((element4 & 0x02) != 0) {
131            text += "F6 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
132        } else {
133            text += "F6 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
134        }
135        if ((element4 & 0x04) != 0) {
136            text += "F7 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
137        } else {
138            text += "F7 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
139        }
140        if ((element4 & 0x08) != 0) {
141            text += "F8 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
142        } else {
143            text += "F8 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
144        }
145        if ((element4 & 0x10) != 0) {
146            text += "F9 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
147        } else {
148            text += "F9 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
149        }
150        if ((element4 & 0x20) != 0) {
151            text += "F10 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
152        } else {
153            text += "F10 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
154        }
155        if ((element4 & 0x40) != 0) {
156            text += "F11 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
157        } else {
158            text += "F11 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
159        }
160        if ((element4 & 0x80) != 0) {
161            text += "F12 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
162        } else {
163            text += "F12 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
164        }
165        return (text);
166    }
167
168    /**
169     * Parse the status of functions F13-F28.
170     *
171     * @param element3 contains F20,F19,F18,F17,F16,F15,F14,F13
172     * @param element4 contains F28,F27,F26,F25,F24,F23,F22,F21
173     * @return readable version of message
174     */
175    public static String parseFunctionHighStatus(int element3, int element4) {
176        String text = "";
177        if ((element3 & 0x01) != 0) {
178            text += "F13 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
179        } else {
180            text += "F13 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
181        }
182        if ((element3 & 0x02) != 0) {
183            text += "F14 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
184        } else {
185            text += "F14 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
186        }
187        if ((element3 & 0x04) != 0) {
188            text += "F15 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
189        } else {
190            text += "F15 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
191        }
192        if ((element3 & 0x08) != 0) {
193            text += "F16 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
194        } else {
195            text += "F16 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
196        }
197        if ((element3 & 0x10) != 0) {
198            text += "F17 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
199        } else {
200            text += "F17 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
201        }
202        if ((element3 & 0x20) != 0) {
203            text += "F18 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
204        } else {
205            text += "F18 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
206        }
207        if ((element3 & 0x40) != 0) {
208            text += "F19 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
209        } else {
210            text += "F19 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
211        }
212        if ((element3 & 0x80) != 0) {
213            text += "F20 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
214        } else {
215            text += "F20 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
216        }
217        if ((element4 & 0x01) != 0) {
218            text += "F21 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
219        } else {
220            text += "F21 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
221        }
222        if ((element4 & 0x02) != 0) {
223            text += "F22 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
224        } else {
225            text += "F22 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
226        }
227        if ((element4 & 0x04) != 0) {
228            text += "F23 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
229        } else {
230            text += "F23 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
231        }
232        if ((element4 & 0x08) != 0) {
233            text += "F24 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
234        } else {
235            text += "F24 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
236        }
237        if ((element4 & 0x10) != 0) {
238            text += "F25 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
239        } else {
240            text += "F25 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
241        }
242        if ((element4 & 0x20) != 0) {
243            text += "F26 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
244        } else {
245            text += "F26 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
246        }
247        if ((element4 & 0x40) != 0) {
248            text += "F27 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
249        } else {
250            text += "F27 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
251        }
252        if ((element4 & 0x80) != 0) {
253            text += "F28 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
254        } else {
255            text += "F28 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
256        }
257        return (text);
258    }
259}