//Mini Evalution Board test program
//Cypress 2006
#include <m8c.h>        // Part specific constants and macros  
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules  

#define Led2Cmd		1	//Led 2 select command
#define Led1Cmd		2	//Led 1 select command
#define EndCmd		1	//End of commands

#define ScanCycles	200//551	//for assume 100 ms integration time
extern BYTE  EzI2Cs_bRAM_RWcntr;
  
struct I2C_Regs {   	 //I2C interface structure  
    unsigned int bTemperature;	//Temperature value 3..4
    unsigned int bLight;		//Light value 5..6
    BYTE bCmd;  		 //Command word 0
    BYTE bParameter1;	 //Parameter word 1
    BYTE bCmdEnd;		 //End of command flag 2

} I2C_Regs;  
  
//Small Delay for data stabilization
void delay(void)
{
int i;
for (i=0; i< 15000; i++);
}

//Main cycle
void main()  
{  
BYTE bOldReg;				
unsigned int tmp=0, i=0;	//i - loop counter, tmp - temporary variable for Light measurement


   bOldReg=ACE01CR1;	//Save current settings

   PWM8_1_Start();		//Start PWM 1
   PWM8_2_Start();		//Start PWM 2
   
   I2C_Regs.bCmdEnd=0;	
   
   // Set up RAM buffer  
   EzI2Cs_SetRamBuffer(sizeof(I2C_Regs), 7, (BYTE *) &I2C_Regs);  
  
   M8C_EnableGInt ;                           // Turn on interrupts  

   EzI2Cs_Start();                            // Turn on I2C  
   
   ADC10_1_Start(ADC10_1_FULLRANGE);  		  // Start the User Module  
   ADC10_1_iCal(0x010A, ADC10_1_CAL_VBG);     // Calibrate the ADC so 1.3V = 0x10A  
   ADC10_1_StartADC();                		  // Start  ADC
	
   while(ADC10_1_fIsDataAvailable() == 0);  
   tmp = ADC10_1_iGetDataClearFlag();  			//Read Light data
   
   //Loop
   while(1) {  
  		//Led 1 driver procedure 
   		if (I2C_Regs.bCmd == Led1Cmd & I2C_Regs.bCmdEnd == 1)
		   	{
		   	PWM8_1_WritePulseWidth(I2C_Regs.bParameter1);
		   	I2C_Regs.bCmdEnd=0;
		   	}
		//Led 2 driver procedure   	
		if (I2C_Regs.bCmd == Led2Cmd & I2C_Regs.bCmdEnd == 1)
		   	{
		   	PWM8_2_WritePulseWidth(I2C_Regs.bParameter1);
		   	I2C_Regs.bCmdEnd=0;
		   	}
		//Read Light data 
	 	while(ADC10_1_fIsDataAvailable() == 0);  
	 	tmp = (tmp + ADC10_1_iGetDataClearFlag())/2; 
		//Increment loop counter
    	i++;
    	//If data integration complete
		if (i == ScanCycles)
		 {
	 	    M8C_DisableGInt ;       // Turn off interrupts  
		 	if (EzI2Cs_bRAM_RWcntr!=3) I2C_Regs.bLight = tmp;	//apply new light value
	 	    M8C_EnableGInt ;        // Turn on interrupts  
	 	    
		 	i=0;		 			//reset loop counter
			//Switch ADC input to temperature sensor
		 	ACE01CR1=0x68;
		 	delay();
		 	
			//Wait for data stabilizations
			while(ADC10_1_fIsDataAvailable() == 0);  
		 	ADC10_1_iGetDataClearFlag();
			
			while(ADC10_1_fIsDataAvailable() == 0);  
		 	ADC10_1_iGetDataClearFlag();
		 			 	
		 	//Read temperature
		 	while(ADC10_1_fIsDataAvailable() == 0);  
	 	    
	 	    M8C_DisableGInt ;       // Turn off interrupts  
    	 	if (EzI2Cs_bRAM_RWcntr!=1) I2C_Regs.bTemperature=ADC10_1_iGetDataClearFlag();
	 	    M8C_EnableGInt ;        // Turn on interrupts  
	 	    		 	
		 	//Switch ADC input to light sensor
		 	ACE01CR1=bOldReg;  
		 	
		 	//Wait for data stabilizations
		 	while(ADC10_1_fIsDataAvailable() == 0);  
		 	ADC10_1_iGetDataClearFlag();

		 	while(ADC10_1_fIsDataAvailable() == 0);  
		 	ADC10_1_iGetDataClearFlag();
		 	
		 }
   	}  //loop
}//main  
