/*******************************************************************************
 * @file    main_timer.c
 * @brief   Main Loop Timer Handler
 ******************************************************************************/

#include "sdk_common.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_drv_gpiote.h"
#include "battery_saadc.h"
#include "app_timer.h"

#include "main.h"
#include "app_raw_main.h"
#include "main_timer.h"
#include "tmp235_q1.h"
#include "meas_pd_imm.h"
/* #include "meas_pd_voltage_full.h" */  /* Moved to unuse/ */
#include "mcp4725_i2c.h"
#include "power_control.h"
#include "power/power_ctrl.h"
#include <cmd_parse.h>
#include "meas_pd_48.h"
#include "debug_print.h"
#include "i2c_manager.h"

APP_TIMER_DEF(m_main_loop_timer_id);

#if FEATURE_DETAIL_VALUE_FULL
#define MAIN_LOOP_INTERVAL              80
extern which_cmd_t cmd_type_t;
#else
#define MAIN_LOOP_INTERVAL              10
#endif

bool go_batt = false;
bool go_temp = false;
bool go_pdread = false;

/* Defined in power_ctrl.c */
extern bool go_device_power_off;
extern bool go_sleep_mode_enter;
extern bool go_NVIC_SystemReset;

bool motion_raw_data_enabled = false;
bool ble_got_new_data = false;
bool motion_data_once = false;
bool adc_enabled = false;
static uint16_t cnt_adc = 0;

void main_loop(void * p_context)
{
	UNUSED_PARAMETER(p_context);

	/* Motion Data Sampling */
	if (motion_raw_data_enabled == true) {
		main_timer_stop();
		if (motion_data_once == true) {
			hw_i2c_init_once();
			icm42670_main();
		} else {
			if (ble_got_new_data == false) {
				DBG_PRINTF("IMU \r\n");
				icm42670_main();
				nrf_delay_ms(10);
				motion_raw_data_enabled = true;
				main_timer_start();
			}
		}
	}

	if (go_batt == true) {
		DBG_PRINTF("IMU BATT\r\n");
		main_timer_stop();
		go_batt = false;
		battery_level_meas();
	}

	if (go_temp == true) {
		DBG_PRINTF("IMU Temp\r\n");
		main_timer_stop();
		go_temp = false;
		motion_data_once = true;
		tmp235_voltage_level_meas();
	}

	if (go_pdread == true) {
		main_timer_stop();
		go_pdread = false;
		m48_adc_start_init();
	}

	if (adc_enabled == true) {
		main_timer_stop();
		DBG_PRINTF("PD48 ADC=%d\r\n", cnt_adc);
		if (ble_got_new_data == false) {
			if (cnt_adc < 500) {
				cnt_adc++;
			} else if (cnt_adc == 500) {
				cnt_adc = 0;
			}
			main_timer_start();
		}
	}

	/* System Control */
	if (go_device_power_off == true) {
		main_timer_stop();
		DBG_PRINTF("Off main_timer\r\n");
		device_power_off();
	}

	if (go_sleep_mode_enter == true) {
		main_timer_stop();
		DBG_PRINTF("sleep main timer\r\n");
		sleep_mode_enter();
	}

	if (go_NVIC_SystemReset == true) {
		main_timer_stop();
		NVIC_SystemReset();
	}
}

void main_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_main_loop_timer_id, APP_TIMER_TICKS(MAIN_LOOP_INTERVAL), NULL));
}

void main_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_main_loop_timer_id));
}

void main_timer_init(void)
{
	APP_ERROR_CHECK(app_timer_create(&m_main_loop_timer_id, APP_TIMER_MODE_SINGLE_SHOT, main_loop));
}
