Skip to content

Commit

Permalink
Merge branch 'Sequal32#16-fix-update-example' of github.com-bitsanddr…
Browse files Browse the repository at this point in the history
…oids:BitsAndDroids/simconnect-rust into Sequal32#16-fix-update-example
  • Loading branch information
BitsAndDroids committed Mar 25, 2024
2 parents 94af7e9 + 56a743b commit 3b82c14
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--
# Before submitting a PR
1. Give the PR a title that describes the changes
2. Make sure you ran cargo fmt
3. Make sure you ran cargo clippy
-->


16 changes: 9 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
![crates.io](https://img.shields.io/crates/v/simconnect)
# SimConnect Bindings for Rust

## Requirements
- [CLang](https://clang.llvm.org/get_started.html) (See the [Rust Bindgen Documentation](https://rust-lang.github.io/rust-bindgen/requirements.html))
- MSVC x64 Rust build (`x86_64-pc-windows-msvc`, see [The rustup book](https://rust-lang.github.io/rustup/installation/windows.html))

## Using
Add this to your `Cargo.toml`
```toml
[dependencies]
simconnect = "0.2.1"
simconnect = "0.3.1"
```

## Building
*The SimConnect binaries are included within this repository, but they may not be up-to-date.*

1. Install [CLang](https://clang.llvm.org/get_started.html). More information available at the [Rust Bindgen Documentation](https://rust-lang.github.io/rust-bindgen/requirements.html).
2. run `cargo build`
3. Add `use simconnect` at the top of your file
1. run `cargo build`
2. Add `use simconnect` at the top of your file

## Example
Read float position data
Expand All @@ -27,8 +32,5 @@ cargo run --example aircraft_updates_on_change

*You must have SimConnect.dll in the same directory as the compiled exe for it to run (e.g. in )*

## Building
*The SimConnect.dll is included in this repository, but might not be up-to-date*

### Remarks
I have not tested every single function from the api. If you find an error, feel free to make an issue or a pull request.
73 changes: 73 additions & 0 deletions examples/aircraft_inputs/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use simconnect::SIMCONNECT_CLIENT_EVENT_ID;
use std::collections::HashMap;

// define a struct that holds the event and the input id
// the events can be found in the SimConnect SDK documentation for your sim
struct Input {
event: String, // The event to be triggered i.e.
input_id: u32, // The id we use to trigger the event
}
fn main() {
let input_parking_brakes = Input {
event: "PARKING_BRAKES".to_string(),
input_id: 1,
};
let input_gear_up = Input {
event: "GEAR_UP".to_string(),
input_id: 2,
};
let input_gear_down = Input {
event: "GEAR_DOWN".to_string(),
input_id: 3,
};

// Define a hashmap to easily cross reference the input id with the event
let mut events: HashMap<u32, Input> = HashMap::new();
events.insert(input_parking_brakes.input_id, input_parking_brakes);
events.insert(input_gear_up.input_id, input_gear_up);
events.insert(input_gear_down.input_id, input_gear_down);

let mut conn = simconnect::SimConnector::new();
conn.connect("Program that inputs commands to the sim"); // Initialize connection with SimConnect

// loop over all the events we want to define and map them to the input id
for event in &events {
println!("Defining event: {}", event.1.event);
println!("Input id: {}", event.1.input_id);
conn.map_client_event_to_sim_event(
// if input id 1 is triggered, the PARKING_BRAKES event is triggered
event.1.input_id as SIMCONNECT_CLIENT_EVENT_ID,
event.1.event.as_str(),
);
}
// loop over user input from console and trigger the corresponding events
loop {
println!("Enter an input id to trigger an event");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let input_id: u32 = input.trim().parse().unwrap();
match events.get(&input_id) {
// if the input id is found in the hashmap, trigger the event
// otherwise print an error message
Some(event) => {
// this is why we've defined a hashmap instead of a vector
println!("Triggering event: {}", event.event);
/* send message to the sim
object_id is 0 because we want to trigger the event on the user aircraft
group_id is 0 because we don't want to group the event with other events
priority is 0 because we don't want to prioritize the event
this is used when multiple events are triggered at the same time
*/
conn.transmit_client_event(
0,
input_id as u32,
0,
simconnect::SIMCONNECT_GROUP_PRIORITY_HIGHEST,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
}
None => println!("No event found for input id: {}", input_id),
}
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub enum DispatchResult<'a> {

/// Handles communication between the client program and SimConnect
/// For more information about the functions provided, refer to the SimConnect SDK Documentation. The functions name closely match up with those defined there.
#[derive(Debug)]
pub struct SimConnector {
sim_connect_handle: HANDLE,
}
Expand Down

0 comments on commit 3b82c14

Please sign in to comment.