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

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

@ -7,14 +7,21 @@ use crate::error::ResultCode; @@ -7,14 +7,21 @@ 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,
))?;
/// Extension trait for [Handle]
pub trait HandleExt {
/// 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.
fn wait_for_event(self, timeout: Duration) -> crate::Result<()>;
}
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