// For Auto updation of values in a screen INITDIALOG function not working. Button Clicked function 2nd time it is displaying.

/*********************************************************************
*                     SEGGER Microcontroller GmbH                    *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2021  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************
----------------------------------------------------------------------
File        : APPW_MainTask.c
Purpose     : APPWIZARD application entry point
---------------------------END-OF-HEADER------------------------------
*/

#include "Generated/Resource.h"
#include "../utilities/fsl_debug_console.h"
#include "../drivers/battery_monitor.h"
//#include "../Source/HHT_Parameters.h"
extern struct netif netif;
extern long long int UDPPktTimerCount;
extern int UDPFirstPktCounter;
extern int Battery_Percentage;
extern char str_Battery_Percentage[5];
fuelGaugeresultsHandle fuelGaugeresults;

// START----Added for Battery on 13-Sep-2021


//	lpi2c_master_config_t masterConfig;
	status_t reVal        = kStatus_Fail;
	uint16_t read_data;
	uint16_t read_bat_conn_status;
	uint8_t battery_capacity;

	/*
	 * Init parameters for Battery Monitor
	 */

	bat_mon_init bat_mon_init_params = {
			0x1450, //DesignCap
			0x0333, //IchgTerm
			0xa561, //VEmpty
			6		//ChargeVoltage
	};

	// END-----Added for Battery on 13-Sep-2021

/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Setup configuration dependent pointers
  //
  APPW_X_Setup();
  //
  // Initialize AppWizard
  //
  APPW_Init(APPW_PROJECT_PATH);
  //
  // Create initial screen...
  //
  APPW_CreateRoot(APPW_INITIAL_SCREEN, WM_HBKWIN);
  //
  // ...and keep it alive
  //
  while (1) {
    while (GUI_Exec1()) {
      APPW_Exec();
    }
    APPW_Exec();
    GUI_Delay(5);
    if(UDPFirstPktCounter==1)
    {
    	UDPSendPacket();
    	UDPFirstPktCounter=2;
    	//START-->Added for Battery on 13-Sep-2021

    		init_bat_mon_i2c();

    		read_data  = read_reg_bat_mon(0x00);

    		//PRINTF("reg val = 0x%04x\n",read_data);

    		init_battery_monitor(&bat_mon_init_params, true);

    		//END-->Added for Battery on 13-Sep-2021
    }
    if(UDPPktTimerCount==400)
    {
    	UDPSendPacket();
//    	read_bat_conn_status = read_reg_bat_conn(0x1D);
//    	PRINTF("reg val 0x1D = 0x%04x\n",read_bat_conn_status);

    	read_bat_conn_status = read_reg_bat_conn(0x09);
    	//PRINTF("reg val 0x09= 0x%04x\n",read_bat_conn_status);
    	read_bat_conn_status = (read_bat_conn_status) >>8;
    	//PRINTF("\n\r read_bat_conn_status int = 0x%04x\n",read_bat_conn_status);
    	if(read_bat_conn_status > 0xd0)
    	{
    		str_Battery_Percentage[0]='X';
    		str_Battery_Percentage[1]='X';
    	}
    	else
    	{
    		reVal = read_FuelGauge_results(&bat_mon_init_params, &fuelGaugeresults);	//Added for Battery on 13-Sep-2021
			Battery_Percentage = (((fuelGaugeresults.RepSOC) >>8) * 100) / 256;
			//PRINTF("\n\rRepSoc Reg = 0x%04x\n",fuelGaugeresults.RepSOC);
			//PRINTF("\n\r Battery_Percentage=%d\n",Battery_Percentage);
			sprintf(str_Battery_Percentage,"%d",Battery_Percentage);
    	}
//    	reVal = read_FuelGauge_results(&bat_mon_init_params, &fuelGaugeresults);	//Added for Battery on 13-Sep-2021
//    	Battery_Percentage = (((fuelGaugeresults.RepSOC) >>8) * 100) / 256;
//    	//PRINTF("\n\rRepSoc Reg = 0x%04x\n",fuelGaugeresults.RepSOC);
//    	//PRINTF("\n\r Battery_Percentage=%d\n",Battery_Percentage);
//    	sprintf(str_Battery_Percentage,"%d",Battery_Percentage);
//    	//PRINTF("\n\r str_Battery_Percentage=%s",str_Battery_Percentage);
    	UDPPktTimerCount=0;
    }
    //PRINTF("\n\r UDPPktTimerCount=%d",UDPPktTimerCount);
    UDPPktTimerCount++;
    ethernetif_input(&netif);
    sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
  }
}

/*************************** End of file ****************************/



