WPILibC++  2020.3.2
SimDevice.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #ifdef __cplusplus
13 #include <initializer_list>
14 
15 #include <wpi/ArrayRef.h>
16 #endif
17 
18 #include "hal/Types.h"
19 #include "hal/Value.h"
20 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
50 HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name);
51 
60 void HAL_FreeSimDevice(HAL_SimDeviceHandle handle);
61 
74 HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device,
75  const char* name, HAL_Bool readonly,
76  const struct HAL_Value* initialValue);
77 
78 #ifdef __cplusplus
79 extern "C++" {
80 inline HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device,
81  const char* name,
82  HAL_Bool readonly,
83  const HAL_Value& initialValue) {
84  return HAL_CreateSimValue(device, name, readonly, &initialValue);
85 }
86 } // extern "C++"
87 #endif
88 
101 inline HAL_SimValueHandle HAL_CreateSimValueDouble(HAL_SimDeviceHandle device,
102  const char* name,
103  HAL_Bool readonly,
104  double initialValue) {
105  struct HAL_Value v = HAL_MakeDouble(initialValue);
106  return HAL_CreateSimValue(device, name, readonly, &v);
107 }
108 
125 HAL_SimValueHandle HAL_CreateSimValueEnum(HAL_SimDeviceHandle device,
126  const char* name, HAL_Bool readonly,
127  int32_t numOptions,
128  const char** options,
129  int32_t initialValue);
130 
143 inline HAL_SimValueHandle HAL_CreateSimValueBoolean(HAL_SimDeviceHandle device,
144  const char* name,
145  HAL_Bool readonly,
146  HAL_Bool initialValue) {
147  struct HAL_Value v = HAL_MakeBoolean(initialValue);
148  return HAL_CreateSimValue(device, name, readonly, &v);
149 }
150 
157 void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value* value);
158 
159 #ifdef __cplusplus
160 extern "C++" {
161 inline HAL_Value HAL_GetSimValue(HAL_SimValueHandle handle) {
162  HAL_Value v;
163  HAL_GetSimValue(handle, &v);
164  return v;
165 }
166 } // extern "C++"
167 #endif
168 
175 inline double HAL_GetSimValueDouble(HAL_SimValueHandle handle) {
176  struct HAL_Value v;
177  HAL_GetSimValue(handle, &v);
178  return v.type == HAL_DOUBLE ? v.data.v_double : 0.0;
179 }
180 
187 inline int32_t HAL_GetSimValueEnum(HAL_SimValueHandle handle) {
188  struct HAL_Value v;
189  HAL_GetSimValue(handle, &v);
190  return v.type == HAL_ENUM ? v.data.v_enum : 0;
191 }
192 
199 inline HAL_Bool HAL_GetSimValueBoolean(HAL_SimValueHandle handle) {
200  struct HAL_Value v;
201  HAL_GetSimValue(handle, &v);
202  return v.type == HAL_BOOLEAN ? v.data.v_boolean : 0;
203 }
204 
211 void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value* value);
212 
213 #ifdef __cplusplus
214 extern "C++" {
215 inline void HAL_SetSimValue(HAL_SimValueHandle handle, const HAL_Value& value) {
216  HAL_SetSimValue(handle, &value);
217 }
218 } // extern "C++"
219 #endif
220 
227 inline void HAL_SetSimValueDouble(HAL_SimValueHandle handle, double value) {
228  struct HAL_Value v = HAL_MakeDouble(value);
229  HAL_SetSimValue(handle, &v);
230 }
231 
238 inline void HAL_SetSimValueEnum(HAL_SimValueHandle handle, int32_t value) {
239  struct HAL_Value v = HAL_MakeEnum(value);
240  HAL_SetSimValue(handle, &v);
241 }
242 
249 inline void HAL_SetSimValueBoolean(HAL_SimValueHandle handle, HAL_Bool value) {
250  struct HAL_Value v = HAL_MakeBoolean(value);
251  HAL_SetSimValue(handle, &v);
252 }
253 
256 #ifdef __cplusplus
257 } // extern "C"
258 #endif
259 
260 #ifdef __cplusplus
261 namespace hal {
262 
266 class SimValue {
267  public:
272  SimValue() = default;
273 
279  /*implicit*/ SimValue(HAL_SimValueHandle val) // NOLINT(runtime/explicit)
280  : m_handle(val) {}
281 
288  explicit operator bool() const { return m_handle != HAL_kInvalidHandle; }
289 
295  operator HAL_SimValueHandle() const { return m_handle; }
296 
302  HAL_Value GetValue() const { return HAL_GetSimValue(m_handle); }
303 
309  void SetValue(const HAL_Value& value) { HAL_SetSimValue(m_handle, value); }
310 
311  protected:
312  HAL_SimValueHandle m_handle = HAL_kInvalidHandle;
313 };
314 
318 class SimDouble : public SimValue {
319  public:
324  SimDouble() = default;
325 
331  /*implicit*/ SimDouble(HAL_SimValueHandle val) // NOLINT(runtime/explicit)
332  : SimValue(val) {}
333 
339  double Get() const { return HAL_GetSimValueDouble(m_handle); }
340 
346  void Set(double value) { HAL_SetSimValueDouble(m_handle, value); }
347 };
348 
352 class SimEnum : public SimValue {
353  public:
358  SimEnum() = default;
359 
365  /*implicit*/ SimEnum(HAL_SimValueHandle val) // NOLINT(runtime/explicit)
366  : SimValue(val) {}
367 
373  int32_t Get() const { return HAL_GetSimValueEnum(m_handle); }
374 
380  void Set(int32_t value) { HAL_SetSimValueEnum(m_handle, value); }
381 };
382 
386 class SimBoolean : public SimValue {
387  public:
392  SimBoolean() = default;
393 
399  /*implicit*/ SimBoolean(HAL_SimValueHandle val) // NOLINT(runtime/explicit)
400  : SimValue(val) {}
401 
407  bool Get() const { return HAL_GetSimValueBoolean(m_handle); }
408 
414  void Set(bool value) { HAL_SetSimValueBoolean(m_handle, value); }
415 };
416 
420 class SimDevice {
421  public:
426  SimDevice() = default;
427 
441  explicit SimDevice(const char* name) : m_handle(HAL_CreateSimDevice(name)) {}
442 
457  SimDevice(const char* name, int index);
458 
474  SimDevice(const char* name, int index, int channel);
475 
481  /*implicit*/ SimDevice(HAL_SimDeviceHandle val) // NOLINT(runtime/explicit)
482  : m_handle(val) {}
483 
484  ~SimDevice() {
485  if (m_handle != HAL_kInvalidHandle) HAL_FreeSimDevice(m_handle);
486  }
487 
488  SimDevice(const SimDevice&) = delete;
489  SimDevice& operator=(const SimDevice&) = delete;
490 
491  SimDevice(SimDevice&& rhs) : m_handle(rhs.m_handle) {
492  rhs.m_handle = HAL_kInvalidHandle;
493  }
494 
495  SimDevice& operator=(SimDevice&& rhs) {
496  m_handle = rhs.m_handle;
497  rhs.m_handle = HAL_kInvalidHandle;
498  return *this;
499  }
500 
507  explicit operator bool() const { return m_handle != HAL_kInvalidHandle; }
508 
514  operator HAL_SimDeviceHandle() const { return m_handle; }
515 
527  SimValue CreateValue(const char* name, bool readonly,
528  const HAL_Value& initialValue) {
529  return HAL_CreateSimValue(m_handle, name, readonly, &initialValue);
530  }
531 
543  SimDouble CreateDouble(const char* name, bool readonly, double initialValue) {
544  return HAL_CreateSimValueDouble(m_handle, name, readonly, initialValue);
545  }
546 
561  SimEnum CreateEnum(const char* name, bool readonly,
562  std::initializer_list<const char*> options,
563  int32_t initialValue) {
564  return HAL_CreateSimValueEnum(m_handle, name, readonly, options.size(),
565  const_cast<const char**>(options.begin()),
566  initialValue);
567  }
568 
583  SimEnum CreateEnum(const char* name, bool readonly,
584  wpi::ArrayRef<const char*> options, int32_t initialValue) {
585  return HAL_CreateSimValueEnum(m_handle, name, readonly, options.size(),
586  const_cast<const char**>(options.data()),
587  initialValue);
588  }
589 
601  SimBoolean CreateBoolean(const char* name, bool readonly, bool initialValue) {
602  return HAL_CreateSimValueBoolean(m_handle, name, readonly, initialValue);
603  }
604 
605  protected:
606  HAL_SimDeviceHandle m_handle = HAL_kInvalidHandle;
607 };
608 
609 } // namespace hal
610 #endif // __cplusplus
HAL_SetSimValue
void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value *value)
Sets a simulated value.
HAL_GetSimValueEnum
int32_t HAL_GetSimValueEnum(HAL_SimValueHandle handle)
Gets a simulated value (enum).
Definition: SimDevice.h:187
HAL_FreeSimDevice
void HAL_FreeSimDevice(HAL_SimDeviceHandle handle)
Frees a simulated device.
hal
WPILib Hardware Abstraction Layer (HAL) namespace.
Definition: UnsafeDIO.h:15
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
HAL_SetSimValueBoolean
void HAL_SetSimValueBoolean(HAL_SimValueHandle handle, HAL_Bool value)
Sets a simulated value (boolean).
Definition: SimDevice.h:249
HAL_CreateSimValueEnum
HAL_SimValueHandle HAL_CreateSimValueEnum(HAL_SimDeviceHandle device, const char *name, HAL_Bool readonly, int32_t numOptions, const char **options, int32_t initialValue)
Creates an enumerated value on a simulated device.
HAL_GetSimValueBoolean
HAL_Bool HAL_GetSimValueBoolean(HAL_SimValueHandle handle)
Gets a simulated value (boolean).
Definition: SimDevice.h:199
HAL_SetSimValueEnum
void HAL_SetSimValueEnum(HAL_SimValueHandle handle, int32_t value)
Sets a simulated value (enum).
Definition: SimDevice.h:238
HAL_GetSimValueDouble
double HAL_GetSimValueDouble(HAL_SimValueHandle handle)
Gets a simulated value (double).
Definition: SimDevice.h:175
wpi::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
HAL_CreateSimValueDouble
HAL_SimValueHandle HAL_CreateSimValueDouble(HAL_SimDeviceHandle device, const char *name, HAL_Bool readonly, double initialValue)
Creates a double value on a simulated device.
Definition: SimDevice.h:101
HAL_Value
HAL Entry Value.
Definition: Value.h:23
HAL_CreateSimValue
HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device, const char *name, HAL_Bool readonly, const struct HAL_Value *initialValue)
Creates a value on a simulated device.
HAL_SetSimValueDouble
void HAL_SetSimValueDouble(HAL_SimValueHandle handle, double value)
Sets a simulated value (double).
Definition: SimDevice.h:227
HAL_CreateSimValueBoolean
HAL_SimValueHandle HAL_CreateSimValueBoolean(HAL_SimDeviceHandle device, const char *name, HAL_Bool readonly, HAL_Bool initialValue)
Creates a boolean value on a simulated device.
Definition: SimDevice.h:143
HAL_GetSimValue
void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value *value)
Gets a simulated value.
HAL_CreateSimDevice
HAL_SimDeviceHandle HAL_CreateSimDevice(const char *name)
Creates a simulated device.