[hal,wpilib] Add DS Display API (#8975)

This commit is contained in:
Thad House
2026-06-20 10:28:24 -07:00
committed by GitHub
parent 481a586009
commit f1c9d82d50
35 changed files with 889 additions and 11 deletions

View File

@@ -0,0 +1,18 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "Robot.hpp"
#include "opmode/DefaultTeleop.hpp"
Robot::Robot() {
AddOpMode<DefaultTeleop>(wpi::RobotMode::TELEOPERATED, "Default Teleop");
PublishOpModes();
}
#ifndef RUNNING_WPILIB_TESTS
int main() {
return wpi::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,47 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "opmode/DefaultTeleop.hpp"
#include <array>
#include <cstdio>
#include "wpi/driverstation/DriverStationDisplay.hpp"
using namespace wpi::units::literals;
namespace {
constexpr std::array<int, 20> COLORS = {196, 202, 208, 214, 220, 226, 118,
46, 48, 51, 45, 39, 33, 27,
21, 57, 93, 129, 165, 201};
} // namespace
void DefaultTeleop::Start() {
wpi::DriverStationDisplay::SetMode(wpi::DriverStationDisplay::Mode::RawAnsi);
m_timer.Restart();
m_seconds = 0;
wpi::DriverStationDisplay::WriteRawAnsi(
"\033[2J\033[H"
"DriverStationDisplay ANSI mode\n"
"This header and footer stay on screen.\n"
"Seconds elapsed: \n"
"Only the number above is rewritten.");
UpdateSecondsDisplay();
}
void DefaultTeleop::Periodic() {
if (m_timer.AdvanceIfElapsed(1_s)) {
++m_seconds;
UpdateSecondsDisplay();
}
}
void DefaultTeleop::UpdateSecondsDisplay() {
int color = COLORS[m_seconds % COLORS.size()];
std::array<char, 64> buffer;
std::snprintf(buffer.data(), buffer.size(),
"\033[3;18H\033[38;5;%dm%5d\033[0m", color, m_seconds);
wpi::DriverStationDisplay::WriteRawAnsi(buffer.data());
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include "wpi/framework/OpModeRobot.hpp"
class Robot : public wpi::OpModeRobot<Robot> {
public:
Robot();
};

View File

@@ -0,0 +1,20 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include "wpi/opmode/PeriodicOpMode.hpp"
#include "wpi/system/Timer.hpp"
class DefaultTeleop : public wpi::PeriodicOpMode {
public:
void Start() override;
void Periodic() override;
private:
void UpdateSecondsDisplay();
wpi::Timer m_timer;
int m_seconds = 0;
};

View File

@@ -0,0 +1,18 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "Robot.hpp"
#include "opmode/DefaultTeleop.hpp"
Robot::Robot() {
AddOpMode<DefaultTeleop>(wpi::RobotMode::TELEOPERATED, "Default Teleop");
PublishOpModes();
}
#ifndef RUNNING_WPILIB_TESTS
int main() {
return wpi::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,29 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "opmode/DefaultTeleop.hpp"
#include <iomanip>
#include <sstream>
#include <string>
#include "wpi/driverstation/DriverStationDisplay.hpp"
void DefaultTeleop::Start() {
wpi::DriverStationDisplay::SetMode(wpi::DriverStationDisplay::Mode::Line);
m_timer.Restart();
m_loopCount = 0;
}
void DefaultTeleop::Periodic() {
std::ostringstream runtime;
runtime << std::fixed << std::setprecision(1) << m_timer.Get().value()
<< " seconds";
wpi::DriverStationDisplay::AddLine("DriverStationDisplay line mode");
wpi::DriverStationDisplay::AddData("Runtime", runtime.str());
wpi::DriverStationDisplay::AddData("Loop Count",
std::to_string(m_loopCount++));
wpi::DriverStationDisplay::UpdateLines();
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include "wpi/framework/OpModeRobot.hpp"
class Robot : public wpi::OpModeRobot<Robot> {
public:
Robot();
};

View File

@@ -0,0 +1,18 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include "wpi/opmode/PeriodicOpMode.hpp"
#include "wpi/system/Timer.hpp"
class DefaultTeleop : public wpi::PeriodicOpMode {
public:
void Start() override;
void Periodic() override;
private:
wpi::Timer m_timer;
int m_loopCount = 0;
};

View File

@@ -250,6 +250,28 @@
"gradlebase": "cpp",
"commandversion": 2
},
{
"name": "Driver Station Display Lines",
"description": "Display lines on the Driver Station display and update them every robot loop.",
"tags": [
"Basic Robot",
"Driver Station"
],
"foldername": "DriverStationDisplayLines",
"gradlebase": "cpp",
"commandversion": 2
},
{
"name": "Driver Station Display ANSI",
"description": "Stream ANSI text to the Driver Station display and update a value in place once per second.",
"tags": [
"Basic Robot",
"Driver Station"
],
"foldername": "DriverStationDisplayAnsi",
"gradlebase": "cpp",
"commandversion": 2
},
{
"name": "DriveDistanceOffboard",
"description": "Drive a differential drivetrain a set distance using TrapezoidProfile and smart motor controller PID.",