diff --git a/libodm/src/image.rs b/libodm/src/image.rs new file mode 100644 index 0000000..15ae7ab --- /dev/null +++ b/libodm/src/image.rs @@ -0,0 +1,10 @@ +#[derive(Debug)] +pub struct Image {} + +impl Image { + pub fn new(width: i32, height: i32, raw_data: &[u8]) -> Self { + Self { + + } + } +} diff --git a/libodm/src/leapmotion/device.rs b/libodm/src/leapmotion/device.rs index ffbf900..029b231 100644 --- a/libodm/src/leapmotion/device.rs +++ b/libodm/src/leapmotion/device.rs @@ -1,16 +1,42 @@ +//! Defines an interface for LeapMotion cameras + use std::time::{Duration, SystemTime}; -use crate::leapmotion::ffi::endEventLoop; +use crate::image::Image; use super::ffi; +/// Defines some errors that can come from the device #[derive(Debug)] pub enum DeviceError { + /// An error caused by too many instances being created InstanceError, + + /// An error caused by a timeout being reached Timeout, + + /// An error caused when no data is being sent by the device + NoData, } -pub struct LeapMotionDevice {} +/// Represents a single data frame generated by the LeapMotion device +#[derive(Debug)] +pub struct DeviceFrame { + /// Number of bytes per pixel + pub bytes_per_pixel: u8, + + /// Image from the left camera + pub left_camera: Image, + + /// Image from the right camera + pub right_camera: Image, +} + +/// Represents a LeapMotion device, and provides safe wrappers around its FFI +#[derive(Debug)] +pub struct LeapMotionDevice { + cameras_flipped: bool, +} impl LeapMotionDevice { /// Creates a LeapMotionDevice by talking to the device driver. This can only be called once @@ -50,7 +76,63 @@ impl LeapMotionDevice { } log::debug!("Device created"); - Ok(Self {}) + Ok(Self { + cameras_flipped: false, + }) + } + + /// Configure the camera to flip its cameras. In normal configuration, the green light is facing the user, while the device is laying flat + pub fn set_cameras_flipped(&mut self, flipped: bool) { + self.cameras_flipped = flipped; + } + + /// Get if the cameras are flipped + pub fn cameras_flipped(&self) -> bool { + self.cameras_flipped + } + + /// Get if the device has a frame to be read + pub fn has_frame(&mut self) -> bool { + unsafe { + return ffi::isControllerCreated() && ffi::imageExists(); + } + } + + /// Get the latest frame from the device + pub fn get_frame(&mut self) -> Result { + if !self.has_frame() { + return Err(DeviceError::NoData); + } + + // Read raw data from the device + let image_width; + let image_height; + let bytes_per_pixel; + let left_image_raw; + let right_image_raw; + unsafe { + image_width = ffi::getImageWidth(); + image_height = ffi::getImageHeight(); + bytes_per_pixel = ffi::getImageBPP(); + left_image_raw = std::slice::from_raw_parts( + ffi::getImageLeft(), + (image_width * image_height * bytes_per_pixel) as usize, + ); + right_image_raw = std::slice::from_raw_parts( + ffi::getImageRight(), + (image_width * image_height * bytes_per_pixel) as usize, + ); + } + + // Build two images + let left_image = Image::new(image_width, image_height, left_image_raw); + let right_image = Image::new(image_width, image_height, right_image_raw); + + Ok(DeviceFrame { + bytes_per_pixel: bytes_per_pixel as u8, + left_camera: left_image, + right_camera: right_image, + }) } } diff --git a/libodm/src/lib.rs b/libodm/src/lib.rs index 5a99a68..0fe478b 100644 --- a/libodm/src/lib.rs +++ b/libodm/src/lib.rs @@ -1,3 +1,4 @@ #![feature(static_nobundle)] -pub mod leapmotion; \ No newline at end of file +pub mod leapmotion; +pub mod image; \ No newline at end of file