Page 1 of 1

(Mouse PS2) Problems with coordinates

Posted: Tue Jul 28, 2009 8:15 am
by Karlosoft

Code: Select all


int x,y;  
unsigned char cycle = 0;
char mouse_bytes[2];

void mouse_handler(struct regs *r)
{

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

  if (cycle == 3) { // if we have all the 3 bytes...
    cycle = 0; // reset the counter
    
    vid.puts("X: ",ALERT);
    vid.putn(mouse_bytes[1], ALERT);
    vid.putc(' ',ALERT);
    vid.puts("Y: ",ALERT);
    vid.putn(mouse_bytes[2], ALERT);
    
    // do what you wish with the bytes, this is just a sample
    if ((mouse_bytes[0] & 0x80) || (mouse_bytes[0] & 0x40))
      return; // the mouse only sends information about overflowing, do not care about it and return
    if (!(mouse_bytes[0] & 0x20))
      y |= 0xFFFFFF00; //delta-y is a negative value
    if (!(mouse_bytes[0] & 0x10))
      x |= 0xFFFFFF00; //delta-x is a negative value
    if (mouse_bytes[0] & 0x4)
      vid.puts("Middle button is pressed!n",ALERT);
    if (mouse_bytes[0] & 0x2)
      vid.puts("Right button is pressed!n",ALERT);
    if (mouse_bytes[0] & 0x1)
      vid.puts("Left button is pressed!n",ALERT);

  }
}

void mouse_wait(unsigned char type)
{
  unsigned int _time_out=100000;
  if(type==0)
  {
    while(_time_out--) //Data
    {
      if((inportb(0x64) & 1)==1)
      {
        return;
      }
    }
    return;
  }
  else
  {
    while(_time_out--) //Signal
    {
      if((inportb(0x64) & 2)==0)
      {
        return;
      }
    }
    return;
  }
}

/* Installs the keyboard handler into IRQ1 */
void keyboard_install()
{
    irq_install_handler(1, keyboard_handler);
}

void mouse_write(unsigned char a_write)
{
//Wait to be able to send a command
mouse_wait(1);
//Tell the mouse we are sending a command
outportb(0x64, 0xD4);
//Wait for the final part
mouse_wait(1);
//Finally write
outportb(0x60, a_write);
}

unsigned char mouse_read()
{
//Get response from mouse
mouse_wait(0);
return inportb(0x60);
}

void mouse_install()
{
     mouse_wait(1);
     outportb(0x64,0xA8);
     
     mouse_wait(1);
     outportb(0x64,0x20);
     
     unsigned char status_byte;
     mouse_wait(0);
     status_byte = (inportb(0x60) | 2);
     
     mouse_wait(1);
     outportb(0x64, 0x60);
     
    mouse_wait(1);
    outportb(0x60, status_byte);
    mouse_write(0xF6);
    mouse_read();
    mouse_write(0xF4);
    mouse_read();
    irq_install_handler(12, mouse_handler);
}


I've tried to use this code taken from an other topic. It works well, I mean when i press a button on the mouse it's reconized, but the asses are often set as 60. The strange thing is when I turn on the computer, the light under the mouse is on, but after I send the command 0xF4 it tun down
Could anyone help me? (I'm sorry for my bad English)

Re: (Mouse) Problems with coordinates

Posted: Tue Jul 28, 2009 10:31 am
by Karlosoft
It's a little strange... I've found an old mouse with the ball inside, and I've connect it... well, it works... At this point... what's the difference between an optical and a traditional mouse?

Re: (Mouse PS2) Problems with coordinates

Posted: Tue Jul 28, 2009 11:42 am
by manonthemoon
That is strange. There should be no difference.

Karlosoft wrote:It works well, I mean when i press a button on the mouse it's reconized, but the asses are often set as 60.
Could you please explain exactly what the problem is? What is set at 60?

I only glanced quickly at your code, but it looks like the global variables x and y are never initialized or updated.

Are you testing on a real computer? Try an emulator, and also take note of what kind of connection the mouse uses. If the optical mouse is USB and the old mouse is PS/2, well, that's the only reason I could imagine they work differently.

Re: (Mouse PS2) Problems with coordinates

Posted: Tue Jul 28, 2009 12:18 pm
by Karlosoft
No I'm testing it on a real machine

I was using an optical mouse PS/2 (without adapter, and according to the write on the back PS/2). The values of the last two bytes sent, that rappresent the x and y asses, is always 60, and the mouse sensor looks turn down. x and y aren't part of this code, there are the counters of the keyboard :)

Well I tried two other optical mice... nothing, they doesn't work...

Code: Select all

Try an emulator, and also take note of what kind of connection the mouse uses.
What do you mean for kind of connection?

Re: (Mouse PS2) Problems with coordinates

Posted: Tue Jul 28, 2009 12:56 pm
by manonthemoon
Karlosoft wrote:The values of the last two bytes sent, that rappresent the x and y asses, is always 60, and the mouse sensor looks turn down. x and y aren't part of this code, there are the counters of the keyboard
Oh, ok. You should say "x and y axes".
What do you mean for kind of connection?
I meant USB or PS/2.


Well, I don't know what the problem is, I can't see any glaring errors in your code. Hopefully someone else will reply and have an answer. All I can do is suggest is this link: http://www.computer-engineering.org/ps2mouse/. At the bottom of the page is a list of commands and an example of communication between the computer and the mouse. They seem to send some initialization commands that you do not (specifically the 0xFF reset command), so maybe that is the problem.

Re: (Mouse PS2) Problems with coordinates

Posted: Tue Jul 28, 2009 1:29 pm
by Karlosoft
I've only now notice my mistake XD... In Italian axes is assi... ehm sorry

Well I'll give a look at the link :)
Thanks anyway :D