diff --git a/.gitignore b/.gitignore index 4a8cdf8..6a6f61f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,8 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk -bazel-* \ No newline at end of file +# Bazel buildsystem +bazel-* + +# Core files +core \ No newline at end of file diff --git a/libodm/cpp/listener.cc b/libodm/cpp/listener.cc index f92de2d..8082e8a 100644 --- a/libodm/cpp/listener.cc +++ b/libodm/cpp/listener.cc @@ -8,21 +8,21 @@ public: void LeapEventListener::onConnect(const Controller &controller) { - std::cout << "LeapMotion Controller: Connected" << std::endl; + // 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; + // std::cout << "LeapMotion Controller: Disconnected" << std::endl; } void LeapEventListener::onFrame(const Controller &controller) { - std::cout << "New frame available" << std::endl; - Frame frame = controller.frame(); + // std::cout << "New frame available" << std::endl; + // Frame frame = controller.frame(); - // Get the camera images - images = frame.images(); + // // Get the camera images + // images = frame.images(); } \ No newline at end of file diff --git a/libodm/cpp/wrapper.cc b/libodm/cpp/wrapper.cc index 3951ebb..e6a50c1 100644 --- a/libodm/cpp/wrapper.cc +++ b/libodm/cpp/wrapper.cc @@ -17,7 +17,7 @@ extern "C" const unsigned char *getImageRight(); //---- Start Globals ----// Controller *controller = nullptr; -ImageList images; +// ImageList images; //---- End Globals ----// #include "listener.cc" @@ -35,25 +35,25 @@ void beginEventLoop() controller->setPolicyFlags(Controller::POLICY_IMAGES); // Set up event handling - LeapEventListener listener; - controller->addListener(listener); + // LeapEventListener listener; + // controller->addListener(listener); } } void endEventLoop() { - // if (controller != nullptr) - // { - // delete controller; - // } + if (controller != nullptr) + { + delete controller; + } } 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(); } +bool imageExists() { return controller->frame().images().count() == 2; } +int getImageHeight() { return controller->frame().images()[0].height(); } +int getImageWidth() { return controller->frame().images()[0].width(); } +int getImageBPP() { return controller->frame().images()[0].bytesPerPixel(); } +const unsigned char *getImageLeft() { return controller->frame().images()[0].data(); } +const unsigned char *getImageRight() { return controller->frame().images()[1].data(); } //---- End Public Function Impls ----// diff --git a/libodm/src/leapmotion/device.rs b/libodm/src/leapmotion/device.rs index 04c9484..ffbf900 100644 --- a/libodm/src/leapmotion/device.rs +++ b/libodm/src/leapmotion/device.rs @@ -1,3 +1,5 @@ +use std::time::{Duration, SystemTime}; + use crate::leapmotion::ffi::endEventLoop; use super::ffi; @@ -5,14 +7,14 @@ use super::ffi; #[derive(Debug)] pub enum DeviceError { InstanceError, + Timeout, } pub struct LeapMotionDevice {} impl LeapMotionDevice { - /// Creates a LeapMotionDevice by talking to the device driver. This can only be called once - pub fn create_device() -> Result { + pub fn create_device(timeout: Duration) -> Result { log::debug!("Creating LeapMotion device access"); // Handle possible error with multiple instances @@ -30,6 +32,23 @@ impl LeapMotionDevice { ffi::beginEventLoop(); } + // Pause a small amount of time for the device to finish init or for the timeout to be hit + let time_now = SystemTime::now(); + loop { + // Handle timeout + if time_now.elapsed().unwrap() > timeout { + log::error!("Hit device init timeout"); + return Err(DeviceError::Timeout); + } + + // Handle device connect + unsafe { + if ffi::isControllerCreated() && ffi::imageExists() { + break; + } + } + } + log::debug!("Device created"); Ok(Self {}) } @@ -38,8 +57,8 @@ impl LeapMotionDevice { impl Drop for LeapMotionDevice { fn drop(&mut self) { log::debug!("Informing wrapper to deallocate LeapMotion device"); - unsafe{ + unsafe { ffi::endEventLoop(); } } -} \ No newline at end of file +} diff --git a/odm.sh b/odm.sh new file mode 100755 index 0000000..2619bdb --- /dev/null +++ b/odm.sh @@ -0,0 +1,10 @@ +#! /bin/bash +# This is a small wrapper script to handle configuring your library path before executing ODM + +set -e + +echo "Enabling core dumps" +ulimit -c unlimited + +echo "Starting cargo with new flags" +LD_LIBRARY_PATH="$(pwd)/libodm/lib:$LD_LIBRARY_PATH" cargo run $@ \ No newline at end of file diff --git a/odm/Cargo.toml b/odm/Cargo.toml index c10af91..544ef1f 100644 --- a/odm/Cargo.toml +++ b/odm/Cargo.toml @@ -9,3 +9,6 @@ edition = "2018" [dependencies] libodm = { version = "0.1.0", path = "../libodm"} clap = "2.33.3" +fern = "0.6.0" +log = "0.4.14" +chrono = "0.4.19" \ No newline at end of file diff --git a/odm/src/cli/logging.rs b/odm/src/cli/logging.rs new file mode 100644 index 0000000..38f8712 --- /dev/null +++ b/odm/src/cli/logging.rs @@ -0,0 +1,21 @@ + +pub fn start_fern() { + fern::Dispatch::new() + // Perform allocation-free log formatting + .format(|out, message, record| { + out.finish(format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + record.level(), + message + )) + }) + // Add blanket level filter - + .level(log::LevelFilter::Debug) + + // Output to stdout, files, and other Dispatch configurations + .chain(std::io::stdout()) + // Apply globally + .apply().unwrap(); +} \ No newline at end of file diff --git a/odm/src/cli/mod.rs b/odm/src/cli/mod.rs new file mode 100644 index 0000000..cdb11a3 --- /dev/null +++ b/odm/src/cli/mod.rs @@ -0,0 +1 @@ +pub mod logging; \ No newline at end of file diff --git a/odm/src/main.rs b/odm/src/main.rs index 9d30ac2..f9960f4 100644 --- a/odm/src/main.rs +++ b/odm/src/main.rs @@ -1,11 +1,17 @@ use std::time::Duration; +use cli::logging::start_fern; use libodm::leapmotion::device::LeapMotionDevice; +mod cli; + fn main() { + + start_fern(); // Init a device - let device = LeapMotionDevice::create_device().unwrap(); + let device = LeapMotionDevice::create_device(Duration::from_secs(1)).unwrap(); + + log::info!("Got data"); - std::thread::sleep(Duration::from_secs(1)); } diff --git a/scripts/inspect-core.sh b/scripts/inspect-core.sh new file mode 100755 index 0000000..de64444 --- /dev/null +++ b/scripts/inspect-core.sh @@ -0,0 +1,5 @@ +#! /bin/bash + +set -e + +LD_LIBRARY_PATH="$(pwd)/libodm/lib:$LD_LIBRARY_PATH" gdb ./target/debug/odm \ No newline at end of file