Skip to content

Commit e33ebd6

Browse files
feat:customize settings
1 parent b7b1050 commit e33ebd6

4 files changed

Lines changed: 637 additions & 29 deletions

File tree

tock-ui/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2626
tockloader-lib = { path = "../../tockloader-lib" }
2727
tokio-serial = {version = "5.4.4", features = ["libudev"] }
2828
probe-rs = { version = "0.24.0" }
29+

tock-ui/src-tauri/src/main.rs

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
22

3+
use std::time::Duration;
4+
35
use probe_rs::probe::list::Lister;
46
use serde::{Deserialize, Serialize};
7+
use tockloader_lib::board_settings::BoardSettings;
8+
use tockloader_lib::connection::SerialConnection;
9+
use tockloader_lib::connection::SerialTargetInfo;
10+
use tockloader_lib::connection::{Connection, ProbeRSConnection, ProbeTargetInfo};
11+
use tockloader_lib::CommandInfo;
512
use tokio_serial::available_ports;
13+
use tokio_serial::FlowControl;
14+
use tokio_serial::Parity;
615
use tokio_serial::SerialPortType;
16+
use tokio_serial::StopBits;
17+
718
#[derive(Debug, Serialize, Deserialize)]
819
pub struct DebugProbeSummary {
920
pub identifier: String,
@@ -88,11 +99,110 @@ async fn list_all_devices() -> Result<ConnectedDevices, String> {
8899
})
89100
}
90101

102+
#[tauri::command]
103+
async fn connect_to_probe(
104+
probe_identifier: String,
105+
chip: String,
106+
core: usize,
107+
) -> Result<String, String> {
108+
let probes = Lister::new().list_all();
109+
let debug_probe_info = probes
110+
.into_iter()
111+
.find(|p| p.identifier == probe_identifier)
112+
.ok_or_else(|| format!("Probe with identifier '{probe_identifier}' not found."))?;
113+
114+
let target_info = ProbeTargetInfo { chip, core };
115+
let mut connection = ProbeRSConnection::new(debug_probe_info, target_info);
116+
117+
match connection.open().await {
118+
Ok(_) => {
119+
let info_result = connection.info(&BoardSettings::default()).await;
120+
println!("Result of connection.info(): {info_result:?}");
121+
Ok(format!(
122+
"Successfully connected to probe '{}' with chip '{}' and core {}.",
123+
probe_identifier, connection.target_info.chip, connection.target_info.core
124+
))
125+
}
126+
Err(e) => {
127+
eprintln!("Error connecting to probe: {e:?}");
128+
Err(format!("Failed to connect to probe: {e}"))
129+
}
130+
}
131+
}
132+
//new code for serial port connection
133+
#[tauri::command]
134+
#[allow(clippy::too_many_arguments)]
135+
async fn connect_to_serial(
136+
port_name: String,
137+
baud_rate: u32,
138+
parity_str: String,
139+
stop_bits_str: String,
140+
flow_control_str: String,
141+
timeout_ms: u64,
142+
request_to_send: bool,
143+
data_terminal_ready: bool,
144+
) -> Result<String, String> {
145+
//added now
146+
println!("Attempting to connect to serial port: {port_name}");
147+
println!("Settings: Baud={baud_rate}, Parity={parity_str}, StopBits={stop_bits_str}, Flow={flow_control_str}, Timeout={timeout_ms}ms, RTS={request_to_send}, DTR={data_terminal_ready}",
148+
);
149+
//added
150+
// Parse string inputs into tokio_serial enums
151+
let parity = match parity_str.as_str() {
152+
"None" => Parity::None,
153+
"Odd" => Parity::Odd,
154+
"Even" => Parity::Even,
155+
_ => return Err(format!("Invalid parity: {parity_str}")),
156+
};
157+
158+
let stop_bits = match stop_bits_str.as_str() {
159+
"One" => StopBits::One,
160+
"Two" => StopBits::Two,
161+
_ => return Err(format!("Invalid stop bits: {stop_bits_str}")),
162+
};
163+
164+
let flow_control = match flow_control_str.as_str() {
165+
"None" => FlowControl::None,
166+
"Software" => FlowControl::Software,
167+
"Hardware" => FlowControl::Hardware,
168+
_ => return Err(format!("Invalid flow control: {flow_control_str}")),
169+
};
170+
171+
let baud_rate_val = baud_rate;
172+
let serial_target_info = SerialTargetInfo {
173+
baud_rate: baud_rate_val,
174+
parity,
175+
stop_bits,
176+
flow_control,
177+
timeout: Duration::from_millis(timeout_ms),
178+
request_to_send,
179+
data_terminal_ready,
180+
};
181+
182+
let mut connection = SerialConnection::new(port_name.clone(), serial_target_info);
183+
// Attempt to open the serial connection
184+
match connection.open().await {
185+
Ok(_) => {
186+
let info_result = connection.info(&BoardSettings::default()).await;
187+
println!("Result of connection.info(): {info_result:?}");
188+
println!("Successfully opened serial port.");
189+
Ok(format!("Successfully connected to serial port '{port_name}' at {baud_rate_val} baud with custom settings."))
190+
}
191+
Err(e) => {
192+
eprintln!("Error connecting to serial port: {e:?}");
193+
Err(format!("Failed to connect to serial port: {e}"))
194+
}
195+
}
196+
}
91197
// The main function for your Tauri application
92198
fn main() {
93199
tauri::Builder::default()
94200
// Register the new command so it can be called from the frontend
95-
.invoke_handler(tauri::generate_handler![list_all_devices])
201+
.invoke_handler(tauri::generate_handler![
202+
list_all_devices,
203+
connect_to_probe,
204+
connect_to_serial
205+
])
96206
.run(tauri::generate_context!())
97207
.expect("error while running tauri application");
98208
}

0 commit comments

Comments
 (0)