001package jmri.jmrix.zimo;
002
003import javax.annotation.Nonnull;
004
005/**
006 * Defines standard operations for Dcc command stations.
007 *
008 * @author Bob Jacobsen Copyright (C) 2001
009 *
010 * Adapted by Sip Bosch for use with Zimo Mx-1
011 *
012 */
013public class Mx1CommandStation implements jmri.CommandStation {
014
015    public Mx1CommandStation(String systemName, String userName) {
016        this.systemName = systemName;
017        this.userName = userName;
018    }
019 
020     public Mx1CommandStation(String systemName) {
021        this(systemName, "MX-1");
022    }
023   
024     // not multi-connection safe
025     public Mx1CommandStation() {
026        this("Z", "MX-1");
027    }
028   
029    String systemName;
030    String userName;
031    
032    /**
033     * {@inheritDoc}
034     * <p>
035     * This implementation always returns false, as sending
036     * a packet isn't implemented for the Zimo command stations
037     */
038    @Override
039    public boolean sendPacket(@Nonnull byte[] packet, int repeats) { return false; }
040
041    @Override
042    public String getUserName() {return userName;}
043
044    @Override
045    public String getSystemPrefix() {return systemName;}
046
047    public Mx1Message resetModeMsg() {
048        Mx1Message m = new Mx1Message(3);
049        m.setElement(0, 0x53);
050        m.setElement(1, 0x45);
051        return m;
052    }
053
054    public Mx1Message getReadPagedCVMsg(int cv) {
055        Mx1Message m = new Mx1Message(4);
056        // break down into two bytes
057        int cvhigh = (cv & 0xF0) / 16;
058        int cvlow = cv & 0x0F;
059        // built message
060        m.setElement(0, 0x51);
061        m.setElement(1, bcdToAsc(cvhigh));
062        m.setElement(2, bcdToAsc(cvlow));
063        return m;
064    }
065
066    public Mx1Message getWritePagedCVMsg(int cv, int val) {
067        Mx1Message m = new Mx1Message(7);
068        // break down into two bytes
069        int cvHigh = (cv & 0xF0) / 16;
070        int cvLow = cv & 0x0F;
071        int valHigh = (val & 0xF0) / 16;
072        int valLow = val & 0x0F;
073        // built message
074        m.setElement(0, 0x52);
075        m.setElement(1, 0x4E);
076        m.setElement(2, bcdToAsc(cvHigh));
077        m.setElement(3, bcdToAsc(cvLow));
078        m.setElement(4, bcdToAsc(valHigh));
079        m.setElement(5, bcdToAsc(valLow));
080        return m;
081    }
082
083    public int bcdToAsc(int hex) {
084        switch (hex) {
085            case 0x0F:
086                return 0x46;
087            case 0x0E:
088                return 0x65;
089            case 0x0D:
090                return 0x44;
091            case 0x0C:
092                return 0x43;
093            case 0x0B:
094                return 0x42;
095            case 0x0A:
096                return 0x41;
097            case 0x09:
098                return 0x39;
099            case 0x08:
100                return 0x38;
101            case 0x07:
102                return 0x37;
103            case 0x06:
104                return 0x36;
105            case 0x05:
106                return 0x35;
107            case 0x04:
108                return 0x34;
109            case 0x03:
110                return 0x33;
111            case 0x02:
112                return 0x32;
113            case 0x01:
114                return 0x31;
115            default:
116                return 0x30;
117        }
118    }
119
120    // private final static Logger log = LoggerFactory.getLogger(Mx1CommandStation.class);
121}