/* 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(9600,N,8,1) connect to MMICON MMICON.EXE must run on I-7188 MMIOCN: (1)LCD 240*64 --> character display=30*8 TestMMI(): (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 */ #include #include #include #include /*#include*/ #include #include #include"..\lib\7188.h" #define StopWatchMMI 1 /* #define KEY_F1 0x1C #define KEY_F2 0x1D #define KEY_F3 0x1E #define KEY_F4 0x1F #define KEY_UP 0x1B */ #define KEY_DN 0x17 #define KEY_UP 'u' #define KEY_DOWN 'd' #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 int AddMmiCmd(int cmd,int p0,int p1,int p2); int IsMmiCmd(void); char Buf[100]; unsigned long MmiTime; int testCursor=0; enum _MMI_MODE_ { _MMI_CheckKey=1, _MMI_ShowCursor, _MMI_HideCursor, _MMI_Putch, _MMI_Puts, _MMI_ChangePage, }; int MmiAddr=0; int ComPort=3,MmiPort=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); } int mmix=0,mmiy=0; void MmiShowCursor(int x,int y) { AddMmiCmd(_MMI_ShowCursor,x,y,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]={ " ", " ", " ", " ", " ", " ", " ", " ", }; char ScrBuf[8][31]; 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); */ StopWatchStart(StopWatchMMI+1); testCursor=1; 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; 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; } MmiMode=0; break; case _MMI_ShowCursor: case _MMI_HideCursor: case _MMI_Putch: case _MMI_Puts: case _MMI_ChangePage: MmiMode=0; break; } } } } #define MMI_BUF_SIZE 1024 unsigned char MmiBuf[MMI_BUF_SIZE]; int MmiInIdx=0; int MmiOutIdx=0; int IsMmiHasData(void) { return (MmiInIdx-MmiOutIdx)&(MMI_BUF_SIZE-1); } void MmiAddData(unsigned char data) { int idx=(MmiInIdx+1) & (MMI_BUF_SIZE-1); if(idx!=MmiOutIdx){ MmiBuf[MmiInIdx]=data; MmiInIdx=idx; } } int MmiReadData(void) { int data; if(MmiInIdx!=MmiOutIdx){ data=MmiBuf[MmiOutIdx]; MmiOutIdx++; MmiOutIdx &= (MMI_BUF_SIZE-1); return data; } return -1; } void MmiConFun(void) { int data; while(IsCom(MmiPort)){ MmiAddData(ReadCom(MmiPort)); } if(MmiWaitEcho){ while(IsMmiHasData()){ data=MmiReadData(); if(data=='\r'){ MmiEchoBuf[MmiEchoIdx]=0; MmiEchoLength=MmiEchoIdx; MmiWaitEcho=0; MmiEchoIdx=0; /* tets */ /* sprintf(Buf,"\n\rMmiEcho:%s",MmiEchoBuf); ToCom4Str(Buf); */ /* end test */ return; } else { MmiEchoBuf[MmiEchoIdx++]=data; } } StopWatchReadValue(StopWatchMMI,&MmiTime); if(MmiTime>=1000){ ToCom4Str("\n\rTimeout"); 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 */ if(cmd != _MMI_CheckKey){ sprintf(Buf,"\n\r[AC](%d)(%d,%d,%d)",cmd,p0,p1,p2); ToCom4Str(Buf); } /* 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 */ if(MmiMode != _MMI_CheckKey){ sprintf(Buf,"\n\r[DC](%d)(%d,%d,%d)",MmiMode, MmiCmdParam[Cmdout_idx][0],MmiCmdParam[Cmdout_idx][1], MmiCmdParam[Cmdout_idx][2]); ToCom4Str(Buf); } /* end test */ switch(MmiMode){ case _MMI_CheckKey: sprintf(szCmd,"$%02XK\r",MmiAddr); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(StopWatchMMI); break; case _MMI_ShowCursor: sprintf(szCmd,"$%02XT%d%02X_\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0]); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(1); break; case _MMI_HideCursor: sprintf(szCmd,"$%02XT%d%02X \r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0]); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(1); break; case _MMI_Putch: sprintf(szCmd,"$%02XT%d%02X%c\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0], MmiCmdParam[Cmdout_idx][2]); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(1); break; case _MMI_Puts: sprintf(szCmd,"$%02XT%d%02X%s\r",MmiAddr, MmiCmdParam[Cmdout_idx][1],MmiCmdParam[Cmdout_idx][0], MmiCmdParam[Cmdout_idx][2]); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(1); break; case _MMI_ChangePage: sprintf(szCmd,"$%02XP%02X\r",MmiAddr,MmiCmdParam[Cmdout_idx][0]); ToComStr(MmiPort,szCmd); MmiWaitEcho=1; StopWatchStart(1); break; } Cmdout_idx++; if(Cmdout_idx>=MaxCmdBufNo) Cmdout_idx=0; return 0; } else return -1; } void TestMMI(void) { int key; if(MmiKbhit()){ key=MmiReadKey(); switch(key){ case '\b': if(mmix){ ToCom4(key); MmiPutch(key); } break; case 0x0d: ToCom4('\n'); ToCom4(key); MmiPutch(key); break; default: ToCom4(key); MmiPutch(key); break; } } MmiCheckKey(); MmiCheck(); MmiDoNextCmd(); } /* ---- main ------------------------------------------------------------- */ void main(void) { int key; int iRet; int quit=0; if(!Is7188()){ printf("MMICON.EXE must run on I-7188"); return; } TimerOpen(); MmiAddr=0; /* Set MMICON's address */ MmiPort=3; BaudRate=115200; /* use I_7188 com 3(RS-232) */ InstallCom4(57600,8,0); /* default */ InstallCom(MmiPort,BaudRate,8,0,1); /* default */ InstallUserTimer(MmiConFun); MmiInitScreen(); while(!quit){ TestMMI(); if(IsCom4()){ key=ReadCom4(); switch(key){ case 3: quit=1; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': MmiChangeToPage(key-'0'); break; case 'a': MmiChangeToPage(10); break; case 'b': MmiShowCursor(mmix,mmiy); break; case 'c': MmiChangeToPage(10); StopWatchStart(StopWatchMMI+1); testCursor=1; break; default: if(isprint(key) || key=='\r'){ MmiPutch(key); } break; } } if(testCursor){ unsigned long t; StopWatchReadValue(StopWatchMMI+1,&t); if(t>200){ testCursor=0; MmiShowCursor(mmix,mmiy); } } } DelayMs(500); TimerClose(); RestoreCom(MmiPort); RestoreCom4(); }