#include "app_eeprom.h"
#include <string.h>

uint8_t app_eeprom_read_buf[520];
uint8_t app_eeprom_write_buf[520];

app_eeprom_read_cache_t app_eeprom_read_cache = {

    .buffer = app_eeprom_read_buf,
    .addr   = 0,
    .len    = 0,
    .cur    = 0,
    .pfun   = 0,
    .used   = 0,
};

app_eeprom_write_cache_t app_eeprom_write_cache = {

    .buffer = app_eeprom_write_buf,
    .addr   = 0,
    .len    = 0,
    .cur    = 0,
    .count  = 0,
    .pfun   = 0,
    .used   = 0,
};

void app_eeprom_read_task(void)
{
    uint16_t read_len;
    uint16_t read_cur;
    uint8_t  err_code = 0;
    uint16_t tmp_read_len;
    uint16_t mod;

    if (app_eeprom_read_cache.used == 1)
    {
        mod = (app_eeprom_read_cache.addr + app_eeprom_read_cache.cur + APP_EEPROM_READ_STEP) % APP_EEPROM_READ_STEP;
        if (mod)
        {
            tmp_read_len = APP_EEPROM_READ_STEP - mod;
        }
        else
        {
            tmp_read_len = APP_EEPROM_READ_STEP;
        }
        if (app_eeprom_read_cache.cur + tmp_read_len < app_eeprom_read_cache.len)
        {
            read_len = tmp_read_len;
        }
        else
        {
            read_len = app_eeprom_read_cache.len - app_eeprom_read_cache.cur;
        }
        read_cur = app_eeprom_read_cache.cur;
        dev_at24cxx_read_bytes(app_eeprom_read_cache.addr + read_cur, &app_eeprom_read_cache.buffer[read_cur], read_len);
        app_eeprom_read_cache.cur += read_len;
        if (app_eeprom_read_cache.cur == app_eeprom_read_cache.len)
        {
            // if (app_eeprom_read_cache.buffer[app_eeprom_read_cache.len-1]!=)
            if (app_eeprom_read_cache.pfun != NULL)
            {
                // app_eeprom_read_cache.pfun(app_eeprom_read_cache)
            }
            app_eeprom_read_cache.used = 0;
        }
    }
}
void app_eeprom_write_task(void)
{
    uint16_t write_len;
    uint16_t write_cur;
    uint16_t tmp_write_len;
    uint16_t mod;

    if (app_eeprom_write_cache.used == 1)
    {
        mod = (app_eeprom_write_cache.addr + app_eeprom_write_cache.cur + APP_EEPROM_WRITE_STEP) % APP_EEPROM_WRITE_STEP;
        if (mod)
        {
            tmp_write_len = APP_EEPROM_WRITE_STEP - mod;
        }
        else
        {
            tmp_write_len = APP_EEPROM_WRITE_STEP;
        }
        if (app_eeprom_write_cache.cur + tmp_write_len < app_eeprom_write_cache.len)
        {
            write_len = tmp_write_len;
        }
        else
        {
            write_len = app_eeprom_write_cache.len - app_eeprom_write_cache.cur;
        }
        write_cur = app_eeprom_write_cache.cur;
        dev_at24cxx_write_bytes(app_eeprom_write_cache.addr + write_cur, &app_eeprom_write_cache.buffer[write_cur], write_len);
        app_eeprom_write_cache.cur += write_len;
        if (app_eeprom_write_cache.cur == app_eeprom_write_cache.len)
        { // 更新记录的数量
            if (app_eeprom_write_cache.count != 0)
            {
                // dev_at24cxx_write_bytes(app_eeprom_write_cache.addr + write_cur, &app_eeprom_write_cache.buffer[write_cur], write_len);
            }
            if (app_eeprom_write_cache.pfun != NULL)
            {
                // app_eeprom_write_cache.pfun(app_eeprom_write_cache)
            }
            app_eeprom_write_cache.used = 0;
        }
    }
}

uint8_t read_buf[50];

uint8_t app_eeprom_read_50_zhuzu(void)
{
    if (app_eeprom_read_cache.used == 1)
    {
        return 1;
    }

    memcpy(app_eeprom_read_cache.buffer, read_buf, 200);

    app_eeprom_read_cache.addr = 0;
    app_eeprom_read_cache.len  = 50;
    app_eeprom_read_cache.cur  = 0;
    app_eeprom_read_cache.used = 1;
    return 0;
}

uint8_t app_eeprom_write_50_zhuzu(void)
{
    uint8_t write_buf[50];
    for (uint8_t i = 0; i < 50; i++)
    {
        write_buf[i] = i;
    }

    if (app_eeprom_write_cache.used == 1)
    {
        return 1;
    }
    memcpy(app_eeprom_write_cache.buffer, write_buf, 50);

    app_eeprom_write_cache.addr = 0;
    app_eeprom_write_cache.len  = 50;
    app_eeprom_write_cache.cur  = 0;
    app_eeprom_write_cache.used = 1;
    return 0;
}