PS2 Mouse Help

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

PS2 Mouse Help

Post by purage »

How would one translate the x and y value given to you from the ps2 mouse packets 1 and 2 (assuming those are right for getting x and y coordinates of mouse pointer) into the proper coordinates on screen? I know that the x and y values in the packets are not literal coordinates, but I am not sure how to use them. thank you
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

Never mind, I figured it out! You have to treat the values like the amount of steps you took since the last time you checked. You then subtract or add those steps to your last position and there you have your present position. It is really cool to see my little cursor moving on screen in sync with my mouse. I just need to keep the mouse in bounds and this driver is done! thanks anyway.
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: PS2 Mouse Help

Post by gzaloprgm »

Yep, it's called Delta, the difference between the last and current coordinates.

Also, I think you might want to implement some quadratic/logaritmic function so if you move the mouse slowly it moves very slowly and when you move it fast it moves fast, sort of what Windows does, for example, if you activate “Enhance pointer precision”.

Cheers,
Gonzalo
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

Thanks Gonzalo for the suggestion.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

Having some issues making it work in real-hardware. It thinks that when I move the mouse that the mouse buttons are being clicked and when buttons are being clicked, nothing happens at all (no message, no cursor movement). I know that I do set the IRQ 12 as I have a message printed to screen each time my handler fires. Look, here is my handler (most of it):

Code: Select all

mouse_bytes[cycle++] = inb(0x60);

	if (cycle == 3) 
	{
		cycle = 0;

		if ((mouse_bytes[0] & 0x80) || (mouse_bytes[0] & 0x40)) //skip overflow
      {
			return; 
      }
		if (mouse_bytes[0] & 0x4)
		{
			kprint("Middle button is pressed!");
		}

		if (mouse_bytes[0] & 0x2)
		{
			kprint("Right button is pressed!");
		}

		if (mouse_bytes[0] & 0x1) //left button pressed
		{
			x=//store mouse_byte 1
			y=//store mouse_byte 2
			if (!(mouse_bytes[0] & 0x20)) //neg y
			{
				//do stuff
			} else {
				//do stuff
			}
			if (!(mouse_bytes[0] & 0x10)) //neg x
			{
				//do stuff
			} else {
				//do stuff
			}	
      }
}
Hope you can see my mistake(s)? This does work great in VPC, but on real hardware it fails as described in my intro above.
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: PS2 Mouse Help

Post by kmtdk »

well
right now i can not see any errors ( but im not too good at c)
but i Think the reason is when you "skip" due to overflow
try to print a msg when it gets overflowed.
....
if the nothing happens, then be sure that the handler gets called, else try another mouse
is it a PS2 mouse, or a USB ???


KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

I tried that and it did seem to overflow a lot. What does that mean? Is that why it fails to work properly because I do not handle the overflow? I tested it on real hardware using both a USB and a PS/2 mouse and here are the scientific results:

USB: overflows like crazy, ignores mouse clicks, thinks movement means mouse clicks
PS/2: works better (completes function), still overflows like crazy, mouse clicks ignored, thinks movement means mouse clicks

Really the only difference is that the PS/2 mouse completes the function (I know because the kprint at the end of the function is triggered), so it must be in the way of how I interpret the mouse_bytes. Maybe it could be the overflow thing, but I am not sure. Any ideas?
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: PS2 Mouse Help

Post by kmtdk »

oh ....
i had a simlary problem when i played with mouse ( ps2)
USB however should not work, since it sends data to the USB IRQ...
im not sure, but when i look at my "code" i can see that i have deactivatede the keyboad, just before i init the mouse. [i think this was a part of the reason, + some inti code]
But i dont see anything "strange" i have made.
i also self test for overflow, but my irq handler does not seems to have a problem with it. ( my handler does not include anything speciel)
at least i can use the mouse with buttons ...
so it might be in the init part.
however my code is in asm, with some "strange comments" ( some danish, and some english) But i have attached it anyway.

EDIT: it does not matter with the keyboard ... ( just tested)


KMT dk
Attachments
mouse cursor.asm
the file contaning code for placing a pointer (cursor) on the screen.
(2.14 KiB) Downloaded 131 times
mouse2.asm
The file contaning code for mouse init and handling
(4.39 KiB) Downloaded 117 times
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

We seem to be doing essentially the same things, so if yours is working with real hardware and mine isn't then it must be my code. I will try to think of a better way to write my handler. thanks
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: PS2 Mouse Help

Post by kmtdk »

well
i have not tried it on RHW, but i dont get overflows.
The problems lies in the init code, and i think it is about the aux port.
eh
can i see your "init" code for the mouse, i might see the error.

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Re: PS2 Mouse Help

Post by xyjamepa »

kmtdk thanks for sharing your code :) ,but unfortunately some comments are not
in English.
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

Sure, here it is:

Code: Select all

void ps2_install()
{
	//Enable the auxiliary mouse device
	MouseWaitOnSignal();
	outb(0x64, 0xA8);

	//Enable the interrupts
	unsigned char Status;
	MouseWaitOnSignal();
	outb(0x64, 0x20);
	MouseWaitOnData();
	Status = (inb(0x60) | 2);
	MouseWaitOnSignal();
	outb(0x64, 0x60);
	MouseWaitOnSignal();
	outb(0x60, Status);

	//Use the default settings
	MouseWriteData(0xF6);
	MouseReadData();
	
	//Enable the mouse
	MouseWriteData(0xF4);
	MouseReadData();

	//Set the cursor position
	MousePositionX = 0;
	MousePositionY = 0;

	//Install the mouse IRQ handler
	irq_install_handler(12, ps2_handler);
}
I am pretty sure we have the same stuff going on there. I am just about convinced that it is with my handler. I noticed that my Y values go mega negative to boot and only increase more negative and the X values only increase more positive. I was using static variables that I then changed to global variables that I then changed to volatile global variables and I still get the same issues as before!

And, his comments though in another language are in what Deutsch? So they are easy to figure out (deaktivere keyboardet = deactivate keyboard) or Babel Fish it. Or if you understand the code then what's the need for comments? ;)

Furthermore, you should really test it on real hardware to find out if it works. I noticed you set the graphics mode 0x13 and were trying to click a button, did that work on emulated hardware? Try to find the time to test it on real hardware if you can and let me know if it worked, OK? Assuming that you would like to finish this driver, then we can figure out why it might not work. thanks
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: PS2 Mouse Help

Post by kmtdk »

well
sorry about the comments, i can surely make the code more read able, but since i normaly keep that kind of test files in a special dir, and only use them to currect the driver, i dont think about it. ( However i might have to do in the futur when posting here..)
and the comments are in a mixture of bad spelled danish and english( my mind is a little special :P )

but as soon as i get home i will test it on RHW, but yes, it works 100% in emuligator ( I might have a little error when printing at the screen).

so i will post later:

KMT dk


results are still not able, due to the fact that i cant get the "mapping" working ( irq mapping, i only have problems with the last 8... wierd)
so tomorw i will see if i get time
sorry for the "wait" time.
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
purage
On Probation
Posts: 119
Joined: Wed Feb 11, 2009 1:04 am

Re: PS2 Mouse Help

Post by purage »

Got it, finally! I am not exactly sure how to help you with your driver, but here is what I will suggest:

01. Turn each delta value that is negative into a positive value. Just multiply the value by -1.
02. Completely ignore the overflow. Unless you can find a reason to use it.
03. To calculate your new position, just take the ABS of your delta value and run it through a while until it is zero. Inside your while (depending on if the value was negative or positive) add or subtract from your old positions (x, y). This will be your new position.
04. Check that the cursor has not passed the bounds of the screen (unless wanted). Do this by checking if the cursor went beyond the screen (at any side) and bring them back 1 pixel deep (at any side).

I guess that is about all you need. Looks like you got the rest going on in your code, so good luck man. Hope you get it working, let me know if you need me to help you with the math or whatever. take care.
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: PS2 Mouse Help

Post by kmtdk »

GOOD :D ( congratulations)
well
1,2,3 is what the "cursor.asm" file does ...
my only problem is now that i can not map the PIC slave...
even if i map it to 0x70 the AUX dont work ..
so i could not test it yesterday ....

and i might not have the time today....
you may tell me why this does not work ...

Code: Select all

        mov     al,0x11
        out     0x20,al
        out     0xa0,al
       ;where to store irq in IVT, from 0-7
        mov     al,0x20                        ; original: 20
        out     0x21,al
       ;where to store irq in IVT, from 8-15
        mov     al,0x70                        ; original: 28
        out     0xa1, al

        mov     al, 0x4                 ; 0x04 => 0100, second bit (IR line 2)
        out     0x21, al
        mov     al, 0x2                 ; 010=> IR line 2
        out     0xa1, al
        mov     al, 1                   ; bit 0 enables 80x86 mode
        out     0x21, al
        out     0xa1, al
        xor     ax,ax
        out     0x21, al
        out     0xa1, al
credits:
Brokenthorn entertainment, since it is a copy ....

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
Post Reply