Skip to content

Commit

Permalink
Add preview to configurator
Browse files Browse the repository at this point in the history
Fixes #210
  • Loading branch information
krzmaz authored and mrozycki committed Dec 12, 2023
1 parent 408740c commit fa82074
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 4 additions & 4 deletions configurator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ the four sides, or you can rotate your tree so that you will be able to capture
Once you are ready, run:

```
cargo run --bin rustmas-configurator -- capture -n <count> -l <pico_url> -c <camera_url> -s
cargo run [--release] --bin rustmas-configurator -- capture -n <count> -l <pico_url> -c <camera_url> -s
```

where `<count>` is the number of lights, `<pico_url>` is the URL of the pico-w-neopixel-webserver
endpoint (remember the `/pixels` at the end!) and `<camera_url>` is the URL of the IP camera
endpoint (remember the `/pixels` at the end if using the [legacy](https://github.com/krzmaz/pico-w-neopixel-server-cpp) driver!) and `<camera_url>` is the URL of the IP camera
as described above (or skip if using local camera).

Before each direction is captured, the configurator will turn on all the lights to make it easier
Expand All @@ -81,7 +81,7 @@ capturing the sides that have been successful by providing paths to the CSV file
e.g. to skip front and left:

```
cargo run --bin rustmas-configurator -- capture -n <count> -l <pico_url> -c <camera> -s \
cargo run [--release] --bin rustmas-configurator -- capture -n <count> -l <pico_url> -c <camera> -s \
--front captures/2022-12-05T19:23:07/front.csv --left captures/2022-12-05T19:29:12/left.csv
```

Expand All @@ -97,7 +97,7 @@ the `Testing: Manual Sweep` animation to find the alignment, and then run the `c
command on your lights CSV file:

```
cargo run --bin rustmas-configurator -- center -x 0.08 -z -0.12
cargo run [--release] --bin rustmas-configurator -- center -x 0.08 -z -0.12
```

The coordinates passed as arguments are where the center of the tree is currently located in the
Expand Down
27 changes: 25 additions & 2 deletions configurator/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use nalgebra::Vector3;
use rustmas_light_client as client;
use serde::{Deserialize, Serialize};

use crate::cv;
use crate::cv::{self, Display};

type Point2 = (f64, f64);

Expand Down Expand Up @@ -79,6 +79,29 @@ impl Capturer {
lightfx::Frame::new_black(self.number_of_lights).with_pixel(index, lightfx::Color::white())
}

fn preview(&mut self) -> Result<(), Box<dyn Error>> {
let display = Display::new("Preview")?;
print!("Press return to continue...");
io::stdout().flush().expect("Can't flush to stdout");
let handler = thread::spawn(|| {
let mut stdin = io::stdin();
stdin.read_exact(&mut [0u8]).expect("Can't read from stdin");
});

loop {
if handler.is_finished() {
break;
}
display.show(&self.camera.capture()?)?;
let key = display.wait_for(Duration::from_millis(10))?;
if key > 0 && key != 255 {
break;
}
}
thread::sleep(Duration::from_millis(100));
Ok(())
}

pub fn read_coordinates_from_file(
path: &str,
) -> Result<Vec<WithConfidence<Point2>>, Box<dyn Error>> {
Expand Down Expand Up @@ -213,7 +236,7 @@ impl Capturer {
.display_frame(&self.all_lights_on())
.await?;
println!("{}", prompt);
pause();
self.preview()?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion configurator/src/cv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ pub struct Display {
window_name: String,
}

#[allow(unused)]
impl Display {
pub fn new(window_name: &str) -> Result<Self, Box<dyn Error>> {
highgui::named_window(window_name, highgui::WINDOW_AUTOSIZE)?;
Expand All @@ -146,6 +145,8 @@ impl Display {
impl Drop for Display {
fn drop(&mut self) {
highgui::destroy_window(&self.window_name).unwrap();
// hack to get the window to close, see https://stackoverflow.com/a/50710994
highgui::wait_key(1).unwrap();
}
}

Expand Down

0 comments on commit fa82074

Please sign in to comment.