WPILibC++ 2024.3.2
SimDevice.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#ifdef __cplusplus
10#include <initializer_list>
11#include <span>
12#include <string>
13#endif
14
15#include "hal/Types.h"
16#include "hal/Value.h"
17
18/**
19 * @defgroup hal_simdevice Simulator Device Framework
20 * @ingroup hal_capi
21 * HAL Simulator Device Framework. This enables creating simulation-only
22 * variables for higher level device access. For example, a device such as
23 * a SPI gyro can expose angle and rate variables to enable direct access
24 * from simulation extensions or user test code instead of requiring that
25 * the SPI bit-level protocol be implemented in simulation code.
26 *
27 * @{
28 */
29
30/**
31 * Direction of a simulated value (from the perspective of user code).
32 */
34 HAL_SimValueInput = 0, /**< input to user code from the simulator */
35 HAL_SimValueOutput, /**< output from user code to the simulator */
36 HAL_SimValueBidir /**< bidirectional between user code and simulator */
37};
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/**
44 * Creates a simulated device.
45 *
46 * The device name must be unique. 0 is returned if the device name already
47 * exists. If multiple instances of the same device are desired, recommend
48 * appending the instance/unique identifier in brackets to the base name,
49 * e.g. "device[1]".
50 *
51 * 0 is returned if not in simulation.
52 *
53 * @param name device name
54 * @return simulated device handle
55 */
57
58/**
59 * Frees a simulated device.
60 *
61 * This also allows the same device name to be used again.
62 * This also frees all the simulated values created on the device.
63 *
64 * @param handle simulated device handle
65 */
67
68/**
69 * Get the name of a simulated device
70 *
71 * @param handle simulated device handle
72 * @return name of the simulated device
73 */
75
76/**
77 * Creates a value on a simulated device.
78 *
79 * Returns 0 if not in simulation; this can be used to avoid calls
80 * to Set/Get functions.
81 *
82 * @param device simulated device handle
83 * @param name value name
84 * @param direction input/output/bidir (from perspective of user code)
85 * @param initialValue initial value
86 * @return simulated value handle
87 */
89 const char* name, int32_t direction,
90 const struct HAL_Value* initialValue);
91
92#ifdef __cplusplus
93extern "C++" {
95 const char* name,
96 int32_t direction,
97 const HAL_Value& initialValue) {
98 return HAL_CreateSimValue(device, name, direction, &initialValue);
99}
100} // extern "C++"
101#endif
102
103/**
104 * Creates an int value on a simulated device.
105 *
106 * Returns 0 if not in simulation; this can be used to avoid calls
107 * to Set/Get functions.
108 *
109 * @param device simulated device handle
110 * @param name value name
111 * @param direction input/output/bidir (from perspective of user code)
112 * @param initialValue initial value
113 * @return simulated value handle
114 */
116 const char* name,
117 int32_t direction,
118 int32_t initialValue) {
119 struct HAL_Value v = HAL_MakeInt(initialValue);
120 return HAL_CreateSimValue(device, name, direction, &v);
121}
122
123/**
124 * Creates a long value on a simulated device.
125 *
126 * Returns 0 if not in simulation; this can be used to avoid calls
127 * to Set/Get functions.
128 *
129 * @param device simulated device handle
130 * @param name value name
131 * @param direction input/output/bidir (from perspective of user code)
132 * @param initialValue initial value
133 * @return simulated value handle
134 */
136 const char* name,
137 int32_t direction,
138 int64_t initialValue) {
139 struct HAL_Value v = HAL_MakeLong(initialValue);
140 return HAL_CreateSimValue(device, name, direction, &v);
141}
142
143/**
144 * Creates a double value on a simulated device.
145 *
146 * Returns 0 if not in simulation; this can be used to avoid calls
147 * to Set/Get functions.
148 *
149 * @param device simulated device handle
150 * @param name value name
151 * @param direction input/output/bidir (from perspective of user code)
152 * @param initialValue initial value
153 * @return simulated value handle
154 */
156 const char* name,
157 int32_t direction,
158 double initialValue) {
159 struct HAL_Value v = HAL_MakeDouble(initialValue);
160 return HAL_CreateSimValue(device, name, direction, &v);
161}
162
163/**
164 * Creates an enumerated value on a simulated device.
165 *
166 * Enumerated values are always in the range 0 to numOptions-1.
167 *
168 * Returns 0 if not in simulation; this can be used to avoid calls
169 * to Set/Get functions.
170 *
171 * @param device simulated device handle
172 * @param name value name
173 * @param direction input/output/bidir (from perspective of user code)
174 * @param numOptions number of enumerated value options (length of options)
175 * @param options array of option descriptions
176 * @param initialValue initial value (selection)
177 * @return simulated value handle
178 */
180 const char* name, int32_t direction,
181 int32_t numOptions,
182 const char** options,
183 int32_t initialValue);
184
185/**
186 * Creates an enumerated value on a simulated device with double values.
187 *
188 * Enumerated values are always in the range 0 to numOptions-1.
189 *
190 * Returns 0 if not in simulation; this can be used to avoid calls
191 * to Set/Get functions.
192 *
193 * @param device simulated device handle
194 * @param name value name
195 * @param direction input/output/bidir (from perspective of user code)
196 * @param numOptions number of enumerated value options (length of options)
197 * @param options array of option descriptions
198 * @param optionValues array of option double values
199 * @param initialValue initial value (selection)
200 * @return simulated value handle
201 */
203 HAL_SimDeviceHandle device, const char* name, int32_t direction,
204 int32_t numOptions, const char** options, const double* optionValues,
205 int32_t initialValue);
206
207/**
208 * Creates a boolean value on a simulated device.
209 *
210 * Returns 0 if not in simulation; this can be used to avoid calls
211 * to Set/Get functions.
212 *
213 * @param device simulated device handle
214 * @param name value name
215 * @param direction input/output/bidir (from perspective of user code)
216 * @param initialValue initial value
217 * @return simulated value handle
218 */
220 const char* name,
221 int32_t direction,
222 HAL_Bool initialValue) {
223 struct HAL_Value v = HAL_MakeBoolean(initialValue);
224 return HAL_CreateSimValue(device, name, direction, &v);
225}
226
227/**
228 * Gets a simulated value.
229 *
230 * @param handle simulated value handle
231 * @param value value (output parameter)
232 */
233void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value* value);
234
235#ifdef __cplusplus
236extern "C++" {
238 HAL_Value v;
239 HAL_GetSimValue(handle, &v);
240 return v;
241}
242} // extern "C++"
243#endif
244
245/**
246 * Gets a simulated value (int).
247 *
248 * @param handle simulated value handle
249 * @return The current value
250 */
252 struct HAL_Value v;
253 HAL_GetSimValue(handle, &v);
254 return v.type == HAL_INT ? v.data.v_int : 0;
255}
256
257/**
258 * Gets a simulated value (long).
259 *
260 * @param handle simulated value handle
261 * @return The current value
262 */
264 struct HAL_Value v;
265 HAL_GetSimValue(handle, &v);
266 return v.type == HAL_LONG ? v.data.v_long : 0;
267}
268
269/**
270 * Gets a simulated value (double).
271 *
272 * @param handle simulated value handle
273 * @return The current value
274 */
276 struct HAL_Value v;
277 HAL_GetSimValue(handle, &v);
278 return v.type == HAL_DOUBLE ? v.data.v_double : 0.0;
279}
280
281/**
282 * Gets a simulated value (enum).
283 *
284 * @param handle simulated value handle
285 * @return The current value
286 */
288 struct HAL_Value v;
289 HAL_GetSimValue(handle, &v);
290 return v.type == HAL_ENUM ? v.data.v_enum : 0;
291}
292
293/**
294 * Gets a simulated value (boolean).
295 *
296 * @param handle simulated value handle
297 * @return The current value
298 */
300 struct HAL_Value v;
301 HAL_GetSimValue(handle, &v);
302 return v.type == HAL_BOOLEAN ? v.data.v_boolean : 0;
303}
304
305/**
306 * Sets a simulated value.
307 *
308 * @param handle simulated value handle
309 * @param value the value to set
310 */
311void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value* value);
312
313#ifdef __cplusplus
314extern "C++" {
315inline void HAL_SetSimValue(HAL_SimValueHandle handle, const HAL_Value& value) {
316 HAL_SetSimValue(handle, &value);
317}
318} // extern "C++"
319#endif
320
321/**
322 * Sets a simulated value (int).
323 *
324 * @param handle simulated value handle
325 * @param value the value to set
326 */
327inline void HAL_SetSimValueInt(HAL_SimValueHandle handle, int value) {
328 struct HAL_Value v = HAL_MakeInt(value);
329 HAL_SetSimValue(handle, &v);
330}
331
332/**
333 * Sets a simulated value (long).
334 *
335 * @param handle simulated value handle
336 * @param value the value to set
337 */
338inline void HAL_SetSimValueLong(HAL_SimValueHandle handle, int64_t value) {
339 struct HAL_Value v = HAL_MakeLong(value);
340 HAL_SetSimValue(handle, &v);
341}
342
343/**
344 * Sets a simulated value (double).
345 *
346 * @param handle simulated value handle
347 * @param value the value to set
348 */
349inline void HAL_SetSimValueDouble(HAL_SimValueHandle handle, double value) {
350 struct HAL_Value v = HAL_MakeDouble(value);
351 HAL_SetSimValue(handle, &v);
352}
353
354/**
355 * Sets a simulated value (enum).
356 *
357 * @param handle simulated value handle
358 * @param value the value to set
359 */
360inline void HAL_SetSimValueEnum(HAL_SimValueHandle handle, int32_t value) {
361 struct HAL_Value v = HAL_MakeEnum(value);
362 HAL_SetSimValue(handle, &v);
363}
364
365/**
366 * Sets a simulated value (boolean).
367 *
368 * @param handle simulated value handle
369 * @param value the value to set
370 */
372 struct HAL_Value v = HAL_MakeBoolean(value);
373 HAL_SetSimValue(handle, &v);
374}
375
376/**
377 * Resets a simulated double or integral value to 0.
378 * Has no effect on other value types.
379 * Use this instead of Set(0) for resetting incremental sensor values like
380 * encoder counts or gyro accumulated angle to ensure correct behavior in a
381 * distributed system (e.g. WebSockets).
382 *
383 * @param handle simulated value handle
384 */
386
387/** @} */
388
389#ifdef __cplusplus
390} // extern "C"
391#endif
392
393#ifdef __cplusplus
394namespace hal {
395
396/**
397 * C++ wrapper around a HAL simulator value handle.
398 */
399class SimValue {
400 public:
401 /**
402 * Default constructor that results in an "empty" object that is false in
403 * a boolean context.
404 */
405 SimValue() = default;
406
407 /**
408 * Wraps a simulated value handle as returned by HAL_CreateSimValue().
409 *
410 * @param val simulated value handle
411 */
412 /*implicit*/ SimValue(HAL_SimValueHandle val) // NOLINT
413 : m_handle(val) {}
414
415 /**
416 * Determine if handle is empty. Should be used to optimize out code paths
417 * that are taken/not taken in simulation.
418 *
419 * @return False if handle is empty, true if handle is valid.
420 */
421 explicit operator bool() const { return m_handle != HAL_kInvalidHandle; }
422
423 /**
424 * Get the internal device handle.
425 *
426 * @return internal handle
427 */
428 operator HAL_SimValueHandle() const { return m_handle; } // NOLINT
429
430 /**
431 * Gets the simulated value.
432 *
433 * @return The current value
434 */
436
437 /**
438 * Sets the simulated value.
439 *
440 * @param value the value to set
441 */
442 void SetValue(const HAL_Value& value) { HAL_SetSimValue(m_handle, value); }
443
444 protected:
446};
447
448/**
449 * C++ wrapper around a HAL simulator int value handle.
450 */
451class SimInt : public SimValue {
452 public:
453 /**
454 * Default constructor that results in an "empty" object that is false in
455 * a boolean context.
456 */
457 SimInt() = default;
458
459 /**
460 * Wraps a simulated value handle as returned by HAL_CreateSimValueInt().
461 *
462 * @param val simulated value handle
463 */
464 /*implicit*/ SimInt(HAL_SimValueHandle val) // NOLINT
465 : SimValue(val) {}
466
467 /**
468 * Gets the simulated value.
469 *
470 * @return The current value
471 */
472 int32_t Get() const { return HAL_GetSimValueInt(m_handle); }
473
474 /**
475 * Sets the simulated value.
476 *
477 * @param value the value to set
478 */
479 void Set(int32_t value) { HAL_SetSimValueInt(m_handle, value); }
480
481 /**
482 * Resets the simulated value to 0. Use this instead of Set(0) for resetting
483 * incremental sensor values like encoder counts or gyro accumulated angle
484 * to ensure correct behavior in a distributed system (e.g. WebSockets).
485 */
487};
488
489/**
490 * C++ wrapper around a HAL simulator long value handle.
491 */
492class SimLong : public SimValue {
493 public:
494 /**
495 * Default constructor that results in an "empty" object that is false in
496 * a boolean context.
497 */
498 SimLong() = default;
499
500 /**
501 * Wraps a simulated value handle as returned by HAL_CreateSimValueLong().
502 *
503 * @param val simulated value handle
504 */
505 /*implicit*/ SimLong(HAL_SimValueHandle val) // NOLINT
506 : SimValue(val) {}
507
508 /**
509 * Gets the simulated value.
510 *
511 * @return The current value
512 */
513 int64_t Get() const { return HAL_GetSimValueLong(m_handle); }
514
515 /**
516 * Sets the simulated value.
517 *
518 * @param value the value to set
519 */
520 void Set(int64_t value) { HAL_SetSimValueLong(m_handle, value); }
521
522 /**
523 * Resets the simulated value to 0. Use this instead of Set(0) for resetting
524 * incremental sensor values like encoder counts or gyro accumulated angle
525 * to ensure correct behavior in a distributed system (e.g. WebSockets).
526 */
528};
529
530/**
531 * C++ wrapper around a HAL simulator double value handle.
532 */
533class SimDouble : public SimValue {
534 public:
535 /**
536 * Default constructor that results in an "empty" object that is false in
537 * a boolean context.
538 */
539 SimDouble() = default;
540
541 /**
542 * Wraps a simulated value handle as returned by HAL_CreateSimValueDouble().
543 *
544 * @param val simulated value handle
545 */
546 /*implicit*/ SimDouble(HAL_SimValueHandle val) // NOLINT
547 : SimValue(val) {}
548
549 /**
550 * Gets the simulated value.
551 *
552 * @return The current value
553 */
554 double Get() const { return HAL_GetSimValueDouble(m_handle); }
555
556 /**
557 * Sets the simulated value.
558 *
559 * @param value the value to set
560 */
561 void Set(double value) { HAL_SetSimValueDouble(m_handle, value); }
562
563 /**
564 * Resets the simulated value to 0. Use this instead of Set(0) for resetting
565 * incremental sensor values like encoder counts or gyro accumulated angle
566 * to ensure correct behavior in a distributed system (e.g. WebSockets).
567 */
569};
570
571/**
572 * C++ wrapper around a HAL simulator enum value handle.
573 */
574class SimEnum : public SimValue {
575 public:
576 /**
577 * Default constructor that results in an "empty" object that is false in
578 * a boolean context.
579 */
580 SimEnum() = default;
581
582 /**
583 * Wraps a simulated value handle as returned by HAL_CreateSimValueEnum().
584 *
585 * @param val simulated value handle
586 */
587 /*implicit*/ SimEnum(HAL_SimValueHandle val) // NOLINT
588 : SimValue(val) {}
589
590 /**
591 * Gets the simulated value.
592 *
593 * @return The current value
594 */
595 int32_t Get() const { return HAL_GetSimValueEnum(m_handle); }
596
597 /**
598 * Sets the simulated value.
599 *
600 * @param value the value to set
601 */
602 void Set(int32_t value) { HAL_SetSimValueEnum(m_handle, value); }
603};
604
605/**
606 * C++ wrapper around a HAL simulator boolean value handle.
607 */
608class SimBoolean : public SimValue {
609 public:
610 /**
611 * Default constructor that results in an "empty" object that is false in
612 * a boolean context.
613 */
614 SimBoolean() = default;
615
616 /**
617 * Wraps a simulated value handle as returned by HAL_CreateSimValueBoolean().
618 *
619 * @param val simulated value handle
620 */
621 /*implicit*/ SimBoolean(HAL_SimValueHandle val) // NOLINT
622 : SimValue(val) {}
623
624 /**
625 * Gets the simulated value.
626 *
627 * @return The current value
628 */
629 bool Get() const { return HAL_GetSimValueBoolean(m_handle); }
630
631 /**
632 * Sets the simulated value.
633 *
634 * @param value the value to set
635 */
636 void Set(bool value) { HAL_SetSimValueBoolean(m_handle, value); }
637};
638
639/**
640 * A move-only C++ wrapper around a HAL simulator device handle.
641 */
643 public:
644 /**
645 * Direction of a simulated value (from the perspective of user code).
646 */
651 };
652
653 /**
654 * Default constructor that results in an "empty" object that is false in
655 * a boolean context.
656 */
657 SimDevice() = default;
658
659 /**
660 * Creates a simulated device.
661 *
662 * The device name must be unique. Returns null if the device name
663 * already exists. If multiple instances of the same device are desired,
664 * recommend appending the instance/unique identifier in brackets to the base
665 * name, e.g. "device[1]".
666 *
667 * If not in simulation, results in an "empty" object that evaluates to false
668 * in a boolean context.
669 *
670 * @param name device name
671 */
672 explicit SimDevice(const char* name) : m_handle(HAL_CreateSimDevice(name)) {}
673
674 /**
675 * Creates a simulated device.
676 *
677 * The device name must be unique. Returns null if the device name
678 * already exists. This is a convenience method that appends index in
679 * brackets to the device name, e.g. passing index=1 results in "device[1]"
680 * for the device name.
681 *
682 * If not in simulation, results in an "empty" object that evaluates to false
683 * in a boolean context.
684 *
685 * @param name device name
686 * @param index device index number to append to name
687 */
688 SimDevice(const char* name, int index);
689
690 /**
691 * Creates a simulated device.
692 *
693 * The device name must be unique. Returns null if the device name
694 * already exists. This is a convenience method that appends index and
695 * channel in brackets to the device name, e.g. passing index=1 and channel=2
696 * results in "device[1,2]" for the device name.
697 *
698 * If not in simulation, results in an "empty" object that evaluates to false
699 * in a boolean context.
700 *
701 * @param name device name
702 * @param index device index number to append to name
703 * @param channel device channel number to append to name
704 */
705 SimDevice(const char* name, int index, int channel);
706
710 }
711 }
712
713 SimDevice(const SimDevice&) = delete;
714 SimDevice& operator=(const SimDevice&) = delete;
715
717 rhs.m_handle = HAL_kInvalidHandle;
718 }
719
721 m_handle = rhs.m_handle;
722 rhs.m_handle = HAL_kInvalidHandle;
723 return *this;
724 }
725
726 /**
727 * Determine if handle is empty. Should be used to optimize out code paths
728 * that are taken/not taken in simulation.
729 *
730 * @return False if handle is empty, true if handle is valid.
731 */
732 explicit operator bool() const { return m_handle != HAL_kInvalidHandle; }
733
734 /**
735 * Get the internal device handle.
736 *
737 * @return internal handle
738 */
739 operator HAL_SimDeviceHandle() const { return m_handle; } // NOLINT
740
741 /**
742 * Get the name of the simulated device.
743 *
744 * @return name
745 */
746 std::string GetName() const {
747 return std::string(HAL_GetSimDeviceName(m_handle));
748 }
749
750 /**
751 * Creates a value on the simulated device.
752 *
753 * If not in simulation, results in an "empty" object that evaluates to false
754 * in a boolean context.
755 *
756 * @param name value name
757 * @param direction input/output/bidir (from perspective of user code)
758 * @param initialValue initial value
759 * @return simulated value object
760 */
761 SimValue CreateValue(const char* name, int32_t direction,
762 const HAL_Value& initialValue) {
763 return HAL_CreateSimValue(m_handle, name, direction, &initialValue);
764 }
765
766 /**
767 * Creates an int value on the simulated device.
768 *
769 * If not in simulation, results in an "empty" object that evaluates to false
770 * in a boolean context.
771 *
772 * @param name value name
773 * @param direction input/output/bidir (from perspective of user code)
774 * @param initialValue initial value
775 * @return simulated double value object
776 */
777 SimInt CreateInt(const char* name, int32_t direction, int32_t initialValue) {
778 return HAL_CreateSimValueInt(m_handle, name, direction, initialValue);
779 }
780
781 /**
782 * Creates a long value on the simulated device.
783 *
784 * If not in simulation, results in an "empty" object that evaluates to false
785 * in a boolean context.
786 *
787 * @param name value name
788 * @param direction input/output/bidir (from perspective of user code)
789 * @param initialValue initial value
790 * @return simulated double value object
791 */
792 SimLong CreateLong(const char* name, int32_t direction,
793 int64_t initialValue) {
794 return HAL_CreateSimValueLong(m_handle, name, direction, initialValue);
795 }
796
797 /**
798 * Creates a double value on the simulated device.
799 *
800 * If not in simulation, results in an "empty" object that evaluates to false
801 * in a boolean context.
802 *
803 * @param name value name
804 * @param direction input/output/bidir (from perspective of user code)
805 * @param initialValue initial value
806 * @return simulated double value object
807 */
808 SimDouble CreateDouble(const char* name, int32_t direction,
809 double initialValue) {
810 return HAL_CreateSimValueDouble(m_handle, name, direction, initialValue);
811 }
812
813 /**
814 * Creates an enumerated value on the simulated device.
815 *
816 * Enumerated values are always in the range 0 to numOptions-1.
817 *
818 * If not in simulation, results in an "empty" object that evaluates to false
819 * in a boolean context.
820 *
821 * @param name value name
822 * @param direction input/output/bidir (from perspective of user code)
823 * @param options array of option descriptions
824 * @param initialValue initial value (selection)
825 * @return simulated enum value object
826 */
827 SimEnum CreateEnum(const char* name, int32_t direction,
828 std::initializer_list<const char*> options,
829 int32_t initialValue) {
830 return HAL_CreateSimValueEnum(m_handle, name, direction, options.size(),
831 const_cast<const char**>(options.begin()),
832 initialValue);
833 }
834
835 /**
836 * Creates an enumerated value on the simulated device.
837 *
838 * Enumerated values are always in the range 0 to numOptions-1.
839 *
840 * If not in simulation, results in an "empty" object that evaluates to false
841 * in a boolean context.
842 *
843 * @param name value name
844 * @param direction input/output/bidir (from perspective of user code)
845 * @param options array of option descriptions
846 * @param initialValue initial value (selection)
847 * @return simulated enum value object
848 */
849 SimEnum CreateEnum(const char* name, int32_t direction,
850 std::span<const char* const> options,
851 int32_t initialValue) {
852 return HAL_CreateSimValueEnum(m_handle, name, direction, options.size(),
853 const_cast<const char**>(options.data()),
854 initialValue);
855 }
856
857 /**
858 * Creates an enumerated value on the simulated device with double values.
859 *
860 * Enumerated values are always in the range 0 to numOptions-1.
861 *
862 * If not in simulation, results in an "empty" object that evaluates to false
863 * in a boolean context.
864 *
865 * @param name value name
866 * @param direction input/output/bidir (from perspective of user code)
867 * @param options array of option descriptions
868 * @param optionValues array of option values (must be the same size as
869 * options)
870 * @param initialValue initial value (selection)
871 * @return simulated enum value object
872 */
873 SimEnum CreateEnumDouble(const char* name, int32_t direction,
874 std::initializer_list<const char*> options,
875 std::initializer_list<double> optionValues,
876 int32_t initialValue) {
877 if (options.size() != optionValues.size()) {
878 return {};
879 }
881 m_handle, name, direction, options.size(),
882 const_cast<const char**>(options.begin()), optionValues.begin(),
883 initialValue);
884 }
885
886 /**
887 * Creates an enumerated value on the simulated device with double values.
888 *
889 * Enumerated values are always in the range 0 to numOptions-1.
890 *
891 * If not in simulation, results in an "empty" object that evaluates to false
892 * in a boolean context.
893 *
894 * @param name value name
895 * @param direction input/output/bidir (from perspective of user code)
896 * @param options array of option descriptions
897 * @param optionValues array of option values (must be the same size as
898 * options)
899 * @param initialValue initial value (selection)
900 * @return simulated enum value object
901 */
902 SimEnum CreateEnumDouble(const char* name, int32_t direction,
903 std::span<const char* const> options,
904 std::span<const double> optionValues,
905 int32_t initialValue) {
906 if (options.size() != optionValues.size()) {
907 return {};
908 }
910 m_handle, name, direction, options.size(),
911 const_cast<const char**>(options.data()), optionValues.data(),
912 initialValue);
913 }
914
915 /**
916 * Creates a boolean value on the simulated device.
917 *
918 * If not in simulation, results in an "empty" object that evaluates to false
919 * in a boolean context.
920 *
921 * @param name value name
922 * @param direction input/output/bidir (from perspective of user code)
923 * @param initialValue initial value
924 * @return simulated boolean value object
925 */
926 SimBoolean CreateBoolean(const char* name, int32_t direction,
927 bool initialValue) {
928 return HAL_CreateSimValueBoolean(m_handle, name, direction, initialValue);
929 }
930
931 protected:
933};
934
935} // namespace hal
936#endif // __cplusplus
struct HAL_Value HAL_MakeBoolean(HAL_Bool v)
Definition: Value.h:35
struct HAL_Value HAL_MakeDouble(double v)
Definition: Value.h:63
struct HAL_Value HAL_MakeEnum(int v)
Definition: Value.h:42
struct HAL_Value HAL_MakeInt(int v)
Definition: Value.h:49
struct HAL_Value HAL_MakeLong(int64_t v)
Definition: Value.h:56
@ HAL_DOUBLE
Definition: Value.h:13
@ HAL_LONG
Definition: Value.h:16
@ HAL_ENUM
Definition: Value.h:14
@ HAL_BOOLEAN
Definition: Value.h:12
@ HAL_INT
Definition: Value.h:15
C++ wrapper around a HAL simulator boolean value handle.
Definition: SimDevice.h:608
void Set(bool value)
Sets the simulated value.
Definition: SimDevice.h:636
bool Get() const
Gets the simulated value.
Definition: SimDevice.h:629
SimBoolean()=default
Default constructor that results in an "empty" object that is false in a boolean context.
SimBoolean(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValueBoolean().
Definition: SimDevice.h:621
A move-only C++ wrapper around a HAL simulator device handle.
Definition: SimDevice.h:642
SimDouble CreateDouble(const char *name, int32_t direction, double initialValue)
Creates a double value on the simulated device.
Definition: SimDevice.h:808
SimLong CreateLong(const char *name, int32_t direction, int64_t initialValue)
Creates a long value on the simulated device.
Definition: SimDevice.h:792
SimDevice & operator=(const SimDevice &)=delete
HAL_SimDeviceHandle m_handle
Definition: SimDevice.h:932
~SimDevice()
Definition: SimDevice.h:707
SimInt CreateInt(const char *name, int32_t direction, int32_t initialValue)
Creates an int value on the simulated device.
Definition: SimDevice.h:777
SimEnum CreateEnum(const char *name, int32_t direction, std::span< const char *const > options, int32_t initialValue)
Creates an enumerated value on the simulated device.
Definition: SimDevice.h:849
SimDevice(const SimDevice &)=delete
SimEnum CreateEnumDouble(const char *name, int32_t direction, std::span< const char *const > options, std::span< const double > optionValues, int32_t initialValue)
Creates an enumerated value on the simulated device with double values.
Definition: SimDevice.h:902
std::string GetName() const
Get the name of the simulated device.
Definition: SimDevice.h:746
SimDevice & operator=(SimDevice &&rhs)
Definition: SimDevice.h:720
SimBoolean CreateBoolean(const char *name, int32_t direction, bool initialValue)
Creates a boolean value on the simulated device.
Definition: SimDevice.h:926
SimDevice(const char *name, int index)
Creates a simulated device.
SimDevice(const char *name)
Creates a simulated device.
Definition: SimDevice.h:672
SimDevice(SimDevice &&rhs)
Definition: SimDevice.h:716
SimEnum CreateEnum(const char *name, int32_t direction, std::initializer_list< const char * > options, int32_t initialValue)
Creates an enumerated value on the simulated device.
Definition: SimDevice.h:827
Direction
Direction of a simulated value (from the perspective of user code).
Definition: SimDevice.h:647
@ kInput
Definition: SimDevice.h:648
@ kBidir
Definition: SimDevice.h:650
@ kOutput
Definition: SimDevice.h:649
SimValue CreateValue(const char *name, int32_t direction, const HAL_Value &initialValue)
Creates a value on the simulated device.
Definition: SimDevice.h:761
SimEnum CreateEnumDouble(const char *name, int32_t direction, std::initializer_list< const char * > options, std::initializer_list< double > optionValues, int32_t initialValue)
Creates an enumerated value on the simulated device with double values.
Definition: SimDevice.h:873
SimDevice()=default
Default constructor that results in an "empty" object that is false in a boolean context.
SimDevice(const char *name, int index, int channel)
Creates a simulated device.
C++ wrapper around a HAL simulator double value handle.
Definition: SimDevice.h:533
void Set(double value)
Sets the simulated value.
Definition: SimDevice.h:561
void Reset()
Resets the simulated value to 0.
Definition: SimDevice.h:568
SimDouble()=default
Default constructor that results in an "empty" object that is false in a boolean context.
SimDouble(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValueDouble().
Definition: SimDevice.h:546
double Get() const
Gets the simulated value.
Definition: SimDevice.h:554
C++ wrapper around a HAL simulator enum value handle.
Definition: SimDevice.h:574
SimEnum()=default
Default constructor that results in an "empty" object that is false in a boolean context.
int32_t Get() const
Gets the simulated value.
Definition: SimDevice.h:595
void Set(int32_t value)
Sets the simulated value.
Definition: SimDevice.h:602
SimEnum(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValueEnum().
Definition: SimDevice.h:587
C++ wrapper around a HAL simulator int value handle.
Definition: SimDevice.h:451
SimInt()=default
Default constructor that results in an "empty" object that is false in a boolean context.
void Set(int32_t value)
Sets the simulated value.
Definition: SimDevice.h:479
void Reset()
Resets the simulated value to 0.
Definition: SimDevice.h:486
int32_t Get() const
Gets the simulated value.
Definition: SimDevice.h:472
SimInt(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValueInt().
Definition: SimDevice.h:464
C++ wrapper around a HAL simulator long value handle.
Definition: SimDevice.h:492
int64_t Get() const
Gets the simulated value.
Definition: SimDevice.h:513
void Reset()
Resets the simulated value to 0.
Definition: SimDevice.h:527
SimLong()=default
Default constructor that results in an "empty" object that is false in a boolean context.
SimLong(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValueLong().
Definition: SimDevice.h:505
void Set(int64_t value)
Sets the simulated value.
Definition: SimDevice.h:520
C++ wrapper around a HAL simulator value handle.
Definition: SimDevice.h:399
SimValue()=default
Default constructor that results in an "empty" object that is false in a boolean context.
void SetValue(const HAL_Value &value)
Sets the simulated value.
Definition: SimDevice.h:442
HAL_Value GetValue() const
Gets the simulated value.
Definition: SimDevice.h:435
HAL_SimValueHandle m_handle
Definition: SimDevice.h:445
SimValue(HAL_SimValueHandle val)
Wraps a simulated value handle as returned by HAL_CreateSimValue().
Definition: SimDevice.h:412
HAL_SimValueHandle HAL_CreateSimValueEnumDouble(HAL_SimDeviceHandle device, const char *name, int32_t direction, int32_t numOptions, const char **options, const double *optionValues, int32_t initialValue)
Creates an enumerated value on a simulated device with double values.
HAL_SimValueHandle HAL_CreateSimValueInt(HAL_SimDeviceHandle device, const char *name, int32_t direction, int32_t initialValue)
Creates an int value on a simulated device.
Definition: SimDevice.h:115
double HAL_GetSimValueDouble(HAL_SimValueHandle handle)
Gets a simulated value (double).
Definition: SimDevice.h:275
HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device, const char *name, int32_t direction, const struct HAL_Value *initialValue)
Creates a value on a simulated device.
int32_t HAL_GetSimValueEnum(HAL_SimValueHandle handle)
Gets a simulated value (enum).
Definition: SimDevice.h:287
HAL_SimValueHandle HAL_CreateSimValueBoolean(HAL_SimDeviceHandle device, const char *name, int32_t direction, HAL_Bool initialValue)
Creates a boolean value on a simulated device.
Definition: SimDevice.h:219
void HAL_SetSimValueEnum(HAL_SimValueHandle handle, int32_t value)
Sets a simulated value (enum).
Definition: SimDevice.h:360
void HAL_SetSimValueDouble(HAL_SimValueHandle handle, double value)
Sets a simulated value (double).
Definition: SimDevice.h:349
void HAL_FreeSimDevice(HAL_SimDeviceHandle handle)
Frees a simulated device.
void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value *value)
Sets a simulated value.
void HAL_SetSimValueLong(HAL_SimValueHandle handle, int64_t value)
Sets a simulated value (long).
Definition: SimDevice.h:338
HAL_SimDeviceHandle HAL_CreateSimDevice(const char *name)
Creates a simulated device.
HAL_SimValueHandle HAL_CreateSimValueEnum(HAL_SimDeviceHandle device, const char *name, int32_t direction, int32_t numOptions, const char **options, int32_t initialValue)
Creates an enumerated value on a simulated device.
HAL_SimValueHandle HAL_CreateSimValueDouble(HAL_SimDeviceHandle device, const char *name, int32_t direction, double initialValue)
Creates a double value on a simulated device.
Definition: SimDevice.h:155
HAL_SimValueHandle HAL_CreateSimValueLong(HAL_SimDeviceHandle device, const char *name, int32_t direction, int64_t initialValue)
Creates a long value on a simulated device.
Definition: SimDevice.h:135
void HAL_SetSimValueInt(HAL_SimValueHandle handle, int value)
Sets a simulated value (int).
Definition: SimDevice.h:327
HAL_SimValueDirection
Direction of a simulated value (from the perspective of user code).
Definition: SimDevice.h:33
HAL_Bool HAL_GetSimValueBoolean(HAL_SimValueHandle handle)
Gets a simulated value (boolean).
Definition: SimDevice.h:299
void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value *value)
Gets a simulated value.
int64_t HAL_GetSimValueLong(HAL_SimValueHandle handle)
Gets a simulated value (long).
Definition: SimDevice.h:263
const char * HAL_GetSimDeviceName(HAL_SimDeviceHandle handle)
Get the name of a simulated device.
void HAL_SetSimValueBoolean(HAL_SimValueHandle handle, HAL_Bool value)
Sets a simulated value (boolean).
Definition: SimDevice.h:371
int32_t HAL_GetSimValueInt(HAL_SimValueHandle handle)
Gets a simulated value (int).
Definition: SimDevice.h:251
void HAL_ResetSimValue(HAL_SimValueHandle handle)
Resets a simulated double or integral value to 0.
@ HAL_SimValueBidir
bidirectional between user code and simulator
Definition: SimDevice.h:36
@ HAL_SimValueOutput
output from user code to the simulator
Definition: SimDevice.h:35
@ HAL_SimValueInput
input to user code from the simulator
Definition: SimDevice.h:34
int32_t HAL_Bool
Definition: Types.h:73
HAL_Handle HAL_SimDeviceHandle
Definition: Types.h:53
HAL_Handle HAL_SimValueHandle
Definition: Types.h:55
#define HAL_kInvalidHandle
Definition: Types.h:15
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: ChipObject.h:40
constexpr const char * name(const T &)
HAL Entry Value.
Definition: Value.h:20
double v_double
Definition: Value.h:26
int64_t v_long
Definition: Value.h:25
HAL_Bool v_boolean
Definition: Value.h:22
int32_t v_int
Definition: Value.h:24
enum HAL_Type type
Definition: Value.h:28
union HAL_Value::@0 data
int32_t v_enum
Definition: Value.h:23