#include <stdio.h>

#include "ImC/imc_kernel.h"
#include "ImC/imc_api.h"
#include "stm32l5xx.h"
#include "cmsis_os.h"

#if(imcUSE_IMC_EXTENSION)
    #include "ImC/imc_extension.h"
#endif

#if(imcUSE_CHECKPOINT_VOLTAGE_CHECK)
    extern ADC_HandleTypeDef ADC_HANDLER_SBC;
#endif

#if(imcUSE_CHECKPOINT_PASS_COUNTER)
    int checkpointCounter = 0;
    const int checkpoint_pass_count = imcCHECKPOINT_PASS_COUNT;
#endif

void imcInit()
{
    FPU->FPCCR &= ~(FPU_FPCCR_LSPEN_Msk); // turn off lazy stacking
    #if(imcUSE_IMC_EXTENSION)
        imc_init_energy_estimation();
        #if(imcPRINT_STATS)
            if(first_checkpoint == -1) {
                no_forward_progress += 1;
                imc_backup_stats();
            }
            first_checkpoint = -1;
        #endif
    #endif
}

int needCheckpointExecution() {
    #if(imcPRINT_STATS)
        imc_checkpoint_triggered++;
    #endif

    #if(imcUSE_CHECKPOINT_VOLTAGE_CHECK)
        int capacitor_voltage0 = measure_voltage(ADC_HANDLER_SBC, EPS_CAP_ID_SBC);
        return capacitor_voltage0 < imcCHECKPOINT_VOLTAGE;
        printf("\t\t\t\t\t\t[EPS] CAP VOLT: %d.%03d\r\n", capacitor_voltage0 / 1000, capacitor_voltage0 % 1000);
    #endif

    #if(imcUSE_CHECKPOINT_PASS_COUNTER)
        if (checkpointCounter == checkpoint_pass_count) {
            checkpointCounter = 0;
            return 1;
        } else {
            checkpointCounter++;
            return 0;
        }
    #endif


    #if(!imcUSE_CHECKPOINT_VOLTAGE_CHECK && !imcUSE_CHECKPOINT_PASS_COUNTER)
        return 1;
    #endif
}

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin) {
    // if(GPIO_Pin == GPIO_PIN_9 || GPIO_Pin == GPIO_PIN_3) {
    //     HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET);
    //     printf("interrupt (falling)\r\n");
    // }
    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET);
}

// void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin) {
//     if(GPIO_Pin == GPIO_PIN_9 || GPIO_Pin == GPIO_PIN_3) {
//         HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET);
//         printf("interrupt (rising)\r\n");
//     }
// }
