Skip to content

Commit

Permalink
Expose data to frontend (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacey-sooty committed Sep 7, 2024
1 parent 498f536 commit 85be754
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tauri-build = { version = "1", features = [] }
[dependencies]
tauri = { version = "1", features = ["shell-open"] }
serde = { version = "1", features = ["derive"] }
ds = { git = "https://github.com/spacey-sooty/ds-rs.git", branch = "updatedeps" }
ds = { git = "https://github.com/spacey-sooty/ds-rs.git", branch = "serialize-joystickval" }
gilrs = { version = "0.10.9", features = ["serde", "serde-serialize"] }
tokio = "1.40.0"
once_cell = "1.19.0"
Expand Down
40 changes: 39 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,41 @@ fn send_packet_no_last(
ds.set_mode(packet.mode.0);
}

#[tauri::command]
async fn last_packet(last_packet: State<'_, Lazy<RwLock<Packet>>>) -> Result<Packet, ()> {
Ok(last_packet.read().await.clone()) // FIXME slow
}

#[tauri::command]
async fn battery_voltage(state: State<'_, Lazy<RwLock<DriverStationState>>>) -> Result<f32, ()> {
Ok(state.read().await.ds.as_ref().unwrap().battery_voltage())
}

#[tauri::command]
fn joystick_values() -> Vec<Vec<JoystickValue>> {
let gilrs = Gilrs::new().expect("Should be able to load Gilrs.");
let mut out: Vec<Vec<JoystickValue>> = vec![];
for (_id, gamepad) in gilrs.gamepads() {
let mut values: Vec<JoystickValue> = vec![];
for i in BUTTONS {
values.push(JoystickValue::Button {
id: i as u8,
pressed: gamepad.is_pressed(i),
})
}

for i in AXIS {
values.push(JoystickValue::Axis {
id: i as u8,
value: gamepad.value(i),
})
}

out.push(values);
}
out
}

#[tauri::command]
async fn send_packet(
last_packet: State<'_, Lazy<RwLock<Packet>>>,
Expand Down Expand Up @@ -314,7 +349,10 @@ fn main() {
restart_code,
estop,
enable,
disable
disable,
last_packet,
battery_voltage,
joystick_values,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down

0 comments on commit 85be754

Please sign in to comment.