Difference between revisions of "83Plus:Software:usb8x/Sample Code"
m (→Assembly (TASM): linked usb8x.inc and usb8xtsm.inc) |
(→Mouse Demo: flag byte is B, grr) |
||
Line 98: | Line 98: | ||
bcall(_MemClear) | bcall(_MemClear) | ||
ld de,DBuf | ld de,DBuf | ||
− | ld | + | ld b,0 |
U_CALL(MouseInit) | U_CALL(MouseInit) | ||
jp c,error | jp c,error | ||
Line 141: | Line 141: | ||
B_CALL MemClear | B_CALL MemClear | ||
ld de,DBuf | ld de,DBuf | ||
− | ld | + | ld b,0 |
U_CALL MouseInit | U_CALL MouseInit | ||
jp c,error | jp c,error |
Revision as of 17:03, 19 September 2006
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 a USB mouse.
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, a TASM-syntax include file is provided as well.. TASM itself is apparently incompatible with usb8x macros, but they have been tested to work correctly with Brass. Use usb8xtsm.inc instead of usb8x.inc.
Mouse Demo
This code uses [[../Asm_Interface/DriverInit|DriverInit]], [[../Asm_Interface/Mouse/MouseInit|MouseInit]], [[../Asm_Interface/Mouse/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(DriverInit) jp c,error ld hl,DBuf ld bc,256 bcall(_MemClear) ld de,DBuf ld b,0 U_CALL(MouseInit) 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(MouseGetKey) call MouseUpdateCursor pop bc djnz gkloop call DispCursor jr MainLoop U_CALL(HostKill) U_CALL(DriverKill) 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 de,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