-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create init event to commonize linux and windows
- Loading branch information
Showing
3 changed files
with
118 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use crate::slint_generatedAppWindow::{socket_info, AppWindow}; | ||
#[cfg(target_os = "windows")] | ||
use pcan_basic::hw::attached_channels as available_interfaces; | ||
use slint::{ComponentHandle, ModelRc, SharedString, VecModel, Weak}; | ||
#[cfg(target_os = "linux")] | ||
use socketcan::available_interfaces; | ||
use std::{process::exit, time::Duration}; | ||
pub struct Init<'a> { | ||
pub ui_handle: &'a Weak<AppWindow>, | ||
} | ||
|
||
impl<'a> Init<'a> { | ||
pub fn run(&self) { | ||
let mut previous_interfaces = Vec::default(); | ||
loop { | ||
match available_interfaces() { | ||
Ok(interface) => { | ||
if interface.is_empty() { | ||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
let socket_info = socket_info { | ||
index: ModelRc::new(VecModel::from(Vec::default())), | ||
name: ModelRc::new(VecModel::from(Vec::default())), | ||
}; | ||
ui.set_can_sockets(socket_info); | ||
ui.set_init_string(SharedString::from("No CAN device found !")); | ||
}); | ||
} else { | ||
#[cfg(target_os = "linux")] | ||
if previous_interfaces != interface { | ||
let interface_clone = interface.clone(); | ||
previous_interfaces = interface.clone(); | ||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
ui.set_init_string(SharedString::from(format!( | ||
"Found {} CAN devices\n Please select your device ", | ||
interface.len() | ||
))); | ||
}); | ||
|
||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
let convert_shared_string: Vec<SharedString> = interface_clone | ||
.into_iter() | ||
.map(SharedString::from) | ||
.collect(); | ||
let socket_info = socket_info { | ||
index: ModelRc::new(VecModel::from(Vec::default())), | ||
name: ModelRc::new(VecModel::from(convert_shared_string)), | ||
}; | ||
ui.set_can_sockets(socket_info); | ||
}); | ||
} | ||
#[cfg(target_os = "windows")] | ||
{ | ||
let mut interface_names = Vec::default(); | ||
let mut interface_index = Vec::default(); | ||
let mut count = 0; | ||
for channel in channels { | ||
let interface_name = SharedString::from(format!( | ||
"{}(0x{:02X})", | ||
channel.device_name(), | ||
channel.channel_information.device_id | ||
)); | ||
interface_names.push(socket_name); | ||
interface_index | ||
.push(channel.channel_information.channel_handle as i32); | ||
count += 1; | ||
} | ||
if previous_interfaces != interface_names { | ||
previous_interfaces = interface_names.clone(); | ||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
ui.set_init_string(SharedString::from(format!( | ||
"Found {} CAN devices\n Please select your device ", | ||
count | ||
))); | ||
let socket_info = socket_info { | ||
index: ModelRc::new(VecModel::from(interface_index)), | ||
name: ModelRc::new(VecModel::from(interface_names)), | ||
}; | ||
ui.set_can_sockets(socket_info); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
Err(e) => { | ||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
ui.set_init_string(SharedString::from(format!( | ||
"Can't get device list: {}", | ||
e | ||
))); | ||
let socket_info = socket_info { | ||
index: ModelRc::new(VecModel::from(Vec::default())), | ||
name: ModelRc::new(VecModel::from(Vec::default())), | ||
}; | ||
ui.set_can_sockets(socket_info); | ||
}); | ||
} | ||
}; | ||
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { | ||
if !ui.window().is_visible() { | ||
exit(1); | ||
} | ||
}); | ||
std::thread::sleep(Duration::from_micros(50)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,13 @@ | ||
pub(crate) mod can_handler; | ||
pub(crate) mod dbc_file; | ||
pub(crate) mod init; | ||
pub(crate) mod packet_filter; | ||
|
||
pub use can_handler::CanHandler; | ||
pub use dbc_file::DBCFile; | ||
pub use init::Init; | ||
pub use packet_filter::PacketFilter; | ||
#[cfg(target_os = "windows")] | ||
use pcan_basic::socket::Baudrate; | ||
use slint::Color; | ||
|
||
const ODD_COLOR: Color = Color::from_rgb_u8(0x18, 0x1c, 0x27); | ||
const EVEN_COLOR: Color = Color::from_rgb_u8(0x13, 0x16, 0x1f); | ||
|
||
#[cfg(target_os = "windows")] | ||
pub fn p_can_bitrate(bitrate: &str) -> Option<Baudrate> { | ||
match bitrate { | ||
"1 Mbit/s" => Some(Baudrate::Baud1M), | ||
"800 kbit/s" => Some(Baudrate::Baud800K), | ||
"500 kbit/s" => Some(Baudrate::Baud500K), | ||
"250 kbit/s" => Some(Baudrate::Baud250K), | ||
"125 kbit/s" => Some(Baudrate::Baud125K), | ||
"100 kbit/s" => Some(Baudrate::Baud100K), | ||
"95.238 kbit/s" => Some(Baudrate::Baud95K), | ||
"83.333 kbit/s" => Some(Baudrate::Baud83), | ||
"50 kbit/s" => Some(Baudrate::Baud50K), | ||
"47.619 kbit/s" => Some(Baudrate::Baud47K), | ||
"33.333 kbit/s" => Some(Baudrate::Baud33K), | ||
"20 kbit/s" => Some(Baudrate::Baud20K), | ||
"10 kbit/s" => Some(Baudrate::Baud10K), | ||
"5 kbit/s" => Some(Baudrate::Baud5K), | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters