Skip to content

Commit f16ab49

Browse files
authored
Merge pull request #158 from robotpy/simenum
Fix SimEnum.createEnum/createEnumDouble
2 parents 895bc49 + 26243e2 commit f16ab49

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

subprojects/robotpy-hal/gen/SimDevice.yml

+11-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ classes:
167167
[](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector<std::string, 8> &options, int32_t initialValue) {
168168
wpi::SmallVector<const char *, 8> coptions;
169169
coptions.reserve(options.size());
170-
for (auto s: options) {
170+
for (auto &s: options) {
171171
coptions.push_back(s.c_str());
172172
}
173173
return self.CreateEnum(name, direction, coptions, initialValue);
@@ -182,7 +182,7 @@ classes:
182182
[](SimDevice &self, const char * name, int32_t direction, const wpi::SmallVector<std::string, 8> &options, const wpi::SmallVector<double, 8> &optionValues, int32_t initialValue) {
183183
wpi::SmallVector<const char *, 8> coptions;
184184
coptions.reserve(options.size());
185-
for (auto s: options) {
185+
for (auto &s: options) {
186186
coptions.push_back(s.c_str());
187187
}
188188
return self.CreateEnumDouble(name, direction, coptions, optionValues, initialValue);
@@ -327,20 +327,28 @@ inline_code: |
327327
if (self) {
328328
int32_t value;
329329
int32_t numOptions;
330+
int32_t numdOptions;
330331
const char ** options;
332+
const double * doptions;
331333
const char * option = "<unknown>";
334+
std::string doption;
332335
{
333336
py::gil_scoped_release release;
334337
value = self.Get();
335338
options = HALSIM_GetSimValueEnumOptions(self, &numOptions);
339+
doptions = HALSIM_GetSimValueEnumDoubleValues(self, &numdOptions);
336340
}
337341
338342
if (options && value >= 0 && value < numOptions) {
339343
option = options[value];
340344
}
341345
346+
if (doptions && value >= 0 && value < numdOptions) {
347+
doption = " dvalue=" + std::to_string(doptions[value]);
348+
}
349+
342350
return "<SimEnum name=" + std::string(option) +
343-
" value=" + std::to_string(value) + ">";
351+
" value=" + std::to_string(value) + doption + ">";
344352
} else {
345353
return "<SimEnum (invalid)>";
346354
}

subprojects/robotpy-hal/tests/test_hal.py

+30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ def test_hal_simdevice():
1111
assert v.get() == 4
1212

1313

14+
def test_hal_simdevice_enum():
15+
device = hal.SimDevice("enumDevice")
16+
names = ["one", "two", "three"]
17+
v = device.createEnum("e", 0, names, 0)
18+
19+
assert v.get() == 0
20+
assert v.type == hal.Type.ENUM
21+
22+
for value, name in enumerate(names):
23+
v.set(value)
24+
assert repr(v) == f"<SimEnum name={name} value={value}>"
25+
26+
27+
def test_hal_simdevice_enum_double():
28+
device = hal.SimDevice("enumDevice")
29+
names = ["one", "two", "three"]
30+
values = [0.0, 1.0, 2.0]
31+
v = device.createEnumDouble("e", 0, names, values, 0)
32+
33+
assert v.get() == 0
34+
assert v.type == hal.Type.ENUM
35+
36+
# TODO: update this test to not use repr once https://github.com/wpilibsuite/allwpilib/issues/7800
37+
# is resolved
38+
39+
for value, (name, dvalue) in enumerate(zip(names, values)):
40+
v.set(value)
41+
assert repr(v) == f"<SimEnum name={name} value={value} dvalue={dvalue:.6f}>"
42+
43+
1444
def test_hal_send_error(capsys):
1545
hal._wpiHal.__test_senderr()
1646
cap = capsys.readouterr()

0 commit comments

Comments
 (0)