#include "mcc_generated_files/mcc.h"
#include "RN41.h"
#include "stdbool.h"

/*
                         Main application
 */

void waitingForRN41Init(void){  //Define what happens when the program is waiting for the RN41 to initialize 
    D2_Toggle();                //Toggle the D2 LED from current state to opposite state
    D4_Toggle();                //Toggle the D4 LED from current state to opposite state

}

void main(void)
{
    uint16_t res;
    bool TransADCCVal;
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    printf("\r\nInitializing BT Click...\r\n");         //Send first command to Bluetooth module per datasheet 
    D2_SetHigh();                                       //Turn D2 ON
    RN41_RegisterWaitCallback(waitingForRN41Init);      //Defines what happens when you are waiting for the RN41 to respond to commands, references function waitingForRN41Init above
    RN41_Initialize();                                  //Intilization commands send to RN41
    D2_SetLow();                                        //Turn OFF D2
    D4_SetLow();                                        //Turn OFF D4
    D5_SetHigh();                                       //Turn ON D5
    TransADCCVal = false;                                 
    printf("\n\n\n\rClick S1 to begin toggle data transmission\r\n");
    while (1)
    {
        // Add your application code

        /* This logic allows you to turn on or off sending the ADCC Values to the RN41 by pressing the S2 switch on the Xpress Board */

        if(!SWITCH_GetValue()){                             //If S2 is pressed
            __delay_ms(150);                                //Debounce Switch
            TransADCCVal = !TransADCCVal;                   //Toggle TransADCCVal Value 
            if(!TransADCCVal){                              //If you are turning OFF the serial, then complete a carriage return
                printf("\r\n");
            }
        }

        if(TransADCCVal){                                   //If TransADCCVal value has not changed via previously loop
            res = ADCC_GetSingleConversion(Pot);            //Grab POT Value from ADCC
            printf("\rADCC = %03X", res);                   //Print POT Value
            __delay_ms(100);
        }

    }
}
/**
 End of File
*/