#include "tiny1616.h"
#include "sensor_error.h"
#include "nrf_log.h"
#include "FreeRTOS.h"
#include "task.h"

#define READ
void tiny1616_test(tiny1616_dev_t *dev)
{
    sensor_serial_if_t *driver = dev->driver;
#ifdef READ
    uint8_t buf[] = {0x01, 0x22, 0x33, 0x44};

    driver->read_reg(driver,buf, 3);
    NRF_LOG_INFO("TINY1616:buf[0]=%x, buf[1]=%x", buf[0], buf[1]);
#endif
#ifdef WRITE
    uint8_t write[2];
    write[0] = 0x33;
    write[1] = 0x66;
    driver->write_reg(driver,write, 1);
#endif
}


bool tiny1616_check_id(tiny1616_dev_t *dev)
{
    DEV_ASSERT(dev);
    uint8_t buf[] = {0x01, 0};
    sensor_serial_if_t *driver = dev->driver;
    driver->read_reg(driver,buf, 1);

    if(buf[1] == TINY1616_WHO_I_AM)
    {
        return true;
    }

    return false;
}


int tiny1616_get_pressed_status(tiny1616_dev_t *dev)
{
    DEV_ASSERT(dev);
    int ret = 0;
    uint8_t buf[] = {0x02, 0};
    sensor_serial_if_t *driver = dev->driver;

    dev->pressed = 0x0000;
    ret = driver->read_reg(driver,buf, 1);
    dev->pressed = buf[1];
    buf[0] = 0x03;
    ret |= driver->read_reg(driver,buf, 1);
    dev->pressed |= ((buf[1] & 0x00FF) << 8);

    return ret;
}

