f1_api/packet/
telemetry.rs

1//! Telemetry data coming from a car
2//!
3//! The F1 games publish telemetry data about each car at a configurable rate. The telemetry data
4//! includes physical properties of the car, e.g. its speed, but also information about the controls
5//! that are applied, e.g. which buttons are being pressed.
6
7use bitflags::bitflags;
8use derive_new::new;
9use getset::{CopyGetters, Getters};
10
11use crate::packet::header::Header;
12use crate::types::CornerProperty;
13
14bitflags! {
15    /// A bit field with currently pressed buttons.
16    ///
17    /// The F1 games publish which buttons are currently being pressed by the user. This information
18    /// is encoded in a bit field, where each bit represents a different button.
19    pub struct Button: u32 {
20        const NONE = 0x0;
21        const CROSS_OR_A = 0x0001;
22        const TRIANGLE_OR_Y = 0x0002;
23        const CIRCLE_OR_B = 0x0004;
24        const SQUARE_OR_X = 0x0008;
25        const DPAD_LEFT = 0x0010;
26        const DPAD_RIGHT = 0x0020;
27        const DPAD_UP = 0x0040;
28        const DPAD_DOWN = 0x0080;
29        const OPTIONS_OR_MENU = 0x0100;
30        const L1_OR_LB = 0x0200;
31        const R1_OR_RB = 0x0400;
32        const L2_OR_LT = 0x0800;
33        const R2_OR_RT = 0x1000;
34        const LEFT_STICK_CLICK = 0x2000;
35        const RIGHT_STICK_CLICK =0x4000;
36    }
37}
38
39impl Default for Button {
40    fn default() -> Self {
41        Button::NONE
42    }
43}
44
45/// Gears of a Formula One car
46#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
47pub enum Gear {
48    Reverse = -1,
49    Neutral = 0,
50    First = 1,
51    Second = 2,
52    Third = 3,
53    Fourth = 4,
54    Fifth = 5,
55    Sixth = 6,
56    Seventh = 7,
57    Eighth = 8,
58}
59
60impl Default for Gear {
61    fn default() -> Self {
62        Gear::Neutral
63    }
64}
65
66/// Surfaces that a tyre can come in contact with in the F1 games
67#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
68pub enum Surface {
69    Tarmac = 0,
70    RumbleStrip = 1,
71    Concrete = 2,
72    Rock = 3,
73    Gravel = 4,
74    Mud = 5,
75    Sand = 6,
76    Grass = 7,
77    Water = 8,
78    Cobblestone = 9,
79    Metal = 10,
80    Ridged = 11,
81}
82
83impl Default for Surface {
84    fn default() -> Self {
85        Surface::Tarmac
86    }
87}
88
89/// Telemetry data coming from a car
90///
91/// The telemetry data provided from the F1 games contains detailed, and quickly changing data on
92/// the inner mechanics of each car, e.g. its speed, engine RPMs, and temperatures.
93#[derive(new, Debug, CopyGetters, Getters, PartialEq, Copy, Clone, PartialOrd, Default)]
94#[allow(clippy::too_many_arguments)]
95pub struct Telemetry {
96    /// Returns the speed of the car in kilometers per hour.
97    #[getset(get_copy = "pub")]
98    speed: u16,
99
100    /// Returns the ratio of the applied throttle.
101    #[getset(get_copy = "pub")]
102    throttle: f32,
103
104    /// Returns the ratio of steering input.
105    ///
106    /// The values range from -1.0 for a full lock left to 1.0 for a full lock right.
107    #[getset(get_copy = "pub")]
108    steering: f32,
109
110    /// Returns the ratio of brake applied.
111    #[getset(get_copy = "pub")]
112    brake: f32,
113
114    /// Returns the percentage that the clutch has been applied.
115    #[getset(get_copy = "pub")]
116    clutch: u8,
117
118    /// Returns the gear the car is in.
119    #[getset(get_copy = "pub")]
120    gear: Gear,
121
122    /// Returns the engine RPM.
123    #[getset(get_copy = "pub")]
124    engine_rpm: u16,
125
126    /// Returns whether the DRS is deployed.
127    #[getset(get_copy = "pub")]
128    drs: bool,
129
130    /// Returns the percentage of how far the rev lights indicator is engaged.
131    #[getset(get_copy = "pub")]
132    rev_lights: u8,
133
134    /// Returns the brake temperature at each corner of the in degrees celsius.
135    #[getset(get = "pub")]
136    brake_temperature: CornerProperty<u16>,
137
138    /// Returns the tyre surface temperature at each corner of the car in degrees celsius.
139    #[getset(get = "pub")]
140    tyre_surface_temperature: CornerProperty<u16>,
141
142    /// Returns the tyre inner temperature at each corner of the car in degrees celsius.
143    #[getset(get = "pub")]
144    tyre_inner_temperature: CornerProperty<u16>,
145
146    /// Returns the engine temperature in degrees celsius.
147    #[getset(get_copy = "pub")]
148    engine_temperature: u16,
149
150    /// Returns the tyre pressure at each corner of the car in psi.
151    #[getset(get = "pub")]
152    tyre_pressure: CornerProperty<f32>,
153
154    /// Returns the type of the surface each tyre fo the car has contact with.
155    #[getset(get = "pub")]
156    surface_type: CornerProperty<Surface>,
157}
158
159/// Packet containing the telemetry of all cars in the session
160///
161/// The F1 games publish telemetry data for each car in the session. The telemetry data includes
162/// parameters such as the car's speed, as well as information in controller inputs from the user.
163#[derive(new, Debug, CopyGetters, Getters, PartialEq, Clone, PartialOrd)]
164pub struct TelemetryPacket {
165    /// Returns the packet header prefixing the telemetry packet.
166    #[getset(get = "pub")]
167    header: Header,
168
169    /// Returns the telemetry data for each car in the session.
170    #[getset(get = "pub")]
171    telemetry: Vec<Telemetry>,
172
173    /// Returns a bit flag indicating which buttons are currently pressed.
174    #[getset(get_copy = "pub")]
175    button_status: Button,
176}