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.
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)
Last edited by Karlosoft on Tue Jul 28, 2009 10:34 am, edited 1 time in total.
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?
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.
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...
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.