vexide_core/
os.rs

1//! VEXos-related functionality.
2//!
3//! This module provides utilities for for interacting and retrieving information from VEXos.
4
5use core::fmt;
6
7use vex_sdk::vexSystemVersion;
8
9/// A VEXos firmware version.
10///
11/// This type represents a version identifier for VEXos firmware. VEXos is versioned using a
12/// slightly modified [semantic versioning] scheme. To check the version currently running on a
13/// brain, use [`system_version`].
14///
15/// [semantic versioning]: https://semver.org/
16///
17/// This type implements `PartialOrd`, meaning it can be compared to other instances of itself.
18///
19/// # Example
20///
21/// ```
22/// use vexide::os::{Version, system_version};
23///
24/// // VEXos 1.1.5b0
25/// const VEXOS_1_1_5_0: Version = Version {
26///     major: 1,
27///     minor: 1,
28///     build: 5,
29///     beta: 0,
30/// };
31///
32/// // Get the currently running VEXos version
33/// let version = system_version();
34///
35/// if version < VEXOS_1_1_5_0 {
36///     println!("Update your firmware!");
37/// }
38/// ```
39#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
40pub struct Version {
41    /// The major version
42    pub major: u8,
43    /// The minor version
44    pub minor: u8,
45    /// The build version
46    pub build: u8,
47    /// The beta version
48    pub beta: u8,
49}
50
51impl fmt::Display for Version {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        write!(
54            f,
55            "{}.{}.{}-b{}",
56            self.major, self.minor, self.build, self.beta
57        )
58    }
59}
60
61/// Returns the currently running VEXos [firmware version] on a brain.
62///
63/// [firmware version]: Version
64///
65/// # Example
66///
67/// ```
68/// use vexide::os::{Version, system_version};
69///
70/// // VEXos 1.1.5b0
71/// const VEXOS_1_1_5_0: Version = Version {
72///     major: 1,
73///     minor: 1,
74///     build: 5,
75///     beta: 0,
76/// };
77///
78/// // Get the currently running VEXos version
79/// let version = system_version();
80///
81/// if version < VEXOS_1_1_5_0 {
82///     println!("Update your firmware!");
83/// }
84/// ```
85#[must_use]
86pub fn system_version() -> Version {
87    let version_bytes = unsafe { vexSystemVersion() }.to_be_bytes();
88
89    Version {
90        major: version_bytes[0],
91        minor: version_bytes[1],
92        build: version_bytes[2],
93        beta: version_bytes[3],
94    }
95}