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.
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
#include "frc/simulation/RoboRioSim.h"
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include <hal/simulation/RoboRioData.h>
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
using namespace frc::sim;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterFPGAButtonCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioFPGAButtonCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioFPGAButtonCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RoboRioSim::GetFPGAButton() {
|
|
|
|
|
return HALSIM_GetRoboRioFPGAButton();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetFPGAButton(bool fPGAButton) {
|
|
|
|
|
HALSIM_SetRoboRioFPGAButton(fPGAButton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterVInVoltageCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioVInVoltageCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioVInVoltageCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::volt_t RoboRioSim::GetVInVoltage() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::volt_t{HALSIM_GetRoboRioVInVoltage()};
|
2020-11-12 01:33:49 -05:00
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetVInVoltage(units::volt_t vInVoltage) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioVInVoltage(vInVoltage.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterVInCurrentCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioVInCurrentCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioVInCurrentCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::ampere_t RoboRioSim::GetVInCurrent() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::ampere_t{HALSIM_GetRoboRioVInCurrent()};
|
2020-11-12 01:33:49 -05:00
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetVInCurrent(units::ampere_t vInCurrent) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioVInCurrent(vInCurrent.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage6VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserVoltage6VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserVoltage6VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::volt_t RoboRioSim::GetUserVoltage6V() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::volt_t{HALSIM_GetRoboRioUserVoltage6V()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserVoltage6V(units::volt_t userVoltage6V) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserVoltage6V(userVoltage6V.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent6VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserCurrent6VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserCurrent6VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::ampere_t RoboRioSim::GetUserCurrent6V() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::ampere_t{HALSIM_GetRoboRioUserCurrent6V()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserCurrent6V(units::ampere_t userCurrent6V) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserCurrent6V(userCurrent6V.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserActive6VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserActive6VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserActive6VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RoboRioSim::GetUserActive6V() {
|
|
|
|
|
return HALSIM_GetRoboRioUserActive6V();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserActive6V(bool userActive6V) {
|
|
|
|
|
HALSIM_SetRoboRioUserActive6V(userActive6V);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage5VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserVoltage5VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserVoltage5VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::volt_t RoboRioSim::GetUserVoltage5V() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::volt_t{HALSIM_GetRoboRioUserVoltage5V()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserVoltage5V(units::volt_t userVoltage5V) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserVoltage5V(userVoltage5V.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent5VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserCurrent5VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserCurrent5VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::ampere_t RoboRioSim::GetUserCurrent5V() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::ampere_t{HALSIM_GetRoboRioUserCurrent5V()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserCurrent5V(units::ampere_t userCurrent5V) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserCurrent5V(userCurrent5V.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserActive5VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserActive5VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserActive5VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RoboRioSim::GetUserActive5V() {
|
|
|
|
|
return HALSIM_GetRoboRioUserActive5V();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserActive5V(bool userActive5V) {
|
|
|
|
|
HALSIM_SetRoboRioUserActive5V(userActive5V);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserVoltage3V3Callback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserVoltage3V3Callback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserVoltage3V3Callback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::volt_t RoboRioSim::GetUserVoltage3V3() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::volt_t{HALSIM_GetRoboRioUserVoltage3V3()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserVoltage3V3(units::volt_t userVoltage3V3) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserVoltage3V3(userVoltage3V3.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserCurrent3V3Callback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserCurrent3V3Callback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserCurrent3V3Callback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
units::ampere_t RoboRioSim::GetUserCurrent3V3() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::ampere_t{HALSIM_GetRoboRioUserCurrent3V3()};
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:33:49 -05:00
|
|
|
void RoboRioSim::SetUserCurrent3V3(units::ampere_t userCurrent3V3) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioUserCurrent3V3(userCurrent3V3.value());
|
2020-07-15 23:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserActive3V3Callback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserActive3V3Callback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserActive3V3Callback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool RoboRioSim::GetUserActive3V3() {
|
|
|
|
|
return HALSIM_GetRoboRioUserActive3V3();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserActive3V3(bool userActive3V3) {
|
|
|
|
|
HALSIM_SetRoboRioUserActive3V3(userActive3V3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserFaults6VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserFaults6VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserFaults6VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int RoboRioSim::GetUserFaults6V() {
|
|
|
|
|
return HALSIM_GetRoboRioUserFaults6V();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserFaults6V(int userFaults6V) {
|
|
|
|
|
HALSIM_SetRoboRioUserFaults6V(userFaults6V);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserFaults5VCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserFaults5VCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserFaults5VCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int RoboRioSim::GetUserFaults5V() {
|
|
|
|
|
return HALSIM_GetRoboRioUserFaults5V();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserFaults5V(int userFaults5V) {
|
|
|
|
|
HALSIM_SetRoboRioUserFaults5V(userFaults5V);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterUserFaults3V3Callback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioUserFaults3V3Callback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioUserFaults3V3Callback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int RoboRioSim::GetUserFaults3V3() {
|
|
|
|
|
return HALSIM_GetRoboRioUserFaults3V3();
|
|
|
|
|
}
|
2020-07-15 23:48:09 -07:00
|
|
|
|
|
|
|
|
void RoboRioSim::SetUserFaults3V3(int userFaults3V3) {
|
|
|
|
|
HALSIM_SetRoboRioUserFaults3V3(userFaults3V3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 19:14:27 -07:00
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterBrownoutVoltageCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioBrownoutVoltageCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioBrownoutVoltageCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
units::volt_t RoboRioSim::GetBrownoutVoltage() {
|
2022-08-17 13:42:36 -07:00
|
|
|
return units::volt_t{HALSIM_GetRoboRioBrownoutVoltage()};
|
2021-10-13 19:14:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetBrownoutVoltage(units::volt_t vInVoltage) {
|
2021-10-25 08:58:12 -07:00
|
|
|
HALSIM_SetRoboRioBrownoutVoltage(vInVoltage.value());
|
2021-10-13 19:14:27 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:51:57 -04:00
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterCPUTempCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioCPUTempCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioCPUTempCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
units::celsius_t RoboRioSim::GetCPUTemp() {
|
|
|
|
|
return units::celsius_t{HALSIM_GetRoboRioCPUTemp()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetCPUTemp(units::celsius_t cpuTemp) {
|
|
|
|
|
HALSIM_SetRoboRioCPUTemp(cpuTemp.value());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 02:34:18 -04:00
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterTeamNumberCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioTeamNumberCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioTeamNumberCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t RoboRioSim::GetTeamNumber() {
|
|
|
|
|
return HALSIM_GetRoboRioTeamNumber();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetTeamNumber(int32_t teamNumber) {
|
|
|
|
|
HALSIM_SetRoboRioTeamNumber(teamNumber);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 00:58:55 -05:00
|
|
|
std::string RoboRioSim::GetSerialNumber() {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
WPI_String serialNum;
|
|
|
|
|
HALSIM_GetRoboRioSerialNumber(&serialNum);
|
|
|
|
|
std::string serial{wpi::to_string_view(&serialNum)};
|
|
|
|
|
WPI_FreeString(&serialNum);
|
|
|
|
|
return serial;
|
2022-12-09 00:58:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetSerialNumber(std::string_view serialNumber) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
auto str = wpi::make_string(serialNumber);
|
|
|
|
|
HALSIM_SetRoboRioSerialNumber(&str);
|
2022-12-09 00:58:55 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-26 14:39:51 -05:00
|
|
|
std::string RoboRioSim::GetComments() {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
WPI_String comments;
|
|
|
|
|
HALSIM_GetRoboRioComments(&comments);
|
|
|
|
|
std::string serial{wpi::to_string_view(&comments)};
|
|
|
|
|
WPI_FreeString(&comments);
|
|
|
|
|
return serial;
|
2022-12-26 14:39:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetComments(std::string_view comments) {
|
Change C APIs to a unified string implementation (#6299)
Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.
For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.
Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.
The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.
WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
2024-05-13 05:35:14 -07:00
|
|
|
auto str = wpi::make_string(comments);
|
|
|
|
|
HALSIM_SetRoboRioComments(&str);
|
2022-12-26 14:39:51 -05:00
|
|
|
}
|
|
|
|
|
|
2023-12-22 13:57:52 -05:00
|
|
|
std::unique_ptr<CallbackStore> RoboRioSim::RegisterRadioLEDStateCallback(
|
|
|
|
|
NotifyCallback callback, bool initialNotify) {
|
|
|
|
|
auto store = std::make_unique<CallbackStore>(
|
|
|
|
|
-1, callback, &HALSIM_CancelRoboRioRadioLEDStateCallback);
|
|
|
|
|
store->SetUid(HALSIM_RegisterRoboRioRadioLEDStateCallback(
|
|
|
|
|
&CallbackStoreThunk, store.get(), initialNotify));
|
|
|
|
|
return store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RadioLEDState RoboRioSim::GetRadioLEDState() {
|
|
|
|
|
return static_cast<RadioLEDState>(HALSIM_GetRoboRioRadioLEDState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RoboRioSim::SetRadioLEDState(RadioLEDState state) {
|
|
|
|
|
HALSIM_SetRoboRioRadioLEDState(static_cast<HAL_RadioLEDState>(state));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 01:50:25 -05:00
|
|
|
void RoboRioSim::ResetData() {
|
2020-12-28 12:58:06 -08:00
|
|
|
HALSIM_ResetRoboRioData();
|
|
|
|
|
}
|