/* demo36.C COM1:-->sst-2400 COM2:-->(01)7016,(02)7033,(03)7065,(04)7060 COM3:-->MMIOCN COM3:-->PC1 mmicon.c : Demo program for I-7188 to connect MMICON modify from MMI.C, the demo program for MMICON that is run on PC Use I-7188 COM3(115200,N,8,1) connect to MMICON MMICON.EXE must run on I-7188 MMIOCN: (1)LCD 240*64 --> character display=30*8 RunMMI(): (1) test MMICON's screen. (2) the key input from PC's keyboard or from the MMICON's keyboard will show on MMICON's LCD display & PC monitor(for test) (3) Use must define his own keymap for the MMICON's keyboard (4) press CTRL_C to quit the test. **** please set MMICON 's baudrate to 115200 (get the better performance), use command %AANNTTBB00 (MMIDOS software user manual page 28) where BB=03 -->1200 04 -->2400 05 -->4800 06 -->9600 07 -->19200 08 -->38400 09 -->57600 0A -->115200 <---- (08,09,0A also can use) and please set to mode 1 or 2 (PC mode)(TT=01 or 02) the program use addr=00 StopWatch #0 --> MMICON key input #1 --> timeout for MMICON */ #include #include #include #include #include #include #include"..\lib\7188.h" /* #define _ShowMmiMsg_ #define _ShowCom2Msg_ */ #define KEY_DN 0x17 #define KEY_UP 'u' #define KEY_DOWN 'x' #define KEY_LEFT 'l' #define KEY_RIGHT 'r' #define KEY_F1 'a' #define KEY_F2 'b' #define KEY_F3 'c' #define KEY_F4 'd' #define KEY_0 0x01 #define KEY_1 0x04 #define KEY_2 0x05 #define KEY_3 0x06 #define KEY_4 0x08 #define KEY_5 0x09 #define KEY_6 0x0a #define KEY_7 0x0c #define KEY_8 0x0d #define KEY_9 0x0e #define KEY_BS 0x0F #define KEY_PLUS 0x07 #define KEY_MINUS 0x0B #define KEY_Enter1 0x03 #define KEY_Enter2 0x13 #define StopWatchMmiKey 0 #define StopWatchMmi 1 #define StopWatch7000 3 #define StopWatch7060 7 #define StopWatchLed 6 extern unsigned _stklen=8192; int AddMmiCmd(int cmd,int p0,int p1,int p2); int IsMmiCmd(void); char Buf[100]; unsigned long MmiTime; unsigned char Com2Buf[80]={0}; int Com2Idx=0; int fCom2HasDataIn=0; unsigned long Timeout=500; /* default timeout=0.5 sec */ int bWait485Echo=0; int fTimeOut=0; float data0=100.00; float data1=200.00; float data2=300.00; float data3=400.00; float da=50.000; float ad=60.000; int DO1=0x0A; int DO2=0x05; int DemoMode=1; enum _MMI_MODE_ { _MMI_CheckKey=1, _MMI_ShowCursor, _MMI_HideCursor, _MMI_Putch, _MMI_Puts, _MMI_ChangePage, }; int MmiAddr=0; int ComPort=3; long BaudRate=9600; char szCmd[80],szResult[80],szKeys[16]; unsigned A,B,C,D,E,P,AA,BB,CC,DD,EE,PP; /* ---- show status ------------------------------------------------------ */ void show_status(void) { printf("\n* STATUS : COM=%d,",ComPort); printf(" Baud_Rate=%5d *",BaudRate); printf("\n*----------------------------------------------------*"); } /* ---- send_and_receive -------------------------------------*/ int send_and_receive(char *Cmd, char *Result) { SendCmdTo7000(ComPort,Cmd,0); return ReceiveResponseFrom7000(ComPort,Result,10000,0); } /* ---- function 1 -------------------------------------------------*/ void pc_demo_1(void) { int iRet; int quit=0; char Cmd[7]="$00P00"; int page=0; Cmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ Cmd[2]=hex_to_ascii[MmiAddr%16]; while(!quit) { szResult[0]=0; Cmd[5]='0'+page; iRet=send_and_receive(Cmd,szResult); printf("\nPage%d, RetVal=%d, Result=%s, press any key to stop", page,iRet,szResult); Delay(1000); if (kbhit4()) {getch4(); quit=1;} else { page++; if(page>4) page=0; } } } void show_val_1(int row, int col, int val) { char str[10]; int i,j; strcpy(szCmd,"$00T000 "); szCmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ szCmd[2]=hex_to_ascii[MmiAddr%16]; szCmd[4]=row+'0'; szCmd[5]=hex_to_ascii[col/16]; szCmd[6]=hex_to_ascii[col%16]; itoa(val,szCmd+7,10); for (i=0; i<11; i++) if (szCmd[i]==0) szCmd[i]=' '; /* sprintf(str,"%d",val); strcat(szCmd,str); */ send_and_receive(szCmd,szResult); Delay(100); } void show_cursor(int p) { if(PP!=0) { switch (PP) { case 1 : sprintf(szCmd,"$00T106 "); break; case 2 : sprintf(szCmd,"$00T306 "); break; case 3 : sprintf(szCmd,"$00T506 "); break; } szCmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ szCmd[2]=hex_to_ascii[MmiAddr%16]; send_and_receive(szCmd,szResult); Delay(10); } switch (p) { case 1 : sprintf(szCmd,"$00T106>"); break; case 2 : sprintf(szCmd,"$00T306>"); break; case 3 : sprintf(szCmd,"$00T506>"); break; } szCmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ szCmd[2]=hex_to_ascii[MmiAddr%16]; send_and_receive(szCmd,szResult); Delay(10); } int KBHIT(void) { int i,k,iRet,key,key1,key2,j; char Cmd[5]="$00K"; k=0; Cmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ Cmd[2]=hex_to_ascii[MmiAddr%16]; iRet=send_and_receive(Cmd,szResult); if(iRet==NoError){ j=4; while (szResult[j]!=0) { if (j==4) for (i=0; i<16; i++) szKeys[i]=0; key1=ascii_to_hex(szResult[j]); key2=ascii_to_hex(szResult[j+1]); key=key1*16+key2; szKeys[k++]=key; j+=2; iRet=1; } } return(iRet); } void key_plus(int p) { switch(p) { case 1 : A++; break; case 2 : B++; break; case 3 : C++; break; } } void key_minus(int p) { switch(p) { case 1 : A--; break; case 2 : B--; break; case 3 : C--; break; } } /* ---- function 2 -------------------------------------------------*/ void pc_demo_2(void) { int iRet,key,i,j,k; char str[10]; int quit=0; char Cmd[7]="$00P02"; szResult[0]=0; Cmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ Cmd[2]=hex_to_ascii[MmiAddr%16]; iRet=send_and_receive(Cmd,szResult); printf("\nPage2, RetVal=%d, Result=%s, press any key to stop", iRet,szResult); Delay(300); A=1; B=2; C=3; D=A+B; E=B+C; P=1; AA=BB=CC=DD=EE=PP=0; while(!quit){ if (A!=AA) {show_val_1(1,7,A); AA=A;} if (B!=BB) {show_val_1(3,7,B); BB=B;} if (C!=CC) {show_val_1(5,7,C); CC=C;} if (D!=DD) {show_val_1(2,20,D); DD=D;} if (E!=EE) {show_val_1(4,20,E); EE=E;} if (P!=PP) {show_cursor(P); PP=P;} if (KBHIT()!=0) { i=0; while (szKeys[i]!=0) { key=szKeys[i++]; switch(key){ case KEY_UP : P--; if (P<1) P=3; break; case KEY_DN : P++; if (P>3) P=1; break; case KEY_PLUS : key_plus(P); break; case KEY_MINUS: key_minus(P); break; case KEY_Enter1 : case KEY_Enter2 :break; } } D=A+B; E=B+C; } if (kbhit4()!=0) {getch4(); quit=1;} } } /* ---- function 3 -------------------------------------------------*/ void pc_demo_3(void) { int iRet,i,j,key,key1,key2; char str[4],show=0; int quit=0; char Cmd[7]="$00P03"; szResult[0]=0; Cmd[1]=hex_to_ascii[MmiAddr/16]; /* address */ Cmd[2]=hex_to_ascii[MmiAddr%16]; iRet=send_and_receive(Cmd,szResult); printf("\ndemo_3, RetVal=%d, Result=%s, press any key to stop", iRet,szResult); Delay(300); sprintf(szCmd,"$%02XT500PC Demo 3,NO UP/DW",MmiAddr); iRet=send_and_receive(szCmd,szResult); printf("\ndemo_3, RetVal=%d, Result=%s, press any key to stop", iRet,szResult); i=0; while(!quit) { show=0; sprintf(szCmd,"$%02XT20A",MmiAddr); sprintf(str,"%d",i); strcat(szCmd,str); iRet=send_and_receive(szCmd,szResult); printf("\ndemo_3, RetVal=%d, Result=%s, press any key to stop", iRet,szResult); sprintf(szCmd,"$%02XK",MmiAddr); iRet=send_and_receive(szCmd,szResult); if(iRet==0) { j=4; while (szResult[j]!=0) { key1=ascii_to_hex(szResult[j]); key2=ascii_to_hex(szResult[j+1]); key=key1*16+key2; printf("\nReceive KEY_CODE=%x",key); if (key==KEY_F1) {i=100; show=1; break;} else if (key==KEY_F2) {i=200; show=1; break;} else if (key==KEY_F3) {i=300; show=1; break;} else if (key==KEY_F4) {i=400; show=1; break;} j+=2; } } i++; if(show!=1) Delay(1000); if(kbhit4()) {getch4(); quit=1;} } } /* ---- function S ------------------------------------------------- */ void pc_fun_s(void) { int iRet; printf("\nCommand="); scanf("%s",szCmd); iRet=send_and_receive(szCmd,szResult); if (iRet==0) printf("Send Command OK, Receive =%s",szResult); else if (iRet==TimeOut) printf("Receive Result TimeOut"); else printf(" --> Error ?"); } /* ----------------------------------------------------------------- */ int change_to_mode_1(void) { int iRet; printf("\n"); sprintf(szCmd,"$%02XM",MmiAddr); iRet=send_and_receive(szCmd,szResult); if (iRet==TimeOut) { printf("Receive Result TimeOut"); return 1; } printf("Send [$%02XM], Receive [%s]",MmiAddr,szResult); if(strncmp(szResult+3,"MMICON",6)){ printf("can't find the MMICON"); return 1; } else { sprintf(szCmd,"%02X%02X010600",MmiAddr,MmiAddr); iRet=send_and_receive(szCmd,szResult); if (iRet==TimeOut) { printf("Receive Result TimeOut"); return 1; } printf("\nchange to PC mode OK"); } return 0; } void MmiChangeToPage(int page) { AddMmiCmd(_MMI_ChangePage,page,0,0); } char ScrBuf[8][31]; int mmix=0,mmiy=0; int MmiPage=5; int NewMmiPage; void MmiShowCursor(int x,int y) { AddMmiCmd(_MMI_ShowCursor,x,y,0); ScrBuf[y][x]=0; } void MmiHideCursor(int x,int y) { AddMmiCmd(_MMI_HideCursor,x,y,0); } /* void MmiGotoxy(int x,int y) { mmix=x; mmiy=y; MmiShowCursor(); } */ char *BlankLine=" "; char OutLinBuf0[8][16]={ " ", " ", " ", " ", " ", " ", " ", " ", }; char OutLinBuf1[8][16]={ " ", " ", " ", " ", " ", " ", " ", " ", }; void MmiPutLine(int y,char *str) { y&=7; sprintf(ScrBuf[y],"%-30.30s",str); memmove(OutLinBuf0[y],ScrBuf[y],15); memmove(OutLinBuf1[y],ScrBuf[y]+15,15); AddMmiCmd(_MMI_Puts,0,y,(int)OutLinBuf0[y]); /* only for SMALL model */ AddMmiCmd(_MMI_Puts,15,y,(int)OutLinBuf1[y]); /* only for SMALL model */ } void MmiClearLine(int y) { y&=7; sprintf(ScrBuf[y],"%30.30s",""); AddMmiCmd(_MMI_Puts,0,y,(int)BlankLine); /* only for SMALL model */ AddMmiCmd(_MMI_Puts,15,y,(int)BlankLine); /* only for SMALL model */ } void MmiScroll(void) { int i; for(i=0;i<7;i++){ MmiPutLine(i,ScrBuf[i+1]); } mmiy=7; } void MmiPutch(int data) { switch(data){ case '\b': if(mmix){ MmiHideCursor(mmix,mmiy); ScrBuf[mmiy][--mmix]=' '; MmiShowCursor(mmix,mmiy); } break; case '\r': ScrBuf[mmiy][mmix]=0; if(mmiy<7){ MmiHideCursor(mmix,mmiy); } mmiy++; mmix=0; if(mmiy>=8){ MmiScroll(); } MmiClearLine(mmiy); MmiShowCursor(mmix,mmiy); break; default: AddMmiCmd(_MMI_Putch,mmix,mmiy,data); ScrBuf[mmiy][mmix]=data; mmix++; if(mmix>=30){ mmix=0; mmiy++; if(mmiy>=8){ MmiScroll(); } MmiClearLine(mmiy); } MmiShowCursor(mmix,mmiy); break; } } void MmiInitScreen(void) { int i,j; /* MmiChangeToPage(10); */ for(i=0;i<8;i++){ for(j=0;j<30;j++) ScrBuf[i][j]=' '; ScrBuf[i][j]=0; } /* MmiShowCursor(mmix,mmiy); */ } #define KEY_SIZE 32 int mmikeycode[]={ /* user can(must) define the keymap he needed */ 1 , /* 0 */ '0' , /* 1 */ '.' , /* 2 */ '\r' , /* 3 */ '1' , /* 4 */ '2' , /* 5 */ '3' , /* 6 */ '+' , /* 7 */ '4' , /* 8 */ '5' , /* 9 */ '6' , /* A */ '-' , /* B */ '7' , /* C */ '8' , /* D */ '9' , /* E */ '\b' , /* F */ 1 , /* 10 */ KEY_LEFT , /* 11 */ KEY_RIGHT, /* 12 */ '\r' , /* 13 */ 'A' , /* 14 */ 'B' , /* 15 */ 'C' , /* 16 */ KEY_DOWN , /* 17 */ 'D' , /* 18 */ 'E' , /* 19 */ 'F' , /* 1A */ KEY_UP , /* 1B */ KEY_F1 , /* 1C */ KEY_F2 , /* 1D */ KEY_F3 , /* 1E */ KEY_F4 , /* 1F */ }; int mmikey[KEY_SIZE]; int in_idx=0,out_idx=0; int AddMmiKey(int key) { int nextidx; nextidx=in_idx+1; if(nextidx>=32) nextidx=0; if(nextidx!=out_idx){ mmikey[in_idx]=key; in_idx=nextidx; return 0; } else return 1; /* keyboard buffer overflow */ } int MmiKbhit(void) { return in_idx-out_idx; } int MmiReadKey(void) { int key; if(MmiKbhit()){ key=mmikeycode[mmikey[out_idx++]]; if(out_idx>=KEY_SIZE) out_idx=0; } else return -1; return key; } int MmiWaitEcho=0; int MmiEchoIdx=0; unsigned long MmiEchoNo=0; int MmiEchoLength=0; char MmiEchoBuf[80]; int MmiMode=0; void MmiCheckKey(void) { if(!MmiMode && !IsMmiCmd()){ AddMmiCmd(_MMI_CheckKey,0,0,0); } } void MmiCheck(void) { int j; if(!MmiWaitEcho){ if(MmiMode){ switch(MmiMode){ case _MMI_CheckKey: /* check key */ j=4; while (MmiEchoBuf[j]) { AddMmiKey((ascii_to_hex(MmiEchoBuf[j])<<4) +ascii_to_hex(MmiEchoBuf[j+1])); j+=2; } if(j==4){ StopWatchReadValue(StopWatchMmiKey,&MmiTime); if(MmiTime>=60000L){ MmiTime-=60000L; MmiTime%=20000; MmiTime/=2000; if(MmiPage!=MmiTime){ MmiChangeToPage(MmiTime); } } } else { } MmiMode=0; break; case _MMI_ShowCursor: MmiMode=0; break; case _MMI_HideCursor: MmiMode=0; break; case _MMI_Putch: MmiMode=0; break; case _MMI_Puts: MmiMode=0; break; case _MMI_ChangePage: { static int cnt=0; if(cnt==0){ MmiPage=NewMmiPage; } MmiMode=0; } break; } } } } void MmiConFun(void) { int data; int checktime=1; if(MmiWaitEcho){ while(IsCom3()){ checktime=0; data=ReadCom3(); if(data=='\r'){ MmiEchoBuf[MmiEchoIdx]=0; MmiEchoLength=MmiEchoIdx; /* Check MMICON echo message */ MmiWaitEcho=0; MmiEchoIdx=0; /* tets */ #ifdef _ShowMmiMsg_ sprintf(Buf,"\n\rMmi[%lu]:%s",++MmiEchoNo,MmiEchoBuf); ToCom4Str(Buf); #endif /* end test */ return; } else { MmiEchoBuf[MmiEchoIdx++]=data; } } if(checktime){ StopWatchReadValue(StopWatchMmi,&MmiTime); if(MmiTime>=2000){ #ifdef _ShowMmiMsg_ ToCom4Str("\n\rMMICON Timeout"); #endif MmiWaitEcho=0; MmiEchoIdx=0; MmiMode=0; } } } } #define MaxCmdBufNo 256 int MmiCmd[MaxCmdBufNo]; int MmiCmdParam[MaxCmdBufNo][3]; int Cmdin_idx=0,Cmdout_idx=0; int AddMmiCmd(int cmd,int p0,int p1,int p2) { int nextidx; nextidx=Cmdin_idx+1; if(nextidx>=MaxCmdBufNo) nextidx=0; if(nextidx!=Cmdout_idx){ /* test */ #ifdef _ShowMmiMsg_ if(cmd != _MMI_CheckKey){ sprintf(Buf,"\n\r[AddMmiCmd](%d)(%d,%d,%d)",cmd,p0,p1,p2); ToCom4Str(Buf); } #endif /* end test */ MmiCmd[Cmdin_idx]=cmd; MmiCmdParam[Cmdin_idx][0]=p0; MmiCmdParam[Cmdin_idx][1]=p1; MmiCmdParam[Cmdin_idx][2]=p2; Cmdin_idx=nextidx; return 0; } else return 1; /* keyboard buffer overflow */ } int oCmdin_idx=11,oCmdout_idx=11; int IsMmiCmd(void) { /* if(Cmdin_idx!=oCmdin_idx || Cmdout_idx!=oCmdout_idx){ printf("\nIn=%d,Out=%d",Cmdin_idx,Cmdout_idx); oCmdin_idx=Cmdin_idx; oCmdout_idx=Cmdout_idx; } */ return Cmdin_idx-Cmdout_idx; } int MmiDoNextCmd(void) { if(MmiMode) return -1; if(IsMmiCmd()){ MmiMode=MmiCmd[Cmdout_idx]; /* test */ #ifdef _ShowMmiMsg_ if(MmiMode != _MMI_CheckKey){ sprintf(Buf,"\n\r[DoCmd](%d)(%d,%d,%d)",MmiMode, MmiCmdParam[Cmdout_idx][0],MmiCmdParam[Cmdout_idx][1], MmiCmdParam[Cmdout_idx][2]); ToCom4Str(Buf); } #endif /* end test */ switch(MmiMode){ case _MMI_CheckKey: sprintf(szCmd,"$%02XK\r",MmiAddr); ToCom3Str(szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMmi); break; case _MMI_ShowCursor: sprintf(szCmd,"$%02XT%d%02X_\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0]); ToCom3Str(szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMmi); break; case _MMI_HideCursor: sprintf(szCmd,"$%02XT%d%02X \r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0]); ToCom3Str(szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMmi); break; case _MMI_Putch: sprintf(szCmd,"$%02XT%d%02X%c\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0], MmiCmdParam[Cmdout_idx][2]); ToCom3Str(szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMmi); break; case _MMI_Puts: sprintf(szCmd,"$%02XT%d%02X%s\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0], MmiCmdParam[Cmdout_idx][2]); ToCom3Str(szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMmi); break; case _MMI_ChangePage: sprintf(szCmd,"$%02XP%02X\r",MmiAddr,MmiCmdParam[Cmdout_idx][0]); ToCom3Str(szCmd); MmiWaitEcho=1; NewMmiPage=MmiCmdParam[Cmdout_idx][0]; StopWatchStart(StopWatchMmi); break; } Cmdout_idx++; if(Cmdout_idx>=MaxCmdBufNo) Cmdout_idx=0; return 0; } else return -1; } void MmiReShowPage10(void) { int i; char buf[31]; buf[30]=0; for(i=0;i<8;i++){ sprintf(buf,ScrBuf[i]); MmiPutLine(i,buf); } } void BackToPage10(void) { StopWatchStart(StopWatchMmiKey); if(MmiPage!=10){ MmiChangeToPage(10); MmiReShowPage10(); MmiShowCursor(mmix,mmiy); } } void RunMMI(void) { int key; if(MmiKbhit()){ key=MmiReadKey(); switch(key){ case '\b': BackToPage10(); if(mmix){ #ifdef _ShowMmiMsg_ ToCom4(key); #endif MmiPutch(key); } break; case 0x0d: #ifdef _ShowMmiMsg_ ToCom4('\n'); ToCom4(key); #endif BackToPage10(); MmiPutch(key); break; case KEY_F1: DemoMode=1; break; case KEY_F2: DemoMode=2; break; case KEY_F3: if(MmiPage!=1){ MmiChangeToPage(1); } break; case KEY_F4: BackToPage10(); break; case KEY_UP: case KEY_LEFT: if(MmiPage) MmiChangeToPage(MmiPage-1); else MmiChangeToPage(9); break; case KEY_RIGHT: case KEY_DOWN: if(MmiPage<9) MmiChangeToPage(MmiPage+1); else MmiChangeToPage(0); break; default: BackToPage10(); #ifdef _ShowMmiMsg_ ToCom4(key); #endif MmiPutch(key); break; } } MmiCheckKey(); MmiCheck(); MmiDoNextCmd(); } unsigned long t; void Com2Fun(void) { unsigned char data; if(fCom2HasDataIn) return; while(IsCom(2)){ data=ReadCom(2); if(data=='\r'){ Com2Buf[Com2Idx]=0; fCom2HasDataIn=1; break; } else { Com2Buf[Com2Idx++]=data; } } } void ToCom485(int port,unsigned char *buf,int checksum) { unsigned char sum=0; if(!buf || !(*buf)) return; Set485DirToTransmit(port); while(*buf){ ToCom(port,*buf); if(checksum){ sum+=*buf; } buf++; } if(checksum){ ToCom(port,hex_to_ascii[sum>>4]); ToCom(port,hex_to_ascii[sum&0x0f]); } ToCom(port,'\r'); WaitTransmitOver(port); Set485DirToReceive(port); } enum _MODE_7000 { M_7016, M_7033, M_7065, }; int mode7000=0; void CheckCom2Data(int checksum) { unsigned char sum=0; int i; for(i=0;i>4) || ascii_to_hex(Com2Buf[i+1])!=(sum&0x0f)){ #ifdef _ShowCom2Msg_ sprintf(Buf,"!!! Checksum error !!! (%02X)-->%c%c",sum,Com2Buf[i],Com2Buf[i+1]); ToCom4Str(Buf); #endif } } else { #ifdef _ShowCom2Msg_ ToCom4(Com2Buf[i++]); ToCom4(Com2Buf[i++]); #endif } #ifdef _ShowCom2Msg_ ToCom4('\r'); ToCom4('\n'); #endif Com2Idx=0; switch(mode7000){ case M_7065: break; case M_7033: sscanf(Com2Buf+1,"%f%f%f",&data0,&data1,&data2); break; case M_7016: data3=atof(Com2Buf+1); break; } } unsigned char InBuf1[30]; int Inidx1=0; char inbuf4[20]; int Inidx4=0; int mask=1; int mode7065=0; char Cmd7065[6]="@03 "; /* unsigned char Pattern7065[]={ 1,2,4,8,16,8,4,2, 1,8,2,16,4, 30,29,27,23,15,23,27,29, 30,23,29,15,27, }; */ unsigned char Pattern7065[]={ 1,2,4,8,16 }; int SizePattern7065=sizeof(Pattern7065); /* unsigned char Pattern7065_2[]={ 3,6,12,24,12,6, 3,5,10,20,10,5, 9,18,5,10,20,9,18, 28,25,19,7,19,15, 28,26,21,11,21,26, 22,13,26,21,11,22,13, }; */ unsigned char Pattern7065_2[]={ 16,8,4,2,1 }; int SizePattern7065_2=sizeof(Pattern7065_2); int idx7065=0,oidx7065=0; int Do7065(void) { switch(mode7065){ case 0: if(DemoMode==1){ Cmd7065[3]=hex_to_ascii[Pattern7065[idx7065]>>4]; Cmd7065[4]=hex_to_ascii[Pattern7065[idx7065]&0x0F]; } else { Cmd7065[3]=hex_to_ascii[Pattern7065_2[idx7065]>>4]; Cmd7065[4]=hex_to_ascii[Pattern7065_2[idx7065]&0x0F]; } ToCom485(2,Cmd7065,0); bWait485Echo=1; StopWatchStart(StopWatch7000); mode7065=1; oidx7065=idx7065; break; case 1: if(fTimeOut){ fTimeOut=0; bWait485Echo=0; mode7065=0; #ifdef _ShowCom2Msg_ ToCom4Str(" !!! 7065 Timeout !!! \n\r"); #endif return M_7016; } if(fCom2HasDataIn){ CheckCom2Data(0); fCom2HasDataIn=0; bWait485Echo=0; mode7065=0; fTimeOut=0; if(DemoMode==1) DO1=Pattern7065[oidx7065]; else DO1=Pattern7065_2[oidx7065]; return M_7016; } break; } return M_7065; } int mode7016=0; char Cmd7016[]="#01"; int Do7016(void) { switch(mode7016){ case 0: ToCom485(2,Cmd7016,0); bWait485Echo=1; StopWatchStart(StopWatch7000); mode7016=1; break; case 1: if(fTimeOut){ fTimeOut=0; bWait485Echo=0; mode7016=0; #ifdef _ShowCom2Msg_ ToCom4Str(" !!! 7016 Timeout !!! \n\r"); #endif return M_7033; } if(fCom2HasDataIn){ CheckCom2Data(0); fCom2HasDataIn=0; bWait485Echo=0; mode7016=0; fTimeOut=0; return M_7033; } break; } return M_7016; } int mode7033=0; char Cmd7033[4]="#02"; int Do7033(void) { switch(mode7033){ case 0: ToCom485(2,Cmd7033,0); bWait485Echo=1; StopWatchStart(StopWatch7000); mode7033=1; break; case 1: if(fTimeOut){ fTimeOut=0; bWait485Echo=0; mode7033=0; #ifdef _ShowCom2Msg_ ToCom4Str(" !!! 7033 Timeout !!! \n\r"); #endif return M_7065; } if(fCom2HasDataIn){ CheckCom2Data(0); fCom2HasDataIn=0; bWait485Echo=0; mode7033=0; fTimeOut=0; return M_7065; } break; } return M_7033; } void Run7000(void) { switch(mode7000){ case M_7033: mode7000=Do7033(); break; case M_7016: /* 7016 */ mode7000=Do7016(); break; case M_7065: /* 7065 */ mode7000=Do7065(); break; } if(bWait485Echo && !fCom2HasDataIn){ t=0L; StopWatchReadValue(StopWatch7000,&t); if(t>Timeout) { #ifdef _ShowCom2Msg_ sprintf(Buf,"time=%lu ",t); ToCom4Str(Buf); #endif fTimeOut=1; bWait485Echo=0; } } } void ControlFun(void) { static int InTimer=0; if(InTimer) return; InTimer=1; MmiConFun(); Com2Fun(); InTimer=0; } void SendCom1Cmd(unsigned char *cmd) { unsigned char sum=0; while(*cmd){ ToCom1(*cmd); sum+=*cmd++; } ToCom1(hex_to_ascii[sum>>4]); ToCom1(hex_to_ascii[sum&0x0F]); ToCom1('\r'); } char _OutBuf4[120]; void ResponseCmd(int port,char *buf) { int cnt; if(buf[0]=='@'){ switch(buf[1]){ case '0': sprintf(_OutBuf4,">0%+7.2f",data0); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '1': sprintf(_OutBuf4,">1%+7.2f",data1); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '2': sprintf(_OutBuf4,">2%+7.2f",data2); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '3': sprintf(_OutBuf4,">3%+7.2f",data3); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '4': sprintf(_OutBuf4,">4%02X",DO1); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '5': sprintf(_OutBuf4,">5%d",DemoMode); if(port==1){ SendCom1Cmd(_OutBuf4); } else { ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '6': if(port==1){ da=atof(buf+2); SendCom1Cmd(">6"); } else { sprintf(_OutBuf4,">6%+7.3f",da); ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '7': if(port==1){ ad=atof(buf+2); SendCom1Cmd(">7"); } else { sprintf(_OutBuf4,">7%+7.3f",ad); ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '8': if(port==1){ DO2=ascii_to_hex(buf[2]); SendCom1Cmd(">8"); } else { sprintf(_OutBuf4,">8%X",DO2); ToCom4Str(_OutBuf4); ToCom4('\r'); } break; case '9': if(buf[2]=='1'){ if(DemoMode==2){ DemoMode=1; idx7065=0; } if(port==1){ SendCom1Cmd(">91"); } else { ToCom4Str(">91\r"); } } else if(buf[2]=='2'){ if(DemoMode==1){ DemoMode=2; idx7065=0; } if(port==1){ SendCom1Cmd(">92"); } else { ToCom4Str(">92\r"); } } else { if(port==1){ SendCom1Cmd("?9"); } else { ToCom4Str("?9\r"); } } break; } /* test */ /* end test */ } else { if(port==1){ SendCom1Cmd("?"); } else { sprintf(_OutBuf4,"?(%s)\r",buf); ToCom4Str(_OutBuf4); } } } /* ---- main ------------------------------------------------------------- */ unsigned long Time7065; char CmdFromCom1[5]; char CmdFromCom4[5]; void ShowSec(int sec) { (DemoMode==1 ? Show5DigitLed:Show5DigitLedWithDot)(4,sec/10); (DemoMode==1 ? Show5DigitLedWithDot:Show5DigitLed)(5,sec%10); } void ShowMin(int min) { Show5DigitLed(2,min/10); Show5DigitLedWithDot(3,min%10); } int Com4TimeOut=0; void main(void) { int key,i; int iRet; int quit=0; int LedMode=0; int Sec=0,Min=0,Hour=0; int quit2=5; if(!Is7188()){ printf("MMICON.EXE must run on I-7188"); return; } Init5DigitLed(); Show5DigitLedWithDot(1,Hour); ShowSec(Sec); ShowMin(Min); MmiAddr=0; /* Set MMICON's address */ BaudRate=115200; InstallCom1(57600,8,0,1); InstallCom2(115200,8,0,1); InstallCom3(BaudRate,8,0); InstallCom4(57600,8,0); TimerOpen(); StopWatchStart(StopWatchMmiKey); StopWatchStart(StopWatch7060); StopWatchStart(StopWatchLed); ClearCom3(); MmiInitScreen(); InstallUserTimer(ControlFun); while(!quit){ Time7065=0; StopWatchReadValue(StopWatchLed,&Time7065); if(Time7065>=500){ StopWatchStart(StopWatchLed); if(LedMode){ LedMode=0; LedOff(); } else { LedMode=1; LedOn(); Sec++; if(Sec>=60){ Sec=0; Min++; if(Min>=60){ Min=0; Hour++; if(Hour>=10) Hour=0; Show5DigitLedWithDot(1,Hour); } ShowMin(Min); } ShowSec(Sec); } } StopWatchReadValue(StopWatch7060,&Time7065); if(Time7065>=1000){ StopWatchStart(StopWatch7060); if(DemoMode==1){ idx7065++; if(idx7065>=SizePattern7065) idx7065=0; } else { idx7065++; if(idx7065>=SizePattern7065_2) idx7065=0; } } Run7000(); RunMMI(); if(IsCom4()){ key=ReadCom4(); switch(key){ case '\r': { int error=0;; inbuf4[Inidx4]=0; if(inbuf4[0]!='@' || Inidx4 < 2) error=1; else if( (Inidx4==2 && inbuf4[1]>='0' && inbuf4[1]<='8' ) || (Inidx4==3 && inbuf4[1]=='9') ){ ResponseCmd(4,inbuf4); } else error=1; if(error){ /* sprintf(_OutBuf4,"?(%s)\r",inbuf4); ToCom4Str(_OutBuf4); */ ToCom4Str("\r"); } Inidx4=0; } case '@': inbuf4[0]=key; Inidx4=1; break; default: inbuf4[Inidx4]=key; if(Inidx4<19) Inidx4++; break; case 11: /* CTRL_K */ if(DemoMode==1) { DemoMode=2; idx7065=0; } else { DemoMode=1; idx7065=0; } break; case 27: quit2--; if(quit2<=0) quit=1; break; } } if(IsCom1()){ key=ReadCom1(); switch(key){ case '\r': { unsigned char sum=0; InBuf1[Inidx1]=0; for(i=0;i>4]==InBuf1[i] && hex_to_ascii[sum & 0x0f]==InBuf1[i+1]){ ResponseCmd(1,CmdFromCom1); } else { /* check sum error */ } Inidx1=0; } break; default: InBuf1[Inidx1]=key; if(Inidx1<19) Inidx1++; break; } } } DelayMs(500); TimerClose(); RestoreCom1(); RestoreCom2(); RestoreCom3(); RestoreCom4(); }