vexide_core/
os.rs

1//! VEXos-related functionality.
2//!
3//! This module provides utilities for for interacting and retrieving
4//! information from VEXos.
5
6use core::fmt;
7
8use vex_sdk::vexSystemVersion;
9
10/// A VEXos firmware version.
11///
12/// This type represents a version identifier for VEXos firmware. VEXos is
13/// versioned using a slightly modified [semantic versioning] scheme. To
14/// check the version currently running on a brain, use [`system_version`].
15///
16/// [semantic versioning]: https://semver.org/
17///
18/// This type implements `PartialOrd`, meaning it can be compared to other
19/// instances of itself.
20///
21/// # Example
22///
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///     panic!("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/// // VEXos 1.1.5b0
69/// const VEXOS_1_1_5_0: Version = Version {
70///     major: 1,
71///     minor: 1,
72///     build: 5,
73///     beta: 0,
74/// };
75///
76/// // Get the currently running VEXos version
77/// let version = system_version();
78///
79/// if version < VEXOS_1_1_5_0 {
80///     panic!("Update your firmware!");
81/// }
82/// ```
83#[must_use]
84pub fn system_version() -> Version {
85    let version_bytes = unsafe { vexSystemVersion() }.to_be_bytes();
86    Version {
87        major: version_bytes[0],
88        minor: version_bytes[1],
89        build: version_bytes[2],
90        beta: version_bytes[3],
91    }
92}