Leap Motion C API  4.1.0
The API to the LeapC library.
Polling Example

Instead of using callback functions, which are invoked on a separate thread, your application can read the latest available data on its own, natural frame rate. This can be more convenient in situations where your application already has an event loop and you only need tracking data at the application's frame rate.

PollingSample.c

The PollingSample.c program simulates a program loop by just running forever in a for loop. Each iteration it calls the ExampleConnection GetFrame() function. If the frame has changed since the last iteration, the sample prints out some basic data from the frame.

1 #undef __cplusplus
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 #ifdef _WIN32
7 #include <Windows.h>
8 #else
9 #include <unistd.h>
10 #endif
11 
12 #include "LeapC.h"
13 #include "ExampleConnection.h"
14 
15 int64_t lastFrameID = 0; //The last frame received
16 
17 int main(int argc, char** argv) {
18  OpenConnection();
19  while(!IsConnected)
20  millisleep(100); //wait a bit to let the connection complete
21 
22  printf("Connected.");
23  LEAP_DEVICE_INFO* deviceProps = GetDeviceProperties();
24  if(deviceProps)
25  printf("Using device %s.\n", deviceProps->serial);
26 
27  for(;;){
28  LEAP_TRACKING_EVENT *frame = GetFrame();
29  if(frame && (frame->tracking_frame_id > lastFrameID)){
30  lastFrameID = frame->tracking_frame_id;
31  printf("Frame %lli with %i hands.\n", (long long int)frame->tracking_frame_id, frame->nHands);
32  for(uint32_t h = 0; h < frame->nHands; h++){
33  LEAP_HAND* hand = &frame->pHands[h];
34  printf(" Hand id %i is a %s hand with position (%f, %f, %f).\n",
35  hand->id,
36  (hand->type == eLeapHandType_Left ? "left" : "right"),
37  hand->palm.position.x,
38  hand->palm.position.y,
39  hand->palm.position.z);
40  }
41  }
42  } //ctrl-c to exit
43  return 0;
44 }
45 //End-of-Sample

ExampleConnection.c

The Polling example uses the same ExampleConnection code as the callback, but makes use of the GetFrame() and setFrame() and the GetDeviceProperties() and setDevice() functions. These functions write the structs received from the LeapPollConnection() function to a global variable where they can be accessed any time by the application. Mutexes are used to guard against both threads accessing the data at the same time.

1 /* Used in Polling Example: */
2 
3 /**
4  * Caches the newest frame by copying the tracking event struct returned by
5  * LeapC.
6  */
7 void setFrame(const LEAP_TRACKING_EVENT *frame){
8  LockMutex(&dataLock);
9  if(!lastFrame) lastFrame = malloc(sizeof(*frame));
10  *lastFrame = *frame;
11  UnlockMutex(&dataLock);
12 }
13 
14 /** Returns a pointer to the cached tracking frame. */
15 LEAP_TRACKING_EVENT* GetFrame(){
16  LEAP_TRACKING_EVENT *currentFrame;
17 
18  LockMutex(&dataLock);
19  currentFrame = lastFrame;
20  UnlockMutex(&dataLock);
21 
22  return currentFrame;
23 }
24 
25 /**
26  * Caches the last device found by copying the device info struct returned by
27  * LeapC.
28  */
29 static void setDevice(const LEAP_DEVICE_INFO *deviceProps){
30  LockMutex(&dataLock);
31  if(lastDevice){
32  free(lastDevice->serial);
33  } else {
34  lastDevice = malloc(sizeof(*deviceProps));
35  }
36  *lastDevice = *deviceProps;
37  lastDevice->serial = malloc(deviceProps->serial_length);
38  memcpy(lastDevice->serial, deviceProps->serial, deviceProps->serial_length);
39  UnlockMutex(&dataLock);
40 }
41 
42 /** Returns a pointer to the cached device info. */
43 LEAP_DEVICE_INFO* GetDeviceProperties(){
44  LEAP_DEVICE_INFO *currentDevice;
45  LockMutex(&dataLock);
46  currentDevice = lastDevice;
47  UnlockMutex(&dataLock);
48  return currentDevice;
49 }
50 
51 //End of polling example-specific code