blob: a8f8f8048e6504820edd7f417c16f99cf2d4964c [file] [log] [blame]
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08001/*----------------------------------------------------------------------------*/
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#include "hal/SimDevice.h"
9
10#include <wpi/SmallString.h>
11#include <wpi/raw_ostream.h>
12
13#include "HALInitializer.h"
14#include "mockdata/SimDeviceDataInternal.h"
15
16using namespace hal;
17
18namespace hal {
19namespace init {
20void InitializeSimDevice() {}
21} // namespace init
22} // namespace hal
23
24extern "C" {
25
26HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) {
27 hal::init::CheckInit();
28 return SimSimDeviceData->CreateDevice(name);
29}
30
31void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) {
32 SimSimDeviceData->FreeDevice(handle);
33}
34
35HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device,
36 const char* name, HAL_Bool readonly,
37 const struct HAL_Value* initialValue) {
38 return SimSimDeviceData->CreateValue(device, name, readonly, 0, nullptr,
39 *initialValue);
40}
41
42HAL_SimValueHandle HAL_CreateSimValueEnum(HAL_SimDeviceHandle device,
43 const char* name, HAL_Bool readonly,
44 int32_t numOptions,
45 const char** options,
46 int32_t initialValue) {
47 return SimSimDeviceData->CreateValue(device, name, readonly, numOptions,
48 options, HAL_MakeEnum(initialValue));
49}
50
51void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value* value) {
52 *value = SimSimDeviceData->GetValue(handle);
53}
54
55void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value* value) {
56 SimSimDeviceData->SetValue(handle, *value);
57}
58
59hal::SimDevice::SimDevice(const char* name, int index) {
60 wpi::SmallString<128> fullname;
61 wpi::raw_svector_ostream os(fullname);
62 os << name << '[' << index << ']';
63
64 m_handle = HAL_CreateSimDevice(fullname.c_str());
65}
66
67hal::SimDevice::SimDevice(const char* name, int index, int channel) {
68 wpi::SmallString<128> fullname;
69 wpi::raw_svector_ostream os(fullname);
70 os << name << '[' << index << ',' << channel << ']';
71
72 m_handle = HAL_CreateSimDevice(fullname.c_str());
73}
74
75} // extern "C"