pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by nexno on Mon 14 Sep 19:33
report abuse | download | new post

  1. //see http://nexno.blogspot.com/
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <time.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <termios.h>
  13. #include <sys/ioctl.h>
  14. #include <getopt.h>
  15. #include <sys/types.h>
  16. #include <ctype.h>
  17. #include <X11/X.h>
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/extensions/XTest.h>
  21.  
  22. #define BUFSIZE 256
  23. #define DATASIZE 6
  24.  
  25. int serialport_init(const char* serialport, int baud);
  26.  
  27. int main(int argc, char *argv[]){
  28.     Display *display;
  29.     Window root;
  30.     Window fromroot, tmpwin;
  31.     if((display = XOpenDisplay(NULL)) == NULL) {
  32.         fprintf(stderr, "Cannot open local X-display.\n");
  33.         exit(1);
  34.     }
  35.     root = DefaultRootWindow(display);
  36.     const char *sep = ";";
  37.     char line[BUFSIZE];
  38.     int data[DATASIZE];
  39.     char *token;
  40.     char *ptr;
  41.     int i=0;
  42.         int x, y, tmp;
  43.         unsigned int tmp2;
  44.         long altmodecnt=0;
  45.         int last_lbtn=0;
  46.         int last_rbtn=0;
  47.         int fd = 0;
  48.         if((fd = serialport_init("/dev/ttyUSB0", 19200)) == -1) {
  49.                 exit(1);
  50.     }
  51.         FILE *sp;
  52.         sp = fdopen(fd, "r");
  53.         while(1){
  54.                 while(fgets(line, BUFSIZE, sp) != NULL ){
  55.                         i = 0;
  56.                         if(*line != '\0') line[strlen(line)-1] = '\0';
  57.                         //printf("%s\n",line); //DEBUG
  58.                         if((token = strtok_r(line, sep, &ptr)) != NULL) {
  59.                                 data[i++] = atoi(token);
  60.                                 while(( token = strtok_r(NULL, sep, &ptr)) != NULL && i < DATASIZE)
  61.                                         data[i++] = atoi(token);
  62.                         }
  63.  
  64.                         if(i == DATASIZE){
  65.  
  66.                             //printf("%i,%i,%i,%i,%i,%i\n",data[0],data[1],data[2],data[3],data[4],(90-data[5])); //DEBUG
  67.                                 if(data[4]>140 || data[4]<-140){
  68.                                         altmodecnt++;
  69.                                         //printf("alternative mode\n"); //DEBUG
  70.                                         if(data[1]>20 && altmodecnt%8==0){ //ZOOM (Compiz Super&Plus)
  71.                                                 XTestFakeKeyEvent(display, 133, True, 0);
  72.                                                 XTestFakeKeyEvent(display, 35, True, 0);
  73.                                                 XTestFakeKeyEvent(display, 35, False, 0);
  74.                                                 XTestFakeKeyEvent(display, 133, False, 0);     
  75.                                                 XFlush(display);
  76.                                         }else if(data[1]<-20 && altmodecnt%8==0){ //ZOOM (Compiz Super&Minus)
  77.                                                 XTestFakeKeyEvent(display, 133, True, 0);
  78.                                                 XTestFakeKeyEvent(display, 61, True, 0);
  79.                                                 XTestFakeKeyEvent(display, 61, False, 0);
  80.                                                 XTestFakeKeyEvent(display, 133, False, 0);
  81.                                                 XFlush(display);
  82.                                         }
  83.                                         if(data[0]>20 && altmodecnt%10==0){ //Volume controll
  84.                                                 system("amixer -c 0 set Master 1%-");
  85.                                         }else if(data[0]<-20 && altmodecnt%10==0){
  86.                                                 system("amixer -c 0 set Master 1%+");
  87.                                         }
  88.                                         continue;
  89.                                 }
  90.  
  91.                                 XQueryPointer(display, root, &fromroot, &tmpwin, &x, &y, &tmp, &tmp, &tmp2);
  92.                                 XWarpPointer(display, None, root, 0, 0, 0, 0, x+(data[0]/2), y+((-1)*data[1]/2));
  93.  
  94.                                 // NORMAL MOUSE BUTTON USAGE
  95.                                 if(     data[2] != last_lbtn ){
  96.                                         last_lbtn = !last_lbtn;
  97.                                         XTestFakeButtonEvent(display, 1, last_lbtn==1?True:False, 0);
  98.                                 }
  99.                                 if(     data[3] != last_rbtn ){
  100.                                         last_rbtn = !last_rbtn;
  101.                                         XTestFakeButtonEvent(display, 3, last_rbtn==1?True:False, 0);
  102.                                 }
  103.                                
  104.                                 XFlush(display);
  105.                         }
  106.                         //else printf("bad data %i\n",i);
  107.                 }
  108.                 usleep(1000*50);
  109.         }
  110.         return 0;
  111. }
  112.  
  113. // takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
  114. // and a baud rate (bps) and connects to that port at that speed and 8N1.
  115. // opens the port in fully raw mode so you can send binary data.
  116. // returns valid fd, or -1 on error
  117. int serialport_init(const char* serialport, int baud){
  118.     struct termios toptions;
  119.     int fd;
  120.     fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
  121.     if (fd == -1){
  122.         perror("init_serialport: Unable to open port ");
  123.         return -1;
  124.     }
  125.     if (tcgetattr(fd, &toptions) < 0) {
  126.         perror("init_serialport: Couldn't get term attributes");
  127.         return -1;
  128.     }
  129.     speed_t brate = baud; // let you override switch below if needed
  130.     switch(baud) {
  131.         case 4800:   brate=B4800;   break;
  132.         case 9600:   brate=B9600;   break;
  133.         case 19200:  brate=B19200;  break;
  134.         case 38400:  brate=B38400;  break;
  135.         case 57600:  brate=B57600;  break;
  136.         case 115200: brate=B115200; break;
  137.         default: brate=B9600;
  138.     }
  139.     cfsetispeed(&toptions, brate);
  140.     cfsetospeed(&toptions, brate);
  141.     toptions.c_cflag &= ~PARENB; // 8N1
  142.     toptions.c_cflag &= ~CSTOPB; // 8N1
  143.     toptions.c_cflag &= ~CSIZE; // 8N1
  144.     toptions.c_cflag |= CS8; // 8N1
  145.     toptions.c_cflag &= ~CRTSCTS; // no flow control
  146.     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
  147.     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  148.     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
  149.     toptions.c_oflag &= ~OPOST; // make raw
  150.     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
  151.     toptions.c_cc[VMIN]  = 0;
  152.     toptions.c_cc[VTIME] = 20;
  153.     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
  154.         perror("init_serialport: Couldn't set term attributes");
  155.         return -1;
  156.     }
  157.     return fd;
  158. }
  159.  
  160. /*
  161. //And here is the Arduino-Code
  162.  
  163. #include <math.h>
  164. #include <Wire.h>
  165. #include <WiiChuck.h>
  166.  
  167. #define MAXANGLE 90
  168. #define MINANGLE -90
  169.  
  170. WiiChuck chuck = WiiChuck();
  171. int angleStart, currentAngle;
  172. int tillerStart = 0;
  173. double angle;
  174.  
  175. void setup() {
  176.   Serial.begin(19200);
  177.   chuck.begin();
  178.   chuck.update();
  179.   chuck.calibrateJoy();
  180. }
  181.  
  182. void loop() {
  183.   delay(20);
  184.   chuck.update();
  185.   Serial.print((int)chuck.readJoyX());
  186.   Serial.print(";");
  187.   Serial.print((int)chuck.readJoyY());
  188.   Serial.print(";");
  189.   Serial.print((int)chuck.zPressed());
  190.   Serial.print(";");
  191.   Serial.print((int)chuck.cPressed());
  192.   Serial.print(";");
  193.   Serial.print((int)chuck.readRoll());
  194.   Serial.print(";");
  195.   Serial.print((int)chuck.readPitch());
  196.   Serial.print("\n");
  197. }
  198.  
  199.  
  200. // Plus this little modification in the WiiChuck.h
  201.     boolean zPressed() {
  202.         //return (buttonZ && ! lastZ);
  203.                 return buttonZ;
  204.     }
  205.     boolean cPressed() {
  206.         //return (buttonC && ! lastC);
  207.                 return buttonC;
  208.     }
  209. */

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post