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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Telemetry data coming from a car
//!
//! The F1 games publish telemetry data about each car at a configurable rate. The telemetry data
//! includes physical properties of the car, e.g. its speed, but also information about the controls
//! that are applied, e.g. which buttons are being pressed.

use crate::packet::header::Header;
use crate::types::CornerProperty;
use bitflags::bitflags;
use derive_new::new;
use getset::{CopyGetters, Getters};

bitflags! {
    /// A bit field with currently pressed buttons.
    ///
    /// The F1 games publish which buttons are currently being pressed by the user. This information
    /// is encoded in a bit field, where each bit represents a different button.
    pub struct Button: u32 {
        const NONE = 0x0;
        const CROSS_OR_A = 0x0001;
        const TRIANGLE_OR_Y = 0x0002;
        const CIRCLE_OR_B = 0x0004;
        const SQUARE_OR_X = 0x0008;
        const DPAD_LEFT = 0x0010;
        const DPAD_RIGHT = 0x0020;
        const DPAD_UP = 0x0040;
        const DPAD_DOWN = 0x0080;
        const OPTIONS_OR_MENU = 0x0100;
        const L1_OR_LB = 0x0200;
        const R1_OR_RB = 0x0400;
        const L2_OR_LT = 0x0800;
        const R2_OR_RT = 0x1000;
        const LEFT_STICK_CLICK = 0x2000;
        const RIGHT_STICK_CLICK =0x4000;
    }
}

impl Default for Button {
    fn default() -> Self {
        Button::NONE
    }
}

/// Gears of a Formula One car
#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
pub enum Gear {
    Reverse = -1,
    Neutral = 0,
    First = 1,
    Second = 2,
    Third = 3,
    Fourth = 4,
    Fifth = 5,
    Sixth = 6,
    Seventh = 7,
    Eighth = 8,
}

impl Default for Gear {
    fn default() -> Self {
        Gear::Neutral
    }
}

/// Surfaces that a tyre can come in contact with in the F1 games
#[derive(Debug, PartialEq, Copy, Clone, Eq, Ord, PartialOrd, Hash)]
pub enum Surface {
    Tarmac = 0,
    RumbleStrip = 1,
    Concrete = 2,
    Rock = 3,
    Gravel = 4,
    Mud = 5,
    Sand = 6,
    Grass = 7,
    Water = 8,
    Cobblestone = 9,
    Metal = 10,
    Ridged = 11,
}

impl Default for Surface {
    fn default() -> Self {
        Surface::Tarmac
    }
}

/// Telemetry data coming from a car
///
/// The telemetry data provided from the F1 games contains detailed, and quickly changing data on
/// the inner mechanics of each car, e.g. its speed, engine RPMs, and temperatures.
#[derive(new, Debug, CopyGetters, Getters, PartialEq, Copy, Clone, PartialOrd, Default)]
#[allow(clippy::too_many_arguments)]
pub struct Telemetry {
    /// Returns the speed of the car in kilometers per hour.
    #[getset(get_copy = "pub")]
    speed: u16,

    /// Returns the ratio of the applied throttle.
    #[getset(get_copy = "pub")]
    throttle: f32,

    /// Returns the ratio of steering input.
    ///
    /// The values range from -1.0 for a full lock left to 1.0 for a full lock right.
    #[getset(get_copy = "pub")]
    steering: f32,

    /// Returns the ratio of brake applied.
    #[getset(get_copy = "pub")]
    brake: f32,

    /// Returns the percentage that the clutch has been applied.
    #[getset(get_copy = "pub")]
    clutch: u8,

    /// Returns the gear the car is in.
    #[getset(get_copy = "pub")]
    gear: Gear,

    /// Returns the engine RPM.
    #[getset(get_copy = "pub")]
    engine_rpm: u16,

    /// Returns whether the DRS is deployed.
    #[getset(get_copy = "pub")]
    drs: bool,

    /// Returns the percentage of how far the rev lights indicator is engaged.
    #[getset(get_copy = "pub")]
    rev_lights: u8,

    /// Returns the brake temperature at each corner of the in degrees celsius.
    #[getset(get = "pub")]
    brake_temperature: CornerProperty<u16>,

    /// Returns the tyre surface temperature at each corner of the car in degrees celsius.
    #[getset(get = "pub")]
    tyre_surface_temperature: CornerProperty<u16>,

    /// Returns the tyre inner temperature at each corner of the car in degrees celsius.
    #[getset(get = "pub")]
    tyre_inner_temperature: CornerProperty<u16>,

    /// Returns the engine temperature in degrees celsius.
    #[getset(get_copy = "pub")]
    engine_temperature: u16,

    /// Returns the tyre pressure at each corner of the car in psi.
    #[getset(get = "pub")]
    tyre_pressure: CornerProperty<f32>,

    /// Returns the type of the surface each tyre fo the car has contact with.
    #[getset(get = "pub")]
    surface_type: CornerProperty<Surface>,
}

/// Packet containing the telemetry of all cars in the session
///
/// The F1 games publish telemetry data for each car in the session. The telemetry data includes
/// parameters such as the car's speed, as well as information in controller inputs from the user.
#[derive(new, Debug, CopyGetters, Getters, PartialEq, Clone, PartialOrd)]
pub struct TelemetryPacket {
    /// Returns the packet header prefixing the telemetry packet.
    #[getset(get = "pub")]
    header: Header,

    /// Returns the telemetry data for each car in the session.
    #[getset(get = "pub")]
    telemetry: Vec<Telemetry>,

    /// Returns a bit flag indicating which buttons are currently pressed.
    #[getset(get_copy = "pub")]
    button_status: Button,
}