Archived
1

cpp wrapping

This commit is contained in:
Evan Pratten 2021-05-29 09:39:54 -04:00
parent 692606229a
commit 434fe34fb9
4 changed files with 80 additions and 51 deletions

View File

@ -8,3 +8,7 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
[build-dependencies]
cc = "1.0"

View File

@ -0,0 +1,3 @@
fn main() {
cc::Build::new().file("cpp/wrapper.cc").flag("-Wno-deprecated").flag("-Wno-deprecated-copy").compile("foo");
}

28
libodm/cpp/listener.cc Normal file
View File

@ -0,0 +1,28 @@
class LeapEventListener : public Listener
{
public:
virtual void onConnect(const Controller &);
virtual void onDisconnect(const Controller &);
virtual void onFrame(const Controller &);
};
void LeapEventListener::onConnect(const Controller &controller)
{
std::cout << "LeapMotion Controller: Connected" << std::endl;
}
//Not dispatched when running in a debugger
void LeapEventListener::onDisconnect(const Controller &controller)
{
std::cout << "LeapMotion Controller: Disconnected" << std::endl;
}
void LeapEventListener::onFrame(const Controller &controller)
{
std::cout << "New frame available" << std::endl;
Frame frame = controller.frame();
// Get the camera images
images = frame.images();
}

View File

@ -3,66 +3,60 @@
using namespace Leap; using namespace Leap;
class LeapEventListener : public Listener //---- Start Public Functions ----//
extern "C"
{ {
public: void beginEventLoop();
virtual void onConnect(const Controller &); bool isControllerCreated();
virtual void onDisconnect(const Controller &); void endEventLoop();
virtual void onFrame(const Controller &); bool imageExists();
}; int getImageHeight();
int getImageWidth();
void LeapEventListener::onConnect(const Controller &controller) int getImageBPP();
{ const unsigned char *getImageLeft();
std::cout << "Connected" << std::endl; const unsigned char *getImageRight();
// Enable gestures, set Config values:
controller.enableGesture(Gesture::TYPE_SWIPE);
controller.config().setFloat("Gesture.Swipe.MinLength", 200.0);
controller.config().save();
} }
//---- End Public Functions ----//
//Not dispatched when running in a debugger //---- Start Globals ----//
void LeapEventListener::onDisconnect(const Controller &controller) Controller *controller = nullptr;
ImageList images;
//---- End Globals ----//
#include "listener.cc"
//---- Start Public Function Impls ----//
void beginEventLoop()
{ {
std::cout << "Disconnected" << std::endl; if (controller == nullptr)
} {
// Create a controller
controller = new Controller();
void LeapEventListener::onFrame(const Controller &controller) // Set device policy
{ controller->setPolicyFlags(Controller::POLICY_IMAGES);
std::cout << "New frame available" << std::endl;
Frame frame = controller.frame();
// Get the camera images
ImageList images = frame.images();
// We require two images // Set up event handling
if (images.count() != 2){ LeapEventListener listener;
std::cout << "Not enough images received" << std::endl; controller->addListener(listener);
return;
} }
// Build a protobuf repr of the image data
FrameStreamMessage message;
message.set_frame_height(images[0].height());
message.set_frame_width(images[0].width());
message.set_pixel_bytes(images[0].bytesPerPixel());
message.set_left_image((char*) images[0].data());
message.set_right_image((char*) images[1].data());
} }
int main() void endEventLoop()
{ {
if (controller != nullptr)
// Access to the LeapMotion device along with callback setup {
Controller controller; delete controller;
LeapEventListener listener;
controller.addListener(listener);
controller.setPolicy(Leap::Controller::POLICY_IMAGES);
while(true){
} }
}
return 0; bool isControllerCreated() { return controller != nullptr; }
} bool imageExists() { return images.count() == 2; }
int getImageHeight() { return images[0].height(); }
int getImageWidth() { return images[0].width(); }
int getImageBPP() { return images[0].bytesPerPixel(); }
const unsigned char *getImageLeft() { return images[0].data(); }
const unsigned char *getImageRight() { return images[1].data(); }
//---- End Public Function Impls ----//