Newer
Older
pyenergenie / src / energenie / drv / delay_posix.c
@David Whale David Whale on 4 Apr 2016 974 bytes Refactored delay into a platform module
/* delay_posix.c  04/04/2016  D.J.Whale
 *
 * Delay routines for posix compliant standard libraries (not Arduino)
 */

//#include <time.h>
#include <sys/time.h>

#include "system.h"

static struct timespec delay_1sec = {1, 0};


void delay(struct timespec time)
{
  nanosleep(&time, NULL);
}


void delaysec(uint8_t secs)
{
  while (secs-- != 0)
  {
    delay(delay_1sec);
  }
}


/* Based on code suggested by Gordon Henderson:
 * https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPi.c
 *
 * Note that his trick of using the hardware timer just didn't work,
 * and this is the best of a bad bunch. nanosleep() delays at least
 * 100uS in some cases.
 */


void delayus(unsigned int us)
{
  struct timeval tNow, tLong, tEnd;

  gettimeofday(&tNow, NULL);
  tLong.tv_sec  = us / 1000000;
  tLong.tv_usec = us % 1000000;
  timeradd(&tNow, &tLong, &tEnd);

  while (timercmp(&tNow, &tEnd, <))
  {
    gettimeofday(&tNow, NULL);
  }
}


/***** END OF FILE *****/