#include"optoskybase.h"
static byte datatoget[10000];
byte echtimedata[1000];
OptoSky::OptoSky()
{
    IsOptoskyinit=false;
    result=datatoget;
}
void OptoSky::SetPortName(int serial)
{
    ISserial=new HardwareSerial(1);
    ISserial->begin(115200,SERIAL_8N1,17,18);
   // ISserial->println("sadfasdfsadfasdf");
    IsOptoskyinit=true;
}
void OptoSky::SendComment(byte *commend,int lenth)
{
    unsigned short lenthsend = lenth + 5;
    byte *senddata=new byte[lenthsend]();


	senddata[0] = 0xaa;
	senddata[1] = 0x55;
	senddata[2] = (lenth+3) / 256;
	senddata[3] = (lenth+3) % 256;
    for (size_t i = 0; i < lenth; i++)
    {
        senddata[i+4]=commend[i];

        /* code */
    }
    
	int sum = 0;
	for (int i = 0; i < lenth+2; i++)
	{
		sum = sum+senddata[i+2];
	}
	senddata[lenthsend-1]=sum;
	if (IsOptoskyinit)
	{
        while (ISserial->read()!=-1)
        {
            /* code */
          
        }
        //Serial.write(senddata,lenthsend);
        ISserial->write(senddata,lenthsend);
        delay(10);
		
	}
    delete[] senddata;
}

void OptoSky::SendComment(byte comid)
{
    SendComment(&comid,1);
}

void OptoSky::SendComment(byte *Comment,int commentlenth, byte *data,int lenth)
{
    byte *commnetdsend=new byte[commentlenth+lenth]();
    memcpy(commnetdsend,Comment,commentlenth);
    memcpy(commnetdsend+commentlenth,data,lenth);
    SendComment(commnetdsend,commentlenth+lenth);
    delete[] commnetdsend;
    
}

void OptoSky::SendComment(byte comid, byte *data,int lenth)
{
    byte *commnetdsend=new byte[lenth+1]();
    memcpy(commnetdsend,&comid,1);
    memcpy(commnetdsend+1,data,lenth);
    SendComment(commnetdsend,1+lenth);
    delete[] commnetdsend;
}

bool OptoSky::GetReturn(byte id,int &lenth,int timewaitbefor)
{
    if (!GetReturn(lenth,timewaitbefor))
	{
		return false;
	}
	
	if (datatoget[4]!=id)
	{
		Serial.println("retrun is not correct with ComId:" );
        
		return false;//�1�7�1�7�1�7�1�7�1�7�1�7�1�0�1�7�1�7�1�7�1�7�1�7�0�8�1�7�1�7�1�7�5�3�1�2�1�7�1�7�1�7
	}
    memcpy(datatoget,datatoget+5,lenth-5);
    lenth=lenth-6;
    return true;
}

bool OptoSky::GetReturn(int &lenth,int timewaitbefor)
{
    lenth=0;
    delay(timewaitbefor);
    while (ISserial->available())
    {
        int dataget=ISserial->readBytes(echtimedata,1000);
        
        for (long i = 0; i < dataget; i++)
        {
            datatoget[i+lenth]=echtimedata[i];
            /* code */
        }
        lenth=lenth+dataget;
        


        delay(40);


        /* code */
    }
    


    int tiemstry=0;
lable1:
	while (lenth < 5)
	{
		delay(20);
        if (ISserial->available())
        {
            /* code */
            int dataget=ISserial->readBytes(echtimedata,1000);
        
              for (long i = 0; i < dataget; i++)
              {
                  datatoget[i+lenth]=echtimedata[i];
            /* code */
                }
            lenth=lenth+dataget;
        }
    
		if (tiemstry> 2000)
		{
            Serial.println("long time no data retrun");
            Serial.print("size now:");
            Serial.println(lenth);
			return false;

		}
        tiemstry++;
	}
	if (datatoget[0] != 0xaa || datatoget[1] != 0x55)
	{
        memcpy(datatoget,datatoget+1,lenth-1);
        lenth--;

		goto lable1;
	}

	int datalenth = datatoget[2] * 256 + datatoget[3] + 2;
    tiemstry=0;
	while (lenth < datalenth)
	{
		delay(20);
        if (ISserial->available())
        {
            /* code */
            int dataget=ISserial->readBytes(echtimedata,1000);
        
              for (long i = 0; i < dataget; i++)
              {
                  datatoget[i+lenth]=echtimedata[i];
            /* code */
                }
            lenth=lenth+dataget;
        }
        if (tiemstry> 500)
		{
            Serial.println("not enouth data retrun");
            Serial.print("size now:");
            Serial.println(lenth);
			return false;

		}
        tiemstry++;

	}

	if (lenth > datalenth)
	{
		lenth=datalenth;
	}
	int checkleth = datalenth - 3;
	unsigned int checknum = 0;
	for (size_t i = 0; i < checkleth; i++)
	{

		checknum = checknum + datatoget[i + 2];
	}
	checknum = checknum % 256;
	if (checknum != datatoget[lenth-1])
	{
		Serial.println("checkerro  " );
		//data.clear();
		//return false;
	}
	return true;




    
}

bool  OptoSky::isSensorInit()
{
    return IsOptoskyinit;
}