//----------------------------------------------------------------------------
// C main line
//----------------------------------------------------------------------------

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules

#define Mode_Button 00
#define Mode_Slider 01

extern BYTE  EzI2Cs_1_bRAM_RWcntr;
extern BYTE  EzI2Cs_1_bState;
struct I2C_Regs{
	BYTE bScanMode;
	unsigned int iI2Cbuf[10];
	WORD centroidPos;
}MyI2C_Regs;
WORD b;

void main(void)
{
		BYTE i;

	/* Enable the global and local interrupts */  
    M8C_EnableGInt;  
      
    //Start CSD UM
    CSD_Start();  
	CSD_InitializeBaselines() ; //scan all sensors first time, init baseline  
  	CSD_SetDefaultFingerThresholds() ;   
 
     
	// Set up RAM buffer  
	EzI2Cs_SetRamBuffer(sizeof(MyI2C_Regs),1,(BYTE *)&MyI2C_Regs); 
	EzI2Cs_Start();

    while(1)  
    { 
		EzI2Cs_DisableInt();
		CSD_ScanAllSensors(); //scan all sensors in array (buttons and sliders)  
        CSD_UpdateAllBaselines(); //Update all baseline levels;   

       	if(MyI2C_Regs.bScanMode == Mode_Button)//Copy button values
		for (i=0;i<5;i++)
        {
 	    	MyI2C_Regs.iI2Cbuf[i]=CSD_waSnsResult[i];
		}
		
		if(MyI2C_Regs.bScanMode == Mode_Slider)//Copy slider values
		for (i=0;i<10;i++)
        {
 	    	MyI2C_Regs.iI2Cbuf[i]=CSD_waSnsResult[i+5];
		}
		
		MyI2C_Regs.centroidPos = CSD_wGetCentroidPos(1); // Copy centroid value
		
		EzI2Cs_ResumeInt();
		for(b=2000; b; b--);
		while(EzI2Cs_bBusy_Flag);
	} 
}
