Browse Source

Add a srv module and move wait_for_event to it

pull/129/head
Mark Drobnak 2 years ago
parent
commit
6229905df2
No known key found for this signature in database
GPG Key ID: 47A133F3BF9D03D3
  1. 9
      ctru-rs/examples/ir-user.rs
  2. 13
      ctru-rs/src/services/ir_user.rs
  3. 1
      ctru-rs/src/services/mod.rs
  4. 20
      ctru-rs/src/services/srv.rs

9
ctru-rs/examples/ir-user.rs

@ -2,6 +2,7 @@
use ctru::prelude::*; use ctru::prelude::*;
use ctru::services::ir_user::{CirclePadProInputResponse, IrDeviceId, IrUser}; use ctru::services::ir_user::{CirclePadProInputResponse, IrDeviceId, IrUser};
use ctru::services::srv;
use ctru_sys::Handle; use ctru_sys::Handle;
use std::io::Write; use std::io::Write;
use std::time::Duration; use std::time::Duration;
@ -37,7 +38,7 @@ fn main() {
// Check if we've received a packet from the circle pad pro // Check if we've received a packet from the circle pad pro
let packet_received = let packet_received =
IrUser::wait_for_event(demo.receive_packet_event, Duration::ZERO).is_ok(); srv::wait_for_event(demo.receive_packet_event, Duration::ZERO).is_ok();
if packet_received { if packet_received {
demo.handle_packets(); demo.handle_packets();
} }
@ -125,7 +126,7 @@ impl<'screen> CirclePadProDemo<'screen> {
// Wait for the connection to establish // Wait for the connection to establish
if let Err(e) = if let Err(e) =
IrUser::wait_for_event(self.connection_status_event, Duration::from_millis(100)) srv::wait_for_event(self.connection_status_event, Duration::from_millis(100))
{ {
if !e.is_timeout() { if !e.is_timeout() {
panic!("Couldn't initialize circle pad pro connection: {e}"); panic!("Couldn't initialize circle pad pro connection: {e}");
@ -145,7 +146,7 @@ impl<'screen> CirclePadProDemo<'screen> {
// Wait for the disconnect to go through // Wait for the disconnect to go through
if let Err(e) = if let Err(e) =
IrUser::wait_for_event(self.connection_status_event, Duration::from_millis(100)) srv::wait_for_event(self.connection_status_event, Duration::from_millis(100))
{ {
if !e.is_timeout() { if !e.is_timeout() {
panic!("Couldn't initialize circle pad pro connection: {e}"); panic!("Couldn't initialize circle pad pro connection: {e}");
@ -171,7 +172,7 @@ impl<'screen> CirclePadProDemo<'screen> {
// Wait for the response // Wait for the response
let recv_event_result = let recv_event_result =
IrUser::wait_for_event(self.receive_packet_event, Duration::from_millis(100)); srv::wait_for_event(self.receive_packet_event, Duration::from_millis(100));
self.print_status_info(); self.print_status_info();
if recv_event_result.is_ok() { if recv_event_result.is_ok() {

13
ctru-rs/src/services/ir_user.rs

@ -6,7 +6,6 @@ use std::cmp::max;
use std::ffi::CString; use std::ffi::CString;
use std::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut}; use std::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};
use std::sync::Mutex; use std::sync::Mutex;
use std::time::Duration;
static IR_USER_ACTIVE: Mutex<usize> = Mutex::new(0); static IR_USER_ACTIVE: Mutex<usize> = Mutex::new(0);
static IR_USER_STATE: Mutex<Option<IrUserState>> = Mutex::new(None); static IR_USER_STATE: Mutex<Option<IrUserState>> = Mutex::new(None);
@ -172,18 +171,6 @@ impl IrUser {
Ok(recv_event) Ok(recv_event)
} }
/// Wait for an event to fire. If the timeout is reached, an error is returned. You can use
/// [`Error::is_timeout`] to check if the error is due to a timeout.
pub fn wait_for_event(event: Handle, timeout: Duration) -> crate::Result<()> {
unsafe {
ResultCode(ctru_sys::svcWaitSynchronization(
event,
timeout.as_nanos() as i64,
))?;
}
Ok(())
}
/// Circle Pad Pro specific request. /// Circle Pad Pro specific request.
/// ///
/// This will send a packet to the CPP requesting it to send back packets /// This will send a packet to the CPP requesting it to send back packets

1
ctru-rs/src/services/mod.rs

@ -8,6 +8,7 @@ pub mod ir_user;
pub mod ps; pub mod ps;
mod reference; mod reference;
pub mod soc; pub mod soc;
pub mod srv;
pub mod sslc; pub mod sslc;
pub use self::apt::Apt; pub use self::apt::Apt;

20
ctru-rs/src/services/srv.rs

@ -0,0 +1,20 @@
//! Service API
//!
//! Not all APIs are wrapped in this module, since a lot are fundamentally unsafe.
//! Most APIs should be used directly from `ctru-sys`.
use crate::error::ResultCode;
use ctru_sys::Handle;
use std::time::Duration;
/// Wait for an event to fire. If the timeout is reached, an error is returned. You can use
/// [`Error::is_timeout`] to check if the error is due to a timeout.
pub fn wait_for_event(event: Handle, timeout: Duration) -> crate::Result<()> {
unsafe {
ResultCode(ctru_sys::svcWaitSynchronization(
event,
timeout.as_nanos() as i64,
))?;
}
Ok(())
}
Loading…
Cancel
Save