Skip to content

Commit c11b5da

Browse files
committed
first commit for debug page feature
1 parent 4cc939f commit c11b5da

File tree

7 files changed

+256
-25
lines changed

7 files changed

+256
-25
lines changed

src/event_handler/debug.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::{collections::HashMap, rc::Rc};
2+
3+
use crate::slint_generatedAppWindow::{raw_can, AppWindow};
4+
use slint::{Model, SharedString, VecModel, Weak};
5+
use socketcan::{CanSocket, EmbeddedFrame, Frame, Socket};
6+
pub struct DebugHandler<'a> {
7+
#[cfg(target_os = "linux")]
8+
pub iface: &'a str,
9+
#[cfg(target_os = "windows")]
10+
pub iface: UsbCanSocket,
11+
pub ui_handle: &'a Weak<AppWindow>,
12+
pub bitrate: String,
13+
pub filter: (u32, u32),
14+
}
15+
16+
impl<'a> DebugHandler<'a> {
17+
pub fn run(&mut self) {
18+
let can_socket = CanSocket::open(self.iface).unwrap();
19+
if let Ok(frame) = can_socket.read_frame() {
20+
let frame_id = frame.raw_id() & !0x80000000;
21+
if frame_id >= self.filter.0 && frame_id <= self.filter.1 {
22+
let bitrate = self.bitrate().unwrap();
23+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
24+
ui.set_bitrate(bitrate as i32);
25+
let raw_data = ui.get_raw_data();
26+
let mut vec_data = Vec::default();
27+
for data in raw_data.iter() {
28+
vec_data.push(data);
29+
}
30+
vec_data.push(raw_can {
31+
data: SharedString::from(format!("{:?}", frame.data())),
32+
id: SharedString::from(frame_id.to_string()),
33+
len: frame.len() as i32,
34+
});
35+
vec_data.reverse();
36+
let message_vec: Rc<VecModel<raw_can>> = Rc::new(VecModel::from(vec_data));
37+
ui.set_raw_data(message_vec.into());
38+
});
39+
}
40+
}
41+
}
42+
43+
fn bitrate(&self) -> Option<u32> {
44+
let bitrate_map: HashMap<&str, u32> = [
45+
("1 Mbit/s", 1_000_000),
46+
("800 kbit/s", 800_000),
47+
("500 kbit/s", 500_000),
48+
("250 kbit/s", 250_000),
49+
("125 kbit/s", 125_000),
50+
("100 kbit/s", 100_000),
51+
("95.238 kbit/s", 95_238),
52+
("83.333 kbit/s", 83_333),
53+
("50 kbit/s", 50_000),
54+
("47.619 kbit/s", 47_619),
55+
("33.333 kbit/s", 33_333),
56+
("20 kbit/s", 20_000),
57+
("10 kbit/s", 10_000),
58+
("5 kbit/s", 5_000),
59+
]
60+
.iter()
61+
.cloned()
62+
.collect();
63+
64+
bitrate_map.get(self.bitrate.as_str()).copied()
65+
}
66+
}
File renamed without changes.

src/event_handler/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
pub(crate) mod can_handler;
21
pub(crate) mod dbc_file;
2+
pub(crate) mod debug;
3+
pub(crate) mod filter;
34
pub(crate) mod init;
4-
pub(crate) mod packet_filter;
5+
pub(crate) mod view;
56

6-
pub use can_handler::CanHandler;
77
pub use dbc_file::DBCFile;
8+
pub use debug::DebugHandler;
9+
pub use filter::PacketFilter;
810
pub use init::Init;
9-
pub use packet_filter::PacketFilter;
1011
#[cfg(target_os = "windows")]
1112
use pcan_basic::socket::Baudrate;
1213
use slint::Color;
14+
pub use view::ViewHandler;
1315

1416
const ODD_COLOR: Color = Color::from_rgb_u8(0x18, 0x1c, 0x27);
1517
const EVEN_COLOR: Color = Color::from_rgb_u8(0x13, 0x16, 0x1f);

src/event_handler/can_handler.rs renamed to src/event_handler/view.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ use can_dbc::DBC;
22
use chrono::Utc;
33
#[cfg(target_os = "windows")]
44
use pcan_basic::socket::usb::UsbCanSocket;
5-
use slint::{Model, VecModel, Weak};
6-
use slint::{ModelRc, SharedString};
5+
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
76
#[cfg(target_os = "linux")]
87
use socketcan::{CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
9-
use std::collections::HashMap;
10-
use std::fmt::Write;
11-
use std::rc::Rc;
12-
use std::sync::mpsc::Receiver;
13-
use std::sync::{Arc, Mutex};
14-
use std::thread::sleep;
15-
use std::time::Duration;
16-
use std::time::Instant;
8+
use std::{
9+
collections::HashMap,
10+
fmt::Write,
11+
rc::Rc,
12+
sync::{mpsc::Receiver, Arc, Mutex},
13+
thread::sleep,
14+
time::{Duration, Instant},
15+
};
1716

18-
use crate::slint_generatedAppWindow::AppWindow;
19-
use crate::slint_generatedAppWindow::CanData;
20-
use crate::slint_generatedAppWindow::CanSignal;
21-
pub struct CanHandler<'a> {
17+
use crate::slint_generatedAppWindow::{AppWindow, CanData, CanSignal};
18+
pub struct ViewHandler<'a> {
2219
#[cfg(target_os = "linux")]
2320
pub iface: &'a str,
2421
#[cfg(target_os = "windows")]
@@ -31,7 +28,7 @@ pub struct CanHandler<'a> {
3128
static mut NEW_DBC_CHECK: bool = false;
3229
use super::{EVEN_COLOR, ODD_COLOR};
3330

34-
impl<'a> CanHandler<'a> {
31+
impl<'a> ViewHandler<'a> {
3532
pub fn process_can_messages(&mut self) {
3633
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
3734
#[cfg(target_os = "linux")]

src/main.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
44

55
mod event_handler;
66
use can_dbc::DBC;
7-
use event_handler::{CanHandler, DBCFile, Init, PacketFilter};
7+
use event_handler::{DBCFile, DebugHandler, Init, PacketFilter, ViewHandler};
88
#[cfg(target_os = "windows")]
99
use pcan_basic::{bus::UsbBus, socket::usb::UsbCanSocket};
1010
#[cfg(target_os = "linux")]
@@ -40,7 +40,9 @@ async fn main() -> io::Result<()> {
4040
init_event.run();
4141
});
4242

43-
let (start_tx, start_rx) = mpsc::channel();
43+
let (start_tx_1, start_rx_1) = mpsc::channel();
44+
let (start_tx_2, start_rx_2) = mpsc::channel();
45+
4446
// Handle start event
4547
let ui_handle = ui.as_weak();
4648
ui.on_start(move |_name, _index, bitrate| {
@@ -52,7 +54,8 @@ async fn main() -> io::Result<()> {
5254
ui.set_init_string(SharedString::from("No device found!!!"));
5355
} else {
5456
ui.set_is_init(true);
55-
let _ = start_tx.send((_name, bitrate));
57+
let _ = start_tx_1.send((_name.clone(), bitrate.clone()));
58+
let _ = start_tx_2.send((_name, bitrate));
5659
}
5760
}
5861
#[cfg(target_os = "windows")]
@@ -72,7 +75,7 @@ async fn main() -> io::Result<()> {
7275
match UsbCanSocket::open(usb_can, baudrate) {
7376
Ok(socket) => {
7477
ui_handle.unwrap().set_is_init(true);
75-
let _ = start_tx.send((socket, bitrate));
78+
let _ = start_tx_1.send((socket, bitrate));
7679
}
7780
Err(e) => {
7881
ui_handle
@@ -85,8 +88,8 @@ async fn main() -> io::Result<()> {
8588

8689
let ui_handle = ui.as_weak();
8790
tokio::spawn(async move {
88-
if let Ok((can_if, bitrate)) = start_rx.recv() {
89-
let mut can_handler = CanHandler {
91+
if let Ok((can_if, bitrate)) = start_rx_1.recv() {
92+
let mut can_handler = ViewHandler {
9093
#[cfg(target_os = "windows")]
9194
iface: can_if,
9295
#[cfg(target_os = "linux")]
@@ -101,6 +104,24 @@ async fn main() -> io::Result<()> {
101104
}
102105
});
103106

107+
let ui_handle = ui.as_weak();
108+
tokio::spawn(async move {
109+
if let Ok((can_if, bitrate)) = start_rx_2.recv() {
110+
let mut can_handler = DebugHandler {
111+
#[cfg(target_os = "windows")]
112+
iface: can_if,
113+
#[cfg(target_os = "linux")]
114+
iface: &can_if,
115+
ui_handle: &ui_handle,
116+
bitrate: bitrate.to_string(),
117+
filter: (0, 0xFFFFFFFF),
118+
};
119+
loop {
120+
can_handler.run();
121+
}
122+
}
123+
});
124+
104125
// Handle open file event
105126
let ui_handle = ui.as_weak();
106127
ui.on_open_dbc_file(move || {

ui/app.slint

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { viewPage } from "view_page.slint";
77
import { filterPage } from "filter_page.slint";
88
import { selectPage } from "page_selection.slint";
99
import { initPage, socket_info } from "init_page.slint";
10+
import { raw_can, debugPage } from "debug_page.slint";
1011

1112
export component AppWindow inherits Window {
1213
in property <bool> is_filter: false;
@@ -20,6 +21,7 @@ export component AppWindow inherits Window {
2021
in-out property <string> state;
2122
in-out property <int> bus_load;
2223
in-out property <int> bitrate;
24+
in property <[raw_can]> raw_data;
2325

2426
in-out property <int> active-page: 0;
2527

@@ -110,6 +112,11 @@ export component AppWindow inherits Window {
110112
open_dbc_file()
111113
}
112114
}
115+
if root.active-page == 2:
116+
debugPage {
117+
bitrate: bitrate;
118+
raw_data: raw_data;
119+
}
113120
}
114121
}
115122
}

ui/debug_page.slint

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
import { ListView, Button } from "std-widgets.slint";
3+
4+
export struct raw_can {
5+
id: string,
6+
len: int,
7+
data: string
8+
}
9+
export component debugPage inherits Rectangle {
10+
in property <string> state;
11+
in property <string> bitrate;
12+
in property <string> bus_load;
13+
in-out property <[raw_can]> raw_data: [
14+
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
15+
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
16+
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
17+
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"}];
18+
VerticalLayout {
19+
HorizontalLayout {
20+
Rectangle {}
21+
Rectangle {
22+
max-height: 30px;
23+
Text {
24+
text: "State: " + state;
25+
color: white;
26+
}
27+
}
28+
Rectangle {}
29+
Rectangle {
30+
Text {
31+
text: "Bitrate: " + bitrate;
32+
color: white;
33+
}
34+
}
35+
Rectangle {}
36+
Rectangle {
37+
Text {
38+
text: "Bus Load: " + bus_load + "%";
39+
color: white;
40+
}
41+
}
42+
Rectangle {
43+
width: 50px;
44+
}
45+
}
46+
Rectangle {
47+
height: 1px;
48+
background: white;
49+
}
50+
HorizontalLayout {
51+
Rectangle {
52+
width: 30px;
53+
Text {
54+
text: "Receive";
55+
color: white;
56+
rotation-angle: 270deg;
57+
}
58+
border-color: white;
59+
border-width: 1px;
60+
}
61+
VerticalLayout {
62+
ListView {
63+
for raw in raw_data: Rectangle {
64+
HorizontalLayout {
65+
Rectangle {
66+
width: parent.width * 30%;
67+
Text {
68+
text: raw.id;
69+
color: white;
70+
}
71+
}
72+
Rectangle {
73+
width: parent.width * 20%;
74+
Text {
75+
text: raw.len;
76+
color: white;
77+
}
78+
}
79+
Rectangle {
80+
width: parent.width * 50%;
81+
Text {
82+
text: raw.data;
83+
color: white;
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
}
91+
Rectangle {
92+
height: 1px;
93+
border-color: white;
94+
border-width: 1px;
95+
}
96+
HorizontalLayout {
97+
Rectangle {
98+
width: 30px;
99+
Text {
100+
text: "Transmit";
101+
color: white;
102+
rotation-angle: 270deg;
103+
}
104+
border-color: white;
105+
border-width: 1px;
106+
}
107+
VerticalLayout {
108+
ListView {
109+
for raw in raw_data: Rectangle {
110+
HorizontalLayout {
111+
Rectangle {
112+
width: parent.width * 30%;
113+
Text {
114+
text: raw.id;
115+
color: white;
116+
}
117+
}
118+
Rectangle {
119+
width: parent.width * 20%;
120+
Text {
121+
text: raw.len;
122+
color: white;
123+
}
124+
}
125+
Rectangle {
126+
width: parent.width * 50%;
127+
Text {
128+
text: raw.data;
129+
color: white;
130+
}
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)