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