Skip to content

Commit

Permalink
first commit for debug page feature
Browse files Browse the repository at this point in the history
  • Loading branch information
TuEmb committed Sep 17, 2024
1 parent 4cc939f commit c11b5da
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 25 deletions.
66 changes: 66 additions & 0 deletions src/event_handler/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{collections::HashMap, rc::Rc};

use crate::slint_generatedAppWindow::{raw_can, AppWindow};
use slint::{Model, SharedString, VecModel, Weak};
use socketcan::{CanSocket, EmbeddedFrame, Frame, Socket};
pub struct DebugHandler<'a> {
#[cfg(target_os = "linux")]
pub iface: &'a str,
#[cfg(target_os = "windows")]
pub iface: UsbCanSocket,
pub ui_handle: &'a Weak<AppWindow>,
pub bitrate: String,
pub filter: (u32, u32),
}

impl<'a> DebugHandler<'a> {
pub fn run(&mut self) {
let can_socket = CanSocket::open(self.iface).unwrap();
if let Ok(frame) = can_socket.read_frame() {
let frame_id = frame.raw_id() & !0x80000000;
if frame_id >= self.filter.0 && frame_id <= self.filter.1 {
let bitrate = self.bitrate().unwrap();
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
ui.set_bitrate(bitrate as i32);
let raw_data = ui.get_raw_data();
let mut vec_data = Vec::default();
for data in raw_data.iter() {
vec_data.push(data);
}
vec_data.push(raw_can {
data: SharedString::from(format!("{:?}", frame.data())),
id: SharedString::from(frame_id.to_string()),
len: frame.len() as i32,
});
vec_data.reverse();
let message_vec: Rc<VecModel<raw_can>> = Rc::new(VecModel::from(vec_data));
ui.set_raw_data(message_vec.into());
});
}
}
}

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),
("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),
("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()
}
}
File renamed without changes.
10 changes: 6 additions & 4 deletions src/event_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
pub(crate) mod can_handler;
pub(crate) mod dbc_file;
pub(crate) mod debug;
pub(crate) mod filter;
pub(crate) mod init;
pub(crate) mod packet_filter;
pub(crate) mod view;

pub use can_handler::CanHandler;
pub use dbc_file::DBCFile;
pub use debug::DebugHandler;
pub use filter::PacketFilter;
pub use init::Init;
pub use packet_filter::PacketFilter;
#[cfg(target_os = "windows")]
use pcan_basic::socket::Baudrate;
use slint::Color;
pub use view::ViewHandler;

const ODD_COLOR: Color = Color::from_rgb_u8(0x18, 0x1c, 0x27);
const EVEN_COLOR: Color = Color::from_rgb_u8(0x13, 0x16, 0x1f);
Expand Down
27 changes: 12 additions & 15 deletions src/event_handler/can_handler.rs → src/event_handler/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ use can_dbc::DBC;
use chrono::Utc;
#[cfg(target_os = "windows")]
use pcan_basic::socket::usb::UsbCanSocket;
use slint::{Model, VecModel, Weak};
use slint::{ModelRc, SharedString};
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
#[cfg(target_os = "linux")]
use socketcan::{CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
use std::collections::HashMap;
use std::fmt::Write;
use std::rc::Rc;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;
use std::{
collections::HashMap,
fmt::Write,
rc::Rc,
sync::{mpsc::Receiver, Arc, Mutex},
thread::sleep,
time::{Duration, Instant},
};

use crate::slint_generatedAppWindow::AppWindow;
use crate::slint_generatedAppWindow::CanData;
use crate::slint_generatedAppWindow::CanSignal;
pub struct CanHandler<'a> {
use crate::slint_generatedAppWindow::{AppWindow, CanData, CanSignal};
pub struct ViewHandler<'a> {
#[cfg(target_os = "linux")]
pub iface: &'a str,
#[cfg(target_os = "windows")]
Expand All @@ -31,7 +28,7 @@ pub struct CanHandler<'a> {
static mut NEW_DBC_CHECK: bool = false;
use super::{EVEN_COLOR, ODD_COLOR};

impl<'a> CanHandler<'a> {
impl<'a> ViewHandler<'a> {
pub fn process_can_messages(&mut self) {
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
#[cfg(target_os = "linux")]
Expand Down
33 changes: 27 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};

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

let (start_tx, start_rx) = mpsc::channel();
let (start_tx_1, start_rx_1) = mpsc::channel();
let (start_tx_2, start_rx_2) = mpsc::channel();

// Handle start event
let ui_handle = ui.as_weak();
ui.on_start(move |_name, _index, bitrate| {
Expand All @@ -52,7 +54,8 @@ 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, bitrate));
let _ = start_tx_1.send((_name.clone(), bitrate.clone()));
let _ = start_tx_2.send((_name, bitrate));
}
}
#[cfg(target_os = "windows")]
Expand All @@ -72,7 +75,7 @@ async fn main() -> io::Result<()> {
match UsbCanSocket::open(usb_can, baudrate) {
Ok(socket) => {
ui_handle.unwrap().set_is_init(true);
let _ = start_tx.send((socket, bitrate));
let _ = start_tx_1.send((socket, bitrate));
}
Err(e) => {
ui_handle
Expand All @@ -85,8 +88,8 @@ async fn main() -> io::Result<()> {

let ui_handle = ui.as_weak();
tokio::spawn(async move {
if let Ok((can_if, bitrate)) = start_rx.recv() {
let mut can_handler = CanHandler {
if let Ok((can_if, bitrate)) = start_rx_1.recv() {
let mut can_handler = ViewHandler {
#[cfg(target_os = "windows")]
iface: can_if,
#[cfg(target_os = "linux")]
Expand All @@ -101,6 +104,24 @@ async fn main() -> io::Result<()> {
}
});

let ui_handle = ui.as_weak();
tokio::spawn(async move {
if let Ok((can_if, bitrate)) = start_rx_2.recv() {
let mut can_handler = DebugHandler {
#[cfg(target_os = "windows")]
iface: can_if,
#[cfg(target_os = "linux")]
iface: &can_if,
ui_handle: &ui_handle,
bitrate: bitrate.to_string(),
filter: (0, 0xFFFFFFFF),
};
loop {
can_handler.run();
}
}
});

// Handle open file event
let ui_handle = ui.as_weak();
ui.on_open_dbc_file(move || {
Expand Down
7 changes: 7 additions & 0 deletions ui/app.slint
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { viewPage } from "view_page.slint";
import { filterPage } from "filter_page.slint";
import { selectPage } from "page_selection.slint";
import { initPage, socket_info } from "init_page.slint";
import { raw_can, debugPage } from "debug_page.slint";

export component AppWindow inherits Window {
in property <bool> is_filter: false;
Expand All @@ -20,6 +21,7 @@ export component AppWindow inherits Window {
in-out property <string> state;
in-out property <int> bus_load;
in-out property <int> bitrate;
in property <[raw_can]> raw_data;

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

Expand Down Expand Up @@ -110,6 +112,11 @@ export component AppWindow inherits Window {
open_dbc_file()
}
}
if root.active-page == 2:
debugPage {
bitrate: bitrate;
raw_data: raw_data;
}
}
}
}
138 changes: 138 additions & 0 deletions ui/debug_page.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

import { ListView, Button } from "std-widgets.slint";

export struct raw_can {
id: string,
len: int,
data: string
}
export component debugPage inherits Rectangle {
in property <string> state;
in property <string> bitrate;
in property <string> bus_load;
in-out property <[raw_can]> raw_data: [
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"},
{id: "181FF1FA", len: 4, data: "00 01 02 03 04 05 06 07"}];
VerticalLayout {
HorizontalLayout {
Rectangle {}
Rectangle {
max-height: 30px;
Text {
text: "State: " + state;
color: white;
}
}
Rectangle {}
Rectangle {
Text {
text: "Bitrate: " + bitrate;
color: white;
}
}
Rectangle {}
Rectangle {
Text {
text: "Bus Load: " + bus_load + "%";
color: white;
}
}
Rectangle {
width: 50px;
}
}
Rectangle {
height: 1px;
background: white;
}
HorizontalLayout {
Rectangle {
width: 30px;
Text {
text: "Receive";
color: white;
rotation-angle: 270deg;
}
border-color: white;
border-width: 1px;
}
VerticalLayout {
ListView {
for raw in raw_data: Rectangle {
HorizontalLayout {
Rectangle {
width: parent.width * 30%;
Text {
text: raw.id;
color: white;
}
}
Rectangle {
width: parent.width * 20%;
Text {
text: raw.len;
color: white;
}
}
Rectangle {
width: parent.width * 50%;
Text {
text: raw.data;
color: white;
}
}
}
}
}
}
}
Rectangle {
height: 1px;
border-color: white;
border-width: 1px;
}
HorizontalLayout {
Rectangle {
width: 30px;
Text {
text: "Transmit";
color: white;
rotation-angle: 270deg;
}
border-color: white;
border-width: 1px;
}
VerticalLayout {
ListView {
for raw in raw_data: Rectangle {
HorizontalLayout {
Rectangle {
width: parent.width * 30%;
Text {
text: raw.id;
color: white;
}
}
Rectangle {
width: parent.width * 20%;
Text {
text: raw.len;
color: white;
}
}
Rectangle {
width: parent.width * 50%;
Text {
text: raw.data;
color: white;
}
}
}
}
}
}
}
}
}

0 comments on commit c11b5da

Please sign in to comment.