83Plus:Software:usb8x/Sample Code
Example code using the usb8x assembly and BASIC interfaces
Contents
BASIC
Mouse Demo
This example uses the [[../BASIC_Interface/MouseInit|MouseInit]], [[../BASIC_Interface/MouseDo|MouseDo]], and [[../BASIC_Interface/KillUSB|KillUSB]] entry points to control a point on the screen with the mouse cursor.
OpenLib(USBDRV8X {2 ;MouseInit ExecLib If Ans(1 ;If MouseInit returns error, Return ; then quit -968->Xmin 968->Xmax -640->Ymin 640->Ymax 0->X 0->Y Repeat getKey {3 ;MouseDo ExecLib Ans->L1 If L1(1 ;If MouseDo returns error, Goto 1 ; restart loop If 127<L1(3 ;If x direction is left, L1(3)-256->L1(3 ; change to negative # If 127<L1(4 ;If y direction is up, L1(4)-256->L1(4 ; change to negative # Pt-Off(X,Y X+L1(3->X Y-L1(4->Y Pt-On(X,Y Lbl 1 End {1 ;KillUSB ExecLib
Assembly (TASM)
Code for TASM-compatible assemblers. Though usb8x is designed for use with ZDS, it should function with a number of TASM-compatible assemblers. TASM itself is apparently incompatible with usb8x macros, but Brass and SPASM work correctly. Use usb8xtsm.inc instead of usb8x.inc.
Mouse Demo
This code uses [[../Asm_Interface/DriverInit|DriverInit]], [[../Asm_Interface/MouseInit|MouseInit]], [[../Asm_Interface/MouseGetKey|MouseGetKey]], [[../Asm_Interface/HostKill|HostKill]], and [[../Asm_Interface/DriverKill|DriverKill]]. It is a replica of the mouse demo code from usb8x, though it doesn't include routines unrelated to USB. In this case the address of the callback routine passed to U_CALL_INIT doesn't matter, because MouseInit redefines the callback to handle the mouse data.
U_CALL_INIT(Stub) ;Initialize U_CALL system jp c,error ld hl,USBDriverBuf U_CALL(uDriverInit) ;Initialize Driver jp c,error ld hl,DBuf ld bc,256 bcall(_MemClear) ;Clear descriptor buffer ld hl,DBuf ld b,0 ;allow diagonal keys U_CALL(uMouseInit) ;Initialize Mouse jp c,error bcall(_GrBufClr) xor a ld (MouseBtn),a ld a,50*2 ld (MouseX),a ld a,32*2 ld (MouseY),a call DispCursor ;Display mouse cursor MainLoop: bcall(_GrBufCpy) bcall(_GetCSC) cp skClear jr z,MouseDone call DispCursor ;Erase cursor ld b,4 ;Buffer to screen copy is kind of slow, so let's gkloop: ; update the cursor a few times for each buffer copy push bc U_CALL(uMouseGetKey) ;Get mouse info from usb8x call MouseUpdateCursor ;Update cursor position pop bc djnz gkloop call DispCursor ;Re-display cursor jr MainLoop MouseDone: U_CALL(uHostKill) ;Disconnect power from mouse U_CALL(uDriverKill) ;Kill driver ret
Assembly (ZDS)
Code for ZDS compatible assemblers. usb8x is designed to be used with a ZDS-compatible assembler.
FindPipe
This code segment, taken from the mass storage driver, illustrates the use of [[../Asm_Interface/FindPipe|FindPipe]] to get the address of an endpoint matching certain characteristics.
ld b,pipeBulk ld c,pipeOut ld hl,DescBuf U_CALL FindPipe ;Find an outgoing bulk endpoint jr c,error ld a,b ld (OutPipe),a ;Store the endpoint address for later use ld b,pipeBulk ld c,pipeIn ld hl,DescBuf U_CALL FindPipe ;Find an incoming bulk endpoint jr c,error ld a,b ld (InPipe),a
Keyboard Detect
This code illustrates the use of [[../Asm_Interface/AutoSetup|AutoSetup]] and [[../Asm_Interface/GetClass|GetClass]] to determine if the attached device is a keyboard.
U_CALL_INIT KBDCallBack ;Inialize U_CALL and Callback RAM calls jr c,NoUSBDriver ;If it returns C, that means USB8X isn't loaded on calc ld hl,USBDriverBuf U_CALL DriverInit ;Initialize USB driver ld hl,DescBuf U_CALL AutoSetup ;Initialize and configure USB device jr c,USBError ld hl,DescBuf U_CALL GetClass ;Get the class information for the attached device jr c,USBError cp 3 ;Keyboard class = 3 jr nz,NotKBD dec b ;subclass = 1 jr nz,NotKBD dec c ;protocol = 1 jr nz,NotKBD ;Keyboard is attached. Code would continue here. ;Typically at this point you would call ReqData to start reading data from the keyboard