Skip to content

Instantly share code, notes, and snippets.

@bengfarrell
Created February 19, 2013 18:26
Show Gist options
  • Save bengfarrell/4988483 to your computer and use it in GitHub Desktop.
Save bengfarrell/4988483 to your computer and use it in GitHub Desktop.
#include <node.h>
#include <v8.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ViiM/ViiMIncludes.h>
#include <ViiM/core/ViiMTypes.h>
#include "Viim/ViiM.h"
using namespace v8;
class EventHandlers {
public:
VVoid showMessage(ViiMLogEvent& e, VVoidP obj)
{
printf("ViiM Event: \n");
}
} eHands;
/* thread loop */
uv_loop_t *loop;
/* async thread ref */
uv_async_t async;
/* if thread should be locked and running */
bool keepWorkerRunning;
/* context for sending NodeJS callbacks */
Persistent<Object> context_obj;
VVoid showMessage(ViiMLogEvent& e, VVoidP obj);
VVoid handCreateHandler(ViiMHandEvent& e, VVoidP object); //create hand
VVoid handUpdateHandler(ViiMHandEvent& e, VVoidP object); //update hand
VVoid handLostHandler(ViiMHandEvent& e, VVoidP object); //lost Hand
/* for swipe gestures; */
VVoid swipeRightHandler(ViiMGestureEvent& e, VVoidP object); //swipe right
VVoid swipeLeftHandler(ViiMGestureEvent& e, VVoidP object); //swipe left
VVoid swipeUpHandler(ViiMGestureEvent& e, VVoidP object); //swipe up
VVoid swipeDownHandler(ViiMGestureEvent& e, VVoidP object); //swipe down
/* for hand profile gestures; */
VVoid handOpenHandler(ViiMGestureEvent& e, VVoidP object); //hand open
VVoid handCloseHandler(ViiMGestureEvent& e, VVoidP object); //hand close
/* for action gestures */
VVoid moveHandler(ViiMGestureEvent& e, VVoidP object); //move gesture
VVoid waveHandler(ViiMGestureEvent& e, VVoidP object); //wave gesture
VVoid steadyHandler(ViiMGestureEvent& e, VVoidP object); //steady gesture
/* NodeJS Methods */
void frameWorker(uv_work_t *req);
void onFrameWorkerThreadComplete(uv_work_t* req);
void onMotionEvent(uv_async_t *handle, int status /*UNUSED*/);
Handle<Value> initialize(const Arguments& args);
Handle<Value> close(const Arguments& args);
/**
* entry point for plugin
*
* @param plugin target
*/
void init(Handle<Object> target) {
fprintf(stderr, "Follow the Hand! \n");
target->Set(String::NewSymbol("init"),
FunctionTemplate::New(initialize)->GetFunction());
target->Set(String::NewSymbol("close"),
FunctionTemplate::New(close)->GetFunction());
context_obj = Persistent<Object>::New(Object::New());
target->Set(String::New("context"), context_obj);
}
/**
* shutdown/cleanup NITE/OpenNI
*
* @param args (none used)
*/
Handle<Value> close(const Arguments& args) {
HandleScope scope;
fprintf(stderr, "Shutdown NITE\n");
return scope.Close(Undefined());
}
/**
* nodeJS method to intialize and start OpenNI/NiTE
*
* @param args (none - don't pass them in here)
*/
Handle<Value> initialize(const Arguments& args) {
HandleScope scope;
fprintf(stderr, "Initialize Depth Camera\n");
loop = uv_default_loop();
keepWorkerRunning = true;
uv_work_t req;
uv_async_init(loop, &async, onMotionEvent);
uv_queue_work(loop, &req, frameWorker, onFrameWorkerThreadComplete);
uv_run(loop);
return scope.Close(Undefined());
}
/**
* on motion event found in frame processing thread
*
* @param async handle
* @param status (?)
*/
void onMotionEvent(uv_async_t *handle, int status /*UNUSED*/) {
}
/**
* thread worker thread for OpenNI/NITE to read frames and do work on them
*
* @param thread request
*/
void onFrameWorkerThreadComplete(uv_work_t *req) {
fprintf(stderr, "OpenNI/NITE Processing Complete\n");
uv_close((uv_handle_t*) &async, NULL);
}
/**
* process frames in separate thread
*
* @param request thread
*/
void frameWorker(uv_work_t *req) {
ViiM _engine;
EventHandlers *eHands = new EventHandlers();
/* It's a good practice to listen for log events. They will give you output for errors and warnings */
_engine.addEventListener(ViiMLogEvent::MESSAGE, showMessage, NULL);
//_gesture.addEventListener(ViiMLogEvent::MESSAGE, showMessage, eHands);
fprintf(stderr, "Started Thread\n");
VUInt16 _width;
VUInt16 _height;
ViiMUnitGesture _gesture;
/* starts the ViiM engine */
/* registers the desired unit, in this case the Gestures Unit */
_engine.setLogLeveToAll(ViiMLogLevel::ALL);
_engine.startDevice(1, ViiMStream::BOTH, ViiMResolution::VGA, ViiMResolution::VGA);
VUInt16 deviceStarted = _engine.hasDeviceStarted(1);
printf("Device is started %d \n", deviceStarted);
//_engine.registerUnitProcess(1, _gesture);
printf("Number of started devices %d \n", _engine.getNumberOfStartedDevices());
_width = _engine.getColorWidth(1);
_height = _engine.getColorHeight(1);
printf("Width, Height: %d x %d \n", _width, _height);
/* flips the displayed image */
//_engine.flipImages(1);
/* chooses the hand focus type, to start trackin the hand */
//_gesture.setFocusType(ViiMFocusType::WAVE_HAND);
/*sets a threshold so that the sensor only "sees" up to 4 meters*/
//_gesture.setFarThreshold(4000);
/*The number of hands in which one wants to compute the gestures*/
//_gesture.setNumberOfHandsToProcess(1); // -> ATTENTION TO THE NITE.ini FILE!!!
/* add listeners to the desired events, the ones that have handlers (methods) defined in the class header */
/*_gesture.addEventListener(ViiMHandEvent::CREATE, handCreateHandler, NULL); //create hand
_gesture.addEventListener(ViiMHandEvent::UPDATE, handUpdateHandler, NULL); //update hand
_gesture.addEventListener(ViiMHandEvent::LOST, handLostHandler, NULL); //lost Hand
_gesture.addEventListener(ViiMGestureEvent::SWIPE_RIGHT, swipeRightHandler, NULL); //swipe right
_gesture.addEventListener(ViiMGestureEvent::SWIPE_LEFT, swipeLeftHandler, NULL); //swipe left
_gesture.addEventListener(ViiMGestureEvent::SWIPE_UP, swipeUpHandler, NULL); //swipe up
_gesture.addEventListener(ViiMGestureEvent::SWIPE_DOWN, swipeDownHandler, NULL); //swipe down
_gesture.addEventListener(ViiMGestureEvent::HAND_OPEN, handOpenHandler, NULL); //hand open
_gesture.addEventListener(ViiMGestureEvent::HAND_CLOSE, handCloseHandler, NULL);*/
while(keepWorkerRunning) {
//deviceStarted = _engine.hasDeviceStarted(1);
//printf("Device is started %d \n", deviceStarted);
//nothing - just keep the thread going
}
}
/* Callback function to listen for log events.
* All callback functions have this structure.
* The first parameter is the event type with the object that contains all event information.
* The second parameter is the listener object that might be use full to access class functions and members.
*/
VVoid showMessage(ViiMLogEvent& e, VVoidP obj)
{
printf("ViiM Event: \n");
}
/* ----- THE EVENT HANDLERS --------------- */
VVoid handCreateHandler(ViiMHandEvent& event, VVoidP object)
{
fprintf(stderr, "Hand Create Event: \n");
return;
}
VVoid handUpdateHandler(ViiMHandEvent& event, VVoidP object)
{
fprintf(stderr, "Hand Update Event: \n");
return;
}
VVoid handLostHandler(ViiMHandEvent& event, VVoidP object)
{
fprintf(stderr, "Hand Lost Event: \n");
return;
}
VVoid swipeRightHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Swipe Right Event: \n");
return;
}
VVoid swipeLeftHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Swipe Left Event: \n");
return;
}
VVoid swipeUpHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Swipe Up Event: \n");
return;
}
VVoid swipeDownHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Swipe Down Event: \n");
return;
}
VVoid handOpenHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Hand Open Event: \n");
return;
}
VVoid handCloseHandler(ViiMGestureEvent& event, VVoidP object)
{
fprintf(stderr, "Hand Close Event: \n");
return;
}
/* Module Declaration */
NODE_MODULE(viimtest, init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment