#include "app.h"
#include "battery.h"
#include "accel.h"

#include <arm_math.h>

//=========================== defines =========================================

//==== Device variables indexing

#define APP_PRIMARY_VAR          4
#define APP_SECONDARY_VAR        5
#define APP_TERTIARY_VAR         6
#define APP_QUATERNARY_VAR       7
#define APP_LAST_VAR             8


//==== Accelerometer data rate
#define ACC_ODR                50 // in Hz

//=========================== typedef =========================================

// data are read from ADXL363 FIFO in this order
typedef struct
{
    int16_t x;
    int16_t y;
    int16_t z;
    int16_t temperature;
} acc_fifoData_t;

typedef enum
{
    ACC_STATE_UNKNOWN,
    ACC_STATE_PRESENT,
    ACC_STATE_NOT_CONNECTED
} acc_state_t;

//=========================== variables =======================================

//==== Device variables
app_deviceVar_t app_deviceVar[8] =
{
    // HART34_VARCODE_BAT_VOLTAGE
    {
        .unit              = HART2_UNIT_NOT_USED,
        .classification    = HART21_CLASS_NONE,
        .status            = HARTA3_LIMIT_CONSTANT | HARTA3_PROCESS_BAD,
    },

    // HART34_VARCODE_BAT_LIFE
    {
        .unit              = HART2_UNIT_Days,
        .classification    = HART21_CLASS_NONE,
        .min               = 0,
        .max               = 255
    },

    // HART34_VARCODE_PERCENT
    {
        .unit              = HART2_UNIT_PerCent,
        .classification    = HART21_CLASS_NONE,
        .min               = 0,
        .max               = 100
    },

    // HART34_VARCODE_LOOP_CURRENT
    {
        .unit              = HART2_UNIT_mA,
        .classification    = HART21_CLASS_NONE,
        .min               = 4,
        .max               = 20
    },

    //HART34_VARCODE_PRIMARY
    {
        .unit              = HART2_UNIT_g,
        .classification    = HART21_CLASS_ACCELERATION,
        .min               = 0,
        .max               = 500
    }

    // HART34_VARCODE_SECONDARY
    ,{
        .unit              = HART2_UNIT_g,
        .classification    = HART21_CLASS_ACCELERATION,
        .min               = -100,
        .max               = 100
    }

    // 	HART34_VARCODE_TERTIARY
    ,{
        .unit              = HART2_UNIT_g,
        .classification    = HART21_CLASS_ACCELERATION,
        .min               = -500,
        .max               = 0
    }

    // HART34_VARCODE_QUATERNARY
    ,{
        .unit              = HART2_UNIT_Celsius,
        .classification    = HART21_CLASS_TEMPERATURE,
        .min               = -50,
        .max               = 150
    }
};

//==== Accelerometer variables
int16_t             acc_data[ADXL372_MAX_FIFO_SAMPLES];  /* Buffer to read samples */
uint16_t            acc_dataCount;                           /* Number of samples to read */
acc_state_t         acc_state = ACC_STATE_UNKNOWN;

//=========================== prototypes ======================================

// Helpers
float32_t rms(int16_t *value, uint8_t offset, uint16_t n);

//============================== public =======================================

app_deviceVar_t* app_getDeviceVar(uint8_t varNum)
{
    int16_t          varIdx = varNum - 242;
    app_deviceVar_t *pVar;

    // check if passed parameter is valid
    if ((varIdx > 7) || (varIdx < 0))
        return NULL;
    else
        pVar = &app_deviceVar[varIdx];

    // check if it is enabled
    if (acc_state == ACC_STATE_UNKNOWN) // first run
        if (!pVar->enabled)
            app_enableMeasurement(varNum, true);

    // if accelerometer is not connected to board, generate random DEMO data
    if (acc_state == ACC_STATE_NOT_CONNECTED)
        if (++(pVar->value) > pVar->max)
            pVar->value = pVar->min;

    // return pointer to this var
    return pVar;
}

int16_t app_enableMeasurement(uint8_t varNum, bool enable)
{
    int16_t returnCode = APP_OK;

    return returnCode;
}

//============================== private ======================================

//=============================================================================
//======================== ACC ROUTINES =======================================
//=============================================================================

/* Accelerometer callback - to indicate when data is ready */
static void AccelCallback(int16_t *data, uint16_t numOfSamples)
{
    return;
}

//============================== helpers ======================================

float32_t rms(int16_t *value, uint8_t offset, uint16_t n)
{
    float32_t rms;
   

    return rms;
}
