Gyro.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include<iostream>
00009 #include<fstream>
00010 #include<string.h>
00011 #include<errno.h>
00012 #include<stdio.h>
00013 #include<sys/types.h>
00014 #include<sys/stat.h>
00015 #include<fcntl.h>
00016 #include<unistd.h>
00017 #include"Gyro.H"
00018
00019
00020 #define XANGLE_CONVERSION .0432
00021 #define YANGLE_CONVERSION .0405
00022
00023 void* Gyro_run(void *c0);
00024 void* Gyro_run(void *c0)
00025 {
00026
00027
00028 Gyro *c = (Gyro *)c0;
00029 c->go(); return NULL;
00030 }
00031
00032
00033
00034 Gyro::Gyro()
00035 {
00036 xpos = 0;
00037 ypos = 0;
00038 xdpos = 0;
00039 ydpos = 0;
00040
00041 pthread_create(&runner, NULL, &Gyro_run, (void *)this);
00042 }
00043
00044
00045
00046 void Gyro::go()
00047 {
00048 int g1, fileno, nbytes = -1, g2, g3;
00049 fileno = open("/dev/mouse" , O_NONBLOCK);
00050 while( 1 )
00051 {
00052 g1 = 999;
00053 g2 = 0;
00054 g3 = 0;
00055 nbytes = read(fileno , &g1 , 1);
00056
00057
00058
00059 if(g1 != 999) g1 = g1 % 256;
00060 if(g1 == 8 || g1 == 24 || g1 == 56 || g1 == 40)
00061 {
00062 nbytes = -1;
00063 while(nbytes == -1)
00064 {
00065 nbytes = read(fileno , &g2 , 1);
00066 if(nbytes == 1)
00067 {
00068 nbytes = -1;
00069 while(nbytes == -1)
00070 {
00071 nbytes = read(fileno , &g3 , 1);
00072 }
00073 }
00074 }
00075 g2 = g2 % 256;
00076 g3 = g3 % 256;
00077 if(g2 > 128) g2 = g2 - 256;
00078 if(g3 > 128) g3 = g3 - 256;
00079 xdpos = xdpos + g2;
00080 ydpos = ydpos + g3;
00081 }
00082 if(nbytes == -1) usleep(3000);
00083 }
00084 close(fileno);
00085 return;
00086 }
00087
00088
00089
00090 void Gyro::getAngle( int &x , int &y )
00091 {
00092 xpos += xdpos;
00093 ypos += ydpos;
00094 xdpos = 0;
00095 ydpos = 0;
00096 x = int(xpos * XANGLE_CONVERSION);
00097 y = int(ypos * YANGLE_CONVERSION);
00098 }
00099
00100
00101
00102 void Gyro::setAngle( int x , int y )
00103 {
00104 xpos = x;
00105 ypos = y;
00106 }
00107
00108
00109
00110
00111
00112