10
20
30 :
40 debug%=FALSE
50 MODE 128
60 PRINT"USB Printer Demo v0.10"
70 DIM ctrl% 31,M%(32),data% 31,print% 72:in%=0:X%=ctrl%:Y%=X%DIV256
80 ON ERROR REPORT:PROCClose_All:END
90 :
100 PRINT"Waiting for PRN device";
110 PROCusb_Init:PROCusb_Sync:PROCusb_Flush
120 PROCusb_Cmd(CHR$&11) :
130 PROCusb_Cmd("IPH") :
140
150 REPEAT:PROCusb_Cmd("QP2") :
160 res%=?usb_result%:PROCusb_Sync
170 IF (res% AND 4)=0:A%=INKEY(100)
180 UNTIL (res% AND 4) :
190 PROCusb_Cmd("SC "+CHR$0) :
200 PRINT " - Connected"'
210 :
220 REPEAT:REPEAT
230 PROCusb_Cmd("PGS") :
240 res%=?usb_result%:PROCusb_Sync
250 PRINT"Printer status: ";
260 IFres%AND8:PRINT"NOERR "; ELSE PRINT"ERROR ";
270 IFres%AND16:PRINT"SELECTED "; ELSE PRINT"NOSEL ";
280 IFres%AND32:PRINT"NOPAPER"; ELSE PRINT"PAPEROK";
290 PRINT
300 REPEATREPEATINPUT LINE "File to print: "in$
310 IF LEFT$(in$,1)="*":OSCLIMID$(in$,2):in$=""
320 UNTILin$<>""
330 in%=OPENIN(in$):IF in%=0:PRINT"File '"in$"' not found"
340 UNTILin%
350 :
360 REPEAT:A%=BGET#in%:UNTIL A%=10 OR A%=13 OR EOF#in%:B%=BGET#in%
370 crlf%=NOT((A%=13 AND B%=10) OR (A%=10 AND B%=13))
380 PROCusb_WrStr("DSD "+CHR$1+CHR$13) :
390 PROCusb_WrStr(CHR$13) :
400 ptr%=0:ext%=EXT#in%:PTR#in%=0:col%=0
410 REPEAT:PRINTCHR$13;"Printing ";INT(100*(ptr%/ext%));"%";
420 len%=32:IFlen%+ptr%>ext%:len%=ext%-ptr%
430 PROCgbpb(4,in%,data%,len%,ptr%)
440 iptr%=0:optr%=0
450 REPEAT
460 A%=data%?iptr%:iptr%=iptr%+1
470 IF A%=26:A%=32
480 print%?optr%=A%:B%=1
490 IF A%=10 OR A%=13:col%=-1:IF crlf%:optr%=optr%+1:print%?optr%=23-A%
500 IF A%=9:B%=8-(col%+1)MOD8:$(print%+optr%)=STRING$(B%," ")
510 col%=col%+B%:optr%=optr%+B%
520 UNTIL iptr%>=len% OR optr%>63
530 PROCusb_Sync
540 PROCusb_WrStr("DSD "+CHR$optr%+CHR$13) :
550 FOR n%=0 TO optr%-1:PROCusb_Wr(print%?n%):NEXT n%
560 ptr%=ptr%+len%:UNTIL ptr%>=ext%
570 PROCusb_WrStr("DSD "+CHR$1+CHR$13) :
580 PROCusb_WrStr(CHR$12) :
590 CLOSE#in%:in%=0:PRINT'
600 UNTIL FALSE
610 :
620 DEFPROCClose_All
630 in%=in%:IFin%:A%=in%:in%=0:CLOSE#A%
640 ENDPROC
650 :
660 DEFPROCgbpb(A%,chn%,addr%,num%,ptr%):?X%=chn%:X%!1=addr%:X%!5=num%:X%!9=ptr%:CALL&FFD1:ENDPROC
670 :
680 :
690
700 :
710
720
730 DEFPROCusb_Sync:REPEATA%=?usb_D:UNTIL(?usb_S AND 128):ENDPROC
740 DEFPROCusb_Flush:IF(?usb_S AND 128):ENDPROC
750 PRINT'"Flushing USB";:REPEAT:REPEATA%=?usb_D:UNTIL(?usb_S AND 128)
760 A%=TIME+20:REPEATUNTILTIME>A%:UNTIL(?usb_S AND 128):ENDPROC
770 DEFFNusb_Err:IF?usb_result%=13 OR usb_result%?1=ASC":":=0 ELSE PRINT"USB Error: "$usb_result%:=TRUE
780 DEFPROCusb_Cmd(A$):PROCusb_WrStr(A$+CHR$13):PROCusb_RdStr:ENDPROC
790 DEFFNusb_Cmd(A$):PROCusb_Cmd(A$):=FNusb_Err
800 DEFPROCusb_WrStr(A$):IFdebug%:COLOUR0:COLOUR129
810 FOR A%=1 TO LEN A$:REPEATUNTIL(?usb_S AND 64)=0:?usb_D=ASCMID$(A$,A%,1):IFdebug%:PRINTFNc0(ASCMID$(A$,A%,1));
820 NEXT:IFdebug%:COLOUR1:COLOUR128
830 ENDPROC
840 DEFPROCusb_RdStr:A%=usb_result%-1:REPEATA%=A%+1:?A%=FNusb_Rd:UNTIL?A%=13:ENDPROC
850 DEFPROCusb_Wr(A%):REPEATUNTIL(?usb_S AND 64)=0:?usb_D=A%:ENDPROC
860 DEFFNusb_Rd:REPEATUNTIL(?usb_S AND 128)=0:Z%=?usb_D:IFdebug%:PRINTFNc0(Z%);:=Z% ELSE =Z%
870 DEFPROCusb_Init
880 usb_D=&FCF8:usb_S=&FCF9
890 usb_result%=usb_result%:IF usb_result%:ENDPROC
900 DIM usb_result% 63,mc% 99
910 FOR P=0 TO 1:P%=mc%:[OPT P*2
920 .usb_RdData
930 TAY:LDA #data% AND 255:STA usb_RdLp+4
940 LDA #data% DIV 256:STA usb_RdLp+5:LDX #0
950 .usb_RdLp:JSR usb_Rd:STA data%,X:INX:BNE usb_RdLp
960 INC usb_RdLp+5:DEY:BNE usb_RdLp:RTS
970 .usb_Rd:BIT usb_S:BPL usb_Rd2:BIT &FF:BPL usb_Rd
980 .usb_Rd2:LDA usb_D:RTS
990 .usb_WrData
1000 TAY:LDA #data% AND 255:STA usb_WrLp+1
1010 LDA #data% DIV 256:STA usb_WrLp+2:LDX #0
1020 .usb_WrLp:LDA data%,X:JSR usb_Wr
1030 .usb_WrLp2:LDA usb_D:BIT usb_S:BPL usb_WrLp2
1040 INX:BNE usb_WrLp
1050 INC usb_WrLp+2:DEY:BNE usb_WrLp:RTS
1060 .usb_Wr:BIT usb_S:BVC usb_Wr2:BIT &FF:BPL usb_Wr
1070 .usb_Wr2:STA usb_D:RTS
1080 ]NEXT:ENDPROC
1090 :