Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions subprojects/robotpy-hal/gen/SimDevice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ classes:
[](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector<std::string, 8> &options, int32_t initialValue) {
wpi::SmallVector<const char *, 8> coptions;
coptions.reserve(options.size());
for (auto s: options) {
for (auto &s: options) {
coptions.push_back(s.c_str());
}
return self.CreateEnum(name, direction, coptions, initialValue);
Expand All @@ -182,7 +182,7 @@ classes:
[](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector<std::string, 8> &options, const wpi::SmallVector<double, 8> &optionValues, int32_t initialValue) {
wpi::SmallVector<const char *, 8> coptions;
coptions.reserve(options.size());
for (auto s: options) {
for (auto &s: options) {
coptions.push_back(s.c_str());
}
return self.CreateEnumDouble(name, direction, coptions, optionValues, initialValue);
Expand Down Expand Up @@ -327,20 +327,28 @@ inline_code: |
if (self) {
int32_t value;
int32_t numOptions;
int32_t numdOptions;
const char ** options;
const double * doptions;
const char * option = "<unknown>";
std::string doption;
{
py::gil_scoped_release release;
value = self.Get();
options = HALSIM_GetSimValueEnumOptions(self, &numOptions);
doptions = HALSIM_GetSimValueEnumDoubleValues(self, &numdOptions);
}

if (options && value >= 0 && value < numOptions) {
option = options[value];
}

if (doptions && value >= 0 && value < numdOptions) {
doption = " dvalue=" + std::to_string(doptions[value]);
}

return "<SimEnum name=" + std::string(option) +
" value=" + std::to_string(value) + ">";
" value=" + std::to_string(value) + doption + ">";
} else {
return "<SimEnum (invalid)>";
}
Expand Down
30 changes: 30 additions & 0 deletions subprojects/robotpy-hal/tests/test_hal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ def test_hal_simdevice():
assert v.get() == 4


def test_hal_simdevice_enum():
device = hal.SimDevice("enumDevice")
names = ["one", "two", "three"]
v = device.createEnum("e", 0, names, 0)

assert v.get() == 0
assert v.type == hal.Type.ENUM

for value, name in enumerate(names):
v.set(value)
assert repr(v) == f"<SimEnum name={name} value={value}>"


def test_hal_simdevice_enum_double():
device = hal.SimDevice("enumDevice")
names = ["one", "two", "three"]
values = [0.0, 1.0, 2.0]
v = device.createEnumDouble("e", 0, names, values, 0)

assert v.get() == 0
assert v.type == hal.Type.ENUM

# TODO: update this test to not use repr once https://github.com/wpilibsuite/allwpilib/issues/7800
# is resolved

for value, (name, dvalue) in enumerate(zip(names, values)):
v.set(value)
assert repr(v) == f"<SimEnum name={name} value={value} dvalue={dvalue:.6f}>"


def test_hal_send_error(capsys):
hal._wpiHal.__test_senderr()
cap = capsys.readouterr()
Expand Down
Loading