simconnect_sdk/
lib.rs

1//! # SimConnect SDK
2//! SimConnect SDK in Rust.
3//!
4//! ## Usage
5//! ```toml
6//! [dependencies]
7//! simconnect-sdk = { version = "0.2", features = ["derive"] }
8//! ```
9//!
10//! ```rust,no_run
11//! use simconnect_sdk::{Notification, SimConnect, SimConnectObject};
12//!
13//! /// A data structure that will be used to receive data from SimConnect.
14//! /// See the documentation of `SimConnectObject` for more information on the arguments of the `simconnect` attribute.
15//! #[derive(Debug, Clone, SimConnectObject)]
16//! #[simconnect(period = "second")]
17//! #[allow(dead_code)]
18//! struct AirplaneData {
19//!     #[simconnect(name = "TITLE")]
20//!     title: String,
21//!     #[simconnect(name = "CATEGORY")]
22//!     category: String,
23//!     #[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
24//!     lat: f64,
25//!     #[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
26//!     lon: f64,
27//!     #[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
28//!     alt: f64,
29//!     #[simconnect(name = "SIM ON GROUND")]
30//!     sim_on_ground: bool,
31//! }
32//!
33//! fn main() -> Result<(), Box<dyn std::error::Error>> {
34//!     let client = SimConnect::new("Receiving data example");
35//!
36//!     match client {
37//!         Ok(mut client) => {
38//!             let mut notifications_received = 0;
39//!
40//!             loop {
41//!                 let notification = client.get_next_dispatch()?;
42//!
43//!                 match notification {
44//!                     Some(Notification::Open) => {
45//!                         println!("Connection opened.");
46//!
47//!                         // After the connection is successfully open, we register the struct
48//!                         client.register_object::<AirplaneData>()?;
49//!                     }
50//!                     Some(Notification::Object(data)) => {
51//!                         if let Ok(airplane_data) = AirplaneData::try_from(&data) {
52//!                             println!("{airplane_data:?}");
53//!
54//!                             notifications_received += 1;
55//!
56//!                             // After we have received 10 notifications, we unregister the struct
57//!                             if notifications_received > 10 {
58//!                                 client.unregister_object::<AirplaneData>()?;
59//!                                 println!("Subscription stopped.");
60//!                                 break;
61//!                             }
62//!                         }
63//!                     }
64//!                     _ => (),
65//!                 }
66//!
67//!                 // sleep for about a frame to reduce CPU usage
68//!                 std::thread::sleep(std::time::Duration::from_millis(16));
69//!             }
70//!         }
71//!         Err(e) => {
72//!             println!("Error: {e:?}")
73//!         }
74//!     }
75//!
76//!     Ok(())
77//! }
78//! ```
79//!
80//! See [more examples](https://github.com/mihai-dinculescu/simconnect-sdk-rs/tree/main/examples).
81
82mod bindings;
83mod domain;
84mod errors;
85mod helpers;
86mod macros;
87mod simconnect;
88mod simconnect_object_ext;
89
90pub(crate) use macros::{as_c_string, ok_if_fail, success};
91
92pub use domain::*;
93pub use errors::SimConnectError;
94pub use helpers::fixed_c_str_to_string;
95pub use simconnect::SimConnect;
96pub use simconnect_object_ext::SimConnectObjectExt;
97
98#[cfg(feature = "simconnect-sdk-derive")]
99extern crate simconnect_sdk_derive;
100#[cfg(feature = "simconnect-sdk-derive")]
101pub use simconnect_sdk_derive::*;