/*
MODEM.PRG is a sample use of the IOLib.LIB

Standard ports list and their IRQs
Com port  HEX    DECIMAL    IRQ
COM1:     3F8h      1016      4
COM2:     2F8h       760      3
COM3:     3E8h      1000      4  or  7
COM4:     2E8h       744      3  or  5 or 7
COM 3 and 4 may use another IRQ. Any IRQ from 2 to 7 may be used.
IRQ 8 to 15 is also available but requiers an AT286 or higher
IRQ15 may not be available to many 286 systems. SOME IRQs have
other uses like your FLOPPY AND HARD drives see your ROMBIOS hand book.
*/
#include "C:\CLIPPER\IOLib.ch"

PUBLIC nkey,cChr,IRQQ
PUBLIC inbuf,fgcl:=10,bgcl:=0
*EXIT PROCEDURE cleanup
SET CONFIRM ON
CLS

*EscSpool:=""
*GotEsc:=.f.
IRQQ:=3
SET CURSOR ON
*4K BUFFER
*inbuf:=space(4000)

IF !INIT_PORT(COM2,BPS2400,EIGHT_DATA_BITS,NO_PARITY,;
           TWO_STOP_BITS,IRQ3,8192)

 ? "No init! Wrong data"
 QUIT
ENDIF

IF ISWORKING()
 setcolor("10/")
 ? "Interrupt is working!"
ELSE
 ? "Nothing yet..."
ENDIF
? ""
minit="!B10"+CHR(13)+CHR(10)
OUTCHR(@minit)

*inkey(2)
*minit:=REPLICATE("test ",30)
*OUTCHR(@minit)

FOR tmp = 1 to 20
@ ROW(),0 say OUTBUFSIZE()
 IF OUTBUFSIZE()=0
  EXIT
 ENDIF
INKEY(.1)
NEXT

cChr:=INCHR(3)
DO WHILE .T.
*IF !cChr=NIL
 ? cChr
*ENDIF
DO DELAY
minit="!B10"+CHR(10)+CHR(13)
OUTCHR(@minit)
ENDDO


nkey:=0
setcolor(TRIM(STR(fgcl))+"/"+TRIM(STR(bgcl)))
 mr=ROW()
 mc=COL()
 SET CURSOR OFF
 mline:=ISONLINE()
  @ MAXROW(),0 SAY " "+IF(ISONLINE()=.t.,"OnLine ","OffLine")+" ³ "+IOLIB_VER()+" ³ DTR "+IF(SETDTR()=.t.,"on ","off")+"Alt-H=Help "  COLOR "W+/R"
 @ mr,mc SAY ""
 SET CURSOR ON


do while !nkey=301   && Alt-X
nkey:=INKEY()
IF  nKey=18   &&PgUp
 XMODEM_TX()
ELSEIF nKey=3    &&PgDn
 XMODEM_RX()
ELSEIF nkey>0 .and. nkey<256
  OUTC(nkey)
ELSEIF nKey=291   && Alt-H

mscr:=SAVESCREEN(5,5,20,65)
@ 5,5,20,65 box "ÉÍ»º¼ÍÈº " color "w+/b"
@ 5,25 say "[ ClipModem Help ]" color "w+/b"
@ 6,7 say "KEY   | Command Snytax" color "w+/b"
@ 7,7 say "Alt-X | Exit program" color "w+/b"
@ 8,7 say "Alt-D | Dial directory?" color "w+/b"
@ 9,7 say "PgDn  | Receive file" color "w+/b"
@ 10,7 say "PgUp  | Send file" color "w+/b"
@ 12,7 say "IN-Buffer MAX size= "+LTRIM(STR(SIZEO()-1)) color "w+/b"
@ 14,8 say "This is a sample only, don't do much.... " color "w+/b"
@ 19,7 SAY "Press Any Key to return to terminal......" color "15*/9"
set cursor off
do while INKEY()=0
@ 6,35 say "Current status is "+IF(ISONLINE()," ONLINE","OFFLINE") color "w+/b"
@ 7,35 say  "   DTR = "+IF(SETDTR(),"ON","OFF") color "w+/b"
@ 8,35 say  "   RTS = "+IF(SETRTS(),"ON","OFF") color "w+/b"
@ 9,35 say  "   DSR = "+IF(DSR(),"ON","OFF") color "w+/b"
@ 10,35 say "   CTS = "+IF(CTS(),"ON","OFF") color "w+/b"
@ 11,35 say "BUFFER = "+LTRIM(STR(INBUFSIZE()))+"   " color "w+/b"
ENDDO
set cursor on
RESTSCREEN(5,5,20,65,mscr)
ENDIF

cChr:=INCHR(1)

 IF !cChr=NIL
  IF ASC(cChr)=10 .and. ROW()>MAXROW()-2
   SCROLL(0,0,MAXROW()-1,79,1)
  ELSEIF ASC(cChr)=27
   EscSpool:=chr(27)
   gotEsc:=.T.
  ELSEIF gotEsc
   EscSpool:=EscSpool+cChr
   IF ISALPHA(cChr)
    ANSI(EscSpool,cChr)
    gotEsc:=.F.
   ELSEIF LEN(EscSpool)>15 .or. !SUBSTR(EscSpool,2,1)=="["
    gotEsc:=.f.
    ?? EscSpool
   ENDIF
  ELSE
  * IF COL()=MAXCOL() .AND. ROW()=MAXROW()-1
  *  SCROLL(0,0,MAXROW()-1,79,1)
  * @ ROW(),0 SAY ""
  * ENDIF
   ?? cChr

  ENDIF
 ENDIF
  IF  ROW()=MAXROW() .OR. !mline=ISONLINE()
   mr=ROW()
   mc=COL()
  SET CURSOR OFF
   @ MAXROW(),0 SAY " "+IF(ISONLINE()=.t.,"OnLine ","OffLine")+" ³ "+IOLIB_VER()+" ³ DTR "+IF(SETDTR()=.t.,"on ","off")+"Alt-H=Help "  COLOR "W+/R"
  IF mr=MAXROW()
   mr=MAXROW()-1
  ENDIF
  @ mr,mc SAY ""

  SET CURSOR ON
  mline:=ISONLINE()
 ENDIF

enddo

QUIT

*******************************************
* ANSI() may have bugs
*
*
*

FUNCTION ANSI(cString,cterm)
LOCAL x,cSpec,nRow:=ROW(),nCol:=COL(),cfg:=NIL,cbg:=NIL
LOCAL nfg:=NIL,nbg:=NIL,cbuf:=""
LOCAL cClrs:={},lBright:=.f.
*@ 24,70 SAY cString COLOR "w+/r"
cString:=SUBSTR(cString,3)

DO CASE
  CASE cString=="2J"
   @ 0,0 CLEAR TO MAXROW()-1,MAXCOL()
   nRow:=0
   nCol:=0
  CASE cString=="0m"
  fgcl:=7
  bgcl:=0
  setcolor("7/0")

  CASE cterm=="p"
   *^&*$(^ trying to remap your keyboard!
  CASE cString=="A"
   nRow:=nRow-1
  CASE cString=="B"
    nRow:=nRow+1
  CASE cString=="C"
    nCol:=nCol+1
  CASE cString=="D"
    nCol:=nCol-1
  CASE cString=="H" .or. cString=="f"
   nRow:=0
   nCol:=0
  CASE cString=="K"
   @ nRow,nCol CLEAR TO nRow,MAXCOL()
  CASE cterm=="A"
   nRow:=nRow-VAL(cString)
  CASE cterm=="B"
   nRow:=nRow+VAL(cString)
  CASE cterm=="C"
   nCol:=nCol+VAL(cString)
  CASE cterm=="D"
   nCol:=nCol-VAL(cString)
  CASE cterm=="H" .OR. cterm=="f"
   nRow:=VAL(cString)
   cString:=SUBSTR(cString,AT(";",cString)+1 )
   nCol:=VAL(cString)
  CASE cterm=="m"   && change color
  cbuf:=SUBSTR(cString,1,LEN(cString)-1)
  x=1
  DO WHILE LEN(cbuf)>0
   AADD(cClrs,VAL(cBuf))
   IF AT(";",cbuf)=0
    EXIT
   ENDIF
   cBuf:=SUBSTR(cBuf,AT(";",cbuf)+1)
  ENDDO
  bright:=IF(fgcl>7,.T.,.F.)
  blink:=IF(bgcl>7,.T.,.F.)
  FOR x=1 to LEN(cClrs)
   DO CASE
     CASE cClrs[x]==NIL
     CASE cClrs[x]==0
      bright:=.f.
      blink:=.f.
      bgcl:=0
     CASE cClrs[x]==1
      bright:=.t.
     CASE cClrs[x]==5
      blink:=.t.
     CASE cClrs[x]==7
      nFg:=NIL
      nbg:=NIL
      fgcl:=0
      bgcl:=7
      bright:=.f.
      blink:=.f.
      EXIT
     CASE cClrs[x]>39
      nBg:=ASCAN({40,44,42,46,41,45,43,47}, (cClrs[x]))-1
     CASE cClrs[x]>29
      nFg:=ASCAN({30,34,32,36,31,35,33,37} ,cClrs[x])-1
   ENDCASE
  NEXT
    fgcl:=IF(nfg=NIL,fgcl,nfg)
    fgcl:=IF(bright .and. fgcl<8 ,fgcl+8,fgcl)
    bgcl:=IF(nbg=NIL,bgcl,nbg)
    bgcl:=IF(blink .and. bgcl<8 ,bgcl+8,bgcl)

    setcolor(TRIM(STR(fgcl))+IF(blink,"*/","/")+TRIM(STR(bgcl)))
OTHERWISE
    * @ 24,10 say " "+ cString+"       "
ENDCASE


nRow:=IF(nRow=MAXROW(),MAXROW()-1,nRow)
nRow:=IF(nRow<0,0,nRow)
nCol:=IF(nCol>MAXCOL(),MAXCOL(),nCol)
nCol:=IF(nCol<0,0,nCol)
@ nRow,nCol SAY ""

RETURN NIL



EXIT PROCEDURE cleanup()
@ MAXROW(),0 say ""
IF ISWORKING()
 ? "Interrupt is working!"
ELSE
 ? "It did not work!"
ENDIF
 UNINT_PORT()
 ? "Port Uninitialized............."
RETURN


