001package jmri.jmrix.zimo;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import javax.annotation.Nonnull;
007
008import jmri.*;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Programmer support for Zimo MXULF operations mode.  
015 * Provide an Ops Mode Programmer via a wrapper that works with the
016 * MX1Programmer object.
017 * <p>
018 * Functionally, this just creates packets to send via the MXULF.
019 *
020 * @see jmri.Programmer
021 * @author Bob Jacobsen Copyright (c) 2002
022 *
023 * Adapted by
024 * @author Alger Pike Copyright (c) 2022
025 * for use with zimo MXULF
026 *
027 */
028public class Mx1OpsModeProgrammer extends Mx1Programmer implements AddressedProgrammer {
029
030    int mAddress;
031    boolean mLongAddr;
032
033     public Mx1OpsModeProgrammer(int pAddress, boolean pLongAddr, Mx1TrafficController tc) {
034        super(tc);
035        mAddress = pAddress;
036        mLongAddr = pLongAddr;
037    }
038
039    /**
040     * {@inheritDoc}
041     *
042     * Types implemented here.
043     */
044    @Override
045    @Nonnull
046    public List<ProgrammingMode> getSupportedModes() {
047        List<ProgrammingMode> ret = new ArrayList<ProgrammingMode>();
048        ret.add(ProgrammingMode.OPSBYTEMODE);
049        return ret;
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    synchronized public void writeCV(String CVname, int val, jmri.ProgListener p) throws jmri.ProgrammerException {
057        final int CV = Integer.parseInt(CVname);
058        if (log.isDebugEnabled()) {
059            log.debug("writeCV {} listens {}", CV, p);
060        }
061        useProgrammer(p);
062        _progRead = false;
063        // set new state & save values
064        progState = INQUIRESENT;
065        _val = val;
066        _cv = CV;
067        // start the error timer
068        startShortTimer();
069        // format and send message to go to program mode
070        if (getMode() == ProgrammingMode.OPSBYTEMODE) {
071            if (tc.getProtocol() == Mx1Packetizer.ASCII) {
072                // Not supporting ASCII protocol for now.
073                throw new ProgrammerException();
074            } else {
075                tc.sendMx1Message(Mx1Message.getDecProgCmd(mAddress, _cv, val, true), this);
076            }
077        }
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    synchronized public void readCV(String CVname, jmri.ProgListener p) throws jmri.ProgrammerException {
085        final int CV = Integer.parseInt(CVname);
086        log.debug("read CV={}", CV);
087        log.error("readCV not available in this protocol");
088        throw new ProgrammerException();
089    }
090
091    /** 
092     * {@inheritDoc}
093     */
094    @Override
095    public synchronized void confirmCV(String CV, int val, ProgListener p) throws ProgrammerException {
096        log.debug("confirm CV={}", CV);
097        log.error("confirmCV not available in this protocol");
098        throw new ProgrammerException();
099    }
100    
101    /** 
102     * {@inheritDoc}
103     *
104     * Can this ops-mode programmer read back values? For now, no, but maybe
105     * later.
106     *
107     * @return always false for now
108     */
109    @Override
110    public boolean getCanRead() {
111        return false;
112    }
113
114    /** 
115     * {@inheritDoc}
116     */
117    @Override
118    public boolean getLongAddress() {
119        return mLongAddr;
120    }
121
122    /** 
123     * {@inheritDoc}
124     */
125    @Override
126    public int getAddressNumber() {
127        return mAddress;
128    }
129
130    /** 
131     * {@inheritDoc}
132     */
133    @Override
134    public String getAddress() {
135        return "" + getAddressNumber() + " " + getLongAddress();
136    }
137
138    private final static Logger log = LoggerFactory.getLogger(Mx1OpsModeProgrammer.class);
139
140}