/* Include Files */
#include "KTag.h"

/* Local Definitions and Constants */

#define CAPSENSE_TASK_PERIOD_IN_ms 50

/* Public Variables */

TaskHandle_t HW_CapSense_Task_Handle;

/* Private Variables */

static const TickType_t CapSense_Task_Delay = CAPSENSE_TASK_PERIOD_IN_ms / portTICK_PERIOD_MS;

static bool CapSense_One_Pressed = false;
static bool CapSense_Two_Pressed = false;

/* Private Function Prototypes */


/* Public Functions */

//! Initializes the capacitive touch sensing.
void HW_CapSense_Init(void)
{
}

//! Capacitive touch sensing task: Manages the capsense, using the PSoC API functions.
/*!
 *
 */
void HW_CapSense_Task(void * pvParameters)
{
    TickType_t xLastWakeTime;

    // Initialize the xLastWakeTime variable with the current time.
    xLastWakeTime = xTaskGetTickCount();
    
    // Start up the capsense component, and initiate the first scan.
    // Note that this can't be done in HW_CapSense_Init(), since it requires interrupts to be enabled.
    CapSense_Start();
    CapSense_ScanAllWidgets();
    
    vTaskDelayUntil(&xLastWakeTime, CapSense_Task_Delay);
    
    while (true)
    {
        // Check to see if the CapSense hardware is still busy with a previous scan.
        if (CapSense_IsBusy() == CapSense_NOT_BUSY)
        {
            // Process all the widgets and read the touch information.
            CapSense_ProcessAllWidgets();
            
            // Perform the on-change logic for "Button One".
            if (CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS0_ID))
            {
                if (CapSense_One_Pressed == false)
                {
                    KEvent_T switch_event = {.ID = KEVENT_CAPSENSE_ONE_PRESSED, .Data = NULL};
                    Post_KEvent(&switch_event);
                }
                CapSense_One_Pressed = true;
            }
            else
            {
                if (CapSense_One_Pressed == true)
                {
                    KEvent_T switch_event = {.ID = KEVENT_CAPSENSE_ONE_RELEASED, .Data = NULL};
                    Post_KEvent(&switch_event);
                }
                CapSense_One_Pressed = false;
            }
            
            // Perform the on-change logic for "Button Two".
            if (CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS1_ID))
            {
                if (CapSense_Two_Pressed == false)
                {
                    KEvent_T switch_event = {.ID = KEVENT_CAPSENSE_TWO_PRESSED, .Data = NULL};
                    Post_KEvent(&switch_event);
                }
                CapSense_Two_Pressed = true;
            }
            else
            {
                if (CapSense_Two_Pressed == true)
                {
                    KEvent_T switch_event = {.ID = KEVENT_CAPSENSE_TWO_RELEASED, .Data = NULL};
                    Post_KEvent(&switch_event);
                }
                CapSense_Two_Pressed = false;
            }
            
            // Initiate the next scan.
            CapSense_ScanAllWidgets();
        }
       
        vTaskDelayUntil(&xLastWakeTime, CapSense_Task_Delay);
    }
}

//! Gets the state of the given CapSense button.
/*!
 *  \param  button  the button in question
 *  \return         true if the button was pressed last time it was checked; false otherwise
 */
bool HW_IsCapsenseButtonPressed(HW_CapSenseButton_T button)
{
    bool pressed = false;
    
    if ((button == HW_CAPSENSE_BUTTON_ONE) && (CapSense_One_Pressed == true))
    {
        pressed = true;
    }
    else if ((button == HW_CAPSENSE_BUTTON_TWO) && (CapSense_Two_Pressed == true))
    {
        pressed = true;
    }
    
    return pressed;
}

/* Private Functions */

