Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phPlayerUtil.cpp

Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------ */
00002 /* This allows me to set the non blocking flag on stdin so I can check to 
00003  * see if the user has closed the displays. We can exit that way easily */
00004 /* ------------------------------------------------------------------------ */
00005 int ph_set_blocking(FILE *f, int block)
00006 {
00007     phFUNCTION("ph_set_blocking")
00008     int flags = 0;
00009     
00010     flags = fcntl(fileno(f),F_GETFL,0);
00011     phCHECK_RC(flags,"fcntl:F_GETFL","fcntl failed to get fd flags");
00012     
00013     if (block)
00014         flags &- ~O_NONBLOCK;
00015     else
00016         flags |= O_NONBLOCK;
00017     
00018     rc = fcntl(fileno(f),F_SETFL,flags);
00019     phCHECK_RC(rc,"fcntl:F_SETFL","fcntl failed to set fd flags");
00020 
00021     return phSUCCESS;
00022 error:
00023     return phFAIL;
00024 }
00025 
00026 /* ------------------------------------------------------------------------ */
00027 int ph_wait_for(int wait )
00028 {
00029     phFUNCTION("ph_wait_for")
00030     int retrc = phSUCCESS;
00031     char byte = 0;
00032     
00033         if (!wait)
00034         {
00035             rc = ph_set_blocking(stdin,0); /* 0 : don't block */
00036             phPRINT_RC(rc,NULL,"ph_set_blocking failed.");
00037         }
00038 
00039         do
00040         {
00041             errno = 0;
00042             byte = getc(stdin);
00043             if (byte == -1)
00044             {
00045                 if (errno == EAGAIN)
00046                 {
00047                     retrc = -2;
00048                     phYield();
00049                 }
00050                 else
00051                 {
00052                     retrc = phFAIL;
00053                     phPRINT_RC(byte,"getc","getc failed.");
00054                 }
00055             }
00056             else if (byte == 'c')
00057             {
00058                 retrc = phSUCCESS;
00059             }
00060             else
00061             {
00062                 retrc = byte;
00063             }
00064         } while ((retrc == -2) && (wait == 1));
00065         
00066         rc = ph_set_blocking(stdin,1);
00067         phPRINT_RC(rc,NULL,"ph_set_blocking failed.");
00068 
00069     return retrc;
00070 error:
00071     return phFAIL;
00072 }
00073 
00074 /* ------------------------------------------------------------------------ */
00075 int ph_handle_keyboard( phPlayerPosition    &position,
00076                         double              inc,
00077                         double              &translate, 
00078                         double              &rotate )
00079 {
00080     phFUNCTION("ph_handle_keyboard")
00081     int cmd = 0;
00082     
00083     cmd = ph_wait_for(0);
00084 
00085     if (cmd == 'f')
00086     {
00087         phPRINT("forward\n");
00088         if (translate < max_translate)
00089             translate += inc;
00090         phPRINT("%0.8f %0.8f",translate,rotate);
00091         rc = position.setCmdVel(translate,0.0,rotate,1);
00092     }
00093     else if (cmd == 'b')
00094     {
00095         phPRINT("backward\n");
00096         if (translate > (max_translate*-1.0))
00097             translate -= inc;
00098         phPRINT("%0.8f %0.8f",translate,rotate);
00099         rc = position.setCmdVel(translate,0.0,rotate,1);
00100     }
00101     else if (cmd == ',')
00102     {
00103         phPRINT("left\n");
00104         if (rotate < max_rotate)
00105             rotate += inc;
00106         phPRINT("%0.8f %0.8f",translate,rotate);
00107         rc = position.setCmdVel(translate,0.0,rotate,1);
00108     }
00109     else if (cmd == '.')
00110     {
00111         phPRINT("right\n");
00112         if (rotate > (max_rotate*-1.0))
00113             rotate -= inc;
00114         phPRINT("%0.8f %0.8f",translate,rotate);
00115         rc = position.setCmdVel(translate,0.0,rotate,1);
00116     }
00117     else if (cmd == 's') /* s == stop */
00118     {
00119         phPRINT("stop\n");
00120         translate = 0.0;
00121         rotate = 0.0;
00122         phPRINT("%0.8f %0.8f",translate,rotate);
00123         rc = position.setCmdVel(translate, 0.0, rotate, 1);
00124     }
00125     else if (cmd == 20) /* SPACE == stop */
00126     {
00127         phPRINT("stop\n");
00128             translate = 0;
00129         rotate = 0;
00130         phPRINT("%0.8f %0.8f",translate,rotate);
00131         rc = position.setCmdVel(translate,0.0,rotate,0);
00132     }
00133     else if (cmd == 'q')
00134         return 1;
00135         
00136     return phSUCCESS;
00137 }
00138 
00139 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:07 2007 for phission by  doxygen 1.4.4