Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "hal/SimDevice.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
| 9 | #include "HALInitializer.h" |
| 10 | #include "mockdata/SimDeviceDataInternal.h" |
| 11 | |
| 12 | using namespace hal; |
| 13 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | void InitializeSimDevice() {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | extern "C" { |
| 19 | |
| 20 | HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) { |
| 21 | hal::init::CheckInit(); |
| 22 | return SimSimDeviceData->CreateDevice(name); |
| 23 | } |
| 24 | |
| 25 | void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) { |
| 26 | SimSimDeviceData->FreeDevice(handle); |
| 27 | } |
| 28 | |
| 29 | HAL_SimValueHandle HAL_CreateSimValue(HAL_SimDeviceHandle device, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | const char* name, int32_t direction, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | const struct HAL_Value* initialValue) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | return SimSimDeviceData->CreateValue(device, name, direction, 0, nullptr, |
| 33 | nullptr, *initialValue); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | HAL_SimValueHandle HAL_CreateSimValueEnum(HAL_SimDeviceHandle device, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | const char* name, int32_t direction, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | int32_t numOptions, |
| 39 | const char** options, |
| 40 | int32_t initialValue) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 41 | return SimSimDeviceData->CreateValue(device, name, direction, numOptions, |
| 42 | options, nullptr, |
| 43 | HAL_MakeEnum(initialValue)); |
| 44 | } |
| 45 | |
| 46 | HAL_SimValueHandle HAL_CreateSimValueEnumDouble( |
| 47 | HAL_SimDeviceHandle device, const char* name, int32_t direction, |
| 48 | int32_t numOptions, const char** options, const double* optionValues, |
| 49 | int32_t initialValue) { |
| 50 | return SimSimDeviceData->CreateValue(device, name, direction, numOptions, |
| 51 | options, optionValues, |
| 52 | HAL_MakeEnum(initialValue)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value* value) { |
| 56 | *value = SimSimDeviceData->GetValue(handle); |
| 57 | } |
| 58 | |
| 59 | void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value* value) { |
| 60 | SimSimDeviceData->SetValue(handle, *value); |
| 61 | } |
| 62 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 63 | void HAL_ResetSimValue(HAL_SimValueHandle handle) { |
| 64 | SimSimDeviceData->ResetValue(handle); |
| 65 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 67 | hal::SimDevice::SimDevice(const char* name, int index) { |
| 68 | m_handle = HAL_CreateSimDevice(fmt::format("{}[{}]", name, index).c_str()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | hal::SimDevice::SimDevice(const char* name, int index, int channel) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 72 | m_handle = HAL_CreateSimDevice( |
| 73 | fmt::format("{}[{},{}]", name, index, channel).c_str()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // extern "C" |