Skip to content

Commit

Permalink
move live to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
romirk committed Oct 19, 2024
1 parent a0a4236 commit 170a146
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 69 deletions.
74 changes: 74 additions & 0 deletions src/examples/live.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::error::Error;
use palette::{Mix, Srgb};
use show_image::{create_window, event, run_context, ImageInfo, ImageView, WindowOptions};
use tqdm::Iter;
use crate::sl::lidar::Lidar;

pub fn live_view(mut lidar: Lidar, points: Option<usize>) -> Result<(), Box<dyn Error>> {
// status information
let info = lidar.get_info();
let health = lidar.get_health();

println!("\nModel {} version {}.{} HW {}", info.model, info.firmware_version >> 8, info.firmware_version & 0xff, info.hardware_version);

if health.status > 0 {
eprintln!(" code: {}\nexiting!", health.error_code);
lidar.reset();
return Ok(());
}

/// number of samples
let n: usize = points.unwrap_or(5000);

const WIDTH: usize = 1920;
const HEIGHT: usize = 1080;
let mut pixel_data: Box<[u8]> = vec![0u8; WIDTH * HEIGHT * 3].into_boxed_slice();

let high: Srgb<f32> = Srgb::from(0xf3ff82).into();
let low: Srgb<f32> = Srgb::from(0x1e4160).into();

run_context(move || {
println!("Starting scan ({} sample{})...", n, if n == 1 { "" } else { "s" });
let rx = lidar.start_scan();
let window = create_window("scan", WindowOptions {
// size: Some([WIDTH as u32 * 4 / 5, HEIGHT as u32 * 4 / 5]),
fullscreen: true,
..WindowOptions::default()
}).unwrap();

// generate pixel data from samples
println!("Scanning...");
for (i, sample) in rx.iter().take(n).enumerate().tqdm() {
let raw_x = (sample.angle as f64).to_radians().cos() * sample.distance as f64;
let raw_y = (sample.angle as f64).to_radians().sin() * sample.distance as f64;
let x = ((raw_x / 5f64) as isize + (WIDTH as isize / 2)) as usize;
let y = ((raw_y / 5f64) as isize + (HEIGHT as isize / 2)) as usize;

if x < WIDTH && y < HEIGHT {
let pos = (y * WIDTH + x) * 3;
let pixel: [f32; 3] = low.mix(high, (sample.intensity as f32) / 64.0).into();
pixel_data[pos] = (pixel[2] * 255f32) as u8;
pixel_data[pos + 1] = (pixel[1] * 255f32) as u8;
pixel_data[pos + 2] = (pixel[0] * 255f32) as u8;
}

if i % 360 == 0 {
// display
let image = ImageView::new(ImageInfo::bgr8(WIDTH as u32, HEIGHT as u32), pixel_data.as_ref());
window.set_image(format!("image-{}", i), image).unwrap();
}
}

println!("\nStopping scan...");
lidar.stop(false);
lidar.join();

for event in window.event_channel().unwrap() {
if let event::WindowEvent::KeyboardInput(event) = event {
if event.input.key_code == Some(event::VirtualKeyCode::Escape) && event.input.state.is_pressed() {
break;
}
}
}
});
}
1 change: 1 addition & 0 deletions src/examples/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod live;
74 changes: 5 additions & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ extern crate core;

mod sl;
mod util;
mod examples;

use clap::Parser;
use palette::{Mix, Srgb};
use show_image::{create_window, event, run_context, ImageInfo, ImageView, WindowOptions};
use palette::Mix;
use sl::lidar::Lidar;
use std::default::Default;
use std::error::Error;
use std::io::Write;
use tqdm::Iter;
use examples::live;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand All @@ -23,72 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

// initialize lidar
let mut lidar = Lidar::init(String::from("COM3"));
let lidar = Lidar::init(String::from("COM3"));

// status information
let info = lidar.get_info();
let health = lidar.get_health();

println!("\nModel {} version {}.{} HW {}", info.model, info.firmware_version >> 8, info.firmware_version & 0xff, info.hardware_version);

if health.status > 0 {
eprintln!(" code: {}\nexiting!", health.error_code);
lidar.reset();
return Ok(());
}

/// number of samples
let n: usize = args.points.unwrap_or(5000);

const WIDTH: usize = 1920;
const HEIGHT: usize = 1080;
let mut pixel_data: Box<[u8]> = vec![0u8; WIDTH * HEIGHT * 3].into_boxed_slice();

let high: Srgb<f32> = Srgb::from(0xf3ff82).into();
let low: Srgb<f32> = Srgb::from(0x1e4160).into();

run_context(move || {
println!("Starting scan ({} sample{})...", n, if n == 1 { "" } else { "s" });
let rx = lidar.start_scan();
let window = create_window("scan", WindowOptions {
// size: Some([WIDTH as u32 * 4 / 5, HEIGHT as u32 * 4 / 5]),
fullscreen: true,
..WindowOptions::default()
}).unwrap();

// generate pixel data from samples
println!("Scanning...");
for (i, sample) in rx.iter().take(n).enumerate().tqdm() {
let raw_x = (sample.angle as f64).to_radians().cos() * sample.distance as f64;
let raw_y = (sample.angle as f64).to_radians().sin() * sample.distance as f64;
let x = ((raw_x / 5f64) as isize + (WIDTH as isize / 2)) as usize;
let y = ((raw_y / 5f64) as isize + (HEIGHT as isize / 2)) as usize;

if x < WIDTH && y < HEIGHT {
let pos = (y * WIDTH + x) * 3;
let pixel: [f32; 3] = low.mix(high, (sample.intensity as f32) / 64.0).into();
pixel_data[pos] = (pixel[2] * 255f32) as u8;
pixel_data[pos + 1] = (pixel[1] * 255f32) as u8;
pixel_data[pos + 2] = (pixel[0] * 255f32) as u8;
}

if i % 100 == 0 {
// display
let image = ImageView::new(ImageInfo::bgr8(WIDTH as u32, HEIGHT as u32), pixel_data.as_ref());
window.set_image(format!("image-{}", i), image).unwrap();
}
}

println!("\nStopping scan...");
lidar.stop(false);
lidar.join();

for event in window.event_channel().unwrap() {
if let event::WindowEvent::KeyboardInput(event) = event {
if event.input.key_code == Some(event::VirtualKeyCode::Escape) && event.input.state.is_pressed() {
break;
}
}
}
});
live::live_view(lidar, args.points)
}

0 comments on commit 170a146

Please sign in to comment.