2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/AnalogOutput.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
#include "AnalogInternal.h"
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Errors.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
#include "hal/handles/IndexedHandleResource.h"
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
2016-06-27 11:32:40 -07:00
|
|
|
namespace {
|
2017-08-23 22:07:46 -07:00
|
|
|
|
2016-06-27 11:32:40 -07:00
|
|
|
struct AnalogOutput {
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t channel;
|
2016-06-27 11:32:40 -07:00
|
|
|
};
|
2017-08-23 22:07:46 -07:00
|
|
|
|
|
|
|
|
} // namespace
|
2016-06-27 11:32:40 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput,
|
2017-12-10 18:02:07 -08:00
|
|
|
kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput>*
|
2016-06-27 11:32:40 -07:00
|
|
|
analogOutputHandles;
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace hal::init {
|
2017-12-10 18:02:07 -08:00
|
|
|
void InitializeAnalogOutput() {
|
|
|
|
|
static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput,
|
|
|
|
|
kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput>
|
|
|
|
|
aoH;
|
|
|
|
|
analogOutputHandles = &aoH;
|
|
|
|
|
}
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace hal::init
|
2017-12-10 18:02:07 -08:00
|
|
|
|
2016-05-26 22:14:25 -07:00
|
|
|
extern "C" {
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
|
|
|
|
|
int32_t* status) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2016-05-26 22:14:25 -07:00
|
|
|
initializeAnalog(status);
|
2016-06-05 15:23:58 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (*status != 0) {
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
2016-06-05 15:23:58 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
int16_t channel = getPortHandleChannel(portHandle);
|
|
|
|
|
if (channel == InvalidHandleIndex) {
|
2016-06-05 15:23:58 -07:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-27 11:32:40 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-10 18:02:07 -08:00
|
|
|
HAL_AnalogOutputHandle handle =
|
|
|
|
|
analogOutputHandles->Allocate(channel, status);
|
2016-06-27 11:32:40 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (*status != 0) {
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-06-27 11:32:40 -07:00
|
|
|
|
2017-12-10 18:02:07 -08:00
|
|
|
auto port = analogOutputHandles->Get(handle);
|
2016-06-27 11:32:40 -07:00
|
|
|
if (port == nullptr) { // would only error on thread issue
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-05 15:23:58 -07:00
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
port->channel = static_cast<uint8_t>(channel);
|
2016-06-27 11:32:40 -07:00
|
|
|
return handle;
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_FreeAnalogOutputPort(HAL_AnalogOutputHandle analogOutputHandle) {
|
2016-06-27 11:32:40 -07:00
|
|
|
// no status, so no need to check for a proper free.
|
2017-12-10 18:02:07 -08:00
|
|
|
analogOutputHandles->Free(analogOutputHandle);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_SetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
double voltage, int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto port = analogOutputHandles->Get(analogOutputHandle);
|
2016-06-27 11:32:40 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 11:32:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-08-11 23:38:45 -07:00
|
|
|
uint16_t rawValue = static_cast<uint16_t>(voltage / 5.0 * 0x1000);
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (voltage < 0.0) {
|
2016-05-26 22:14:25 -07:00
|
|
|
rawValue = 0;
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (voltage > 5.0) {
|
2016-05-26 22:14:25 -07:00
|
|
|
rawValue = 0x1000;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
analogOutputSystem->writeMXP(port->channel, rawValue, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
double HAL_GetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2017-12-10 18:02:07 -08:00
|
|
|
auto port = analogOutputHandles->Get(analogOutputHandle);
|
2016-06-27 11:32:40 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-27 11:32:40 -07:00
|
|
|
return 0.0;
|
|
|
|
|
}
|
2016-05-26 22:14:25 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
uint16_t rawValue = analogOutputSystem->readMXP(port->channel, status);
|
2016-05-26 22:14:25 -07:00
|
|
|
|
|
|
|
|
return rawValue * 5.0 / 0x1000;
|
|
|
|
|
}
|
2017-08-23 22:07:46 -07:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_Bool HAL_CheckAnalogOutputChannel(int32_t channel) {
|
|
|
|
|
return channel < kNumAnalogOutputs && channel >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 22:07:46 -07:00
|
|
|
} // extern "C"
|