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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::prelude::Pstring;
use core::ffi::c_int;

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum Base {
    Bin = 2,
    Oct = 8,
    Dec = 10,
    Hex = 16,
}

pub trait Printable
where
    Self: Sized,
{
    type Parameters;

    fn print_2(self, params: Self::Parameters);
    fn default_parameters() -> Self::Parameters;

    fn print(self) {
        self.print_2(Self::default_parameters());
    }
}

impl Printable for i16 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Printable for u16 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_unsigned_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Printable for i32 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Printable for u32 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_unsigned_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Printable for &[u8] {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_chars(self as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Printable for &str {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}
impl<const N: usize> Printable for crate::heapless::String<N> {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Printable for Pstring {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            crate::library::arduboy2::print_chars_progmem(self.pointer);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}