Archived
1

Properly read from the device

This commit is contained in:
Evan Pratten 2021-05-29 10:38:51 -04:00
parent 2c1ef92dfb
commit eadf2d7fc2
10 changed files with 95 additions and 26 deletions

6
.gitignore vendored
View File

@ -9,4 +9,8 @@ Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
bazel-* # Bazel buildsystem
bazel-*
# Core files
core

View File

@ -8,21 +8,21 @@ public:
void LeapEventListener::onConnect(const Controller &controller) 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 //Not dispatched when running in a debugger
void LeapEventListener::onDisconnect(const Controller &controller) 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) void LeapEventListener::onFrame(const Controller &controller)
{ {
std::cout << "New frame available" << std::endl; // std::cout << "New frame available" << std::endl;
Frame frame = controller.frame(); // Frame frame = controller.frame();
// Get the camera images // // Get the camera images
images = frame.images(); // images = frame.images();
} }

View File

@ -17,7 +17,7 @@ extern "C" const unsigned char *getImageRight();
//---- Start Globals ----// //---- Start Globals ----//
Controller *controller = nullptr; Controller *controller = nullptr;
ImageList images; // ImageList images;
//---- End Globals ----// //---- End Globals ----//
#include "listener.cc" #include "listener.cc"
@ -35,25 +35,25 @@ void beginEventLoop()
controller->setPolicyFlags(Controller::POLICY_IMAGES); controller->setPolicyFlags(Controller::POLICY_IMAGES);
// Set up event handling // Set up event handling
LeapEventListener listener; // LeapEventListener listener;
controller->addListener(listener); // controller->addListener(listener);
} }
} }
void endEventLoop() void endEventLoop()
{ {
// if (controller != nullptr) if (controller != nullptr)
// { {
// delete controller; delete controller;
// } }
} }
bool isControllerCreated() { return controller != nullptr; } bool isControllerCreated() { return controller != nullptr; }
bool imageExists() { return images.count() == 2; } bool imageExists() { return controller->frame().images().count() == 2; }
int getImageHeight() { return images[0].height(); } int getImageHeight() { return controller->frame().images()[0].height(); }
int getImageWidth() { return images[0].width(); } int getImageWidth() { return controller->frame().images()[0].width(); }
int getImageBPP() { return images[0].bytesPerPixel(); } int getImageBPP() { return controller->frame().images()[0].bytesPerPixel(); }
const unsigned char *getImageLeft() { return images[0].data(); } const unsigned char *getImageLeft() { return controller->frame().images()[0].data(); }
const unsigned char *getImageRight() { return images[1].data(); } const unsigned char *getImageRight() { return controller->frame().images()[1].data(); }
//---- End Public Function Impls ----// //---- End Public Function Impls ----//

View File

@ -1,3 +1,5 @@
use std::time::{Duration, SystemTime};
use crate::leapmotion::ffi::endEventLoop; use crate::leapmotion::ffi::endEventLoop;
use super::ffi; use super::ffi;
@ -5,14 +7,14 @@ use super::ffi;
#[derive(Debug)] #[derive(Debug)]
pub enum DeviceError { pub enum DeviceError {
InstanceError, InstanceError,
Timeout,
} }
pub struct LeapMotionDevice {} pub struct LeapMotionDevice {}
impl LeapMotionDevice { impl LeapMotionDevice {
/// Creates a LeapMotionDevice by talking to the device driver. This can only be called once /// Creates a LeapMotionDevice by talking to the device driver. This can only be called once
pub fn create_device() -> Result<Self, DeviceError> { pub fn create_device(timeout: Duration) -> Result<Self, DeviceError> {
log::debug!("Creating LeapMotion device access"); log::debug!("Creating LeapMotion device access");
// Handle possible error with multiple instances // Handle possible error with multiple instances
@ -30,6 +32,23 @@ impl LeapMotionDevice {
ffi::beginEventLoop(); 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"); log::debug!("Device created");
Ok(Self {}) Ok(Self {})
} }
@ -38,8 +57,8 @@ impl LeapMotionDevice {
impl Drop for LeapMotionDevice { impl Drop for LeapMotionDevice {
fn drop(&mut self) { fn drop(&mut self) {
log::debug!("Informing wrapper to deallocate LeapMotion device"); log::debug!("Informing wrapper to deallocate LeapMotion device");
unsafe{ unsafe {
ffi::endEventLoop(); ffi::endEventLoop();
} }
} }
} }

10
odm.sh Executable file
View File

@ -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 $@

View File

@ -9,3 +9,6 @@ edition = "2018"
[dependencies] [dependencies]
libodm = { version = "0.1.0", path = "../libodm"} libodm = { version = "0.1.0", path = "../libodm"}
clap = "2.33.3" clap = "2.33.3"
fern = "0.6.0"
log = "0.4.14"
chrono = "0.4.19"

21
odm/src/cli/logging.rs Normal file
View File

@ -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();
}

1
odm/src/cli/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod logging;

View File

@ -1,11 +1,17 @@
use std::time::Duration; use std::time::Duration;
use cli::logging::start_fern;
use libodm::leapmotion::device::LeapMotionDevice; use libodm::leapmotion::device::LeapMotionDevice;
mod cli;
fn main() { fn main() {
start_fern();
// Init a device // 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));
} }

5
scripts/inspect-core.sh Executable file
View File

@ -0,0 +1,5 @@
#! /bin/bash
set -e
LD_LIBRARY_PATH="$(pwd)/libodm/lib:$LD_LIBRARY_PATH" gdb ./target/debug/odm