Navigation Menu

Skip to content

Commit

Permalink
added support for the sleep() function to be called under a Win32 env…
Browse files Browse the repository at this point in the history
…ironment

Signed-off-by: Marcos Paulo Berteli Slomp <mslomp@gmail.com>
  • Loading branch information
slomp authored and qdot committed Dec 27, 2010
1 parent 32fe089 commit 4bf4e83
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions examples/tiltdemo.c
Expand Up @@ -31,28 +31,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#ifndef _WIN32
#include <unistd.h>
#else
// Microsoft Visual C++ does not provide the <unistd.h> header, but most of
// its contents can be found within the <stdint.h> header:
#include <stdint.h>
// except for the UNIX sleep() function that has to be emulated:
#include <windows.h>
// http://pubs.opengroup.org/onlinepubs/009695399/functions/sleep.html
// According to the link above, the semantics of UNIX sleep() is as follows:
// "If sleep() returns because the requested time has elapsed, the value
// returned shall be 0. If sleep() returns due to delivery of a signal, the
// return value shall be the "unslept" amount (the requested time minus the
// time actually slept) in seconds."
// The following function does not implement the return semantics, but
// will do for now... A proper implementation would require Performance
// Counters before and after the forward call to the Windows Sleep()...
unsigned sleep(unsigned seconds)
{
Sleep(seconds*1000); // The Windows Sleep operates on milliseconds
return(0);
}
// Note for MinGW-gcc users: MinGW-gcc also does not provide the UNIX sleep()
// function within <unistd.h>, but it does provide usleep(); trivial wrapping
// of sleep() aroung usleep() is possible, however the usleep() documentation
// (http://docs.hp.com/en/B2355-90682/usleep.2.html) clearly states that:
// "The useconds argument must be less than 1,000,000. (...)
// (...) The usleep() function may fail if:
// [EINVAL]
// The time interval specified 1,000,000 or more microseconds."
// which means that something like below can be potentially dangerous:
// unsigned sleep(unsigned seconds)
// {
// usleep(seconds*1000000); // The usleep operates on microseconds
// return(0);
// }
// So, it is strongly advised to stick with the _WIN32/_MSC_VER
// http://www.xinotes.org/notes/note/439/
#endif//_WIN32



/* This is a simple demo. It connects to the kinect and plays with the motor,
the accelerometers, and the LED. It doesn't do anything with images. And,
unlike the other examples, no OpenGL is required!
unlike the other examples, no OpenGL is required!
So, this should serve as the reference example for working with the motor,
accelerometers, and LEDs. */

void no_kinect_quit(void)
{
printf("Error: Kinect not connected?\n");
exit(1);
}

int main(int argc, char *argv[])
{
srand(time(0));

while (1) {
// Pick a random tilt and a random LED state
int led = rand() % 6;
freenect_led_options led = (freenect_led_options) (rand() % 6); // explicit cast
int tilt = (rand() % 30)-15;
freenect_raw_tilt_state *state = 0;
double dx, dy, dz;
Expand All @@ -71,8 +112,8 @@ int main(int argc, char *argv[])

printf("led[%d] tilt[%d] accel[%lf,%lf,%lf]\n", led, tilt, dx,dy,dz);

sleep(3);
sleep(3);
}
}


0 comments on commit 4bf4e83

Please sign in to comment.