Add support for String

This commit is contained in:
Mihai Dinculescu
2022-10-20 15:42:51 +01:00
parent 60b73c1557
commit 4ac94cdb96
21 changed files with 574 additions and 404 deletions

View File

@@ -5,13 +5,19 @@ use simconnect_sdk::{Notification, SimConnect, SimConnectObject};
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second")]
#[allow(dead_code)]
struct GpsData {
struct AirplaneData {
#[simconnect(name = "TITLE")]
title: String,
#[simconnect(name = "CATEGORY")]
category: String,
#[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
lat: f64,
#[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
lon: f64,
#[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
alt: f64,
#[simconnect(name = "SIM ON GROUND")]
sim_on_ground: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -29,17 +35,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
client.register_object::<AirplaneData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
if let Ok(airplane_data) = AirplaneData::try_from(&data) {
println!("{airplane_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
client.unregister_object::<AirplaneData>()?;
println!("Subscription stopped.");
break;
}

View File

@@ -19,8 +19,20 @@ struct GpsData {
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
#[allow(dead_code)]
struct AirplaneData {
#[simconnect(name = "TITLE")]
title: String,
#[simconnect(name = "CATEGORY")]
category: String,
}
/// A third data structure that will be used to receive data from SimConnect.
/// See the documentation of `SimConnectObject` for more information on the arguments of the `simconnect` attribute.
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
#[allow(dead_code)]
pub struct OnGround {
#[simconnect(name = "SIM ON GROUND", unit = "bool")]
#[simconnect(name = "SIM ON GROUND")]
sim_on_ground: bool,
}
@@ -37,6 +49,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// After the connection is successfully open, we register the structs
client.register_object::<GpsData>()?;
client.register_object::<AirplaneData>()?;
client.register_object::<OnGround>()?;
}
Some(Notification::Object(data)) => {
@@ -45,6 +58,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// We've already got our data, there's no point in trying another in this iteration
continue;
}
if let Ok(airplane_data) = AirplaneData::try_from(&data) {
println!("{airplane_data:?}");
// We've already got our data, there's no point in trying another in this iteration
continue;
}
if let Ok(on_ground) = OnGround::try_from(&data) {
println!("{on_ground:?}");
// We've already got our data, there's no point in trying another in this iteration

View File

@@ -7,13 +7,19 @@ use tracing_subscriber::{fmt, prelude::*, EnvFilter};
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second")]
#[allow(dead_code)]
struct GpsData {
struct AirplaneData {
#[simconnect(name = "TITLE")]
title: String,
#[simconnect(name = "CATEGORY")]
category: String,
#[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
lat: f64,
#[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
lon: f64,
#[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
alt: f64,
#[simconnect(name = "SIM ON GROUND")]
sim_on_ground: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -33,17 +39,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("Open");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
client.register_object::<AirplaneData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
info!("{gps_data:?}");
if let Ok(airplane_data) = AirplaneData::try_from(&data) {
info!("{airplane_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
client.unregister_object::<AirplaneData>()?;
println!("Subscription stopped.");
break;
}

View File

@@ -1,22 +1,41 @@
/// This example shows all the work that the [`simconnect_sdk::SimConnectObject`] macro is doing behind the scenes.
/// You're probably better off using the macro in a real-life use-case.
use simconnect_sdk::{
Condition, DataType, Notification, Object, Period, SimConnect, SimConnectError,
SimConnectObjectExt,
fixed_c_str_to_string, Condition, DataType, Notification, Object, Period, SimConnect,
SimConnectError, SimConnectObjectExt,
};
/// A data structure that will be used to receive data from SimConnect.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct GpsData {
pub struct AirplaneData {
title: String,
category: String,
lat: f64,
lon: f64,
alt: f64,
sim_on_ground: bool,
}
impl SimConnectObjectExt for GpsData {
/// An intermediate data structure that will map 1:1 to the object received from SimConnect.
#[repr(C, packed)]
struct AirplaneDataCPacked {
title: [i8; 256],
category: [i8; 256],
lat: f64,
lon: f64,
alt: f64,
sim_on_ground: bool,
}
impl SimConnectObjectExt for AirplaneData {
fn register(client: &mut SimConnect, id: u32) -> Result<(), SimConnectError> {
client.add_to_data_definition(id, "TITLE", "", DataType::String)?;
client.add_to_data_definition(id, "CATEGORY", "", DataType::String)?;
client.add_to_data_definition(id, "PLANE LATITUDE", "degrees", DataType::Float64)?;
client.add_to_data_definition(id, "PLANE LONGITUDE", "degrees", DataType::Float64)?;
client.add_to_data_definition(id, "PLANE ALTITUDE", "feet", DataType::Float64)?;
client.add_to_data_definition(id, "SIM ON GROUND", "", DataType::Float64)?;
client.request_data_on_sim_object(id, Period::Second, Condition::None, 0)?;
@@ -24,11 +43,20 @@ impl SimConnectObjectExt for GpsData {
}
}
impl TryFrom<&Object> for GpsData {
impl TryFrom<&Object> for AirplaneData {
type Error = SimConnectError;
fn try_from(value: &Object) -> Result<Self, Self::Error> {
value.try_transmute::<GpsData>()
let raw = value.try_transmute::<AirplaneData, AirplaneDataCPacked>()?;
Ok(AirplaneData {
title: fixed_c_str_to_string(&raw.title),
category: fixed_c_str_to_string(&raw.category),
lat: raw.lat,
lon: raw.lon,
alt: raw.alt,
sim_on_ground: raw.sim_on_ground,
})
}
}
@@ -47,17 +75,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connection opened.");
// After the connection is successfully open, we register the struct
client.register_object::<GpsData>()?;
client.register_object::<AirplaneData>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
if let Ok(gps_data) = AirplaneData::try_from(&data) {
println!("{gps_data:?}");
notifications_received += 1;
// After we have received 10 notifications, we unregister the struct
if notifications_received > 10 {
client.unregister_object::<GpsData>()?;
client.unregister_object::<AirplaneData>()?;
println!("Subscription stopped.");
break;
}