
Member • Apr 25, 2008
Member • Apr 25, 2008
Administrator • Apr 25, 2008
Member • Apr 25, 2008
Member • Apr 26, 2008
hi i chek out kanetkar but not give more deeply.any stnd. book 4 this.kidakakaPrabhat, check out Let us C by Kanetkar, he has given some sample programs for interfacing with a mouse through C language.
Administrator • Apr 26, 2008
Member • Apr 26, 2008
1) Program to find whether mouse driver is loaded or not #include <dos.h> main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) printf("No Mouse Available....."); else printf("Mouse Available......"); }The above program declares two variables of type union REGS.union REGS, which is declared in dos.h,contains two structures(struct WORDREGS x, struct BYTEREGS h).These two structures contain some 1-byte long and 2-byte long variables which indirectly represent CPU's registers. By placing 0 (sub-function) in ax register and invoking mouse interrupt(33h),we can check whether mouse driver is loaded or not.we used int86() function to invoke the interrupt.int86() takes 3 arguements:interrupt no and two union REGS type variables. If mouse driver is not loaded,it returns 0 in ax register.All return values are accessed using 'o' i.e why we have o.x.ax==0 in if statement.
#include <dos.h> main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available....."); exit(); } i.x.ax=1; int86(0x33,&i,&o); }The above program is same as our first program except for the last few lines.We have placed 1 in ax register and invoked mouse interrupt to see the pointer.Since we are in text mode,our pointer is a rectangular box.We can observe an arrow if we switch to graphics mode.
#include <dos.h> main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available....."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(24,23); printf("Press any key to hide mouse cursor..."); getch(); i.x.ax=2; int86(0x33,&i,&o); gotoxy(10,23); printf("Mouse cursor is hidden !! Press any key to terminate the program ..."); getch(); }The above program uses sub-function 2 and invokes mouse interrupt to hide the mouse pointer.This function is quite a useful function,when writing programs that draw a line or rectangle as we move the mouse. While writing those programs we don't want mouse pointer to erase what we draw hence we hide the pointer.
#include <dos.h> main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Available..."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(25,23); printf("Press any key to exit..."); while(!kbhit()) { i.x.ax=3; int86(0x33,&i,&o); gotoxy(2,2); printf("x->co-ordinate=%d \n y->co-ordinate=%d ",o.x.cx,o.x.dx); } i.x.ax=2; int86(0x33,&i,&o); }In the above program we have a while loop.This loop continues until a key is hit.In loop,we used sub-function 3 and invoked mouse interrupt. Sub-function 3 returns X->co-ordinate in cx register and Y->co-ordinate in dx register.printf statements prints x and y co-ordinates as long as the loop continues.Maximum screen resolution for mouse in text mode is 640x200 and in graphics mode is 640x480.
#include <dos.h> main() { union REGS i,o; int button; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available...."); exit(); } i.x.ax=1; int86(0x33,&i,&o); gotoxy(24,23); printf("Press any key to exit...."); while(!kbhit()) { i.x.ax=3; int86(0x33,&i,&o); button=o.x.bx&7; gotoxy(23,11); switch(button) { case 1: printf("Left button pressed "); break; case 2: printf("Right button pressed "); break; case 4: printf("Middle button pressed "); break; case 3: printf("Left and Right buttons pressed "); break; case 5: printf("Left and Middle buttons pressed "); break; case 6: printf("Right and Middle buttons pressed "); break; case 7: printf("All the three buttons pressed "); break; default: printf("No button pressed...."); } } i.x.ax=2; int86(0x33,&i,&o); }The above program is same as the previous program except we have little extra in while loop.In while we used the same sub-function 3 and invoked mouse interrupt.This subfunction even returns button press information in bx register.Entire button press information is stored in the first 3 bits of the bx register.So we ANDED bx with 7 to separate the first 3 bits and stored them in button variable.
#include <dos.h> main() { union REGS i,o; clrscr(); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No mouse available"); exit(); } i.x.ax=1; int86(0x33,&i,&o); i.x.ax=3; int86(0x33,&i,&o); gotoxy(1,1); printf("Current Position:x=%d y=%d ",o.x.cx,o.x.dx); gotoxy(15,23); printf("Press any key to set the mouse pointer to (150,100)..."); getch(); i.x.ax=4; i.x.cx=150; i.x.dx=100; int86(0x33,&i,&o); gotoxy(15,23); printf("Cursor is set ... press a key to exit "); getch(); }In the above program,we use sub-function 4 to set the pointer's position. We set the X->co-ordinate by placing a value in the cx register and Y->co-ordinate by placing a value in the dx register.
#include <graphics.h> #include <dos.h> main() { int gd=DETECT,gm; union REGS i,o; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse Avaialable.."); restorecrtmode(); exit(); } i.x.ax=1; int86(0x33,&i,&o); outtextxy(100,400,"Mouse Pointer in graphics mode!!Press any key to exit"); getch(); i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); }In the above program,we used standard library function initgraph() to initialize graphics system.This function takes 3 arguments;graphics driver, graphics mode,path to the driver.By using DETECT,we tell the function to select a suitable driver by itselt.When DETECT is used,no need to assign anything to graphics mode.Path is null since the driver files are located in the current directory.This function initializes graphics system and when the program terminates we come to text mode by using restorecrtmode() function.
#include <graphics.h> #include <dos.h> main() { union REGS i,o; int gd=DETECT,gm; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { restorecrtmode(); printf("No Mouse Available....."); exit(); } rectangle(99,49,501,151); i.x.ax=1; int86(0x33,&i,&o); i.x.ax=7; i.x.cx=100; i.x.dx=500; int86(0x33,&i,&o); i.x.ax=8; i.x.cx=50; i.x.dx=150; int86(0x33,&i,&o); while(!kbhit()) ; i.x.ax=2; int86(0x33,&i,&o); restorecrtmode(); }In the above program,sub-fuction 7 is used to specify two x->co-ordinates and sub-function 8 is used to specify two y->co-ordinates and these co-ordinates form a rectangular boundary within which mouse is restricted.
#include <graphics.h> #include <dos.h> union REGS i,o; main() { int gd=DETECT,gm,button,x1,y1,x2,y2; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } outtextxy(230,400,"Press any key to exit...."); while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); x2=x1; y2=y1; while(button==1) { hide_mouse(); line(x1,y1,x2,y2); x1=x2; y1=y2; get_mouse_pos(&x2,&y2,&button); } } restorecrtmode(); } show_mouse() { i.x.ax=1; int86(0x33,&i,&o); } hide_mouse() { i.x.ax=2; int86(0x33,&i,&o); } get_mouse_pos(int *x,int *y,int *button) { i.x.ax=3; int86(0x33,&i,&o); *x=o.x.cx; *y=o.x.dx; *button=o.x.bx&1; }There is nothing in this program to explain.Since you have gone through the program,you must have understood the logic.
#include <graphics.h> #include <alloc.h> #include <dos.h> union REGS i,o; char far *p; main() { int gd=DETECT,gm,button,x1,y1,x2,y2,prevx2,prevy2,x,y; initgraph(&gd,&gm,""); i.x.ax=0; int86(0x33,&i,&o); if(o.x.ax==0) { printf("No Mouse is available.."); exit(); restorecrtmode(); } while(!kbhit()) { show_mouse(); get_mouse_pos(&x1,&y1,&button); if(button==1) { hide_mouse(); x2=x1; y2=y1; save(x1,y1,x2,y2); line(x1,y1,x2,y2); prevx2=x2; prevy2=y2; get_mouse_pos(&x2,&y2,&button); while(button==1) { if(x2!=prevx2 || y2!=prevy2) { setcolor(BLACK); line(x1,y1,prevx2,prevy2); x=x1<prevx2?x1:prevx2; y=y1<prevy2?y1:prevy2; restore(x,y); setcolor(WHITE); save(x1,y1,x2,y2); line(x1,y1,x2,y2); prevx2=x2; prevy2=y2; } get_mouse_pos(&x2,&y2,&button); } farfree(p); } } restorecrtmode(); } show_mouse() { i.x.ax=1; int86(0x33,&i,&o); } hide_mouse() { i.x.ax=2; int86(0x33,&i,&o); } get_mouse_pos(int *x,int *y,int *button) { i.x.ax=3; int86(0x33,&i,&o); *x=o.x.cx; *y=o.x.dx; *button=o.x.bx&1; } save(int x1,int y1,int x2,int y2) { unsigned area; area=imagesize(x1,y1,x2,y2); p=farmalloc(area); if(p==NULL) { restorecrtmode(); printf("No Memory..."); exit(); } getimage(x1,y1,x2,y2,p); } restore(int x1,int y1) { putimage(x1,y1,p,OR_PUT); farfree(p); }When drawing lines interactively,we must make sure that the currently drawn line doesn't wipe off already drawn lines when it intersects them.In order to do that,the above program uses save and restore functions.These two functions captures and restores screen contents.
Member • Apr 28, 2008
Member • Feb 4, 2010
Member • Feb 4, 2010
Member • Feb 12, 2010
Member • Dec 24, 2010
Member • Dec 24, 2010
Member • May 4, 2012
Please start a new thread. And close this thread #-Link-Snipped-#! 😀rajivnayanhi
i am working on a project which combine hardware and software.
i have to control a robotic arm with the help of mouse through parallel port. it will be having 3 dimensional movement.i m not using any microcontroller, just motor driving IC. parallel port ll be taken as input for IC to drive motor. i need some help guys😕😕😕