Skip to content

Commit ecf345a

Browse files
committed
update UI and add raw can data
1 parent 51788b5 commit ecf345a

File tree

5 files changed

+142
-82
lines changed

5 files changed

+142
-82
lines changed

src/event_handler/can_handler.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use can_dbc::DBC;
2-
use slint::{Model, VecModel, Weak};
2+
use slint::{Color, Model, VecModel, Weak};
33
use slint::{ModelRc, SharedString};
44
use socketcan::{CanSocket, EmbeddedFrame, Frame, Socket};
55
use std::collections::HashMap;
6+
use std::fmt::Write;
67
use std::rc::Rc;
78
use std::sync::mpsc::Receiver;
89
use std::thread::sleep;
@@ -51,14 +52,15 @@ impl<'a> CanHandler<'a> {
5152
for message in dbc.messages() {
5253
if frame_id == (message.message_id().raw() & !0x80000000) {
5354
let padding_data = Self::pad_to_8_bytes(frame.data());
55+
let hex_string = Self::array_to_hex_string(frame.data());
5456
let signal_data = message.parse_from_can(&padding_data);
5557
let is_filter = ui.get_is_filter();
5658
let messages: ModelRc<CanData> = if !is_filter {
5759
ui.get_messages()
5860
} else {
5961
ui.get_filter_messages()
6062
};
61-
Self::update_ui_with_signals(&messages, frame_id, signal_data);
63+
Self::update_ui_with_signals(&messages, frame_id, signal_data, hex_string);
6264
}
6365
}
6466
}
@@ -69,6 +71,7 @@ impl<'a> CanHandler<'a> {
6971
messages: &ModelRc<CanData>,
7072
frame_id: u32,
7173
signal_data: HashMap<String, f32>,
74+
raw_can: String,
7275
) {
7376
for (message_count, message) in messages.iter().enumerate() {
7477
if message.can_id == frame_id.to_string() {
@@ -80,6 +83,12 @@ impl<'a> CanHandler<'a> {
8083
packet_name: message.packet_name.clone(),
8184
signal_value: can_signals.into(),
8285
counter: message.counter + 1,
86+
raw_can: raw_can.into(),
87+
color: if message_count % 2 == 0 {
88+
Color::from_rgb_u8(0xc8, 0xc8, 0xcc)
89+
} else {
90+
Color::from_rgb_u8(0xda, 0xda, 0xda)
91+
},
8392
},
8493
);
8594
break;
@@ -133,4 +142,14 @@ impl<'a> CanHandler<'a> {
133142
// Return the padded vector
134143
padded_data
135144
}
145+
146+
fn array_to_hex_string(data: &[u8]) -> String {
147+
// Preallocate space for efficiency
148+
let mut hex_string = String::with_capacity(data.len() * 3);
149+
for byte in data {
150+
write!(hex_string, "{:02X} ", byte).unwrap();
151+
}
152+
hex_string.pop(); // Remove the trailing space
153+
hex_string
154+
}
136155
}

src/event_handler/dbc_file.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use can_dbc::DBC;
22
use rfd::FileDialog;
3-
use slint::{Model, VecModel};
3+
use slint::{Color, Model, VecModel};
44
use slint::{ModelRc, SharedString, Weak};
55
use std::fs::File;
66
use std::io::Read;
@@ -35,6 +35,8 @@ impl<'a> DBCFile<'a> {
3535
packet_name: SharedString::from("default"),
3636
signal_value: ModelRc::default(),
3737
counter: 0,
38+
raw_can: SharedString::from("default"),
39+
color: Color::from_rgb_u8(0xc8, 0xc8, 0xcc),
3840
}]
3941
.to_vec(),
4042
));
@@ -73,6 +75,12 @@ impl<'a> DBCFile<'a> {
7375
packet_name: SharedString::from(message.message_name()),
7476
signal_value: can_signals.into(),
7577
counter: 0,
78+
raw_can: SharedString::from(""),
79+
color: if message_count % 2 == 0 {
80+
Color::from_rgb_u8(0xc8, 0xc8, 0xcc)
81+
} else {
82+
Color::from_rgb_u8(0xda, 0xda, 0xda)
83+
},
7684
};
7785

7886
if message_count == 0 {

src/event_handler/packet_filter.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use slint::Weak;
1+
use slint::{Color, Weak};
22
use slint::{Model, VecModel};
33
use std::rc::Rc;
44

@@ -16,13 +16,32 @@ impl<'a> PacketFilter<'a> {
1616
let mut list_filter: Vec<CanData> = ui.get_filter_messages().iter().collect();
1717
if self.is_check {
1818
// Add filter ID
19-
list_filter.push(self.filter);
19+
list_filter.push(CanData {
20+
can_id: self.filter.can_id,
21+
color: if list_filter.len() % 2 == 0 {
22+
Color::from_rgb_u8(0xc8, 0xc8, 0xcc)
23+
} else {
24+
Color::from_rgb_u8(0xda, 0xda, 0xda)
25+
},
26+
counter: self.filter.counter,
27+
packet_name: self.filter.packet_name,
28+
raw_can: self.filter.raw_can,
29+
signal_value: self.filter.signal_value,
30+
});
2031
} else {
2132
// Remove filter ID
2233
for (filter_count, can_filter) in list_filter.clone().into_iter().enumerate() {
2334
if can_filter.can_id == self.filter.can_id {
2435
list_filter.remove(filter_count);
2536
}
37+
38+
if filter_count % 2 == 0 {
39+
if let Some(data) = list_filter.get_mut(filter_count) {
40+
data.color = Color::from_rgb_u8(0xda, 0xda, 0xda);
41+
}
42+
} else if let Some(data) = list_filter.get_mut(filter_count) {
43+
data.color = Color::from_rgb_u8(0xc8, 0xc8, 0xcc);
44+
}
2645
}
2746
}
2847

ui/app.slint

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { CanMessage, CanSignal } from "messages.slint";
77
struct CanData {
88
can_id: string,
99
packet_name: string,
10+
raw_can: string,
1011
counter: int,
1112
signal_value: [CanSignal],
13+
color: color
1214
}
1315

1416
export component AppWindow inherits Window {
@@ -20,13 +22,13 @@ export component AppWindow inherits Window {
2022
callback open_dbc_file();
2123
callback filter_id(CanData, bool);
2224
title: @tr("CAN VIEWER (version 0.1.0)");
23-
background: #c7b6a7;
24-
default-font-family: "Roboto";
25+
background: #555555;
26+
default-font-family: "Noto Sans";
2527
default-font-size: 12px;
2628

2729
button := Button {
2830
x: root.x;
29-
y: root.y;
31+
y: root.y + 1px;
3032
text: "Open";
3133
clicked => {
3234
open_dbc_file();
@@ -40,44 +42,57 @@ export component AppWindow inherits Window {
4042

4143
main_page := Tab {
4244
title: "view";
43-
if !root.is_filter:
44-
ScrollView {
45+
46+
Rectangle {
4547
width: root.width;
46-
height: root.height - 120px;
47-
VerticalBox{
48-
width: root.width;
49-
for message in messages: CanMessage {
50-
message_id: message.can-id;
51-
message_name: message.packet-name;
52-
counter: message.counter;
53-
signals: message.signal-value;
48+
height: root.height - button.height - 5px;
49+
background: #c8c8cc;
50+
if !root.is_filter:
51+
ScrollView {
52+
VerticalLayout{
53+
width: root.width;
54+
for message in messages: CanMessage {
55+
message_id: message.can-id;
56+
message_name: message.packet-name;
57+
counter: message.counter;
58+
signals: message.signal-value;
59+
raw_data: message.raw-can;
60+
back_ground: message.color;
61+
}
5462
}
5563
}
56-
}
57-
if root.is_filter:
58-
ScrollView {
59-
width: root.width;
60-
height: root.height - 120px;
61-
62-
VerticalBox{
64+
if root.is_filter:
65+
ScrollView {
6366
width: root.width;
64-
for message in filter_messages: CanMessage {
65-
message_id: message.can-id;
66-
message_name: message.packet-name;
67-
counter: message.counter;
68-
signals: message.signal-value;
67+
height: root.height - 120px;
68+
69+
VerticalLayout{
70+
width: root.width;
71+
for message in filter_messages: CanMessage {
72+
message_id: message.can-id;
73+
message_name: message.packet-name;
74+
counter: message.counter;
75+
signals: message.signal-value;
76+
raw_data: message.raw-can;
77+
back_ground: message.color;
78+
}
6979
}
7080
}
7181
}
7282
}
7383
Select_page := Tab {
7484
title: "filter";
75-
ScrollView {
76-
VerticalBox{
77-
for message in messages: CheckBox {
78-
text: message.can-id + "(" + message.packet-name + ")";
79-
toggled => {
80-
filter_id(message, self.checked)
85+
Rectangle {
86+
width: root.width;
87+
height: root.height - button.height - 5px;
88+
background: #c8c8cc;
89+
ScrollView {
90+
VerticalLayout{
91+
for message in messages: CheckBox {
92+
text: message.can-id + "(" + message.packet-name + ")";
93+
toggled => {
94+
filter_id(message, self.checked)
95+
}
8196
}
8297
}
8398
}

ui/messages.slint

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ export struct CanSignal {
77
factor: string,
88
}
99

10-
export component CanMessage inherits HorizontalLayout {
10+
export component CanMessage inherits Rectangle {
1111
in property <string> message_id: "0x00000000";
12+
in property <string> raw_data: "0x01 0x02 0x03 0x04";
1213
in property <string> message_name: "packet_xxx";
1314
in property <int> counter: 0;
15+
in property <color> back_ground;
1416
in property <[CanSignal]> signals: [
1517
{signal_name: "signal_1", signal_value: "100", unit: "Hz", factor: "1.0"},
1618
{signal_name: "signal_2", signal_value: "999", unit: "Km", factor: "1.0"},
@@ -19,53 +21,50 @@ export component CanMessage inherits HorizontalLayout {
1921
{signal_name: "signal_5", signal_value: "20.9", unit: "A", factor: "1.0"},
2022
{signal_name: "signal_6", signal_value: "10", unit: "Volt", factor: "1.0"},
2123
{signal_name: "signal_7", signal_value: "1", unit: "mA", factor: "1.0"}];
24+
background: root.back_ground;
2225

23-
// Message ID
24-
Rectangle {
25-
width: root.width * 30%;
26-
border-color: black;
27-
border-width: 0.75px;
28-
Text {
29-
text: root.message_id + "\n" + root.message_name + "\n" + root.counter;
30-
horizontal-alignment: center;
31-
vertical-alignment: center;
32-
}
33-
}
34-
35-
VerticalLayout {
36-
for signal in signals: HorizontalLayout {
37-
Rectangle {
38-
width: root.width * 30%;
39-
border-color: black;
40-
border-width: 0.35px;
41-
Text {
42-
text: signal.signal_name;
43-
}
44-
}
45-
Rectangle {
46-
width: root.width * 20%;
47-
border-color: black;
48-
border-width: 0.35px;
49-
Text {
50-
text: signal.signal_value;
51-
}
52-
}
53-
Rectangle {
54-
width: root.width * 10%;
55-
border-color: black;
56-
border-width: 0.35px;
57-
Text {
58-
text: signal.unit;
59-
}
60-
}
61-
Rectangle {
62-
width: root.width * 10%;
63-
border-color: black;
64-
border-width: 0.35px;
65-
Text {
66-
text: signal.factor;
67-
}
26+
HorizontalLayout{
27+
Rectangle {
28+
width: root.width * 20%;
29+
border-color: black;
30+
border-width: 0.75px;
31+
Text {
32+
text: root.message_id + "\n" + root.message_name + "\n" + root.counter;
33+
horizontal-alignment: center;
34+
vertical-alignment: center;
35+
}
36+
}
37+
38+
VerticalLayout {
39+
for signal in signals: HorizontalLayout {
40+
Rectangle {
41+
width: root.width * 25%;
42+
border-color: black;
43+
border-width: 0.75px;
44+
Text {
45+
text: signal.signal_name;
46+
}
47+
}
48+
Rectangle {
49+
width: root.width * 15%;
50+
border-color: black;
51+
border-width: 0.75px;
52+
Text {
53+
text: signal.signal_value + " " + signal.unit;
54+
}
55+
}
6856
}
6957
}
58+
59+
Rectangle {
60+
width: root.width * 40%;
61+
border-color: black;
62+
border-width: 0.75px;
63+
Text {
64+
text: root.raw_data;
65+
horizontal-alignment: center;
66+
vertical-alignment: center;
67+
}
68+
}
7069
}
7170
}

0 commit comments

Comments
 (0)