/* ========================================================================== 
 Demo for I-8088W

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 

   File level history (record changes for this file here.)

   v 0.0.1  16 Apr 2012 by Cindy Huang create, blah blah... *
                                                                        
   demo8088.c                                                               
   (c) 2012 Author                                                                                                                             
 ========================================================================== */

#include "msw.h"
#include <string.h>
#include <stdio.h> // for prototype for string interpretation routine
#include <stdlib.h>
#include "i8088W.h"

int slotIndex=-1;
int runFlag=1;
int startMode=0;
char title[]=" Demo for i-8088W PWM";

void showMenu(int step)
{
	printf("\n\n****************************************************\n\r");
	switch(step)
	{
		case 1:
			printf("Enter '1' Step 1 to set PWM mode.\n\r");
			break;
		case 2:
			printf("Enter '2' Step 2 to set PWM duty.\n\r");
			break;
		case 3:
			printf("Enter '3' Step 3 to start PWM.\n\r");
			break;
		case 9:
			printf("Enter '9' Step 4 to stop PWM.\n\r");
			break;
	}

	printf("Enter '9' to Exit the program.\n\r");
}

int main(void)
{
 	int ch,c,c1,c2;
	unsigned int cnt,duty,flag,flag2,flag3;
	unsigned long hz;
	float fhz,fduty;

	printf("Choice slot:");
	scanf("%d",&slotIndex);
	Open_Slot(slotIndex);
	i8088W_Init(slotIndex);
 
	printf("\n      %s ",title);
	printf("\n     Lattice Version =: %04X",i8088W_GetFirmwareVersion(slotIndex));
	printf("\n     Library Version =: %04X",i8088W_GetLibVersion());
    
    //showMenu(1);
	c=1;

while(runFlag)
{
	switch (c)
        {
        	case 1: // Set PWM Mode
                	printf("\n\n******** ~ SET PWM MODE ~ **********\n\r");
	                printf("Enter '0' to Set Burst Count\n");
        	        printf("Enter '1' to Set Continous\n");
	                printf("Enter '9' to Exit the program.\n\r");
        	        printf("*************************************\n\r");
			printf("Type your answer then press ENTER: ");
			scanf("%d",&c1);

			switch(c1)
			{
				case 0:
					printf("Choice Burst Count Mode\n");
					printf("Please Input Burst Count 0~65535: ");
					scanf("%u",&cnt);
					for(ch=0;ch<8;ch++)
					{
						i8088W_SetPWMCountMode(slotIndex,ch,0);
						i8088W_SetBurstCount(slotIndex,ch,cnt);
					}
					c=2;
					break;
				case 1:
					printf("Choice Continous Mode\n");
					for(ch=0;ch<8;ch++)
					{
						i8088W_SetPWMCountMode(slotIndex,ch,1);
						i8088W_SetBurstCount(slotIndex,ch,1);
					}
					c=2;
					break;
				case 9:
					runFlag= 0;
					break;
				default:
					printf("Select Error, please select again ! \n");
					break;
			}
	                //showMenu(2);
			break;
		case 2: //Set PWM Duty
			printf("\n\n****************** ~ SET PWM DUTY ~ ********************\n\r");
			printf("Enter '0' to Set Normal integer Duty,for example 50 means 50%% duty\n");
			printf("Enter '1' to Set x10 integer Duty,for example 500 means 50.0%% duty\n");
			printf("Enter '2' to Set float Duty,for example 50.0 means 50.0%% duty\n");
			printf("Enter '9' to Exit the program.\n\r");
			printf("*********************************************************\n\r");
			printf("Type your answer then press ENTER: ");
			scanf("%d",&c1);

			switch (c1)
			{
				case 0:
					printf("Choice Set Normal integer Duty\n");
					printf("First Input Hz 1~450000: ");
					scanf("%lu",&hz);
					printf("Then Input duty 1~99: ");
					scanf("%u",&duty);
					printf("The PWM will be %lu Hz with %d %% Duty\n",hz,duty);
					for(ch=0;ch<8;ch++)
					{
						i8088W_SetPWMDuty(slotIndex,ch,hz,duty);
						//i8088W_PWM_Start(slotIndex,ch); 
					}
					//printf("PWM Start\n");
					c=3;
					// showMenu(3);
					break;
				case 1:
					printf("Choice Set x10 integer Duty\n");
					printf("First Input x10 Hz 1~4500000: ");
					scanf("%lu",&hz);
					printf("Then Input x10 duty 1~999: ");
					scanf("%u",&duty);
					fhz=((float)hz)/10;
					fduty=((float)duty)/10;
					printf("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;
					// printf("PWM Start\n");
					break;
				case 2:
					printf("Choice Set float Duty\n");
					printf("First Input x.1 Hz 0.1~450000.0: ");
					scanf("%f",&fhz);
					printf("Then Input x.1 duty 0.1~99.9: ");
					scanf("%f",&fduty);
					printf("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;
					// printff("PWM Start\n");
					break;
				case 9:
					runFlag= 0;
					break;
			}
			break;
		case 3:	//start PWM
			printf("\n\n******** ~ START OR STOP PWM ~ **********\n\r");
			printf("Enter '0' to Set Normal PWM\n");
			printf("Enter '1' to Set Synchronous PWM\n");
			printf("Enter '2' to Set Hardware Trigger PWM\n");
			printf("Enter '3' to Set PWM Mode\n");
			printf("Enter '9' to Exit the program.\n\r");
			printf("*****************************************\n\r");
			printf("Type your answer then press ENTER: ");
			scanf("%d",&c1);

			switch (c1)
			{
				case 0:
					//startMode=0;
					printf("\n\n**** ~ START OR STOP Normal PWM ~ ****\n\r");
					printf("Enter '0' to Stop Normal PWM\n");
					printf("Enter '1' to Start Normal PWM\n"); 
					printf("****************************************\n\r");
					printf("Type your answer then press ENTER: ");
					scanf("%u",&flag);
					if(flag==1)
					{
						for(ch=0;ch<8;ch++)
							i8088W_PWM_Start(slotIndex,ch);
						printf("PWM Start\n");
					}
					else
					{
						printf("PWM Stoped\n");
						for(ch=0;ch<8;ch++)
							i8088W_PWM_Stop(slotIndex,ch);
					}
					//showMenu(3);
					c=3;
					break;
				case 1:
					printf("\n\n******** ~ Enable/Disable Synchronous PWM ~ **********\n\r");
					printf("Enter '0' to Disable Synchronous PWM\n");
					printf("Enter '1' to Enable Synchronous PWM\n");
					printf("*****************************************************\n\r");
					printf("Type your answer then press ENTER: ");
					scanf("%u",&flag2);
					if(flag2==1)
					{
						for(ch=0;ch<8;ch++)
							i8088W_SetSyncChannel(slotIndex,ch,1);
						printf("Set PWM as Synchronous mode\n\n");
						printf("\n\n********** ~ Stop/Start Synchronous PWM ~ ***********\n\r");
						printf("Enter '0' to Stop Synchronous PWM\n");
						printf("Enter '1' to Start Synchronous PWM\n");
						printf("*****************************************************\n\r");
						printf("Type your answer then press ENTER: ");
						scanf("%u",&flag3);
						if(flag3==1)
							i8088W_Sync_Start(slotIndex);
						else
							i8088W_Sync_Stop(slotIndex);
					}
					else
					{
						printf("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:
					printf("\n\n******** ~ Set Hardware Triggered PWM ~ *********\n\r");
					printf("Enter '0' to Set Hardware Triggered disable\n");
					printf("Enter '1' to Set Hardware Triggered start\n");
					printf("Enter '2' to Set Hardware Triggered stop\n");
					printf("***************************************************\n\r");
					printf("Type your answer then press ENTER: ");
					scanf("%u",&flag3);
					switch (flag3) {
						case 0:   //disable
							printf("Set Hardware Triggered to disable PWM\n");
							break;
						case 1:   //start
							printf("Set Hardware Triggered to start PWM\n");
							break;
						case 2:   //stop
							printf("Set Hardware Triggered to stop PWM \n");
							break;
					}

					for(ch=0;ch<8;ch++)
						i8088W_SetHardwareTrigChannel(slotIndex,ch,flag3);
					c=3;  	    
					//showMenu(3);
					break;	
				case 3://SET PWM MODE
					c=1;
					//showMenu(1);
					break;
				case 9:
					runFlag= 0;
					break;
				default:
					printf("Select Error, please select again ! \n");
					c=3;
					//showMenu(3);
					break;
			}
			break;
		case 9:
			runFlag= 0;
			break;
		}
	}

	Close_Slot(slotIndex);
	return 0;
}

