package com.xixun.communicate;

import com.xixun.display.MyLog;
import com.xixun.unique.BoardCard;
import com.xixun.unique.CardType;

import java.io.FileDescriptor;

import static android.system.OsConstants.O_RDWR;

/**
 * Created by Administrator on 2018/05/12.
 */

public class SpiDevice {
    public static String DeviceName="/dev/spidev0.0";
    public static int  iSpiMaxBaudRate=12*1000*1000;//12000000;//16675000; if iSpiMaxBaudRate != 12Mhz y30 wil have problem;
    public static int MODE = 1;

    FileDescriptor fileDescriptor=null;
    String deviceName=DeviceName;
    int iBaudRate=iSpiMaxBaudRate;
    int mode=MODE;

    MooncellFpga fatherClass=null;

    public boolean GetReady()
    {
        return  (fileDescriptor!=null);
    }

    public SpiDevice(MooncellFpga fatherClass) {
        this.fatherClass=fatherClass;
        SpiDeveiceOpen();
    }

    public SpiDevice(MooncellFpga fatherClass,String deviceName) {
        this.fatherClass=fatherClass;
        this.deviceName=deviceName;
        SpiDeveiceOpen();
    }

    public SpiDevice(MooncellFpga fatherClass,String deviceName, int iBaudRate) {
        this.fatherClass=fatherClass;
        this.deviceName=deviceName;
        this.iBaudRate=iBaudRate;
        SpiDeveiceOpen();
    }

    public SpiDevice(MooncellFpga fatherClass,String deviceName, int iBaudRate,int mode) {
        this.fatherClass=fatherClass;
        this.deviceName=deviceName;
        this.iBaudRate=iBaudRate;
        this.mode=mode;
        SpiDeveiceOpen();
    }

    public void SpiDeveiceOpen() {
        int bSetBrg=1;
        if (fatherClass!=null) {
            if (LedCardCommunication.bTestInNewFpga) iBaudRate=0;
            if (fatherClass.ledCardCommunication.CardType("Y"))
                Define.SetFileAuthority(fatherClass.bXixunSuCommand, deviceName);
            if (fatherClass.ledCardCommunication.CardType("E"))
                iBaudRate=1000000*6;
            if (CardType.D == new BoardCard().getCardType() || CardType.D_1 == new BoardCard().getCardType()){
                deviceName = "/dev/spidev2.0";
                iBaudRate = 12*1000*1000;
                mode = 3;
            }
            if (CardType.C == new BoardCard().getCardType()){
                deviceName = "/dev/spidev0.0";
                iBaudRate = 12*1000*1000;
                mode = 3;
            }
        }
        fileDescriptor=Open(deviceName,iBaudRate,mode);
        if( fileDescriptor == null ) {
            MyLog.i("it is v19.1");
            deviceName = "/dev/spidev0.0";
            fileDescriptor = Open("/dev/spidev0.0", iBaudRate,mode);
        }
        MyLog.i("SpiDevice.java Open Device:"+deviceName + " ,fileDescriptor = "+ fileDescriptor);
        if (fileDescriptor==null){
            MyLog.i("SpiDevice.java Open Device Fail:"+deviceName);
        }else {
            MyLog.i("SpiDevice.java Open Device Success:"+deviceName);
        }
    }

    public void Exit(){
        MyLog.i(" "+ Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
        );
        Close();
        fileDescriptor=null;
    }


//    public native FileDescriptor Open(String deviceName, int iBaudRate);
    public native FileDescriptor Open(String deviceName, int iBaudRate,int mode);
    public native int Operator(byte [] Databuffer,int iLength);
    public native int Trans(byte []sendBuf, byte []recvBuf, int iLength);
    public native int Close();
}
