Browse Source

Make wait_for_event a method on Handle

pull/129/head
Mark Drobnak 2 years ago
parent
commit
eb8628d9bc
No known key found for this signature in database
GPG Key ID: 47A133F3BF9D03D3
  1. 23
      ctru-rs/examples/ir-user.rs
  2. 25
      ctru-rs/src/services/srv.rs

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

@ -2,7 +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::services::srv::HandleExt;
use ctru_sys::Handle; use ctru_sys::Handle;
use std::io::Write; use std::io::Write;
use std::time::Duration; use std::time::Duration;
@ -40,8 +40,10 @@ 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 = demo
srv::wait_for_event(demo.receive_packet_event, Duration::ZERO).is_ok(); .receive_packet_event
.wait_for_event(Duration::ZERO)
.is_ok();
if packet_received { if packet_received {
demo.handle_packets(); demo.handle_packets();
} }
@ -128,8 +130,9 @@ impl<'screen> CirclePadProDemo<'screen> {
.expect("Couldn't initialize circle pad pro connection"); .expect("Couldn't initialize circle pad pro connection");
// Wait for the connection to establish // Wait for the connection to establish
if let Err(e) = if let Err(e) = self
srv::wait_for_event(self.connection_status_event, Duration::from_millis(100)) .connection_status_event
.wait_for_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}");
@ -148,8 +151,9 @@ impl<'screen> CirclePadProDemo<'screen> {
.expect("Failed to disconnect circle pad pro connection"); .expect("Failed to disconnect circle pad pro connection");
// Wait for the disconnect to go through // Wait for the disconnect to go through
if let Err(e) = if let Err(e) = self
srv::wait_for_event(self.connection_status_event, Duration::from_millis(100)) .connection_status_event
.wait_for_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}");
@ -174,8 +178,9 @@ impl<'screen> CirclePadProDemo<'screen> {
self.print_status_info(); self.print_status_info();
// Wait for the response // Wait for the response
let recv_event_result = let recv_event_result = self
srv::wait_for_event(self.receive_packet_event, Duration::from_millis(100)); .receive_packet_event
.wait_for_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() {

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

@ -7,14 +7,21 @@ use crate::error::ResultCode;
use ctru_sys::Handle; use ctru_sys::Handle;
use std::time::Duration; use std::time::Duration;
/// Wait for an event to fire. If the timeout is reached, an error is returned. You can use /// Extension trait for [Handle]
/// [`Error::is_timeout`] to check if the error is due to a timeout. pub trait HandleExt {
pub fn wait_for_event(event: Handle, timeout: Duration) -> crate::Result<()> { /// Wait for an event to fire. If the timeout is reached, an error is returned. You can use
unsafe { /// [`Error::is_timeout`] to check if the error is due to a timeout.
ResultCode(ctru_sys::svcWaitSynchronization( fn wait_for_event(self, timeout: Duration) -> crate::Result<()>;
event, }
timeout.as_nanos() as i64,
))?; impl HandleExt for Handle {
fn wait_for_event(self, timeout: Duration) -> crate::Result<()> {
unsafe {
ResultCode(ctru_sys::svcWaitSynchronization(
self,
timeout.as_nanos() as i64,
))?;
}
Ok(())
} }
Ok(())
} }

Loading…
Cancel
Save