binding work
This commit is contained in:
parent
ea244ed3ee
commit
c119a2478e
@ -1,6 +1,7 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"libleap",
|
"libleap-sys",
|
||||||
"libodm",
|
# "libleap",
|
||||||
"snapscan",
|
# "libodm",
|
||||||
|
# "snapscan",
|
||||||
]
|
]
|
12
libleap-sys/Cargo.toml
Normal file
12
libleap-sys/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "libleap-sys"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Evan Pratten <ewpratten@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
build = "build.rs"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
bindgen = "0.58.1"
|
6112
libleap-sys/LeapSDK/include/Leap.h
Normal file
6112
libleap-sys/LeapSDK/include/Leap.h
Normal file
File diff suppressed because it is too large
Load Diff
1191
libleap-sys/LeapSDK/include/Leap.i
Normal file
1191
libleap-sys/LeapSDK/include/Leap.i
Normal file
File diff suppressed because it is too large
Load Diff
1050
libleap-sys/LeapSDK/include/LeapMath.h
Normal file
1050
libleap-sys/LeapSDK/include/LeapMath.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
libleap-sys/LeapSDK/lib/x64/libLeap.so
Executable file
BIN
libleap-sys/LeapSDK/lib/x64/libLeap.so
Executable file
Binary file not shown.
BIN
libleap-sys/LeapSDK/lib/x86/libLeap.so
Executable file
BIN
libleap-sys/LeapSDK/lib/x86/libLeap.so
Executable file
Binary file not shown.
62
libleap-sys/build.rs
Normal file
62
libleap-sys/build.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
extern crate bindgen;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Tell cargo to tell rustc to link the system bzip2
|
||||||
|
// shared library.
|
||||||
|
// println!("cargo:rustc-link-lib=bz2");
|
||||||
|
|
||||||
|
// Tell cargo to invalidate the built crate whenever the wrapper changes
|
||||||
|
println!("cargo:rerun-if-changed=wrapper.hpp");
|
||||||
|
|
||||||
|
// The bindgen::Builder is the main entry point
|
||||||
|
// to bindgen, and lets you build up options for
|
||||||
|
// the resulting bindings.
|
||||||
|
let bindings = bindgen::Builder::default()
|
||||||
|
|
||||||
|
// Hide issue types
|
||||||
|
.blocklist_item("FP_NAN")
|
||||||
|
.blocklist_item("FP_INFINITE")
|
||||||
|
.blocklist_item("FP_ZERO")
|
||||||
|
.blocklist_item("FP_SUBNORMAL")
|
||||||
|
.blocklist_item("FP_NORMAL")
|
||||||
|
.blocklist_item("int_type")
|
||||||
|
.blocklist_item("size_type")
|
||||||
|
.blocklist_item("char_type")
|
||||||
|
.blocklist_item("FP_INT_TONEAREST")
|
||||||
|
.blocklist_item("FP_INT_TONEARESTFROMZERO")
|
||||||
|
.blocklist_item("FP_INT_TOWARDZERO")
|
||||||
|
.blocklist_item("FP_INT_DOWNWARD")
|
||||||
|
.blocklist_item("FP_INT_UPWARD")
|
||||||
|
.blocklist_item("std_collate_string_type")
|
||||||
|
.blocklist_item("std_collate_byname_string_type")
|
||||||
|
.blocklist_item("std_numpunct_string_type")
|
||||||
|
.blocklist_item("std_numpunct_byname_string_type")
|
||||||
|
.blocklist_item("std_value")
|
||||||
|
.blocklist_item("std_size_type")
|
||||||
|
.blocklist_item("std_basic_ostream_sentry")
|
||||||
|
.blocklist_item("std_basic_istream_sentry_traits_type")
|
||||||
|
.blocklist_item("std_basic_istream_sentry___streambuf_type")
|
||||||
|
.blocklist_item("std_basic_istream_sentry___istream_type")
|
||||||
|
.blocklist_item("__gnu_cxx___min")
|
||||||
|
.blocklist_item("__gnu_cxx___max")
|
||||||
|
.blocklist_item("_Value")
|
||||||
|
// The input header we would like to generate
|
||||||
|
// bindings for.
|
||||||
|
.header("wrapper.hpp")
|
||||||
|
// Tell cargo to invalidate the built crate whenever any of the
|
||||||
|
// included header files changed.
|
||||||
|
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
|
||||||
|
// Finish the builder and generate the bindings.
|
||||||
|
.generate()
|
||||||
|
// Unwrap the Result and panic on failure.
|
||||||
|
.expect("Unable to generate bindings");
|
||||||
|
|
||||||
|
// Write the bindings to the $OUT_DIR/bindings.rs file.
|
||||||
|
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||||
|
bindings
|
||||||
|
.write_to_file(out_path.join("bindings.rs"))
|
||||||
|
.expect("Couldn't write bindings!");
|
||||||
|
}
|
5
libleap-sys/src/lib.rs
Normal file
5
libleap-sys/src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#![allow(non_upper_case_globals)]
|
||||||
|
#![allow(non_camel_case_types)]
|
||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
1
libleap-sys/wrapper.hpp
Normal file
1
libleap-sys/wrapper.hpp
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include "LeapSDK/include/Leap.h"
|
@ -10,4 +10,4 @@ edition = "2018"
|
|||||||
leap-sys = "0.1.1"
|
leap-sys = "0.1.1"
|
||||||
num = "0.4"
|
num = "0.4"
|
||||||
num-derive = "0.3"
|
num-derive = "0.3"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
@ -1,6 +1,6 @@
|
|||||||
use leap_sys::eLeapRS;
|
use leap_sys::eLeapRS;
|
||||||
|
|
||||||
#[derive(FromPrimitive)]
|
#[derive(FromPrimitive, Debug)]
|
||||||
pub enum LeapError {
|
pub enum LeapError {
|
||||||
/// The operation completed successfully.
|
/// The operation completed successfully.
|
||||||
Success = 0,
|
Success = 0,
|
||||||
|
@ -1,23 +1,22 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate num_derive;
|
extern crate num_derive;
|
||||||
|
|
||||||
use std::{
|
use std::{sync::{
|
||||||
sync::{
|
|
||||||
mpsc::{self, Sender},
|
mpsc::{self, Sender},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
}, thread::Thread, time::{Duration, SystemTime}};
|
||||||
thread::Thread,
|
|
||||||
};
|
|
||||||
|
|
||||||
use error::LeapError;
|
use error::LeapError;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use leap_sys::{
|
use leap_sys::{
|
||||||
LeapCreateConnection, LeapOpenConnection, LeapPollConnection, _eLeapRS_eLeapRS_Success,
|
_eLeapRS_eLeapRS_Success,
|
||||||
LEAP_CONNECTION, LEAP_CONNECTION_MESSAGE,
|
LEAP_CONNECTION, LEAP_CONNECTION_MESSAGE,LeapCreateConnection, LeapOpenConnection, LeapPollConnection
|
||||||
};
|
};
|
||||||
|
// use ffi::{LeapCreateConnection, LeapOpenConnection, LeapPollConnection};
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
// pub mod ffi;
|
||||||
|
|
||||||
// Cheaty way to ensure only one LeapDevice is created
|
// Cheaty way to ensure only one LeapDevice is created
|
||||||
static mut LEAP_DEVICE_EXISTS: bool = false;
|
static mut LEAP_DEVICE_EXISTS: bool = false;
|
||||||
@ -29,10 +28,39 @@ pub struct LeapDevice {
|
|||||||
|
|
||||||
/// Storage for incoming messages
|
/// Storage for incoming messages
|
||||||
latest_message: *mut LEAP_CONNECTION_MESSAGE,
|
latest_message: *mut LEAP_CONNECTION_MESSAGE,
|
||||||
|
|
||||||
|
/// Weather the device is connected
|
||||||
|
connected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LeapDevice {
|
impl LeapDevice {
|
||||||
pub fn new() -> Result<Self, LeapError> {
|
|
||||||
|
/// Open a connection to the first found LeapMotion device and wait for a connection or timeout
|
||||||
|
pub fn new(timeout: Duration) -> Result<Self, LeapError> {
|
||||||
|
|
||||||
|
// Connect to the device
|
||||||
|
let mut device = Self::new_raw()?;
|
||||||
|
|
||||||
|
// Track the start time
|
||||||
|
let start = SystemTime::now();
|
||||||
|
loop {
|
||||||
|
|
||||||
|
// Handle timeout
|
||||||
|
if start.elapsed().unwrap() > timeout {
|
||||||
|
return Err(LeapError::Timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Poll for an event and let the handler do its work
|
||||||
|
let _ = device.fetch_event();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(device)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Open a connection to the first found LeapMotion device
|
||||||
|
pub fn new_raw() -> Result<Self, LeapError> {
|
||||||
// Handle being called too many times
|
// Handle being called too many times
|
||||||
unsafe {
|
unsafe {
|
||||||
if LEAP_DEVICE_EXISTS {
|
if LEAP_DEVICE_EXISTS {
|
||||||
@ -70,10 +98,14 @@ impl LeapDevice {
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
handle: *handle,
|
handle: *handle,
|
||||||
latest_message: std::ptr::null_mut(),
|
latest_message: std::ptr::null_mut(),
|
||||||
|
connected: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Fetch the latest event from the device
|
||||||
pub fn fetch_event(&mut self) -> Result<Event, LeapError> {
|
pub fn fetch_event(&mut self) -> Result<Event, LeapError> {
|
||||||
// Poll for a new message
|
// Poll for a new message
|
||||||
let result;
|
let result;
|
||||||
@ -84,9 +116,20 @@ impl LeapDevice {
|
|||||||
return Err(result.into());
|
return Err(result.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe{
|
// Deref and convert into an event
|
||||||
Ok((*self.latest_message).into())
|
let event: Event;
|
||||||
|
unsafe {
|
||||||
|
event = (*self.latest_message).into();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle checking connection status
|
||||||
|
match event {
|
||||||
|
Event::Connection(_) => self.connected = true,
|
||||||
|
Event::ConnectionLost(_) => self.connected = false,
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,5 +5,7 @@ authors = ["Evan Pratten <ewpratten@gmail.com>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# 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]
|
||||||
|
clap = "2.33.3"
|
||||||
|
libleap = { verison = "0.1.0", path = "../libleap" }
|
||||||
|
libodm = { verison = "0.1.0", path = "../libodm" }
|
||||||
|
@ -1,3 +1,15 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use clap::{value_t, App, Arg};
|
||||||
|
use libleap::LeapDevice;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let matches = App::new("snapscan")
|
||||||
|
.author("Evan Pratten <ewpratten@gmail.com>")
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
// Open a connection to the device
|
||||||
|
let device = LeapDevice::new(Duration::from_secs(1)).unwrap();
|
||||||
|
|
||||||
|
println!("Connected to LeapMotion device");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user