Difference between revisions of "83Plus:Software:usb8x/Sample Code"

From WikiTI
Jump to: navigation, search
(Added tasm mouse demo)
m (typo)
Line 36: Line 36:
 
Code for TASM-compatible assemblers.  Use usb8xtsm.inc instead of usb8x.inc.  TASM itself is apparently incompatible with usb8x macros, but Brass and SPASM work correctly.
 
Code for TASM-compatible assemblers.  Use usb8xtsm.inc instead of usb8x.inc.  TASM itself is apparently incompatible with usb8x macros, but Brass and SPASM work correctly.
 
=== Mouse Demo ===
 
=== 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 redefine the callback to handle the mouse data.
+
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.
 
  <nowiki> U_CALL_INIT(Stub) ;Initialize U_CALL system
 
  <nowiki> U_CALL_INIT(Stub) ;Initialize U_CALL system
 
jp c,error
 
jp c,error

Revision as of 10:06, 16 July 2006

Example code using the usb8x assembly and BASIC interfaces

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. Use usb8xtsm.inc instead of usb8x.inc. TASM itself is apparently incompatible with usb8x macros, but Brass and SPASM work correctly.

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