#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LaserPoint {
pub x: f32,
pub y: f32,
pub r: u16,
pub g: u16,
pub b: u16,
pub intensity: u16,
}
impl LaserPoint {
pub fn new(x: f32, y: f32, r: u16, g: u16, b: u16, intensity: u16) -> Self {
Self {
x,
y,
r,
g,
b,
intensity,
}
}
pub fn blanked(x: f32, y: f32) -> Self {
Self {
x,
y,
..Default::default()
}
}
#[cfg(any(feature = "helios", feature = "lasercube-wifi"))]
#[inline]
pub(crate) fn coord_to_u12_inverted(v: f32) -> u16 {
((1.0 - (v + 1.0) / 2.0).clamp(0.0, 1.0) * 4095.0).round() as u16
}
#[cfg(any(feature = "lasercube-usb", feature = "lasercube-wifi"))]
#[inline]
pub(crate) fn coord_to_u12(v: f32) -> u16 {
(((v.clamp(-1.0, 1.0) + 1.0) / 2.0) * 4095.0).round() as u16
}
#[inline]
pub(crate) fn coord_to_i16_inverted(v: f32) -> i16 {
(v.clamp(-1.0, 1.0) * -32767.0).round() as i16
}
#[inline]
pub(crate) fn color_to_u8(v: u16) -> u8 {
(v >> 8) as u8
}
#[cfg(feature = "lasercube-wifi")]
#[inline]
pub(crate) fn color_to_u12(v: u16) -> u16 {
v >> 4
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_laser_point_blanked_sets_all_colors_to_zero() {
let point = LaserPoint::blanked(0.25, 0.75);
assert_eq!(point.x, 0.25);
assert_eq!(point.y, 0.75);
assert_eq!(point.r, 0);
assert_eq!(point.g, 0);
assert_eq!(point.b, 0);
assert_eq!(point.intensity, 0);
}
}