/****************************************************************************/
/*	8050 Demo																*/
/*      																	*/
/*	Compiler: BC++ 3.1														*/
/*	Compile mode: large														*/
/*	Project file:															*/
/*    	8050.c																*/
/*    	..\Lib\8000L.Lib  [2004/07/26 or later]								*/
/*    	MiniOS7 8k040921.IMG [2004/9/21 or later]			     			*/
/*    																		*/
/*  Warning:(English)														*/
/*     A channel may be destroyed if it is subjected to an input 			*/
/*	   signal(Vcc) while it is configured to be an output channel.			*/
/*	   Please remove the external signal which connects to DI,				*/
/*	   before changing the channel status from DI mode to DO mode			*/
/*																			*/
/*	Warning:(Traditional-Chinese)                                           */
/*  警告:()                                                                 */
/*		8050上有一個通道設定為DI且此通道有訊號輸入(Vcc)，如果此時將此通道的	*/
/*	    狀態設定為DO，則這個通道可能會損壞								    */
/*		通道狀態由DI改為DO之前，請先將DI的外部輸入線路移除					*/
/*    																		*/
/*	[25/Jul/2004] by Sean    												*/
/****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\..\lib\vp2k.h"

//#define MSC //Used for MSC
unsigned int NumberInput(int mode)
{
	
    unsigned char sTemp[20];
    unsigned int iRet;
    /*
	Parameter:
		 mode: 16 denotes Hex 
			   others denotes DEC
	   Return:
		       return a interger number					  
	*/	
#ifdef MSC
	LineInput(sTemp,20);
	if(mode==16)
		sscanf(sTemp,"%x",&iRet);
	else
		sscanf(sTemp,"%d",&iRet);

#else
	if(mode==16)	
		Scanf("%x",&iRet);
	else
		Scanf("%d",&iRet);
#endif
	return iRet;
}


void main (void)
{
    int i,iTemp,iRet,iQuit;
    unsigned char cSlot,cMode[3];
    unsigned int iChannel,imode,DIOdata,DOdata;
    unsigned char sTemp[20];
    
          
    Print("/*******************/\n\r");
    Print("/*    8050 demo    */\n\r");
    Print("/*                 */\n\r");
    Print("/*  [20,Oct,2003]  */\n\r");
    Print("/*******************/\n\r");
    Print("\n\r");
    
    Print("Please Input Slot Number(0 to 7)");
    
	cSlot=NumberInput(10);       //Use the NumberInput() of function ,input slot vlaue.

	Print("You Are using the slot %d\n\r\n\r",cSlot);
    
    while(1)
    {
        Print("  0) Set DIO configuration\n\r");
        Print("  1) Get DIO configuration\n\r");
        Print("  2) Write DIO\n\r");
        Print("  3) Read DIO\n\r");
        Print("  4) Write single channel DIO\n\r");//
        Print("  5) Read single channel DIO\n\r");
        Print("  6) Quit\n\r");
        Print("  Please choose(0~6):");
        
        iTemp=NumberInput(10);
        	
        switch(iTemp)
            {
            //Set DIO configuration
            case 0:
            	Print("\r\nPlease Input the new configured value(0000~FFFF) :");
            	DIOdata=NumberInput(16)& 0xffff; //Select DI and DO,use form of the hex.
            	UDIO_WriteConfig_16(cSlot,DIOdata);
                break;
            //Get DIO configuration
            case 1:
            	DIOdata=UDIO_ReadConfig_16(cSlot);
            	Print("\r\nThe configured value is %X\r\n",DIOdata);
                break;
            //Write DIO
            case 2:
            	Print("\r\nPlease Input the DO Data(Hex):");
            	DOdata=NumberInput(16)& 0xffff; 
            	UDIO_DO16(cSlot,DOdata);
            	Delay(5);
            	//Due to hardware limitation. It must delay 5 ms or more as reading di value, 
            	//after writing DO value.
            	Print("\r\nThe DIO data is %X\r\n",UDIO_DI16(cSlot));
                break;
            //Read DIO
            case 3:
            DIOdata=UDIO_DI16(cSlot);
            	Print("\r\nThe DIO data is %X\r\n",DIOdata);
                break;
            //Write single channel DIO
            case 4:
            	Print("\r\nPlease Input the channel(0~15):");
            	iChannel=NumberInput(10)& 0xf;
            	Print("\r\nPlease switch on or switch off channel(0:OFF,1:ON)");
            	iRet=NumberInput(10);
            	if(iChannel>15 || iRet>1)
            		Print("The invalid command, please input again\r\n");
                else
                {
                	DIOdata=UDIO_DI16(cSlot);
                	
                	
                	
                	if(iRet)
                	{
                		//Print("=====%d=====",DIOdata);
                		//DIOdata|= (1<< iChannel);
                		Print("DOdata:%X, iChannel:%d\r\n",DIOdata,iChannel);
                    }
                	else
                	{
                		DIOdata &= ~(1<<iChannel);
                		Print("DOdata:%X, iChannel:%d\r\n",DIOdata,iChannel);
                	}
                	UDIO_DO16(cSlot,DOdata);
                }
                break;
   		    //Write single channel DIO
            case 5:
            	Print("\r\nPlease Input the channel(0~15)");
            	iChannel=NumberInput(10)& 0xffff;
            	if(iChannel>15)
            		Print("The invalid command, please input again\r\n");
            	else
            	{
            		DOdata=UDIO_DI16(cSlot);	
            		DOdata=DOdata>> iChannel & 0x1;
            		Print("\r\nThe %d channel of DI is %X (0:OFF, 1:ON)\r\n",iChannel,DOdata);
            	}   		    		
                break;
            //Quit
            case 6:
            	exit(0);
            	break;   		       
   		}
        Print("Please any key to continue\n\r\n\r");
        Getch();
    }
}
