#ifndef __ACMP_H
#define __ACMP_H

// ACMP Number
#define ACMP_NO_0       0
#define ACMP_NO_1       1
#define ACMP_NO_2       2
#define ACMP_NO_3       3

// Positive Input Source
#define PSEL_INPUT_ACMPX_0P     0
#define PSEL_INPUT_ACMPX_1P     1
#define PSEL_INPUT_BAND_GAP     3
#define PSEL_INPUT_ACMP0_P      4
#define PSEL_INPUT_ACMP1_P      5
#define PSEL_INPUT_ACMP2_P      6

// Negative Input Source
#define NSEL_INPUT_ACMPX_0N     0
#define NSEL_INPUT_BAND_GAP     3

#define NSEL_INPUT_ACMP0_N      0
#define NSEL_INPUT_ACMP1_N      1
#define NSEL_INPUT_ACMP2_N      2

// Hysteresis Range (mV)
#define HYS_RANGE_0     0
#define HYS_RANGE_50    1
#define HYS_RANGE_100   2
#define HYS_RANGE_150   3
#define HYS_RANGE_200   4
#define HYS_RANGE_250   5
#define HYS_RANGE_300   6
#define HYS_RANGE_350   7

// Output Latch Select
#define LATCHSEL_RAW_DATA       0
#define LATCHSEL_PWM_TRIGGER    1

// Trigger Type
#define ACMP_TRG_TYPE_FALLING   b00000001
#define ACMP_TRG_TYPE_RISING    b00000010

// Interrupt Type
#define ACMP_IE_TYPE_FALLING    b00000001
#define ACMP_IE_TYPE_RISING     b00000010
#define ACMP_IE_TYPE_LATCH      b00000100

// Output Status Type
#define ACMP_OUTSTA_TYPE_OUT    0
#define ACMP_OUTSTA_TYPE_SRC    1
#define ACMP_OUTSTA_TYPE_LATCH  2

/*===================================================================
[ACMP_Open] Open ACMP.
[ACMP_Close] Close ACMP.
 - nACMP_NO   : The ACMP Number. EX : ACMP_NO_0
 - nPSEL      : Select positive input source.
 - nHYS_RANGE : Set internal hysteresis value. EX : HYS_RANGE_0
===================================================================*/
void ACMP_Open(uint8_t nACMP_NO, uint8_t nPSEL, uint8_t nNSEL, uint8_t nHYS_RANGE);
void ACMP_Close(uint8_t nACMP_NO);

/*===================================================================
[ACMP_SetOutputCfg] Set output configuration.
 - nACMP_NO   : The ACMP Number. EX : ACMP_NO_0
 - nLATCHSEL  : Data latch select. EX : LATCHSEL_RAW_DATA
 - nPinInvert : Output pin invert, can be 0 or 1.
===================================================================*/
void ACMP_SetOutputCfg(uint8_t nACMP_NO, uint8_t nLATCHSEL, uint8_t nPinInvert);

/*===================================================================
[ACMP_GetOutputStatus] Get output status. return the status 0/1.
 - nACMP_NO     : The ACMP Number. EX : ACMP_NO_0
 - nOUTSTA_TYPE : Output status type. EX : ACMP_OUTSTA_TYPE_OUT
===================================================================*/
uint8_t ACMP_GetOutputStatus(uint8_t nACMP_NO, uint8_t nOUTSTA_TYPE);

/*===================================================================
[ACMP_SetTrgADCx] Set configuration to trigger ADCx.
 - nACMP_NO   : The ACMP Number. EX : ACMP_NO_0
 - nTRG_TYPE  : The trigger type. EX : ACMP_TRG_TYPE_FALLING
                If all you need, set nTRG_TYPE = (ACMP_TRG_TYPE_FALLING | ACMP_TRG_TYPE_RISING);
===================================================================*/
void ACMP_SetTrgADC0(uint8_t nACMP_NO, uint8_t nTRG_TYPE);


/*===================================================================
[ACMP_EnableIE] Enable ACMP interrupt.
[ACMP_DisableIE] Disable ACMP interrupt.
 - nACMP_NO  : The ACMP Number. EX : ACMP_NO_0
 - nIE_TYPE  : The interrupt type. EX : ACMP_IE_TYPE_FALLING
               If both you need, set nIE_TYPE = (ACMP_IE_TYPE_FALLING | ACMP_IE_TYPE_RISING | ACMP_IE_TYPE_LATCH);
===================================================================*/
void ACMP_EnableIE(uint8_t nACMP_NO, uint8_t nIE_TYPE);
void ACMP_DisableIE(uint8_t nACMP_NO, uint8_t nIE_TYPE);

/*===================================================================
[ACMP_GetIEStatus] Get interrupt status.
                    return 1, interrupt triggered.
                    return 0, no interrupt happened.
[ACMP_ClearIEStatus] Clear interrupt status.
 - nACMP_NO  : The ACMP Number. EX : ACMP_NO_0
 - nIE_TYPE  : The interrupt type. EX : ACMP_IE_TYPE_FALLING
               If both you need, set nIE_TYPE = (ACMP_IE_TYPE_FALLING | ACMP_IE_TYPE_RISING | ACMP_IE_TYPE_LATCH);
===================================================================*/
uint8_t ACMP_GetIEStatus(uint8_t nACMP_NO, uint8_t nIE_TYPE);
void    ACMP_ClearIEStatus(uint8_t nACMP_NO, uint8_t nIE_TYPE);

#endif
