Graphics/
Area.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::{Point_type, LVGL};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Area_type(Point_type, Point_type);

impl Area_type {
    pub fn New(Point_1: Point_type, Point_2: Point_type) -> Self {
        Self(Point_1, Point_2)
    }

    pub fn Get_point_1(&self) -> Point_type {
        self.0
    }

    pub fn Get_point_2(&self) -> Point_type {
        self.1
    }

    pub fn Get_width(&self) -> u16 {
        (self.1.Get_x() - self.0.Get_x()).unsigned_abs() + 1
    }

    pub fn Get_height(&self) -> u16 {
        (self.1.Get_y() - self.0.Get_y()).unsigned_abs() + 1
    }

    pub fn Set_point_1(mut self, Value: Point_type) -> Self {
        self.0 = Value;
        self
    }

    pub fn Set_point_2(mut self, Value: Point_type) -> Self {
        self.1 = Value;
        self
    }
}

impl From<LVGL::lv_area_t> for Area_type {
    fn from(Value: LVGL::lv_area_t) -> Self {
        Self::New(
            Point_type::New(Value.x1 as i16, Value.y1 as i16),
            Point_type::New(Value.x2 as i16, Value.y2 as i16),
        )
    }
}

impl AsRef<[u8]> for Area_type {
    fn as_ref(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of::<Self>())
        }
    }
}

impl AsRef<Area_type> for [u8; std::mem::size_of::<Area_type>()] {
    fn as_ref(&self) -> &Area_type {
        unsafe { &*(self as *const _ as *const Area_type) }
    }
}