#include "adc081c.h"


int bsp_adc081c_init(adc_081c_t* dev, const char* name)
{
    if (dev)
    {
        uint8_t buff[128];
        dev->name = name;
        sprintf(buff, "%s/in_voltage_raw", name);
        dev->fp_ADC = fopen(buff, "r");
        sprintf(buff, "%s/in_voltage_scale", name);
        dev->fp_SCALE = fopen(buff, "r");
        return 0;
    }
    return -1;
}

void bsp_adc081c_exit(adc_081c_t* dev)
{
    if (dev)
    {
        if (dev->fp_ADC) fclose(dev->fp_ADC);
        if (dev->fp_SCALE) fclose(dev->fp_SCALE);
    }
}

float bsp_adc081c_getVoltage(adc_081c_t* dev)
{
    if (dev)
    {
        int value;
        float scale;
        fseek(dev->fp_ADC, 0, SEEK_SET); fflush(dev->fp_ADC); fscanf(dev->fp_ADC, "%d", &value);
        fseek(dev->fp_SCALE, 0, SEEK_SET); fflush(dev->fp_SCALE); fscanf(dev->fp_SCALE, "%f", &scale);
        // printv("ADC081C(%s)->(value:%d,scale:%f), Voltage:%f\n", dev->name, value, scale, (float)(value * scale));
        return (float)(value * scale);
    }
    return 0.0f;
}
