Skip to content

Commit

Permalink
Replace spinlock with thread::sleep; print timings
Browse files Browse the repository at this point in the history
  • Loading branch information
Austen Adler committed Jul 18, 2021
1 parent 784e4e6 commit f69279b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion examples/src/bin/moving-pixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! with DIN-Pin. You just need DIN pin, no clock. WS2818 uses one-wire-protocol.
//! See the specification for details
use std::ops::Add;
use std::time::{Duration, Instant};
use std::thread;
use ws2818_examples::{get_led_num_from_args, sleep_busy_waiting_ms};
use ws2818_rgb_led_spi_driver::adapter_gen::WS28xxAdapter;
use ws2818_rgb_led_spi_driver::adapter_spi::WS28xxSpiAdapter;
Expand Down Expand Up @@ -31,6 +34,28 @@ fn main() {
adapter.write_encoded_rgb(&data).unwrap();

i = (i + 1) % num_leds;
sleep_busy_waiting_ms(1000 / 10); // 100ms / 10Hz
let ms = 1000 / 10; // 100ms / 10Hz
let before = Instant::now(); // For printing time this takes

// Using thread::sleep() - DOES NOT WORK - lights turn solid white
thread::sleep(Duration::from_millis(ms));

// Original code - WORKS FINE
/*
let target_time = Instant::now().add(Duration::from_millis(ms));
loop {
if Instant::now() >= target_time {
break;
}
}
*/

let after = Instant::now();

// With original code:
// 100.0010 to 100.0025 (about 1 to 2.5 microseconds extra)
// With thread::sleep
// 100.09 to 100.1 (about 90-100 microseconds extra)
println!("{:?}", after - before);

This comment has been minimized.

Copy link
@stonewareslord

stonewareslord Jul 18, 2021

Owner

Shows total time the 100ms wait should take

}
}

0 comments on commit f69279b

Please sign in to comment.