util.cpp
Go to the documentation of this file.
00001 /*
00002     util.c - utilities
00003 
00004     Copyright (C) 2011 Hugo Perquin - http://blog.perquin.com
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019     MA 02110-1301 USA.
00020 */
00021 #include <stdio.h>   /* Standard input/output definitions */
00022 #include <fcntl.h>   /* File control definitions */
00023 #include <termios.h> /* POSIX terminal control definitions */
00024 #include <unistd.h>  /* UNIX standard function definitions */
00025 #include <time.h>  
00026 #include <sys/time.h>
00027 
00028 #include "util.h"
00029 
00030 //non blocking getchar
00031 int util_getch(void)
00032 {
00033 struct termios oldt,
00034 newt;
00035 int ch=-1;
00036 tcgetattr( STDIN_FILENO, &oldt );
00037 newt = oldt;
00038 newt.c_lflag &= ~( ICANON | ECHO );
00039 tcsetattr( STDIN_FILENO, TCSANOW, &newt );
00040 fcntl(STDIN_FILENO, F_SETFL, FNDELAY);
00041 ch = getchar();
00042 fcntl(STDIN_FILENO, F_SETFL, 0);
00043 tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
00044 return ch;
00045 }
00046 
00047 //return timestamp in seconds with microsecond resolution
00048 double util_timestamp()
00049 {
00050   struct timeval tv;
00051   gettimeofday(&tv, NULL); 
00052   return (double)tv.tv_sec+((double)tv.tv_usec)/1000000;
00053 }
00054 
00055 //return timestamp in microseconds since first call to this function
00056 int util_timestamp_int()
00057 {
00058   static struct timeval tv1;
00059   struct timeval tv;
00060   if(tv1.tv_usec==0 && tv1.tv_sec==0) gettimeofday(&tv1, NULL); 
00061   gettimeofday(&tv, NULL); 
00062   return (int)(tv.tv_sec-tv1.tv_sec)*1000000+(int)(tv.tv_usec-tv1.tv_usec);
00063 }


ardrone2islab
Author(s): Trung Nguyen , Oscar De Silva
autogenerated on Thu Jun 6 2019 20:53:51