Software Archive
Read-only legacy content
17061 Discussions

millis function for Eclipse/Intel IDE

Stan_G_
Beginner
188 Views
extern "C" unsigned long millis();

Porting some code from Arduino environment to the Intel IDE (Eclipse) environment, I had need for the arduino mills() function (which is very useful).

The arduino version return ms from program start - my version returns ms from the first time it is called (so I tend to call it first this in main()

It may be useful for someone else so I post the header and the code below - Note: The code works - but I am sure it could have been written betterer!

Header;

 

Code.

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
static struct timespec tstart={0,0};
static int start_retrieved=0;
unsigned long millis() {


	  unsigned long milliseconds;
	  unsigned long n2;
      unsigned long n;

struct timespec tend={0,0};

if (start_retrieved==0)
{
	 clock_gettime(CLOCK_MONOTONIC, &tstart);
	 start_retrieved=1;
}



 clock_gettime(CLOCK_MONOTONIC, &tend);



n2=(((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
	        ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
 n=((((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
	        ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec)) -n2)*1000;
 milliseconds=(n2 * 1000)+n;

return(milliseconds);
}

 

Stan

0 Kudos
1 Reply
Stan_G_
Beginner
188 Views

Header code did not make it.

extern "C" unsigned long millis();

0 Kudos
Reply