//****************************************************************************

#include "RGBLed.h"


/****************************************************************************
  Function:
    void RGBSetBlue( WORD saturation )

  Description:
    This function sets the blue RGB LED channel to the indicated saturation
    level.  If the saturation level is 0, then OxRS must be set to 0x100 to
    completely turn off that component of the color.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/
void RGBSetBlue( WORD saturation )
{
    if (saturation == 0)
    {
        OC3RS = 0x100;
    }
    else
    {
        OC3RS = CONVERT_TO_COLOR( saturation );
    }
}


/****************************************************************************
  Function:
    void RGBSetGreen( WORD saturation )

  Description:
    This function sets the green RGB LED channel to the indicated saturation
    level.  If the saturation level is 0, then OxRS must be set to 0x100 to
    completely turn off that component of the color.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/

void RGBSetGreen( WORD saturation )
{
    if (saturation == 0)
    {
        OC2RS = 0x100;
    }
    else
    {
        OC2RS = CONVERT_TO_COLOR( saturation );
    }
}


/****************************************************************************
  Function:
    void RGBSetRed( WORD saturation )

  Description:
    This function sets the red RGB LED channel to the indicated saturation
    level.  If the saturation level is 0, then OxRS must be set to 0x100 to
    completely turn off that component of the color.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/

void RGBSetRed( WORD saturation )
{
    if (saturation == 0)
    {
        OC1RS = 0x100;
    }
    else
    {
        OC1RS = CONVERT_TO_COLOR( saturation );
    }
}


/****************************************************************************
  Function:
    void RGBMapColorPins( void )

  Description:
    This function sets up the Peripheral Pin Select to configure the three
    color control PWM's to the correct pins as per the schematic.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/

void RGBMapColorPins( void )
{
    // Configure the LED Red Control, pins 31 (RP10) and 32 (RP17), for Output Compare 1 (function 18)
    RPOR5bits.RP10R  = 18;
    RPOR8bits.RP17R  = 18;

    // Configure the LED Green Control, pins 6 (RP19) and 8 (RP27), for Output Compare 2 (function 19)
    RPOR9bits.RP19R  = 19;
    RPOR13bits.RP27R = 19;

    // Configure the LED Blue Control, pins 4 (RP21) and 5 (RP26), for Output Compare 3 (function 20)
    RPOR10bits.RP21R = 20;
    RPOR13bits.RP26R = 20;

    RPOR4            = 0;   // AN8 and AN9 PPS, leave analog
}


/****************************************************************************
  Function:
    void RGBTurnOffLED( void )

  Description:
    This function turns off the LED by turning off the timers, turning off
    the PWM's, and setting the port pins to inputs.  It should be called at
    the completion of the RGB LED demo.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/

void RGBTurnOffLED( void )
{
    T2CON               = 0x0000;
    OC1CON1             = PWM_OFF;
    OC2CON1             = PWM_OFF;
    OC3CON1             = PWM_OFF;

    TRISFbits.TRISF4    = TRIS_INPUT;
    TRISFbits.TRISF5    = TRIS_INPUT;
    TRISGbits.TRISG8    = TRIS_INPUT;
    TRISGbits.TRISG9    = TRIS_INPUT;
    TRISGbits.TRISG6    = TRIS_INPUT;
    TRISGbits.TRISG7    = TRIS_INPUT;
}


/****************************************************************************
  Function:
    void RGBTurnOnLED( void )

  Description:
    This function turns on the LED by turning on the timers, turning on
    the PWM's, and setting the port pins to outputs.  It should be called at
    the beginning of the RGB LED demo.

  Precondition:
    None

  Parameters:
    None

  Returns:
    None

  Remarks:
    None
  ***************************************************************************/

void RGBTurnOnLED( WORD initialRed, WORD initialGreen, WORD initialBlue )
{
    // Initialize the timer for the PWM's.  We'll select a period value that
    // makes it simple to align the saturation values.
    T2CON                           = 0x0030;
    PR2                             = 0x00FF;

    // Initialize the PWM's
    RGBSetRed( initialRed );
    OC1R                            = 0;
    OC1CON2                         = PWM_CONFIGURATION_2;
    OC1CON1                         = PWM_CONFIGURATION_1;
    RGBSetGreen( initialGreen );
    OC2R                            = 0;
    OC2CON2                         = PWM_CONFIGURATION_2;
    OC2CON1                         = PWM_CONFIGURATION_1;
    RGBSetBlue( initialBlue );
    OC3R                            = 0;
    OC3CON2                         = PWM_CONFIGURATION_2;
    OC3CON1                         = PWM_CONFIGURATION_1;

    // Configure the PWM pins for output.
    TRISFbits.TRISF4                = TRIS_OUTPUT;   // Red
    TRISFbits.TRISF5                = TRIS_OUTPUT;
    TRISGbits.TRISG8                = TRIS_OUTPUT;   // Green
    TRISGbits.TRISG9                = TRIS_OUTPUT;
    TRISGbits.TRISG6                = TRIS_OUTPUT;   // Blue
    TRISGbits.TRISG7                = TRIS_OUTPUT;

    // Enable open drain.
    ODCF                            = 0x0030;       // Red
    ODCG                            = 0x03C0;       // Green and Blue

    // Turn on the timer.
    T2CON                           |= 0x8000;
}

/****************************************************************************
  Function:
    WORD ProcessMessageBarDemo( WORD translatedMsg, OBJ_HEADER* pObj,
                    GOL_MSG* pMsg )

  Description:
    This function processes the messages for the RGB LED =manipulation
    screen.  It allows the user to control the Red, Green, and Blue
    components of the light emitted by the LED.  The up and down touchpads
    select the component, and the right and left touchpads increase and
    decrease the saturation value. The center touchpad exits the demo.

  Precondition:
    Call ShowScreenRGB() prior to using this function to display the
    correct screen.

  Parameters:
    WORD translatedMsg  - The translated control-level message
    OBJ_HEADER* pObj    - Object to which the message applies
    GOL_MSG* pMsg       - The original system message information

  Return Values:
    0   - Do not call the default message handler.
    1   - Call the default message handler.

  Remarks:
    None.
  ***************************************************************************/
