Reading Controllers

From veswiki
Revision as of 21:23, 16 November 2012 by E5frog (talk | contribs) (1 revision)
Jump to: navigation, search

Wait for controller movement

The simplest use of hand controller is to just pause and wait for a movement, this subroutine does just that, you call it with:

pi wait.4.controller.input

; program

; wait for controller movement

pi	wait.4.controller.input

; either hand control has been moved in some direction

; program continues....

	; see if one of the hand controllers has moved
	outs	0
	outs	1						; check right hand controller
	ins	1
	com							; re-invert controller data
	bnz	wait.4.controller.input.end			; if no movement then input is 0 -> no branch
	; check the other controller
	outs	4						; check left hand controller
	ins	4
	bnz	wait.4.controller.input.end
	br	wait.4.controller.input				; re-test until we have some movement



Controller directions in the register

After the hand controller data has been read it needs to be inverted since it is inverted when read - we do that to get the real data back. The 8 bits is stored in Ackumulator (A) and the bits mean this:

bit  0  right
bit  1  left
bit  2  backward
bit  3  forward
bit  4  counterclockwise
bit  5  clockwise
bit  6  pull up
bit  7  push down

So if we get the value %10000000 that means the hand controller read is being pushed down.

%00000001  right
%00000010  left
%00000100  backward
%00001000  forward
%00010000  counterclockwise
%00100000  clockwise
%01000000  pull up
%10000000  push down

Combinations are of course possible to:

%10000010  push down + left

And there are combinations that are impossible, since they are opposite directions:

%11000000  push down + pull up

After reading the controller/s you can store the result in a register to use later or check directly which direction was chosen. Let's suppose you have a game where you can move forward, backward, right or left with the right hand controller. If we store the result of the controller in let say register 8, this is how to do it:

Store result for later use

	outs	0
	outs	1						; check right hand controller
	ins	1
	com							; re-invert controller data
	lr	8, A						; store result in register 8


Later in the program we can load the movement and mask away the directions we're interested in.

	; 	program program
	; 	...
	lr	A, 8						; copy register 8 to Ackumulator
	ni	%00001111					; AND result and only keep the last nibble
	lr	8, A						; back up result in r8 again

	;	...
	;	program continues

We now have one of these bitpatterns in r8:

%00000000	no movement
%00000001	right
%00000010	left
%00000100	backward
%00000101	backward + right
%00000110	backward + left
%00001000	forward
%00001001	forward + right
%00001010	forward + left

Nothing else is possible unless something is broken.

Reading the four console buttons

This code reads the buttons, you can then mask this result similar to the hand controller:

	; read buttons
	ins	0				; get input from port 0
	com					; invert 
	lr	4, A				; store button result in register 4
	li	128				; load timer value for debounce
	lr	5, A				; in register 5
debounce: 			; this is a delay to wait until button contacts stops bouncing
	ds	5				
	bnz	debounce			; decrease r5 until zero
	lr	A, 4				; load button result into Ackumulator again
	ni	%00000010			; mask out button #2
	bnz	main.buttons.used.two		; not zero means button 2 was pressed
	lr	A, 4				; load read result into A again
	ni	%00000001			; check if it was button #1
	bnz		; if that's not 0 it means button 1 was held

	br	main.continue			; continue program

The intelligent reader has already figured out the rest of the inputs available:

%00000001  button 1
%00000010  button 2
%00000100  button 3
%00001000  button 4
%00001100  button 4 + 3
%00001111   all buttons pressed

Combinations are pretty clear.


Read right hand controller this way:

	outs	0
	outs	1
	ins	1

Read left hand controller this way:

	outs	4
	ins	4

Read buttons with:

	ins	0

	; you may need to debounce as described above

And you have the result in A