Archived
1

binding work

This commit is contained in:
Evan Pratten 2021-05-28 16:23:39 -04:00
parent ea244ed3ee
commit c119a2478e
15 changed files with 8508 additions and 17 deletions

View File

@ -1,6 +1,7 @@
[workspace]
members = [
"libleap",
"libodm",
"snapscan",
"libleap-sys",
# "libleap",
# "libodm",
# "snapscan",
]

12
libleap-sys/Cargo.toml Normal file
View 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"

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

62
libleap-sys/build.rs Normal file
View 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
View 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
View File

@ -0,0 +1 @@
#include "LeapSDK/include/Leap.h"

View File

@ -10,4 +10,4 @@ edition = "2018"
leap-sys = "0.1.1"
num = "0.4"
num-derive = "0.3"
num-traits = "0.2"
num-traits = "0.2"

View File

@ -1,6 +1,6 @@
use leap_sys::eLeapRS;
#[derive(FromPrimitive)]
#[derive(FromPrimitive, Debug)]
pub enum LeapError {
/// The operation completed successfully.
Success = 0,

View File

@ -1,23 +1,22 @@
#[macro_use]
extern crate num_derive;
use std::{
sync::{
use std::{sync::{
mpsc::{self, Sender},
Arc,
},
thread::Thread,
};
}, thread::Thread, time::{Duration, SystemTime}};
use error::LeapError;
use event::Event;
use leap_sys::{
LeapCreateConnection, LeapOpenConnection, LeapPollConnection, _eLeapRS_eLeapRS_Success,
LEAP_CONNECTION, LEAP_CONNECTION_MESSAGE,
_eLeapRS_eLeapRS_Success,
LEAP_CONNECTION, LEAP_CONNECTION_MESSAGE,LeapCreateConnection, LeapOpenConnection, LeapPollConnection
};
// use ffi::{LeapCreateConnection, LeapOpenConnection, LeapPollConnection};
pub mod error;
pub mod event;
// pub mod ffi;
// Cheaty way to ensure only one LeapDevice is created
static mut LEAP_DEVICE_EXISTS: bool = false;
@ -29,10 +28,39 @@ pub struct LeapDevice {
/// Storage for incoming messages
latest_message: *mut LEAP_CONNECTION_MESSAGE,
/// Weather the device is connected
connected: bool,
}
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
unsafe {
if LEAP_DEVICE_EXISTS {
@ -70,10 +98,14 @@ impl LeapDevice {
Ok(Self {
handle: *handle,
latest_message: std::ptr::null_mut(),
connected: false
})
}
}
/// Fetch the latest event from the device
pub fn fetch_event(&mut self) -> Result<Event, LeapError> {
// Poll for a new message
let result;
@ -84,9 +116,20 @@ impl LeapDevice {
return Err(result.into());
}
unsafe{
Ok((*self.latest_message).into())
// Deref and convert into an event
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)
}
}

View File

@ -5,5 +5,7 @@ authors = ["Evan Pratten <ewpratten@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33.3"
libleap = { verison = "0.1.0", path = "../libleap" }
libodm = { verison = "0.1.0", path = "../libodm" }

View File

@ -1,3 +1,15 @@
use std::time::Duration;
use clap::{value_t, App, Arg};
use libleap::LeapDevice;
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");
}