/******************************************************************************
*@file    example_adc.c
*@brief   example of adc
*
*  ---------------------------------------------------------------------------
*
*  Copyright (c) 2018 fibocom Technologies, Inc.
*  All Rights Reserved.
*  Confidential and Proprietary - fibocom Technologies, Inc.
*  ---------------------------------------------------------------------------
*******************************************************************************/
/*===========================================================================
                                Header file
===========================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>

#include "qapi_fs_types.h"
#include "qapi_status.h"
#include "qapi_adc.h"

#include "qapi_uart.h"
#include "qapi_timer.h"
#include "qapi_diag.h"

#include <locale.h>
#include "odm_ght_log.h"

#define ADC_INPUT_ADC0            "GPIO_02"

/**************************************************************************
*                                GLOBAL
***************************************************************************/
TX_BYTE_POOL *byte_pool_uart;
#define UART_BYTE_POOL_SIZE		10*8*1024
UCHAR free_memory_uart[UART_BYTE_POOL_SIZE];

/* uart rx tx buffer */
static char *rx_buff = NULL;
static char *tx_buff = NULL;


/*ADC handle*/
qapi_ADC_Handle_t adc_handle;


/**************************************************************************
*                                 FUNCTION
***************************************************************************/
qapi_Status_t adc_open_handle(void)
{
    qapi_Status_t status = QAPI_ERROR;
    status = qapi_ADC_Open(&adc_handle, 0);
    if (QAPI_OK != status)
    {
        odm_ght_printf(debug_uart_context_D.uart_handle,"Get ADC Handle ERROR!\r\n");
    }
    return status;
}

/*
@func
        adc_get_properties
@brief
        Get properties of the ADC
*/
qapi_Status_t adc_get_properties(const char *Channel_Name_Ptr,
                                 qapi_Adc_Input_Properties_Type_t *Properties_Ptr)
{
    qapi_Status_t status = QAPI_ERROR;
    uint32_t Channel_Name_Size = strlen(Channel_Name_Ptr) + 1;

    status = qapi_ADC_Get_Input_Properties(adc_handle, Channel_Name_Ptr, Channel_Name_Size, Properties_Ptr);
    if (QAPI_OK != status)
    {
        odm_ght_printf(debug_uart_context_D.uart_handle,"ADC Get Channel-%s Properties ERROR!\r\n", Channel_Name_Ptr);
        odm_ght_printf(debug_uart_context_D.uart_handle,"ADC Get Channel %x \r\n", status);
    }
    return status;
}

void fibocom_adc_task_entry(void)
{
    qapi_ADC_Read_Result_t result;
    qapi_Status_t status = QAPI_ERROR;

    const char *Channel_Name_ADC0 = ADC_INPUT_ADC0;
    qapi_Adc_Input_Properties_Type_t Properties_ADC0;


    /*get an adc handle*/
    status = adc_open_handle();
    if(status != QAPI_OK)
    {
        odm_ght_printf(debug_uart_context_D.uart_handle,"Get ADC Handle ERROR!");
    }

    /*get the adc channel configuration*/
    status = adc_get_properties(Channel_Name_ADC0, &Properties_ADC0);
    if(status != QAPI_OK)
    {
        odm_ght_printf(debug_uart_context_D.uart_handle,"Get ADC channel-%s Configuration ERROR!", Channel_Name_ADC0);
    }
    odm_ght_printf(debug_uart_context_D.uart_handle,"Read ADC %x !\r\n", status);

    while(1)
    {
        /*read channel ADC0*/
        memset(&result, 0, sizeof(result));
        status = qapi_ADC_Read_Channel(adc_handle,	&Properties_ADC0, &result);
        odm_ght_printf(debug_uart_context_D.uart_handle,"Input %s  Voltage: %d mV~\r\n",
        Channel_Name_ADC0,
        result.nMicrovolts/1000);

        /* wait 5sec for reading again */
       qapi_Timer_Sleep(5, QAPI_TIMER_UNIT_SEC, true);
    }
}

int fibocom_task_entry(void)
{
    int ret = -1;
    qapi_ADC_Read_Result_t result;
    qapi_Status_t status = QAPI_ERROR;

    /* wait 10sec for device startup */
    qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_SEC, true);
    debug_uart_init(&debug_uart_context_D);
    odm_ght_printf(debug_uart_context_D.uart_handle,"ght# task_entry Start");

    odm_ght_printf(debug_uart_context_D.uart_handle,"ADC Example...\r\n");

    fibocom_adc_task_entry();

    if(ret != TX_SUCCESS)
    {
        odm_ght_printf(debug_uart_context_D.uart_handle,"[task] : Thread creation failed");
        odm_ght_printf(debug_uart_context_D.uart_handle,"[task] Thread creation failed");
    }
    return 0;
}



