diff --git a/subprojects/robotpy-hal/gen/SimDevice.yml b/subprojects/robotpy-hal/gen/SimDevice.yml index 5975ce78f..a798067b6 100644 --- a/subprojects/robotpy-hal/gen/SimDevice.yml +++ b/subprojects/robotpy-hal/gen/SimDevice.yml @@ -167,7 +167,7 @@ classes: [](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector &options, int32_t initialValue) { wpi::SmallVector 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); @@ -182,7 +182,7 @@ classes: [](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector &options, const wpi::SmallVector &optionValues, int32_t initialValue) { wpi::SmallVector 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); @@ -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 = ""; + 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 ""; + " value=" + std::to_string(value) + doption + ">"; } else { return ""; } diff --git a/subprojects/robotpy-hal/tests/test_hal.py b/subprojects/robotpy-hal/tests/test_hal.py index 0846f69bd..9970edb5d 100644 --- a/subprojects/robotpy-hal/tests/test_hal.py +++ b/subprojects/robotpy-hal/tests/test_hal.py @@ -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"" + + +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"" + + def test_hal_send_error(capsys): hal._wpiHal.__test_senderr() cap = capsys.readouterr()