/*********************************************************************
 *                 Flowcode Cap Touch Component Code
 *
 * File: CapTouch.c
 *
 * (c) 2011 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 241111 | BR | Created component
 *        |    |
 *        |    |
 *        |    |
 *        |    |
 ********************************************************************/

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

 Return & parameter types:
   void
   MX_UINT8
   MX_SINT16
   MX_STRING

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={4FFA7653-1978-4FDA-8900-9DBF554C9255}
IsAnalogue=1
MultipleAllowed=1
Description=CapTouch Flowcode Component

**********************************************************************

NOTE: For simulation to work, these macros _MUST_ remain at the indicated index:
	1=Read_As_Byte
	2=Read_As_Int

[MacroNames]
Count=2
1=Read_As_Byte
2=Read_As_Int

[MacroReturns]
1=MX_UINT8
2=MX_UINT16

[MacroIsPrivate]
1=0
2=0

[MacroParameters_Read_As_Byte]
Count=0

[MacroParamTypes_Read_As_Byte]


[MacroParameters_Read_As_Int]
Count=0

[MacroParamTypes_Read_As_Int]


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

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
a = Unique Reference - Same as ADC Component
b = Which ADC Channel - Same as ADC Component
c = Acquisition time - Same as ADC Component
d = Conversion Speed - Same as ADC Component
e = VRef+ Option - Same as ADC Component
f = Number of filter iterations - default 5 times - range 1 to 15
g = Cap Touch Charge Time - default 125us - range 1 to 255
h = Cap Touch Discharge Time - default 125us - range 1 to 255
******************************/

//ADC Defines
#define %a_MX_ADC_CHANNEL		%b
#define %a_MX_ADC_ACTIME		%c
#define %a_MX_ADC_CONVSP		%d
#define %a_MX_ADC_VREFOP		%e

//Cap Touch Defines
#define %a_MX_ADC_FILTER		%f
#define %a_MX_ADC_CHARGE		%g
#define %a_MX_ADC_DISCHARGE		%h

//ADC CAL
#ifndef MX_ADC_CHANNEL_%b
 #define MX_ADC_CHANNEL_%b			//Inform CAL ADC channel %b is now in use.
#endif

#ifndef MX_ADC_REF					//Inform CAL ADC peripheral is now in use
 #define MX_ADC_REF
#endif

//Captouch CAL
#ifndef MX_CAPTOUCH_REF
 #define MX_CAPTOUCH_REF
#endif

//CAL Function Prototypes
extern void FC_CAL_Enable_ADC (MX_UINT8 Channel, MX_UINT8 Conv_Speed, MX_UINT8 Vref, MX_UINT8 T_Charge);
extern MX_UINT16 FC_CAL_Sample_ADC (MX_UINT8 Sample_Mode);
extern void FC_CAL_Disable_ADC (void);

extern void FC_CAL_CAP_Touch_Init (void);
extern void FC_CAL_CAP_Touch_Reset (void);
extern void FC_CAL_CAP_Touch_Drain (void);
extern void FC_CAL_CAP_Touch_Charge (void);
extern void FC_CAL_CAP_Touch_Stop (void);

/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/


MX_UINT8 Read_As_Byte (void)
{
/*Macro_Read_As_Byte_Start*/

	MX_UINT8 count = 0;
	MX_UINT16 RetVal = 0;

	FC_CAL_CAP_Touch_Init();

	//Configure & Enable ADC Channel
	FC_CAL_Enable_ADC ( %a_MX_ADC_CHANNEL , %a_MX_ADC_CONVSP , %a_MX_ADC_VREFOP , %a_MX_ADC_ACTIME );

	FC_CAL_CAP_Touch_Reset();

	for (count = 0; count < %a_MX_ADC_FILTER; count++)
	{
		FC_CAL_CAP_Touch_Drain();

		delay_us( %a_MX_ADC_CHARGE );

		FC_CAL_CAP_Touch_Charge();

		delay_us( %a_MX_ADC_DISCHARGE );

		FC_CAL_CAP_Touch_Stop();

		RetVal = RetVal + FC_CAL_Sample_ADC(0);			//Sample ADC as byte
	}

	if (%a_MX_ADC_FILTER > 1)
		RetVal = RetVal / %a_MX_ADC_FILTER;

	FC_CAL_Disable_ADC ();

	return (RetVal & 0xFF);

/*Macro_Read_As_Byte_End*/
}


MX_UINT16 Read_As_Int (void)
{
/*Macro_Read_As_Int_Start*/

	MX_UINT8 count = 0;
	MX_UINT16 RetVal = 0;

	FC_CAL_CAP_Touch_Init();

	//Configure & Enable ADC Channel
	FC_CAL_Enable_ADC ( %a_MX_ADC_CHANNEL , %a_MX_ADC_CONVSP , %a_MX_ADC_VREFOP , %a_MX_ADC_ACTIME );

	FC_CAL_CAP_Touch_Reset();

	for (count = 0; count < %a_MX_ADC_FILTER; count++)
	{
		FC_CAL_CAP_Touch_Drain();

		delay_us( %a_MX_ADC_CHARGE );

		FC_CAL_CAP_Touch_Charge();

		delay_us( %a_MX_ADC_DISCHARGE );

		FC_CAL_CAP_Touch_Stop();

		RetVal = RetVal + FC_CAL_Sample_ADC(1);			//Sample ADC as int
	}

	if (%a_MX_ADC_FILTER > 1)
		RetVal = RetVal / %a_MX_ADC_FILTER;

	FC_CAL_Disable_ADC ();

	return (RetVal);

/*Macro_Read_As_Int_End*/
}

