/************	
	Author: Martin Hsu
	Email: service@icpdas.com
	Date 2007/11/6
	
	i8088 is a PWM module with 8 DI Pins can be defined as hardware trigger	pin
	it also can set PWM as synchrous mode
	
	[Normal PWM pulse define as duty]
	1. i8088W_InitLib(slot)
	2. i8088W_SetPWMCountMode(slot,ch,countMode); //0 burst; 1 continous
		if burst mode
			i8088W_SetBurstCount(slot,ch,burstCount);
	3. i8088W_SetPWMDuty(slot,ch,hz,duty);
	4. i8088W_PWM_Start(slot,ch);

	[Normal PWM pulse define pulse width]
	1. i8088W_InitLib(slot)
	2. i8088W_SetPWMCountMode(slot,ch,countMode); //0 burst; 1 continous
		if burst mode
			i8088W_SetBurstCount(slot,ch,burstCount);
	3. i8088W_SetPWMDutyCount(slot,ch,hz,highDutyCount,lowDutyCount);
	4. i8088W_PWM_Start(slot,ch);
	

	[Synchrous PWM pulse define as duty]
	1. i8088W_InitLib(slot)
	2. i8088W_SetPWMCountMode(slot,ch,countMode); //0 burst; 1 continous
		if burst mode
			i8088W_SetBurstCount(slot,ch,burstCount);
	3. i8088W_SetPWMDuty(slot,ch,hz,duty);
	4. i8088W_SetSyncChannel(slot,ch,enBit);//enBit=1 to set as synchrous channel
	5. i8088W_SetSyncStart(slot);
	
	[Hardware trigger PWM pulse define as duty]
	1. i8088W_InitLib(slot)
	2. i8088W_SetPWMCountMode(slot,ch,countMode); //0 burst; 1 continous
		if burst mode
			i8088W_SetBurstCount(slot,ch,burstCount);
	3. i8088W_SetPWMDuty(slot,ch,hz,duty);
	4. i8088W_SetHardwareTrigChannel(slot,ch,enBit);//enBit=1 to set as start flag; enBit=0 to set as stop flag.
	5. The wait the hardware DI pin trigger signal to start or to stop the PWM pulse;	
	
#define OK 		 				0	
#define ID_ERROR 				-1
#define SLOT_OUT_RANGE			-2
#define CHANNEL_OUT_RANGE		-3
#define SELECT_CHANNEL_ERROR 	-4
#define HI_DUTY_OUT_RANGE		-5
#define LO_DUTY_OUT_RANGE		-6
	
*******************************/




/********************************
	i8088 Error Code definition	
*******************************/




#include "..\..\lib\vp2k.h"
#include "..\..\Lib\8088W.h"

int slotIndex=-1;
int runFlag=1;
int startMode=0;
char title[]="Demo for i-8088 PWM";
void showMenu(int step)
{
    
    Print("\n\n****************************************************\n\r");    
    switch(step)
    {
    	case 1:
    		Print("Enter '1' Step 1 to set PWM mode.\n\r");
    	break;
    	case 2:
    		Print("Enter '2' Step 2 to set PWM duty.\n\r");
    	break;
    	case 3:
    		Print("Enter '3' Step 3 to start PWM.\n\r"); 
    	break;
    	case 4:
    		Print("Enter '4' Step 4 to stop PWM.\n\r"); 
    	break;    	
    }
 	Print("Enter 'Q' or 'q' to Exit the program.\n\r");
}
void Find_i8088W(void)
{
	int i,id,Base;
	
	
	for (i=0; i<GetNumberOfSlot(); i++)
	{
	    slotIndex=i;
	    
	    id=NameOfModule[i];
	    if (id==88) 
	    {
	    	Print("There is a i-8088 located at Slot %d\n",slotIndex);	
	    	return;    
	    }
	}
	slotIndex=0xff;
	Print("There is no i-8088 Found\n");	
}
int main(void)
{
	int ch;
    char c,c1,c2;
    
    unsigned int hz,cnt,duty,flag,flag2,flag3;
    float fhz,fduty;
    
    char buf[32];
    

    
    InitLib();
    Find_i8088W();
    Print("\n****************************");
    Print("\n%s",title);
    Print("\nLattice Version =: %04X",i8088W_GetFirmwareVersion(slotIndex));
    Print("\nLibrary Version =: %04X",i8088W_GetLibVersion());
    i8088W_GetLibDate(buf);
    Print("\nBuild Date =: %s",buf);
    Print("\n****************************\n");
    
    
    //showMenu(1);
    c='1';
    while(runFlag)
    {
       // c=Getch();
        switch(c)
        {
            case '1': // Set PWM Mode               
                Print("\n\n******** ~ SET PWM MODE ~ **********\n\r");  
                Print("Enter '0' to Set Burst Count\n");
                Print("Enter '1' to Set Continous\n");
                Print("Enter 'Q' or 'q' to Exit the program.\n\r");
                c1=Getch();
                 switch(c1)
        		{
        			case '0':
        				Print("Choice Burst Count Mode\n");
        				Print("Please Input Burst Count 0~65535\n");
        				Scanf("%u",&cnt);
        				for(ch=0;ch<8;ch++)
        				{
			    			i8088W_SetPWMCountMode(slotIndex,ch,0);
			    			i8088W_SetBurstCount(slotIndex,ch,cnt);
			    		}
			    		c='2';	  
			    	break;
			    	case '1':
			    		Print("Choice Continous Mode\n");
			    		for(ch=0;ch<8;ch++)
			    		{
			    			i8088W_SetPWMCountMode(slotIndex,ch,1);
			    			i8088W_SetBurstCount(slotIndex,ch,1);	
			    		}	
			    		c='2';	  	    	
			    	break;
		            case 'Q':case 'q':
		                runFlag= 0;
		            break;			    	
			    }
			      
                //showMenu(2);   
                     
            break;
            case '2': //Set PWM Duty
                Print("\n\n******** ~ SET PWM DUTY ~ **********\n\r");   
                Print("Enter '0' to Set Normal integer Duty,for example 50 means 50% duty\n");
                Print("Enter '1' to Set x10 integer Duty,for example 500 means 50.0% duty\n");
                Print("Enter '2' to Set float Duty,for example 50.0 means 50.0% duty\n");
                Print("Enter 'Q' or 'q' to Exit the program.\n\r");
                c1=Getch();
                 switch(c1)
        		{
        			case '0':
        				Print("Choice Set Normal integer Duty\n");
        				Print("First Input Hz 1~450000\n");
        				Scanf("%u",&hz);        			
        				Print("Then Input duty 1~99\n");
        				Scanf("%u",&duty);
        				Print("The PWM will be %u Hz with %d % Duty\n",hz,duty);
        				for(ch=0;ch<8;ch++)
        				{
			    			i8088W_SetPWMDuty(slotIndex,ch,hz,duty);
					    	//i8088W_PWM_Start(slotIndex,ch); 
					    }
					    //Print("PWM Start\n");   
					    c='3'; 	    
		               // showMenu(3);			    		
			    	break;
			    	case '1':
        				Print("Choice Set x10 integer Duty\n");
        				Print("First Input x10 Hz 1~4500000\n");
        				Scanf("%u",&hz);        			
        				Print("Then Input x10 duty 1~999\n");
        				Scanf("%u",&duty);
        				fhz=((float)hz)/10;
        				fduty=((float)duty)/10;
        				Print("The PWM will be %.1f Hz with %.1f % Duty\n",fhz,fduty);
        				for(ch=0;ch<8;ch++)
        				{
			    			i8088W_SetPWMDuty_Deci(slotIndex,ch,hz,duty);	  
					    	//i8088W_PWM_Start(slotIndex,ch);
					    } 
					    c='3'; 
					   // Print("PWM Start\n");    	    
		                			    		  	
			    	break;
			    	case '2':
			    		Print("Choice Set float Duty\n");
        				Print("First Input x.1 Hz 0.1~450000.0\n");
        				Scanf("%f",&fhz);        			
        				Print("Then Input x.1 duty 0.1~99.9\n");
        				Scanf("%f",&fduty);
        				Print("The PWM will be %.1f Hz with %.1f % Duty\n",fhz,fduty);
        				for(ch=0;ch<8;ch++)
        				{
			    			i8088W_SetPWMDuty_Float(slotIndex,ch,fhz,fduty);
					    	//i8088W_PWM_Start(slotIndex,ch); 
					    }	
					    c='3'; 
					   // Print("PWM Start\n");    
		                		    			    	
			    	break;
		            case 'Q':case 'q':
		                runFlag= 0;
		            break;			    		
			    	default:
			    		
			    	break;	    	
			    }   
                		    

            break;
            case '3':	//start PWM
                Print("\n\n******** ~ START OR STOP PWM ~ **********\n\r");  
                Print("Enter '0' to Set Normal PWM\n");
                Print("Enter '1' to Set Synchronous PWM\n");
                Print("Enter '2' to Set Hardware Trigger PWM\n");
                
                Print("Enter '3' to Set PWM Mode\n");                
                Print("Enter 'Q' or 'q' to Exit the program.\n\r");
                c1=Getch();
                 switch(c1)
        		{
        			case '0':  
        				//startMode=0; 
		                Print("\n\n******** ~ START OR STOP Normal PWM ~ **********\n\r");  
		                Print("Enter '0' to Stop Normal PWM\n");
		                Print("Enter '1' to Start Normal PWM\n"); 
		                Scanf("%u",&flag);  
		                if(flag==1)
		                {       				
	        				for(ch=0;ch<8;ch++)
	        				{     				      				
						    	i8088W_PWM_Start(slotIndex,ch); 
						    }
						    Print("PWM Start\n");  
						}
						else
						{
	        				Print("PWM Stoped\n");	
	        				for(ch=0;ch<8;ch++)
	        				{   
			    				i8088W_PWM_Stop(slotIndex,ch); 
			    			}							
						}
						
					      	    
		                //showMenu(3);
		                c='3';			    		
			    	break;
			    	case '1':  
		                Print("\n\n******** ~ Enable/Disable Synchronous PWM ~ **********\n\r");  
		                Print("Enter '0' to Disable Synchronous PWM\n");
		                Print("Enter '1' to Enable Synchronous PWM\n"); 
		                //flag2=Getch();
		                Scanf("%u",&flag2); 
		                if(flag2==1)
		                {       				
	        				for(ch=0;ch<8;ch++)
	        				{
				    			i8088W_SetSyncChannel(slotIndex,ch,1);	 
				    		} 
			    			Print("Set PWM as Synchronous mode\n\n"); 
			                Print("\n\n******** ~ Stop/Start Synchronous PWM ~ **********\n\r");  
			                Print("Enter '0' to Stop Synchronous PWM\n");
			                Print("Enter '1' to Start Synchronous PWM\n"); 
			                //flag3=Getch();
			                Scanf("%u",&flag3); 
			                if(flag3==1)
			                {
			                	i8088W_Sync_Start(slotIndex); 
			                }
			                else
			                {
			                	i8088W_Sync_Stop(slotIndex); 	
			                }			    			
				    		
				    	}
				    	else
				    	{
				    		Print("Set PWM as non-Synchronous mode\n");
	        				for(ch=0;ch<8;ch++)
	        				{				    		
				    			i8088W_SetSyncChannel(slotIndex,ch,0);				    			
				    		}
				    		 
				    	}				    		
					    
					     
					    c='3';  	    
		                //showMenu(3);			    		  	
			    	break;
			    	case '2':
		                Print("\n\n******** ~ Set Hardware Triggered PWM ~ **********\n\r");  
		                Print("Enter '0' to Set Hardware Triggered stop\n");
		                Print("Enter '1' to Set Hardware Triggered start\n"); 
		                //flag2=Getch();
		                Scanf("%u",&flag3); 
		                if(flag3==1)
		                { 
	        				for(ch=0;ch<8;ch++)
	        				{
				    			i8088W_SetHardwareTrigChannel(slotIndex,ch,1);	  
				    		}					    
						    Print("\n Set Hardware Triggered to start PWM\n");  
						}
						else
						{
			    			Print("Set Hardware Triggered to stop PWM \n");	
		    				for(ch=0;ch<8;ch++)
		    				{
				    			i8088W_SetHardwareTrigChannel(slotIndex,ch,0);	  
				    		}							
						}
					    c='3';  	    
		                //showMenu(3);			    		    			    	
			    	break;	
        			
		    		case '3':
		    			c='1';
		    			//showMenu(1); 
		    		break;		    	
		            case 'Q':case 'q':
		                runFlag= 0;
		            break;			    	
			    	
			    	default:
			    		Print("Select Error ,please select again \n");
			    		c='3';
			    		//showMenu(3); 
			    	break;	    	
			    }   
            break;                 
            
            case 'Q':case 'q':
                runFlag= 0;
            break;
        }    
    }    
    return 0;
}









