pros_simulator_interface/
lib.rs

1use serde::{Deserialize, Serialize};
2
3pub const LCD_HEIGHT: u32 = 8;
4pub const LCD_WIDTH: u32 = 40;
5pub type LcdLines = [String; LCD_HEIGHT as usize];
6
7#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
8pub struct DigitalControllerState {
9    pub l1: bool,
10    pub l2: bool,
11    pub r1: bool,
12    pub r2: bool,
13    pub up: bool,
14    pub down: bool,
15    pub left: bool,
16    pub right: bool,
17    pub x: bool,
18    pub b: bool,
19    pub y: bool,
20    pub a: bool,
21}
22
23#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
24pub struct AnalogControllerState {
25    pub left_x: i8,
26    pub left_y: i8,
27    pub right_x: i8,
28    pub right_y: i8,
29}
30
31#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
32pub struct ControllerState {
33    pub digital: DigitalControllerState,
34    pub analog: AnalogControllerState,
35}
36
37#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default)]
38pub struct CompetitionPhase {
39    pub autonomous: bool,
40    pub enabled: bool,
41    pub is_competition: bool,
42}
43
44/// An event that happens inside the simulator that the API consumer might want to know about.
45/// Use this to monitor robot code progress, simulated LCD updates, log messages, and more.
46#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
47pub enum SimulatorEvent {
48    /// A warning message has been emitted by the simulator backend. The robot code is likely using the PROS API incorrectly.
49    Warning(String),
50    /// The robot code has written the following text to the simulated serial port. A trailing newline should not be assumed.
51    ConsoleMessage(String),
52
53    /// The robot code is being loaded into the simulator and compiled.
54    RobotCodeLoading,
55    /// The robot code has begun executing and the initialize/opcontrol task is about to be spawned.
56    RobotCodeStarting,
57    /// All tasks have finished executing.
58    RobotCodeFinished,
59    /// The robot code has panicked or otherwise faulted.
60    RobotCodeError { message: String, backtrace: String },
61
62    /// The LCD has been initialized and may be updated in the future.
63    LcdInitialized,
64    /// The LCD has been updated and should be redrawn.
65    LcdUpdated(LcdLines),
66    /// The robot code has requested that the LCD color change to the provided foreground/background (RGBA).
67    LcdColorsUpdated { foreground: u32, background: u32 },
68    /// The LCD has shut down and should be blanked.
69    LcdShutdown,
70}
71
72/// A message sent to the simulator to control the robot code environment.
73/// The `pros-simulator` API accepts these over an async stream, and API consumers can use
74/// them to simulate changes in robot hardware (like controller input and LCD touch events).
75#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
76pub enum SimulatorMessage {
77    /// Master and Partner controllers have updated (in that order). None = disconnected.
78    ControllerUpdate(Option<ControllerState>, Option<ControllerState>),
79
80    /// An LCD button has been pressed/released. The 3 booleans represent
81    /// whether each button is being pressed, from left to right. This API technically supports
82    /// pressing multiple buttons at once, but that won't ever happen on a real robot.
83    LcdButtonsUpdate([bool; 3]), // {"LcdButtonsUpdate": [true, false, false]}
84    /// The robot has switched competition modes (opcontrol or autonomous or disabled).
85    PhaseChange(CompetitionPhase),
86}