#include "ads1115.h"

int bsp_ads1115_init(ads_1115_t* dev, const char* name)
{
    if (dev)
    {
        uint8_t buff[128];
        dev->name = name;

        sprintf(buff, "echo \"0.125\" > %s/in_voltage0_scale", name);
        system(buff); printv("%s\n", buff);
        sprintf(buff, "echo \"0.125\" > %s/in_voltage1_scale", name);
        system(buff); printv("%s\n", buff);
        sprintf(buff, "echo \"0.125\" > %s/in_voltage2_scale", name);
        system(buff); printv("%s\n", buff);
        sprintf(buff, "echo \"0.125\" > %s/in_voltage3_scale", name);
        system(buff); printv("%s\n", buff);

        sprintf(buff, "%s/in_voltage0_raw", name);
        dev->fp_ADC0 = fopen(buff, "r");
        sprintf(buff, "%s/in_voltage0_scale", name);
        dev->fp_SCALE0 = fopen(buff, "r");

        sprintf(buff, "%s/in_voltage1_raw", name);
        dev->fp_ADC1 = fopen(buff, "r");
        sprintf(buff, "%s/in_voltage1_scale", name);
        dev->fp_SCALE1 = fopen(buff, "r");
        
        sprintf(buff, "%s/in_voltage2_raw", name);
        dev->fp_ADC2 = fopen(buff, "r");
        sprintf(buff, "%s/in_voltage2_scale", name);
        dev->fp_SCALE2 = fopen(buff, "r");

        sprintf(buff, "%s/in_voltage3_raw", name);
        dev->fp_ADC3 = fopen(buff, "r");
        sprintf(buff, "%s/in_voltage3_scale", name);
        dev->fp_SCALE3 = fopen(buff, "r");

        return 0;
    }
    return -1;
}

void bsp_ads1115_exit(ads_1115_t* dev)
{
    if (dev)
    {
        if (dev->fp_ADC0) fclose(dev->fp_ADC0);
        if (dev->fp_SCALE0) fclose(dev->fp_SCALE0);

        if (dev->fp_ADC1) fclose(dev->fp_ADC1);
        if (dev->fp_SCALE1) fclose(dev->fp_SCALE1);

        if (dev->fp_ADC2) fclose(dev->fp_ADC2);
        if (dev->fp_SCALE2) fclose(dev->fp_SCALE2);

        if (dev->fp_ADC3) fclose(dev->fp_ADC3);
        if (dev->fp_SCALE3) fclose(dev->fp_SCALE3);
    }
}

float bsp_ads1115_getVoltage0(ads_1115_t* dev)
{
    if (dev)
    {
        int value;
        float scale;
        fseek(dev->fp_ADC0, 0, SEEK_SET); fflush(dev->fp_ADC0); fscanf(dev->fp_ADC0, "%d", &value);
        fseek(dev->fp_SCALE0, 0, SEEK_SET); fflush(dev->fp_SCALE0); fscanf(dev->fp_SCALE0, "%f", &scale);

        float mV = value * scale;
        // mV = 5000*(Rt/(2000 + Rt) - 910/(2000 + 910))*240/47
        // Rt/(2000 + Rt) = 3.916666e-5 * mV + 0.31275394329896907216494845360825
        // Rt = 3.916666e-5 * mV * 2000 + 0.31275394329896907216494845360825 * 2000  + 3.916666e-5 * mV * Rt + 0.31275394329896907216494845360825 * Rt
        // Rt * (0.68724605670103092783505154639175 - 3.916666e-5 * mV) = 7.8333333e-2 * mV + 625.5078866
        float Rt = (mV * 78.3333333e-3 + 625.5078866) / (0.68724605670103092783505154639175 - mV * 3.9166667e-5);
        // float Rt = 1000*mV/(3300 - mV);
        // float R0 = 1000;
        // float A = 3.9083e-3;
        // float B = -5.775e-7;
        // float C = -4.183e-12;
        // float Rt = R0*(1 + A*T + B*T^2 + C*(T- 100)*T^3));
        // float T = (sqrt(A*A*R0*R0 - 4*B*R0*R0 + 4*B*R0*Rt) - A*R0)/(2*B*R0);
        float T = (3.9083 - sqrt(17.58480889 - Rt * 2.31e-3))/1.155e-3;
        // printv("mV:%f, Rt:%f, T:%f value:%d, scale:%f\n", mV, Rt, T, value, scale);
        return T;
    }
    return 0.0f;
}

float bsp_ads1115_getVoltage1(ads_1115_t* dev)
{
    if (dev)
    {
        int value;
        float scale;
        fseek(dev->fp_ADC1, 0, SEEK_SET);  fflush(dev->fp_ADC1);  fscanf(dev->fp_ADC1, "%d", &value);
        fseek(dev->fp_SCALE1, 0, SEEK_SET); fflush(dev->fp_SCALE1); fscanf(dev->fp_SCALE1, "%f", &scale);

        float mV = value * scale;
        float Rt = (mV * 78.3333333e-3 + 625.5078866) / (0.68724605670103092783505154639175 - mV * 3.9166667e-5);
        // float Rt = 1000*mV/(3300 - mV);
        float T = (3.9083 - sqrt(17.58480889 - Rt * 2.31e-3))/1.155e-3;
        // printv("mV:%f, Rt:%f, T:%f value:%d, scale:%f\n", mV, Rt, T, value, scale);
        return T;
    }
    return 0.0f;
}

float bsp_ads1115_getVoltage2(ads_1115_t* dev)
{
    if (dev)
    {
        int value;
        float scale;
        fseek(dev->fp_ADC2, 0, SEEK_SET);  fflush(dev->fp_ADC2); fscanf(dev->fp_ADC2, "%d", &value);
        fseek(dev->fp_SCALE2, 0, SEEK_SET); fflush(dev->fp_SCALE2); fscanf(dev->fp_SCALE2, "%f", &scale);

        float mV = value * scale;
        float Rt = (mV * 78.3333333e-3 + 625.5078866) / (0.68724605670103092783505154639175 - mV * 3.9166667e-5);
        // float Rt = 1000*mV/(3300 - mV);
        float T = (3.9083 - sqrt(17.58480889 - Rt * 2.31e-3))/1.155e-3;
        // printv("mV:%f, Rt:%f, T:%f value:%d, scale:%f\n", mV, Rt, T, value, scale);
        return T;
    }
    return 0.0f;
}

float bsp_ads1115_getVoltage3(ads_1115_t* dev)
{
    if (dev)
    {
        int value;
        float scale;
        fseek(dev->fp_ADC3, 0, SEEK_SET);  fflush(dev->fp_ADC3); fscanf(dev->fp_ADC3, "%d", &value);
        fseek(dev->fp_SCALE3, 0, SEEK_SET); fflush(dev->fp_SCALE3); fscanf(dev->fp_SCALE3, "%f", &scale);

        float mV = value * scale;
        float Rt = (mV * 78.3333333e-3 + 625.5078866) / (0.68724605670103092783505154639175 - mV * 3.9166667e-5);
        // float Rt = 1000*mV/(3300 - mV);
        float T = (3.9083 - sqrt(17.58480889 - Rt * 2.31e-3))/1.155e-3;
        // printv("mV:%f, Rt:%f, T:%f value:%d, scale:%f\n", mV, Rt, T, value, scale);
        return T;
    }
    return 0.0f;
}