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 -1452->Xmin 1452->Xmax -960->Ymin 960->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.
MainLoop: gkloop: MouseDone: |
U_CALL_INIT(Stub) jp c,error ld hl,USBDriverBuf U_CALL(uDriverInit) jp c,error ld hl,DBuf ld bc,256 bcall(_MemClear) ld hl,DBuf ld b,0 U_CALL(uMouseInit) 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 bcall(_GrBufCpy) bcall(_GetCSC) cp skClear jr z,MouseDone call DispCursor ld b,4 push bc U_CALL(uMouseGetKey) call MouseUpdateCursor pop bc djnz gkloop call DispCursor jr MainLoop U_CALL(uHostKill) U_CALL(uDriverKill) ret |
U_CALL_INIT Stub jp c,error ld hl,USBDriverBuf U_CALL DriverInit jp c,error ld hl,DBuf ld bc,256 B_CALL MemClear ld hl,DBuf ld b,0 U_CALL MouseInit jp c,error B_CALL GrBufClr xor a ld (MouseBtn),a ld a,50*2 ld (MouseX),a ld a,32*2 ld (MouseY),a call DispCursor B_CALL GrBufCpy B_CALL GetCSC cp skClear jr z,MouseDone call DispCursor ld b,4 push bc U_CALL MouseGetKey call MouseUpdateCursor pop bc djnz gkloop call DispCursor jr MainLoop U_CALL HostKill U_CALL DriverKill ret |
;Initialize U_CALL system ;Initialize Driver ;Clear descriptor buffer ;allow diagonal keys ;Initialize Mouse ;Display mouse cursor ;Erase cursor ;Buffer copy is slow, so ; update cursor a few times ;Get mouse info from usb8x ;Update cursor position ;Re-display cursor ;Disconnect power from mouse ;Kill driver |
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