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
use core::fmt::Display;
use core::time::Duration;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Status(pub i32);
impl Status {
pub const OK: Status = Status(0);
pub const ERR: Status = Status(-1);
pub const ERR_TIMEOUT: Status = Status(-2);
pub const ERR_RESOURCE: Status = Status(-3);
pub const ERR_PARAMETER: Status = Status(-4);
pub const ERR_NO_MEMORY: Status = Status(-5);
pub const ERR_ISR: Status = Status(-6);
pub fn description(self) -> &'static str {
match self {
Self::OK => "Operation completed successfully",
Self::ERR => "Unspecified RTOS error",
Self::ERR_TIMEOUT => "Operation not completed within the timeout period",
Self::ERR_RESOURCE => "Resource not available",
Self::ERR_PARAMETER => "Parameter error",
Self::ERR_NO_MEMORY => "System is out of memory",
Self::ERR_ISR => "Not allowed in ISR context",
_ => "Unknown",
}
}
pub fn is_ok(self) -> bool {
self == Self::OK
}
pub fn is_err(self) -> bool {
self != Self::OK
}
pub fn err_or<T>(self, ok: T) -> Result<T, Self> {
if self.is_err() {
Err(self)
} else {
Ok(ok)
}
}
pub fn err_or_else<T>(self, or_else: impl Fn(Self) -> T) -> Result<T, Self> {
if self.is_err() {
Err(self)
} else {
Ok(or_else(self))
}
}
}
impl Display for Status {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}: {}", self, self.description())
}
}
impl From<i32> for Status {
fn from(code: i32) -> Self {
Status(code)
}
}
#[inline]
pub fn duration_to_ticks(duration: Duration) -> u32 {
let duration_ms: u32 = duration.as_millis().try_into().unwrap_or(u32::MAX);
unsafe { crate::furi_ms_to_ticks(duration_ms) }
}