|
| 1 | +// A custom rerun viewer capable of showing and editing settings |
| 2 | + |
| 3 | +use probe_plotter_tools::{gui::MyApp, parse, probe_background_thread, setting::Setting}; |
| 4 | +use rerun::external::{eframe, re_crash_handler, re_grpc_server, re_viewer, tokio}; |
| 5 | +use std::{env, io::Read, sync::mpsc, thread, time::Duration}; |
| 6 | + |
| 7 | +#[tokio::main] |
| 8 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 9 | + let help = "Usage: \nprobe-plotter /path/to/elf chip update_rate"; |
| 10 | + |
| 11 | + let elf_path = env::args() |
| 12 | + .nth(1) |
| 13 | + .expect("Usage: \nprobe-plotter /path/to/elf chip update_rate"); |
| 14 | + |
| 15 | + let target = env::args() |
| 16 | + .nth(2) |
| 17 | + .unwrap_or_else(|| "stm32g474retx".to_owned()); |
| 18 | + |
| 19 | + let update_rate = env::args() |
| 20 | + .nth(2) |
| 21 | + .map(|s| { |
| 22 | + Duration::from_millis( |
| 23 | + s.parse() |
| 24 | + .unwrap_or_else(|_| panic!("Invalid update_rate\n\n{help}")), |
| 25 | + ) |
| 26 | + }) |
| 27 | + .unwrap_or_else(|| Duration::from_millis(10)); |
| 28 | + |
| 29 | + let mut elf_bytes = Vec::new(); |
| 30 | + std::fs::File::open(elf_path) |
| 31 | + .unwrap() |
| 32 | + .read_to_end(&mut elf_bytes) |
| 33 | + .unwrap(); |
| 34 | + |
| 35 | + let (metrics, settings) = parse(&elf_bytes); |
| 36 | + |
| 37 | + let main_thread_token = rerun::MainThreadToken::i_promise_i_am_on_the_main_thread(); |
| 38 | + |
| 39 | + // Direct calls using the `log` crate to stderr. Control with `RUST_LOG=debug` etc. |
| 40 | + |
| 41 | + // Install handlers for panics and crashes that prints to stderr and send |
| 42 | + // them to Rerun analytics (if the `analytics` feature is on in `Cargo.toml`). |
| 43 | + re_crash_handler::install_crash_handlers(rerun::build_info()); |
| 44 | + |
| 45 | + // Listen for gRPC connections from Rerun's logging SDKs. |
| 46 | + // There are other ways of "feeding" the viewer though - all you need is a `re_smart_channel::Receiver`. |
| 47 | + let (rx, _) = re_grpc_server::spawn_with_recv( |
| 48 | + "0.0.0.0:9876".parse().unwrap(), |
| 49 | + "75%".parse().unwrap(), |
| 50 | + re_grpc_server::shutdown::never(), |
| 51 | + ); |
| 52 | + |
| 53 | + let (settings_update_sender, settings_update_receiver) = mpsc::channel::<Setting>(); |
| 54 | + |
| 55 | + let mut native_options = re_viewer::native::eframe_options(None); |
| 56 | + native_options.viewport = native_options.viewport.with_app_id("probe-plotter"); |
| 57 | + |
| 58 | + let startup_options = re_viewer::StartupOptions::default(); |
| 59 | + |
| 60 | + // This is used for analytics, if the `analytics` feature is on in `Cargo.toml` |
| 61 | + let app_env = re_viewer::AppEnvironment::Custom("probe-plotter-tools".to_owned()); |
| 62 | + |
| 63 | + let (initial_settings_sender, initial_settings_receiver) = mpsc::channel(); |
| 64 | + |
| 65 | + // probe-thread |
| 66 | + thread::spawn(move || { |
| 67 | + probe_background_thread( |
| 68 | + update_rate, |
| 69 | + &target, |
| 70 | + &elf_bytes, |
| 71 | + settings, |
| 72 | + metrics, |
| 73 | + settings_update_receiver, |
| 74 | + initial_settings_sender, |
| 75 | + ) |
| 76 | + }); |
| 77 | + |
| 78 | + // Receive initial settings from to probe-thread thread |
| 79 | + let settings = initial_settings_receiver.recv().unwrap(); |
| 80 | + |
| 81 | + let window_title = "probe-plotter"; |
| 82 | + eframe::run_native( |
| 83 | + window_title, |
| 84 | + native_options, |
| 85 | + Box::new(move |cc| { |
| 86 | + re_viewer::customize_eframe_and_setup_renderer(cc)?; |
| 87 | + |
| 88 | + let mut rerun_app = re_viewer::App::new( |
| 89 | + main_thread_token, |
| 90 | + re_viewer::build_info(), |
| 91 | + &app_env, |
| 92 | + startup_options, |
| 93 | + cc, |
| 94 | + None, |
| 95 | + re_viewer::AsyncRuntimeHandle::from_current_tokio_runtime_or_wasmbindgen()?, |
| 96 | + ); |
| 97 | + rerun_app.add_log_receiver(rx); |
| 98 | + Ok(Box::new(MyApp::new( |
| 99 | + rerun_app, |
| 100 | + settings, |
| 101 | + settings_update_sender, |
| 102 | + ))) |
| 103 | + }), |
| 104 | + )?; |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments