James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 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 | #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 | |
| 16 | using namespace hal; |
| 17 | |
| 18 | namespace hal { |
| 19 | namespace init { |
| 20 | void InitializeSimDevice() {} |
| 21 | } // namespace init |
| 22 | } // namespace hal |
| 23 | |
| 24 | extern "C" { |
| 25 | |
| 26 | HAL_SimDeviceHandle HAL_CreateSimDevice(const char* name) { |
| 27 | hal::init::CheckInit(); |
| 28 | return SimSimDeviceData->CreateDevice(name); |
| 29 | } |
| 30 | |
| 31 | void HAL_FreeSimDevice(HAL_SimDeviceHandle handle) { |
| 32 | SimSimDeviceData->FreeDevice(handle); |
| 33 | } |
| 34 | |
| 35 | HAL_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 | |
| 42 | HAL_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 | |
| 51 | void HAL_GetSimValue(HAL_SimValueHandle handle, struct HAL_Value* value) { |
| 52 | *value = SimSimDeviceData->GetValue(handle); |
| 53 | } |
| 54 | |
| 55 | void HAL_SetSimValue(HAL_SimValueHandle handle, const struct HAL_Value* value) { |
| 56 | SimSimDeviceData->SetValue(handle, *value); |
| 57 | } |
| 58 | |
| 59 | hal::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 | |
| 67 | hal::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" |