/*******************************************************************************
 TEST medi50  Dec 23
 //=========power_control.c====================
 ******************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "power_control.h"
#include "nrf_delay.h"


#include "nrf_log.h"
#include "app_timer.h"
#include "debug_print.h"
#include "i2c_manager.h"
#define EEP_WP	NRF_GPIO_PIN_MAP(0, 24)
APP_TIMER_DEF(m_power_timer_id);

// 2025-12-08 change to #define POWER_LOOP_INTERVAL 30 -> 20
#define POWER_LOOP_INTERVAL 20




static uint8_t p_order;
extern volatile bool processing;
bool lock_check = false;
void power_gpio_init(void)
{
	nrf_gpio_cfg_output(EEP_WP);
}


void eeprom_control(on_off_cont_t eeprom_st)
{
	if(eeprom_st == OFF) {
	    nrf_gpio_pin_clear(EEP_WP);
	}else if(eeprom_st == ON){
	    nrf_gpio_pin_set(EEP_WP);
	}
}

int device_sleep_mode(void){
	int rc = 0;
	eeprom_control(OFF);
	nrf_delay_ms(2);
	DBG_PRINTF("Device_Sleep_Mode OK!\r\n");
	nrf_delay_ms(10);
	processing = false;
	return rc;
}


int device_activated(void){
	int rc = 0;
	p_order = 0;
	lock_check =true;
	power_timer_start();
	eeprom_control(OFF);

	return rc;
}

/**
 * @brief Power-up sequence state machine
 *
 * Executes hardware initialization steps
 * Called by app_timer at POWER_LOOP_INTERVAL (20ms)
 *
 * Sequence:
 *   0: I2C init
 *   1: (reserved)
 *   2: Complete
 */
void power_loop(void *p_context)
{
    UNUSED_PARAMETER(p_context);

    power_timer_stop();

#if DEBUG_MINIMAL_BOOT
    /* Minimal Boot: Skip sensor initialization */
    DBG_PRINTF("[PWR] Minimal mode - skipping sensor init\r\n");
    p_order = 2;  // Jump to complete
#else
    /* Full Boot: Execute power sequence */
    switch (p_order) {
        /* Step 0: I2C Initialize */
        case 0:
            sw_i2c_init_once();
            nrf_delay_ms(10);
            DBG_PRINTF("[PWR] Step %d: I2C Init\r\n", p_order);
            break;

        /* Step 1: Reserved */
        case 1:
            DBG_PRINTF("[PWR] Step %d: (reserved)\r\n", p_order);
            break;

        /* Step 2: Complete */
        case 2:
            DBG_PRINTF("[PWR] Step %d: Sequence Complete\r\n", p_order);
            break;

        default:
            break;
    }
#endif

    /* Advance to next step or finish */
    if (p_order < 2) {
        p_order++;
        power_timer_start();
    } else {
        DBG_PRINTF("[PWR] Device Activated OK!\r\n");
    }
}


int device_reactivated(void){
	int rc = 0;
	sw_i2c_init_once();
	nrf_delay_ms(10);
	lock_check = true;
	p_order = 0;
	power_timer_start();
	return rc;
}

void power_timer_start(void)
{
	APP_ERROR_CHECK(app_timer_start(m_power_timer_id, APP_TIMER_TICKS(POWER_LOOP_INTERVAL), NULL));
}


void power_timer_stop(void)
{
	APP_ERROR_CHECK(app_timer_stop(m_power_timer_id));

}


void power_timer_init(void)  //active start
{
 APP_ERROR_CHECK(app_timer_create(&m_power_timer_id, APP_TIMER_MODE_SINGLE_SHOT, power_loop));

	// 2025-12-08 change to  APP_TIMER_MODE_REPEATED mode
	//APP_ERROR_CHECK(app_timer_create(&m_power_timer_id,  APP_TIMER_MODE_REPEATED, power_loop));


}
