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

@@ -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;
}