mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
This deals with the majority of the user-facing code in wpilibC++Devices and a substantial portion of it in wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests are untouched except where it is necessary to make them work with the rest of the libraries. There is still a lot to do in the following areas: -The HAL (which we may not want to touch at all). -The I2C, Serial, and SPI interfaces in wpilibC++Devices, which I haven't gotten around to doing yet. -Most wpilibC++Devices classes have void* pointers for interacting with the HAL. -InterruptableSensorBase passes a void *params for the interrupt handler. -I haven't converted all the const char* to std::strings. -There are plenty of other cases of raw pointers still existing. -This doesn't fall directly under raw pointer stuff, but move syntax and rvalue references could be introduced in many places. -I haven't touched vision code. -The Resource classes conflict (one is in the hal, the other in wpilibC++). Someone should figure out a more permanent fix (eg, just renaming them), then doing what I did (making a new namespace for one of them, essentially the same as renaming it). A few other things: -I created a NullDeleter class which is marked as deprecated. What this does is it can be passed as the deleter to a std::shared_ptr so that when you are converting raw pointers to shared_ptrs the shared_ptr doesn't do any deletion if someone else owns the raw pointer. This should only be used in making old raw pointer UIs. -I had to alter the build.gradle so that it did not emit errors when deprecated functions called deprecated functions. Unfortunately, gradle doesn't appear to be actually printing out gcc warnigns for some reason. The best way I have found to fix this is to patch the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff) so that a deprecated function calling a deprecated function is fine but a non-deprecated function calling a deprecated function will throw a warning (which we then elevate with -Werror). I believe that clang deals with this properly, although I have not tried it myself. Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) FIRST 2011. All Rights Reserved.
|
|
*/
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#include "Commands/Subsystem.h"
|
|
|
|
#include "Commands/Command.h"
|
|
#include "Commands/Scheduler.h"
|
|
#include "WPIErrors.h"
|
|
|
|
/**
|
|
* Creates a subsystem with the given name
|
|
* @param name the name of the subsystem
|
|
*/
|
|
Subsystem::Subsystem(const char *name) {
|
|
m_name = name;
|
|
Scheduler::GetInstance().RegisterSubsystem(this);
|
|
}
|
|
/**
|
|
* Initialize the default command for this subsystem
|
|
* This is meant to be the place to call SetDefaultCommand in a subsystem and
|
|
* will be called
|
|
* on all the subsystems by the CommandBase method before the program starts
|
|
* running by using
|
|
* the list of all registered Subsystems inside the Scheduler.
|
|
*
|
|
* This should be overridden by a Subsystem that has a default Command
|
|
*/
|
|
void Subsystem::InitDefaultCommand() {}
|
|
|
|
/**
|
|
* Sets the default command. If this is not called or is called with null,
|
|
* then there will be no default command for the subsystem.
|
|
*
|
|
* <p><b>WARNING:</b> This should <b>NOT</b> be called in a constructor if the
|
|
* subsystem is a
|
|
* singleton.</p>
|
|
*
|
|
* @param command the default command (or null if there should be none)
|
|
*/
|
|
void Subsystem::SetDefaultCommand(Command *command) {
|
|
if (command == nullptr) {
|
|
m_defaultCommand = nullptr;
|
|
} else {
|
|
bool found = false;
|
|
Command::SubsystemSet requirements = command->GetRequirements();
|
|
auto iter = requirements.begin();
|
|
for (; iter != requirements.end(); iter++) {
|
|
if (*iter == this) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
wpi_setWPIErrorWithContext(
|
|
CommandIllegalUse, "A default command must require the subsystem");
|
|
return;
|
|
}
|
|
|
|
m_defaultCommand = command;
|
|
}
|
|
if (m_table != nullptr) {
|
|
if (m_defaultCommand != nullptr) {
|
|
m_table->PutBoolean("hasDefault", true);
|
|
m_table->PutString("default", m_defaultCommand->GetName());
|
|
} else {
|
|
m_table->PutBoolean("hasDefault", false);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the default command (or null if there is none).
|
|
* @return the default command
|
|
*/
|
|
Command *Subsystem::GetDefaultCommand() {
|
|
if (!m_initializedDefaultCommand) {
|
|
m_initializedDefaultCommand = true;
|
|
InitDefaultCommand();
|
|
}
|
|
return m_defaultCommand;
|
|
}
|
|
|
|
/**
|
|
* Sets the current command
|
|
* @param command the new current command
|
|
*/
|
|
void Subsystem::SetCurrentCommand(Command *command) {
|
|
m_currentCommand = command;
|
|
m_currentCommandChanged = true;
|
|
}
|
|
|
|
/**
|
|
* Returns the command which currently claims this subsystem.
|
|
* @return the command which currently claims this subsystem
|
|
*/
|
|
Command *Subsystem::GetCurrentCommand() const { return m_currentCommand; }
|
|
|
|
/**
|
|
* Call this to alert Subsystem that the current command is actually the
|
|
* command.
|
|
* Sometimes, the {@link Subsystem} is told that it has no command while the
|
|
* {@link Scheduler}
|
|
* is going through the loop, only to be soon after given a new one. This will
|
|
* avoid that situation.
|
|
*/
|
|
void Subsystem::ConfirmCommand() {
|
|
if (m_currentCommandChanged) {
|
|
if (m_table != nullptr) {
|
|
if (m_currentCommand != nullptr) {
|
|
m_table->PutBoolean("hasCommand", true);
|
|
m_table->PutString("command", m_currentCommand->GetName());
|
|
} else {
|
|
m_table->PutBoolean("hasCommand", false);
|
|
}
|
|
}
|
|
m_currentCommandChanged = false;
|
|
}
|
|
}
|
|
|
|
std::string Subsystem::GetName() { return m_name; }
|
|
|
|
std::string Subsystem::GetSmartDashboardType() const { return "Subsystem"; }
|
|
|
|
void Subsystem::InitTable(::std::shared_ptr<ITable> table) {
|
|
m_table = table;
|
|
if (m_table != nullptr) {
|
|
if (m_defaultCommand != nullptr) {
|
|
m_table->PutBoolean("hasDefault", true);
|
|
m_table->PutString("default", m_defaultCommand->GetName());
|
|
} else {
|
|
m_table->PutBoolean("hasDefault", false);
|
|
}
|
|
if (m_currentCommand != nullptr) {
|
|
m_table->PutBoolean("hasCommand", true);
|
|
m_table->PutString("command", m_currentCommand->GetName());
|
|
} else {
|
|
m_table->PutBoolean("hasCommand", false);
|
|
}
|
|
}
|
|
}
|
|
|
|
::std::shared_ptr<ITable> Subsystem::GetTable() const { return m_table; }
|