added bomb blink
This commit is contained in:
parent
08b0b655ed
commit
539e8ff21d
43
src/led.rs
43
src/led.rs
@ -19,6 +19,10 @@ const FLASHED_X2: f64 = 0.00003113;
|
|||||||
const FLASHED_X1: f64 = -0.17396053;
|
const FLASHED_X1: f64 = -0.17396053;
|
||||||
const FLASHED_X0: f64 = 244.51964267;
|
const FLASHED_X0: f64 = 244.51964267;
|
||||||
|
|
||||||
|
const BOMB_MAX_TIME: u64 = 40_000;
|
||||||
|
const BOMB_BLINK_DURATION: u64 = 300;
|
||||||
|
const BOMB_BLINK_SPEED: u64 = 700;
|
||||||
|
|
||||||
fn interpolate_flashed(time: u64) -> u8 {
|
fn interpolate_flashed(time: u64) -> u8 {
|
||||||
if time == 0 {
|
if time == 0 {
|
||||||
return 0;
|
return 0;
|
||||||
@ -33,7 +37,7 @@ fn resolve_time_from_flashed(flashed: u8) -> u64 {
|
|||||||
let discriminant = FLASHED_X1.powi(2) - 4.0 * FLASHED_X2 * (FLASHED_X0 - flashed as f64);
|
let discriminant = FLASHED_X1.powi(2) - 4.0 * FLASHED_X2 * (FLASHED_X0 - flashed as f64);
|
||||||
|
|
||||||
let x = (-FLASHED_X1 - discriminant.sqrt()) / (2.0 * FLASHED_X2);
|
let x = (-FLASHED_X1 - discriminant.sqrt()) / (2.0 * FLASHED_X2);
|
||||||
return x as u64;
|
return x.min(5000.0) as u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
|
pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
|
||||||
@ -44,6 +48,9 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
|
|||||||
let mut interpolated_flashed: u8;
|
let mut interpolated_flashed: u8;
|
||||||
let mut time_since_last_flashed: u64 = 0;
|
let mut time_since_last_flashed: u64 = 0;
|
||||||
|
|
||||||
|
let mut last_up_planted: bool = false;
|
||||||
|
let mut time_bomb_planted: u64 = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
thread::sleep(std::time::Duration::from_millis(sleep_time));
|
thread::sleep(std::time::Duration::from_millis(sleep_time));
|
||||||
|
|
||||||
@ -75,10 +82,6 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
|
|||||||
last_up_flashed = upstream_flashed;
|
last_up_flashed = upstream_flashed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if upstream_flashed > 0 || interpolated_flashed > 0 {
|
|
||||||
println!("{},{}", upstream_flashed, interpolated_flashed);
|
|
||||||
}
|
|
||||||
|
|
||||||
let pack = build_led_package(
|
let pack = build_led_package(
|
||||||
len,
|
len,
|
||||||
(
|
(
|
||||||
@ -90,11 +93,31 @@ pub fn led_loop(fps: u32, addr: &str, len: u32, state: State) {
|
|||||||
socket.send_to(&pack, addr).unwrap();
|
socket.send_to(&pack, addr).unwrap();
|
||||||
|
|
||||||
// let planted = state.bomb_planted.load(Ordering::Relaxed);
|
// let planted = state.bomb_planted.load(Ordering::Relaxed);
|
||||||
|
let planted = true;
|
||||||
|
|
||||||
// if planted {
|
|
||||||
// let packet = build_led_package(len, (255, 0, 0));
|
if planted != last_up_planted {
|
||||||
// socket.send_to(&packet, addr).unwrap();
|
// A bomb has been planted or defused reset the timer
|
||||||
// continue;
|
time_bomb_planted = 0;
|
||||||
// }
|
last_up_planted = planted;
|
||||||
|
}
|
||||||
|
|
||||||
|
if planted {
|
||||||
|
time_bomb_planted += sleep_time;
|
||||||
|
|
||||||
|
println!("Time: {}", time_bomb_planted);
|
||||||
|
|
||||||
|
let should_blink = (time_bomb_planted) % (BOMB_BLINK_SPEED * (BOMB_MAX_TIME / time_bomb_planted).max(1)) < BOMB_BLINK_DURATION;
|
||||||
|
|
||||||
|
let color = if should_blink {
|
||||||
|
(255, 0, 0)
|
||||||
|
} else {
|
||||||
|
(0, 0, 0)
|
||||||
|
};
|
||||||
|
|
||||||
|
let pack = build_led_package(len, color);
|
||||||
|
socket.send_to(&pack, addr).unwrap();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user