Skip to content

Commit

Permalink
support CAN bus bitrate configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
TuEmb committed Sep 16, 2024
1 parent 0176621 commit 0f5b37b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "view real-time CAN packages"

[dependencies]
chrono = "0.4.38"
sudo = "0.6"
can-dbc = { git="https://github.com/TuEmb/can-dbc.git", branch="dev" }
rfd = "0.14.1"
slint = { version = "1.7.1", default-features = false, features = ["backend-winit", "compat-1-2", "renderer-winit-femtovg"] }
Expand All @@ -16,6 +17,7 @@ winapi = { version = "0.3.9", features = ["winuser"] }
pcan-basic = { git = "https://github.com/TuEmb/pcan-basic.git", branch="main"}

[target.'cfg(unix)'.dependencies]
privilege-rs = "0.1.0"
socketcan = { git = "https://github.com/socketcan-rs/socketcan-rs.git", rev="e0d7760eca8085b247f37ea22f0aa41e00fa25fa", features = ["enumerate"] }

[build-dependencies]
Expand Down
32 changes: 31 additions & 1 deletion src/event_handler/can_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct CanHandler<'a> {
pub iface: UsbCanSocket,
pub ui_handle: &'a Weak<AppWindow>,
pub mspc_rx: &'a Arc<Mutex<Receiver<DBC>>>,
pub bitrate: String,
}

static mut NEW_DBC_CHECK: bool = false;
Expand All @@ -35,8 +36,11 @@ impl<'a> CanHandler<'a> {
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
#[cfg(target_os = "linux")]
{
let can_socket = self.open_can_socket();
let can_if = CanInterface::open(self.iface).unwrap();
let _ = can_if.bring_down();
let _ = can_if.set_bitrate(self.bitrate().unwrap(), None);
let _ = can_if.bring_up();
let can_socket = self.open_can_socket();
self.process_ui_events(dbc, can_socket, can_if);
}
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -284,4 +288,30 @@ impl<'a> CanHandler<'a> {
hex_string.pop(); // Remove the trailing space
hex_string
}

fn bitrate(&self) -> Option<u32> {
let bitrate_map: HashMap<&str, u32> = [
("1 Mbit/s", 1_000_000),
("800 kbit/s", 800_000),
("500 kbit/s", 500_000),
("250 kbit/s", 250_000),
("200 kbit/s", 200_000),
("125 kbit/s", 125_000),
("100 kbit/s", 100_000),
("95.238 kbit/s", 95_238),
("83.333 kbit/s", 83_333),
("50 kbit/s", 50_000),
("47.619 kbit/s", 47_619),
("40 kbit/s", 40_000),
("33.333 kbit/s", 33_333),
("20 kbit/s", 20_000),
("10 kbit/s", 10_000),
("5 kbit/s", 5_000),
]
.iter()
.cloned()
.collect();

bitrate_map.get(self.bitrate.as_str()).copied()
}
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use pcan_basic::{
hw::attached_channels,
socket::{usb::UsbCanSocket, Baudrate},
};
#[cfg(target_os = "linux")]
use privilege_rs::privilege_request;
#[cfg(target_os = "windows")]
use slint::Model;
use slint::{ModelRc, SharedString, VecModel};
Expand All @@ -25,6 +27,7 @@ slint::include_modules!();

#[tokio::main]
async fn main() -> io::Result<()> {
privilege_request();
let ui = AppWindow::new().unwrap();

let (tx, rx) = mpsc::channel::<DBC>();
Expand Down Expand Up @@ -161,7 +164,7 @@ async fn main() -> io::Result<()> {
let (start_tx, start_rx) = mpsc::channel();
// Handle start event
let ui_handle = ui.as_weak();
ui.on_start(move |_name, _index| {
ui.on_start(move |_name, _index, bitrate| {
// start_tx.send((_name, _index));
#[cfg(target_os = "linux")]
{
Expand All @@ -170,7 +173,7 @@ async fn main() -> io::Result<()> {
ui.set_init_string(SharedString::from("No device found!!!"));
} else {
ui.set_is_init(true);
let _ = start_tx.send(_name);
let _ = start_tx.send((_name, bitrate));
}
}
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -201,14 +204,15 @@ async fn main() -> io::Result<()> {

let ui_handle = ui.as_weak();
tokio::spawn(async move {
if let Ok(can_if) = start_rx.recv() {
if let Ok((can_if, bitrate)) = start_rx.recv() {
let mut can_handler = CanHandler {
#[cfg(target_os = "windows")]
iface: can_if,
#[cfg(target_os = "linux")]
iface: &can_if,
ui_handle: &ui_handle,
mspc_rx: &rx,
bitrate: bitrate.to_string(),
};
loop {
can_handler.process_can_messages();
Expand Down
6 changes: 3 additions & 3 deletions ui/app.slint
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export component AppWindow inherits Window {

callback open_dbc_file();
callback filter_id(CanData, bool);
callback start(string, int);
callback start(string, int, string);
title: @tr("CAN VIEWER (version 0.2.0)");
icon: @image-url("images/can_viewer_128px.png");
background: #1a1f2b;
Expand All @@ -38,8 +38,8 @@ export component AppWindow inherits Window {
initPage {
out: init_string;
can_sockets: can_sockets;
start(name, index) => {
start(name, index);
start(name, index, bitrate) => {
start(name, index, bitrate);
}
}

Expand Down
16 changes: 8 additions & 8 deletions ui/init_page.slint
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export component initPage inherits Rectangle {
background: #1a1f2b;
in property <socket_info> can_sockets;
in property <string> out: "Please select CAN device to start";
callback start(string /* name */, int /* index */);
callback start(string /* name */, int /* index */, string /* */);
VerticalLayout {
Rectangle {}
Text {
Expand All @@ -31,16 +31,16 @@ export component initPage inherits Rectangle {
current-value: can_sockets.name[0];
}
}
// bitrate_box := ComboBox {
// model: ["1 Mbit/s", "800 kbit/s", "500 kbit/s", "250 kbit/s", "200 kbit/s", "125 kbit/s",
// "100 kbit/s", "95.238 kbit/s", "83.333 kbit/s", "50 kbit/s", "47.619 kbit/s",
// "40 kbit/s", "33.333 kbit/s", "20 kbit/s", "10 kbit/s", "5 kbit/s"];
// current-value: "250 kbit/s";
// }
bitrate_box := ComboBox {
model: ["1 Mbit/s", "800 kbit/s", "500 kbit/s", "250 kbit/s", "200 kbit/s", "125 kbit/s",
"100 kbit/s", "95.238 kbit/s", "83.333 kbit/s", "50 kbit/s", "47.619 kbit/s",
"40 kbit/s", "33.333 kbit/s", "20 kbit/s", "10 kbit/s", "5 kbit/s"];
current-value: "250 kbit/s";
}
Button {
text: "start";
clicked => {
start(socket_can_box.current-value, socket_can_box.current-index);
start(socket_can_box.current-value, socket_can_box.current-index, bitrate_box.current-value);
}
}
Rectangle {}
Expand Down

0 comments on commit 0f5b37b

Please sign in to comment.