#include "pca9865.h"

static pca9865_t pca9865;

void bsp_pca9865_set_PWMLeds(pca9865_t* dev, int idx, int ratio)
{
    // dev = &pca9865;
    if (idx<0) idx = 0;
    if (idx>15) idx = 15;
    if (dev->mled == NULL)
    {
        char filename[256];
        // sprintf(filename, "/sys/class/leds/mled%d/brightness", dev->name, idx);
        sprintf(filename, "/sys/class/leds/mled%d/brightness", dev->name, idx);
        dev->mled[idx]=fopen(filename, "rw");
    }
    if (dev->mled)
    {
        char cmdline[256];
        sprintf(cmdline, "%d", ratio);
        fwrite(cmdline, strlen(cmdline), 1, dev->mled[idx]);
    }
}


int bsp_pca9865_init(pca9865_t* dev, const char* name)
{
    // dev = &pca9865;
    if (dev)
    {
        if (name)
        {
            int i;
            dev->name = name;
            for (i=0; i<16; i++)
            {
                dev->mled[i] = NULL;
            }
        }
    }
    return -1;
}

void bsp_pca9865_exit(pca9865_t* dev)
{
    // dev = &pca9865;
    if (dev)
    {
        int i;
        for (i=0; i<16; i++)
        {
            if (dev->mled[i] != NULL)
            {
                fclose(dev->mled[i]);
                dev->mled[i] = NULL;
            }
        }
    }
}