#include "mcc_generated_files/system.h"
#include "mcc_generated_files/mcc.h"

// sensor initial configuration
const uint8_t myCAP1xxxConfig[][2] =   
{   //  Address     Value
    {   0x00,       0x00    },      // Main Control
    {   0x1F,       0x2F    },      // Sensitivity Control
    {   0x20,       0x20    },      // General Configuration
    {   0x21,       0xFF    },      // Sensor Input Enable
    {   0x22,       0xA4    },      // Sensor Input Configuration
    {   0x23,       0x07    },      // Sensor Input Configuration 2
    {   0x24,       0x39    },      // Averaging and Sampling Config
    {   0x26,       0x00    },      // Calibration Activate
    {   0x27,       0xFF    },      // Interrupt Enable
    {   0x28,       0xFF    },      // Repeat Rate Enable
};

int main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // the CAP1xxx device needs at least 200ms at power up to respond the I2C command
    // this is a user-defined function to delay at least 200ms before excute CAP1xxx initialization
    my_delay_function();

    // initialize the CAP1xxx device
    CAP_init(myCAP1xxxConfig,sizeof(myCAP1xxxConfig)/2);

    // wait until all the sensor is calibrated.
    while(!CAP_isCalibrated());

    while (1)
    {
        CAP_GENERAL_STATUSbits_t CAP_current_status;

        // read out the general status of the CAP1xxx device
        CAP_current_status.value = CAP_getGeneralStatus();

        if(CAP_current_status.TOUCH)
        {
            // Touch Event
            CAP_SENSOR_INPUT_STATUSbits_t CAP_sensor_input_status;
            // Read out sensor status to check which sensor is pressed
            CAP_sensor_input_status.value = CAP_getSensorStatus();

            printf("Sensor Status = %d \r\n",CAP_sensor_input_status.value);

            if (CAP_sensor_input_status.CS1)
            {
                // application process if CS1 is pressed
                LED_SetHigh();
            }
            else
            {
                // application process if CS1 is released 
                LED_SetLow();

            }

        }
    }
    return 1; 
}
}