Browse Source

New error variant and fixed nits

pull/94/head
Andrea Ciliberti 2 years ago
parent
commit
27e4f7fb31
  1. 5
      ctru-rs/examples/camera-image.rs
  2. 8
      ctru-rs/src/error.rs
  3. 14
      ctru-rs/src/services/cam.rs

5
ctru-rs/examples/camera-image.rs

@ -119,9 +119,10 @@ fn rotate_image_to_screen(src: &[u8], framebuf: *mut u8, width: usize, height: u
let draw_index = (draw_x * height + draw_y) * 2; // This 2 stands for the number of bytes per pixel (16 bits) let draw_index = (draw_x * height + draw_y) * 2; // This 2 stands for the number of bytes per pixel (16 bits)
unsafe { unsafe {
// We'll work with pointers since the frambuffer is a raw pointer regardless.
// The offsets are completely safe as long as the width and height are correct.
let pixel_pointer = framebuf.offset(draw_index as isize); let pixel_pointer = framebuf.offset(draw_index as isize);
*pixel_pointer = src[read_index]; pixel_pointer.copy_from(src.as_ptr().offset(read_index as isize), 2);
*pixel_pointer.offset(1) = src[read_index + 1];
} }
} }
} }

8
ctru-rs/src/error.rs

@ -55,6 +55,8 @@ pub enum Error {
Libc(String), Libc(String),
ServiceAlreadyActive, ServiceAlreadyActive,
OutputAlreadyRedirected, OutputAlreadyRedirected,
/// The first member is the length of the buffer provided by the user, the second parameter is the size of the requested data (in bytes).
BufferTooShort(usize, usize),
} }
impl Error { impl Error {
@ -101,6 +103,7 @@ impl fmt::Debug for Error {
Self::Libc(err) => f.debug_tuple("Libc").field(err).finish(), Self::Libc(err) => f.debug_tuple("Libc").field(err).finish(),
Self::ServiceAlreadyActive => f.debug_tuple("ServiceAlreadyActive").finish(), Self::ServiceAlreadyActive => f.debug_tuple("ServiceAlreadyActive").finish(),
Self::OutputAlreadyRedirected => f.debug_tuple("OutputAlreadyRedirected").finish(), Self::OutputAlreadyRedirected => f.debug_tuple("OutputAlreadyRedirected").finish(),
Self::BufferTooShort(provided, wanted) => f.debug_tuple("BufferTooShort").field(provided).field(wanted).finish(),
} }
} }
} }
@ -110,7 +113,7 @@ impl fmt::Display for Error {
match self { match self {
&Self::Os(err) => write!( &Self::Os(err) => write!(
f, f,
"libctru result code 0x{err:08X}: [{} {}] {}: {}", "Libctru result code 0x{err:08X}: [{} {}] {}: {}",
result_code_level_str(err), result_code_level_str(err),
result_code_module_str(err), result_code_module_str(err),
result_code_summary_str(err), result_code_summary_str(err),
@ -119,8 +122,9 @@ impl fmt::Display for Error {
Self::Libc(err) => write!(f, "{err}"), Self::Libc(err) => write!(f, "{err}"),
Self::ServiceAlreadyActive => write!(f, "Service already active"), Self::ServiceAlreadyActive => write!(f, "Service already active"),
Self::OutputAlreadyRedirected => { Self::OutputAlreadyRedirected => {
write!(f, "output streams are already redirected to 3dslink") write!(f, "Output streams are already redirected to 3dslink")
} }
Self::BufferTooShort(provided, wanted) => write!(f, "The provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})")
} }
} }
} }

14
ctru-rs/src/services/cam.rs

@ -3,7 +3,7 @@
//! The CAM service provides access to the cameras. Cameras can return 2D images //! The CAM service provides access to the cameras. Cameras can return 2D images
//! in the form of byte vectors which can be used for display or other usages. //! in the form of byte vectors which can be used for display or other usages.
use crate::error::ResultCode; use crate::error::{Error, ResultCode};
use crate::services::gspgpu::FramebufferFormat; use crate::services::gspgpu::FramebufferFormat;
use bitflags::bitflags; use bitflags::bitflags;
use ctru_sys::Handle; use ctru_sys::Handle;
@ -710,10 +710,6 @@ pub trait Camera {
/// ///
/// This will error if the camera is busy or if the timeout duration is reached. /// This will error if the camera is busy or if the timeout duration is reached.
/// ///
/// # Panics
///
/// This function will panic if `buffer.len() < (width * height * 2)`.
///
/// # Arguments /// # Arguments
/// ///
/// * `width` - Width of the desired image /// * `width` - Width of the desired image
@ -745,9 +741,9 @@ pub trait Camera {
))?; ))?;
}; };
let screen_size = u32::from(width) * u32::from(height) * 2; let screen_size: usize = usize::from(width) * usize::from(height) * 2;
if buffer.len() < screen_size as usize { if buffer.len() < screen_size as usize {
panic!("Provided buffer's length is shorter than the desired width and height matrix.") return Err(Error::BufferTooShort(buffer.len(), screen_size))
} }
unsafe { unsafe {
@ -760,9 +756,9 @@ pub trait Camera {
let mut completion_handle: Handle = 0; let mut completion_handle: Handle = 0;
ResultCode(ctru_sys::CAMU_SetReceiving( ResultCode(ctru_sys::CAMU_SetReceiving(
&mut completion_handle, &mut completion_handle,
buffer.as_mut_ptr() as *mut ::libc::c_void, buffer.as_mut_ptr().cast(),
self.port_as_raw(), self.port_as_raw(),
screen_size, screen_size as u32,
transfer_unit.try_into().unwrap(), transfer_unit.try_into().unwrap(),
))?; ))?;
Ok::<Handle, i32>(completion_handle) Ok::<Handle, i32>(completion_handle)

Loading…
Cancel
Save