#include "slave.h"

double new_emissivity =0.95;


Adafruit_MLX90614 mlx = Adafruit_MLX90614();
TwoWire wireme(0);

u_char a[]={0x01,0x04,0x00,0x00,0x00 ,0x06,0x70,0x08};
u_char yuliang[]={0x02, 0x03, 0x00, 0x00 ,0x00, 0x01, 0x84, 0x39};
u_char ClearYL[]={0x02,0x06,0x00, 0x00 ,0x00 ,0x5A ,0X09 ,0Xc2};
u_char yuliangfuliangdu[]={0x02,0x03,0x00,0x02,0x00,0x02,0x65,0xf8};

u_char openGNRMC[]={0xF1,0xD9,0x06,0x01,0x03,0x00,0xF0,0x05,0x01,0x00,0x1A};
u_char gpsSaveConfig[]={0xF1,0xD9,0x06,0x09,0x08,0x00,0x00,0x00,0x00,0x00,0x2F,0x00,0x00,0x00,0x46,0xB7};



SoftwareSerial *my485Port;
void slave::init(int8_t MY485PORT_RX,int8_t MY485PORT_TX){
    // put your setup code here, to run once:
    //Wire.begin(4,5);

    Serial.println("slave init ...");
    my485Port = new SoftwareSerial(MY485PORT_RX ,MY485PORT_TX,false);
    my485Port->begin(9600, SWSERIAL_8N1, MY485PORT_RX, MY485PORT_TX, false);

    ///////////// 光谱电源控制,重启
    pinMode(36,OUTPUT);
    digitalWrite(36,LOW);
    delay(2000);
    digitalWrite(36,HIGH);


    // GPSSeiral.begin(115200, SWSERIAL_8N1, 2, 12, false);
    wireme.setPins(7,8);
    wireme.begin();
    Serial.println("slave init ...");
    if (!mlx.begin(MLX90614_I2CADDR,&wireme)) {
        Serial.println("Error connecting to MLX sensor. Check wiring.");
    //while (1);
        return ;
    };
    isinit=true;
    
    mlx.writeEmissivity(new_emissivity); 
    
    Serial.println(sizeof(openGNRMC));


}

double slave::getMLX(){
    if (/* condition */!isinit)
    {
        return 0;
        /* code */
    }
    
    double a =mlx.readObjectTempC();
    return a;
    // Serial.write((char*) &a,8);
}

void slave::getWehter(){

    while (my485Port->read()!=-1);
    my485Port->write(a,8);
    delay(90);
    for (int j = 0; j <17 ; ++j) {
        ret[j]=my485Port->read();
    }
    float VV=(ret[3]*256+ret[4])*1.0/100;
    float DD=(ret[5]*256+ret[6])*1.0/10;
    float TT=(ret[7]*256+ret[8])*1.0/100;
    float HH=(ret[9]*256+ret[10])*1.0/100;
    long PP=(ret[13]*256+ret[14])*256*256+(ret[11]*256+ret[12]);
#ifdef DEBG
    String str_getWehter = "VV:"+String(VV)+" DD:"+String(DD)+" TT:"+String(TT)+" HH:"+String(HH)+" PP:"+String(PP);
    // return str;
    // Serial.println("VV:"+String(VV)+" DD:"+String(DD)+" TT:"+String(TT)+" HH:"+String(HH)+" PP:"+String(PP));
#endif
    // Serial.write(ret,17);
}

float slave::getYuliang(){
    while (my485Port->read()!=-1);
    my485Port->write(yuliang,8);
    delay(90);

    for (int j = 0; j <7; ++j) {
        ret[j]=my485Port->read();
    }
    float Value=ret[3]*256+ret[4];
    Value=Value/10;
    // Serial.write(ret,7);
    // Serial.print(Value);
    // Serial.print("#");
    
    return Value;

}

int32_t slave::getYuliangfuliang(){

    while (my485Port->read()!=-1);
    my485Port->write(yuliangfuliangdu,8);
    delay(90);

    for (int j = 0; j <7; ++j) {
        ret[j]=my485Port->read();
    }
    int32_t  Value=(ret[3]*256+ret[4])*256*256+ret[5]*256+ret[6];
    // Value=Value/10;
    // Serial.write(ret,7);
    // Serial.print(Value);
    // Serial.print("#");

    return Value;
}

/// 返回1，表示ok
uint8_t slave::claeryuliang(){

    while (my485Port->read()!=-1);
    my485Port->write(ClearYL,8);
    delay(90);
    for (int j = 0; j <8; ++j) {
        ret[j]=my485Port->read();
    }
    // Serial.print("OK");
    return 1;
}   


