/*          Class DtoA
*
*           This class contains constructor and methods that will
*           1.  Simulate a Digital to Analogue Convertor (DAC)
*           or
*           2. Simply act as a marker to be used in OpenPath and
*           ClosedLoop to indicate the presence of an DAC. In the
*           latter case the output is equal to the input plus any delay set.
*
*           This class is a subclass of the superclass BlackBox.
*
*           Author:  Michael Thomas Flanagan.
*
*           Created: 27 June 2003
*           Revised: 18 August 2003, 9 May 2005, April 2008, 7 November 2009, 18 January 2011
*
*           DOCUMENTATION:
*           See Michael T Flanagan's JAVA library on-line web page:
*           http://www.ee.ucl.ac.uk/~mflanaga/java/DtoA.html
*           http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2003 - 2011   Michael Thomas Flanagan
*
*   PERMISSION TO COPY:
*   Permission to use, copy and modify this software and its documentation for
*   NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
*   to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
*
*   Dr Michael Thomas Flanagan makes no representations about the suitability
*   or fitness of the software for any or for a particular purpose.
*   Michael Thomas Flanagan shall not be liable for any damages suffered
*   as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/

package flanagan.control;

import flanagan.complex.*;
import flanagan.math.Conv;

public class DtoA  extends BlackBox{

    private int nBits = 0;              // Number of bits, n
    private long maximumDecimal =  0;   // 2^n-1
    private double vRef = 0.0D;         // Reference voltage
    private int[] vBinary = null;       // array holding binary input
    private boolean trueDtoA = true;    // if true, a real DAC is simulated
                                        // if false, the instance is simply an DtoA marker
    private double outputVoltage = 0.0D;// output voltage
    private double voltageInput =0.0D;  // input as voltage - if this is the input the output is put equal to this input
    private String binaryInput = "";    // input as a binary String
    private long decimalInput = 0L;     // input as decimal representation of a binary String
    private boolean inputSet = false;   // = true when input is set

    // Constructor
    // Simulates a DAC
    public DtoA(int nBits, double vRef ){
        super("DtoA");
        super.setSnumer(new ComplexPoly(1.0D));
        super.setSdenom(new ComplexPoly(1.0D));
        super.setZtransformMethod(1);
        this.nBits = nBits;
        this.vBinary = new int[nBits+1];
        this.maximumDecimal = (long)Math.pow(2, this.nBits)-1L;
        this.vRef = vRef;
        this.trueDtoA = true;
    }

    // Constructor
    // Simply marks an DtoA event
    public DtoA(){
        super("DtoA");
        this.trueDtoA = false;
        super.sNumerDeg = 0;
        super.sDenomDeg = 0;
        super.setSnumer(new ComplexPoly(1.0D));
        super.setSdenom(new ComplexPoly(1.0D));
        super.setZtransformMethod(1);
    }

    // Return the true DtoA option
    public boolean getTrueDtoAoption(){
        if(this.trueDtoA){
            System.out.println("This instance of DtoA is a true simulation of an ADC");
            System.out.println("getTrueDtoAoption has returned 'true'");
        }
        else{
            System.out.println("This instance of DtoA is not a true simulation of an ADC");
            System.out.println("It is simple an 'D to A marker'");
            System.out.println("getTrueDtoAoption has returned 'false'");
        }
        return this.trueDtoA;
    }

    // Set input entered as a Sting representing the binary inut in two's complement
    // of n+1 bits where n = this.nBits and the extra bit is the sign bit
    public void setInput(String input){
        this.binaryInput = input.trim();
        int len = this.binaryInput.length();
        if(len>this.nBits+1)throw new IllegalArgumentException("length of input String is greater than the DAC bit number plus one");
        if(len=0; i--){
            c = this.binaryInput.charAt(i);
            if(c=='1'){
                ii=1;
            }
            else{
                if(c=='0'){
                    ii = 0;
                }
                else{
                    throw new IllegalArgumentException("String input must be '0's or '1's");
                }
            }
            jj = len-i-1;
            this.vBinary[jj] = ii;
        }

        // Check if input is negative
        long sign = 1L;
        int[] vPosBinary = Conv.copy(this.vBinary);
        if(this.vBinary[len-1]==1){
            sign = -1L;
            vPosBinary = this.negateNegativeBinary(vPosBinary);
        }

        // convert positive binary to decimal equivalent
        this.decimalInput = DtoA.binaryToDecimal(vPosBinary);

        // adjust sign
        if(sign==-1L)this.decimalInput = -this.decimalInput;

        // convert to voltage
        this.outputVoltage = (this.decimalInput*this.vRef)/(this.maximumDecimal+1L);

        this.inputSet = true;
    }

    // Set input entered as an integer array representing the binary inut in two's complement
    // of n+1 bits where n = this.nBits and the extra bit is the sign bit
    // Zeroth array element is the least significant bit (LSB)
    public void setInput(int[] input){
        int len = input.length;
        if(len>this.nBits+1)throw new IllegalArgumentException("length of input array is greater than the DAC bit number plus  one");
        for(int i=0; i=0; i--){
            this.binaryInput = this.binaryInput + this.vBinary[i];
        }

        // Check if input is negative
        long sign = 1L;
        int[] vPosBinary = Conv.copy(this.vBinary);
        if(this.vBinary[len-1]==1){
            sign = -1L;
            vPosBinary = this.negateNegativeBinary(this.vBinary);
        }

        // convert positive binary to decimal equivalent
        this.decimalInput = DtoA.binaryToDecimal(vPosBinary);

        // adjust sign
        if(sign==-1L)this.decimalInput = -this.decimalInput;

         // convert to voltage
        this.outputVoltage = (this.decimalInput*this.vRef)/(this.maximumDecimal+1L);

        this.inputSet = true;
    }


    // Set input entered as a decimal equivalent the binary input
    public void setInput(long input){
        if(Math.abs(input)>this.maximumDecimal)throw new IllegalArgumentException("abs(input), "+input+", is greater than the maximum decimal representation, "+this.maximumDecimal+", allowed by the set number of bits, "+this.nBits);
        this.decimalInput = input;

        // convert to voltage
        this.outputVoltage = (input*this.vRef)/(this.maximumDecimal+1L);

        // convert decimal to binary
        long dec = this.decimalInput;
        int sign = 1;
        if(dec<0){
            sign = -1;
            dec = -dec;
        }

        for(int i=0; i=0; i--){
            this.binaryInput = this.binaryInput + this.vBinary[i];
        }

        this.inputSet = true;
    }

    // Enter input as a voltage
    public void setInput(double input){

        if(this.trueDtoA){
            if(Math.abs(input)>this.vRef){
                throw new IllegalArgumentException("The input voltage in this simulation of a DAC must be less than nor equal to the reference voltage\nIf you choose the constructor without an argument list, i.e. an instance of DtoA that is simply a DAC marker\nyou may imput any voltage and the output will be made equal to that voltage");
            }
            else{
                this.voltageInput=input;
                AtoD adc = new AtoD(this.nBits, this.vRef);
                adc.setInput(input);
                this.decimalInput = adc.decimalOutput();
                this.binaryInput = adc.binaryOutput();
                this.vBinary = adc.binaryArray();
            }
        }
        else{
            this.outputVoltage = input;
        }
        super.sNumer.resetCoeff(0, new Complex(this.outputVoltage/this.voltageInput, 0.0D));

        this.inputSet = true;
    }

    // Convert positive binary to decimal equivalent
    private static long binaryToDecimal(int[] binary){
        long decimal = 0L;
        for(int i=0; in){
            lenMax = m;
            lenMin = n;
        }
        int[] addition = new int[lenMax];
        int carry = 0;
        int sum = 0;
        for(int i=0; i 

Ads help maintain this website.