2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-12-02 23:27:33 -08:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/SimDevice.h>
|
|
|
|
|
#include <hal/simulation/SimDeviceData.h>
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace frc::sim {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to control the simulation side of a SimDevice.
|
|
|
|
|
*/
|
2019-12-02 23:27:33 -08:00
|
|
|
class SimDeviceSim {
|
|
|
|
|
public:
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs a SimDeviceSim.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the SimDevice
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
explicit SimDeviceSim(const char* name);
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-02-13 08:17:13 +02:00
|
|
|
/**
|
|
|
|
|
* Constructs a SimDeviceSim.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the SimDevice
|
|
|
|
|
* @param index device index number to append to name
|
|
|
|
|
*/
|
|
|
|
|
SimDeviceSim(const char* name, int index);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a SimDeviceSim.
|
|
|
|
|
*
|
|
|
|
|
* @param name name of the SimDevice
|
|
|
|
|
* @param index device index number to append to name
|
|
|
|
|
* @param channel device channel number to append to name
|
|
|
|
|
*/
|
|
|
|
|
SimDeviceSim(const char* name, int index, int channel);
|
|
|
|
|
|
2023-01-11 14:45:15 -05:00
|
|
|
/**
|
|
|
|
|
* Constructs a SimDeviceSim.
|
|
|
|
|
*
|
|
|
|
|
* @param handle the low level handle for the corresponding SimDevice.
|
|
|
|
|
*/
|
|
|
|
|
explicit SimDeviceSim(HAL_SimDeviceHandle handle);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the name of this object.
|
|
|
|
|
*
|
|
|
|
|
* @return name
|
|
|
|
|
*/
|
|
|
|
|
std::string GetName() const;
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
hal::SimValue GetValue(const char* name) const;
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
|
|
|
|
hal::SimInt GetInt(const char* name) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
|
|
|
|
hal::SimLong GetLong(const char* name) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
hal::SimDouble GetDouble(const char* name) const;
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
hal::SimEnum GetEnum(const char* name) const;
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the property object with the given name.
|
|
|
|
|
*
|
|
|
|
|
* @param name the property name
|
|
|
|
|
* @return the property object
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
hal::SimBoolean GetBoolean(const char* name) const;
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get all options for the given enum.
|
|
|
|
|
*
|
|
|
|
|
* @param val the enum
|
|
|
|
|
* @return names of the different values for that enum
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static std::vector<std::string> GetEnumOptions(hal::SimEnum val);
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get all properties.
|
|
|
|
|
*
|
|
|
|
|
* @param callback callback called for each property (SimValue). Signature
|
|
|
|
|
* of the callback must be const char*, HAL_SimValueHandle,
|
|
|
|
|
* int, const HAL_Value*
|
|
|
|
|
*/
|
2019-12-02 23:27:33 -08:00
|
|
|
template <typename F>
|
|
|
|
|
void EnumerateValues(F callback) const {
|
|
|
|
|
return HALSIM_EnumerateSimValues(
|
|
|
|
|
m_handle, &callback,
|
|
|
|
|
[](const char* name, void* param, HAL_SimValueHandle handle,
|
2021-01-11 21:55:45 -08:00
|
|
|
int direction, const struct HAL_Value* value) {
|
|
|
|
|
std::invoke(*static_cast<F*>(param), name, handle, direction, value);
|
2019-12-02 23:27:33 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the raw handle of this object.
|
|
|
|
|
*
|
|
|
|
|
* @return the handle used to refer to this object
|
|
|
|
|
*/
|
2020-12-26 22:06:28 -08:00
|
|
|
operator HAL_SimDeviceHandle() const { return m_handle; } // NOLINT
|
2019-12-02 23:27:33 -08:00
|
|
|
|
2021-01-12 00:38:58 -08:00
|
|
|
/**
|
|
|
|
|
* Get all sim devices with the given prefix.
|
|
|
|
|
*
|
|
|
|
|
* @param prefix the prefix to filter sim devices
|
|
|
|
|
* @param callback callback function to call for each sim device
|
|
|
|
|
*/
|
2019-12-02 23:27:33 -08:00
|
|
|
template <typename F>
|
|
|
|
|
static void EnumerateDevices(const char* prefix, F callback) {
|
|
|
|
|
return HALSIM_EnumerateSimDevices(
|
|
|
|
|
prefix, &callback,
|
|
|
|
|
[](const char* name, void* param, HAL_SimDeviceHandle handle) {
|
|
|
|
|
std::invoke(*static_cast<F*>(param), name, handle);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Reset all SimDevice data.
|
|
|
|
|
*/
|
2020-07-15 23:48:09 -07:00
|
|
|
static void ResetData();
|
2019-12-02 23:27:33 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
HAL_SimDeviceHandle m_handle;
|
|
|
|
|
};
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace frc::sim
|