Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12365

How to write Piano using thread programming?

$
0
0

I have written a program of piano that is controlled by user mouse movement. Actually in this program i am capturing the mouse event and detect the mouse cursor current position and according to that i am printing the piano node which will sound . But i want to write the same program with thread and char device driver. Can anyone guide for this?

#include <curses.h>#include <stdio.h>int main(){  /* The initscr() routine must be called first for any curses routines, */  initscr();  /* To disable the buffering of typed characters by the TTY driver and get a character-at-a-time    input */  cbreak();  /* No echo i.e To suppress the automatic echoing of typed characters */  noecho();  keypad(stdscr, TRUE);  /* To allow each and every mouse events */  mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);  /* Makes the terminal report mouse movement events */  printf("\033[?1003h\n");   while(1)  {     /* routines read a character from the window */    int c = wgetch(stdscr);    char buffer[512];    size_t max_size = sizeof(buffer);    if (c == KEY_MOUSE)     {        MEVENT event;        /* All routines return the integer ERR upon failure and an OK upon successful completion */        if (getmouse(&event) == OK)         {          snprintf(buffer, max_size, "Mouse at row=%d, column=%d, bstate=0x%08x",event.y, event.x,                      event.bstate);                /* if cursor distance 0.2 cm from (x = 0,y = 0) and press left button on mouse in X                 axis */                 if((event.x == 2 && event.y == 0) && (event.bstate & BUTTON1_PRESSED))                 {                   printw("\nPiano node C#\n");                  }                 /* if cursor distance 1 cm from (x = 0,y = 0) and press left button on mouse in X                  axis */                 else if((event.x == 10 && event.y == 0) && (event.bstate & BUTTON1_PRESSED))                 {                   printw("\nPiano node D#\n");                    }                 /* if cursor distance 3 cm from (x = 0,y = 0) and press left button on mouse in X                  axis */                 else if((event.x == 30 && event.y == 0) && (event.bstate & BUTTON1_PRESSED))                 {                   printw("\nPiano node F#\n");                      }                 /* if cursor distance 4 cm from (x = 0,y = 0) and press left button on mouse in X                  axis */                 else if((event.x == 40 && event.y == 0) && (event.bstate & BUTTON1_PRESSED))                 {                   printw("\nPiano node G#\n");                 }                  /* if cursor distance 5 cm from (x = 0,y = 0) and press left button on mouse in X                  axis */                 else if((event.x == 50 && event.y == 0) && (event.bstate & BUTTON1_PRESSED))                 {                   printw("\nPiano node A#\n");                 }                 /* if cursor distance 0.2cm from (x = 0,y = 0) in X axis */                 else if(event.x == 2 && event.y == 0)                 {                   printw("\nPiano node C\n");                          }                 /* if cursor distance 1 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 10 && event.y == 0)                 {                   printw("\nPiano node D\n");                   }                 /* if cursor distance 2cm from (x = 0,y = 0) in X axis */                 else if(event.x == 20 && event.y == 0)                 {                   printw("\nPiano node E\n");                     }                 /* if cursor distance 3 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 30 && event.y == 0)                 {                   printw("\nPiano node F\n");                     }                 /* if cursor distance 4 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 40 && event.y == 0)                 {                   printw("\nPiano node G\n");                     }                 /* if cursor distance 5 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 50 && event.y == 0)                 {                   printw("\nPiano node A\n");                     }                 /* if cursor distance 6 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 60 && event.y == 0)                 {                   printw("\nPiano node B\n");                     }                 /* if cursor distance 7 cm from (x = 0,y = 0) in X axis */                 else if(event.x == 70 && event.y == 0)                 {                   printw("\nPiano node C\n");                      }                 /* For any default mouse movement */                 else                 {                   printw("\nMouse movement going wrong\n");                   }      }      else       {        snprintf(buffer, max_size, "Got bad mouse event.");      }    }    /* Move the cursor in (x=0, y=0) position */    move(0, 0);    /* Print buffer contained string */    addstr(buffer);    /* Erase the current line to the right of the cursor  */    clrtoeol();    /* Move the cursor in (x=0, y=0) position */    move(0, 0);  }  /* Disable mouse movement events, as l = low */  printf("\033[?1003l\n");   endwin();  return 0;}

Viewing all articles
Browse latest Browse all 12365

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>