mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal] Switch SmartIO to mrclib (#8960)
This commit is contained in:
@@ -66,7 +66,7 @@ void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
|||||||
SimDutyCycleData[dutyCycle->index].initialized = false;
|
SimDutyCycleData[dutyCycle->index].initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
|
void HAL_SetDutyCycleSimDevice(HAL_DutyCycleHandle handle,
|
||||||
HAL_SimDeviceHandle device) {
|
HAL_SimDeviceHandle device) {
|
||||||
auto dutyCycle = dutyCycleHandles->Get(handle);
|
auto dutyCycle = dutyCycleHandles->Get(handle);
|
||||||
if (dutyCycle == nullptr) {
|
if (dutyCycle == nullptr) {
|
||||||
|
|||||||
@@ -27,18 +27,9 @@
|
|||||||
|
|
||||||
using namespace wpi::hal;
|
using namespace wpi::hal;
|
||||||
|
|
||||||
#define IO_PREFIX "/io/"
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr const char* kLedsKey = IO_PREFIX "leds";
|
|
||||||
|
|
||||||
struct AddressableLEDs {
|
struct AddressableLEDs {
|
||||||
explicit AddressableLEDs(wpi::nt::NetworkTableInstance inst)
|
|
||||||
: rawPub{inst.GetRawTopic(kLedsKey).Publish(
|
|
||||||
"raw", {.periodic = 0.005, .sendAll = true})} {}
|
|
||||||
|
|
||||||
wpi::nt::RawPublisher rawPub;
|
|
||||||
uint8_t s_buffer[HAL_ADDRESSABLE_LED_MAX_LEN * 3];
|
uint8_t s_buffer[HAL_ADDRESSABLE_LED_MAX_LEN * 3];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -77,7 +68,7 @@ void ConvertAndCopyLEDData(void* dst, const struct HAL_AddressableLEDData* src,
|
|||||||
|
|
||||||
namespace wpi::hal::init {
|
namespace wpi::hal::init {
|
||||||
void InitializeAddressableLED() {
|
void InitializeAddressableLED() {
|
||||||
static AddressableLEDs leds_static{wpi::hal::GetSystemServer()};
|
static AddressableLEDs leds_static;
|
||||||
leds = &leds_static;
|
leds = &leds_static;
|
||||||
}
|
}
|
||||||
} // namespace wpi::hal::init
|
} // namespace wpi::hal::init
|
||||||
@@ -106,7 +97,8 @@ HAL_AddressableLEDHandle HAL_InitializeAddressableLED(
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status = port->InitializeMode(SmartIoMode::AddressableLED);
|
*status =
|
||||||
|
port->InitializeMode(MRC_SmartIOMode::MRC_SmartIOMode_AddressableLED);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::ADDRESSABLE_LED);
|
smartIoHandles->Free(handle, HAL_HandleEnum::ADDRESSABLE_LED);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
@@ -172,6 +164,6 @@ void HAL_SetAddressableLEDData(int32_t start, int32_t length,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ConvertAndCopyLEDData(&leds->s_buffer[start * 3], data, length, colorOrder);
|
ConvertAndCopyLEDData(&leds->s_buffer[start * 3], data, length, colorOrder);
|
||||||
leds->rawPub.Set(leds->s_buffer);
|
MRC_SmartIO_SetLedBuffer(leds->s_buffer, sizeof(leds->s_buffer));
|
||||||
}
|
}
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status = port->InitializeMode(SmartIoMode::AnalogInput);
|
*status = port->InitializeMode(MRC_SmartIOMode::MRC_SmartIOMode_AnalogInput);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::ANALOG_INPUT);
|
smartIoHandles->Free(handle, HAL_HandleEnum::ANALOG_INPUT);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ HAL_CounterHandle HAL_InitializeCounter(int channel, HAL_Bool risingEdge,
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status =
|
*status = port->InitializeMode(
|
||||||
port->InitializeMode(risingEdge ? SmartIoMode::SingleCounterRising
|
risingEdge ? MRC_SmartIOMode::MRC_SmartIOMode_SingleCounterRising
|
||||||
: SmartIoMode::SingleCounterFalling);
|
: MRC_SmartIOMode::MRC_SmartIOMode_SingleCounterFalling);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::COUNTER);
|
smartIoHandles->Free(handle, HAL_HandleEnum::COUNTER);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
|
|||||||
@@ -46,8 +46,9 @@ HAL_DigitalHandle HAL_InitializeDIOPort(int32_t channel, HAL_Bool input,
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status = port->InitializeMode(input ? SmartIoMode::DigitalInput
|
*status = port->InitializeMode(
|
||||||
: SmartIoMode::DigitalOutput);
|
input ? MRC_SmartIOMode::MRC_SmartIOMode_DigitalInput
|
||||||
|
: MRC_SmartIOMode::MRC_SmartIOMode_DigitalOutput);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::DIO);
|
smartIoHandles->Free(handle, HAL_HandleEnum::DIO);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
@@ -158,9 +159,9 @@ HAL_Bool HAL_GetDIODirection(HAL_DigitalHandle dioPortHandle, int32_t* status) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (port->currentMode) {
|
switch (port->currentMode) {
|
||||||
case SmartIoMode::DigitalInput:
|
case MRC_SmartIOMode::MRC_SmartIOMode_DigitalInput:
|
||||||
return true;
|
return true;
|
||||||
case SmartIoMode::DigitalOutput:
|
case MRC_SmartIOMode::MRC_SmartIOMode_DigitalOutput:
|
||||||
return false;
|
return false;
|
||||||
default:
|
default:
|
||||||
*status = HAL_INCOMPATIBLE_STATE;
|
*status = HAL_INCOMPATIBLE_STATE;
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status = port->InitializeMode(SmartIoMode::PwmInput);
|
*status = port->InitializeMode(MRC_SmartIOMode::MRC_SmartIOMode_PwmInput);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::DUTY_CYCLE);
|
smartIoHandles->Free(handle, HAL_HandleEnum::DUTY_CYCLE);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
@@ -68,7 +68,7 @@ void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
|||||||
while (port.use_count() != 1) {
|
while (port.use_count() != 1) {
|
||||||
auto current = wpi::hal::monotonic_clock::now();
|
auto current = wpi::hal::monotonic_clock::now();
|
||||||
if (start + std::chrono::seconds(1) < current) {
|
if (start + std::chrono::seconds(1) < current) {
|
||||||
std::puts("DIO handle free timeout");
|
std::puts("DutyCycle handle free timeout");
|
||||||
std::fflush(stdout);
|
std::fflush(stdout);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
|
void HAL_SetDutyCycleSimDevice(HAL_DutyCycleHandle handle,
|
||||||
HAL_SimDeviceHandle device) {}
|
HAL_SimDeviceHandle device) {}
|
||||||
|
|
||||||
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
|
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
|
|||||||
auto [handle, port] = *resource;
|
auto [handle, port] = *resource;
|
||||||
port->channel = channel;
|
port->channel = channel;
|
||||||
|
|
||||||
*status = port->InitializeMode(SmartIoMode::PwmOutput);
|
*status = port->InitializeMode(MRC_SmartIOMode::MRC_SmartIOMode_PwmOutput);
|
||||||
if (*status != 0) {
|
if (*status != 0) {
|
||||||
smartIoHandles->Free(handle, HAL_HandleEnum::PWM);
|
smartIoHandles->Free(handle, HAL_HandleEnum::PWM);
|
||||||
return HAL_INVALID_HANDLE;
|
return HAL_INVALID_HANDLE;
|
||||||
@@ -112,7 +112,7 @@ void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
*status = port->SetPwmMicroseconds(microsecondPulseTime);
|
*status = port->SetPwmOutputMicroseconds(microsecondPulseTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||||
@@ -124,7 +124,7 @@ int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint16_t microseconds = 0;
|
uint16_t microseconds = 0;
|
||||||
*status = port->GetPwmMicroseconds(µseconds);
|
*status = port->GetPwmOutputMicroseconds(µseconds);
|
||||||
return microseconds;
|
return microseconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,14 +138,17 @@ void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
|
|||||||
|
|
||||||
switch (period) {
|
switch (period) {
|
||||||
case 0:
|
case 0:
|
||||||
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k5ms);
|
*status = port->SetPwmOutputPeriod(
|
||||||
|
MRC_PwmOutputPeriod::MRC_PwmOutputPeriod_5ms);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k10ms);
|
*status = port->SetPwmOutputPeriod(
|
||||||
|
MRC_PwmOutputPeriod::MRC_PwmOutputPeriod_10ms);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k20ms);
|
*status = port->SetPwmOutputPeriod(
|
||||||
|
MRC_PwmOutputPeriod::MRC_PwmOutputPeriod_20ms);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
*status = HAL_PARAMETER_OUT_OF_RANGE;
|
*status = HAL_PARAMETER_OUT_OF_RANGE;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "SmartIo.hpp"
|
#include "SmartIo.hpp"
|
||||||
|
|
||||||
#include "HALInitializer.hpp"
|
#include "HALInitializer.hpp"
|
||||||
#include "SystemServerInternal.hpp"
|
#include "mrclib/SmartIO.h"
|
||||||
#include "wpi/hal/AddressableLEDTypes.h"
|
#include "wpi/hal/AddressableLEDTypes.h"
|
||||||
#include "wpi/hal/Errors.h"
|
#include "wpi/hal/Errors.h"
|
||||||
|
|
||||||
@@ -19,204 +19,118 @@ void InitializeSmartIo() {
|
|||||||
static DigitalHandleResource<HAL_DigitalHandle, SmartIo, kNumSmartIo> dcH;
|
static DigitalHandleResource<HAL_DigitalHandle, SmartIo, kNumSmartIo> dcH;
|
||||||
smartIoHandles = &dcH;
|
smartIoHandles = &dcH;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace init
|
} // namespace init
|
||||||
|
|
||||||
int32_t SmartIo::InitializeMode(SmartIoMode mode) {
|
namespace {
|
||||||
auto inst = wpi::hal::GetSystemServer();
|
struct SmartIoInitializer {
|
||||||
|
MRC_Status status = MRC_SmartIO_Initialize();
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
wpi::nt::PubSubOptions options;
|
SmartIo::~SmartIo() noexcept {
|
||||||
options.sendAll = true;
|
MRC_SmartIO_Close(channel);
|
||||||
options.keepDuplicates = true;
|
}
|
||||||
options.periodic = 0.005;
|
|
||||||
|
|
||||||
auto channelString = std::to_string(channel);
|
int32_t SmartIo::InitializeMode(MRC_SmartIOMode mode) {
|
||||||
auto subTableString = "/io/" + channelString + "/";
|
static SmartIoInitializer mrcSmartIo;
|
||||||
|
|
||||||
modePublisher =
|
if (mrcSmartIo.status != 0) {
|
||||||
inst.GetIntegerTopic(subTableString + "type").Publish(options);
|
return mrcSmartIo.status;
|
||||||
getSubscriber =
|
|
||||||
inst.GetIntegerTopic(subTableString + "valget").Subscribe(0, options);
|
|
||||||
periodGetSubscriber =
|
|
||||||
inst.GetIntegerTopic(subTableString + "periodget").Subscribe(0, options);
|
|
||||||
setPublisher =
|
|
||||||
inst.GetIntegerTopic(subTableString + "valset").Publish(options);
|
|
||||||
periodSetPublisher =
|
|
||||||
inst.GetIntegerTopic(subTableString + "periodset").Publish(options);
|
|
||||||
ledcountPublisher =
|
|
||||||
inst.GetIntegerTopic(subTableString + "ledcount").Publish(options);
|
|
||||||
ledoffsetPublisher =
|
|
||||||
inst.GetIntegerTopic(subTableString + "ledoffset").Publish(options);
|
|
||||||
|
|
||||||
currentMode = mode;
|
|
||||||
switch (mode) {
|
|
||||||
// These need to set a 0 output
|
|
||||||
case SmartIoMode::DigitalOutput:
|
|
||||||
case SmartIoMode::PwmOutput:
|
|
||||||
setPublisher.Set(0);
|
|
||||||
break;
|
|
||||||
case SmartIoMode::AddressableLED:
|
|
||||||
ledcountPublisher.Set(0);
|
|
||||||
ledoffsetPublisher.Set(0);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// These don't need to set any value
|
|
||||||
case SmartIoMode::DigitalInput:
|
|
||||||
case SmartIoMode::AnalogInput:
|
|
||||||
case SmartIoMode::PwmInput:
|
|
||||||
case SmartIoMode::SingleCounterRising:
|
|
||||||
case SmartIoMode::SingleCounterFalling:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
modePublisher.Set(static_cast<int>(mode));
|
MRC_Status ret = MRC_SmartIO_InitializeMode(channel, mode);
|
||||||
return 0;
|
if (ret == 0) {
|
||||||
|
currentMode = mode;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SwitchDioDirection(bool input) {
|
int32_t SmartIo::SwitchDioDirection(bool input) {
|
||||||
if (currentMode != SmartIoMode::DigitalInput &&
|
MRC_Status ret = MRC_SmartIO_SwitchDirection(channel, input);
|
||||||
currentMode != SmartIoMode::DigitalOutput) {
|
if (ret == 0) {
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
currentMode = input ? MRC_SmartIOMode::MRC_SmartIOMode_DigitalInput
|
||||||
|
: MRC_SmartIOMode::MRC_SmartIOMode_DigitalOutput;
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
modePublisher.Set(input ? 0 : 1);
|
|
||||||
currentMode = input ? SmartIoMode::DigitalInput : SmartIoMode::DigitalOutput;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SetDigitalOutput(bool value) {
|
int32_t SmartIo::SetDigitalOutput(bool value) {
|
||||||
if (currentMode != SmartIoMode::DigitalInput &&
|
return MRC_SmartIO_SetDigitalOutput(channel, value);
|
||||||
currentMode != SmartIoMode::DigitalOutput) {
|
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
|
||||||
}
|
|
||||||
setPublisher.Set(value ? 255.0 : 0.0);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetDigitalInput(bool* value) {
|
int32_t SmartIo::GetDigitalInput(bool* value) {
|
||||||
if (currentMode != SmartIoMode::DigitalInput &&
|
MRC_Bool val;
|
||||||
currentMode != SmartIoMode::DigitalOutput) {
|
int32_t status = MRC_SmartIO_GetDigitalInput(channel, &val);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
if (status == 0) {
|
||||||
|
*value = val ? true : false;
|
||||||
}
|
}
|
||||||
*value = getSubscriber.Get() != 0;
|
return status;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetPwmInputMicroseconds(uint16_t* microseconds) {
|
int32_t SmartIo::GetPwmInputMicroseconds(uint16_t* microseconds) {
|
||||||
if (currentMode != SmartIoMode::PwmInput) {
|
int32_t microsecondsInt;
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
int32_t status =
|
||||||
|
MRC_SmartIO_GetPwmInputMicroseconds(channel, µsecondsInt);
|
||||||
|
if (status == 0) {
|
||||||
|
*microseconds = microsecondsInt;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
int val = getSubscriber.Get();
|
|
||||||
*microseconds = val;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetPwmInputPeriodMicroseconds(uint16_t* microseconds) {
|
int32_t SmartIo::GetPwmInputPeriodMicroseconds(uint16_t* microseconds) {
|
||||||
if (currentMode != SmartIoMode::PwmInput) {
|
int32_t microsecondsInt;
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
int32_t status =
|
||||||
|
MRC_SmartIO_GetPwmInputPeriodMicroseconds(channel, µsecondsInt);
|
||||||
|
if (status == 0) {
|
||||||
|
*microseconds = microsecondsInt;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
int val = periodGetSubscriber.Get();
|
|
||||||
*microseconds = val;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SetPwmOutputPeriod(PwmOutputPeriod period) {
|
int32_t SmartIo::SetPwmOutputPeriod(MRC_PwmOutputPeriod period) {
|
||||||
if (currentMode != SmartIoMode::PwmOutput) {
|
return MRC_SmartIO_SetPwmOutputPeriod(channel, period);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (period) {
|
|
||||||
case PwmOutputPeriod::k20ms:
|
|
||||||
case PwmOutputPeriod::k10ms:
|
|
||||||
case PwmOutputPeriod::k5ms:
|
|
||||||
case PwmOutputPeriod::k2ms:
|
|
||||||
periodSetPublisher.Set(static_cast<int>(period));
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return HAL_PARAMETER_OUT_OF_RANGE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SetPwmMicroseconds(uint16_t microseconds) {
|
int32_t SmartIo::SetPwmOutputMicroseconds(uint16_t microseconds) {
|
||||||
if (currentMode != SmartIoMode::PwmOutput) {
|
MRC_Status ret = MRC_SmartIO_SetPwmOutputMicroseconds(channel, microseconds);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
if (ret != 0) {
|
||||||
|
setPwmOutputMicrosecondsValue = 0;
|
||||||
|
} else {
|
||||||
|
setPwmOutputMicrosecondsValue = microseconds;
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
if (microseconds > 4095) {
|
|
||||||
microseconds = 4095;
|
|
||||||
}
|
|
||||||
|
|
||||||
setPublisher.Set(microseconds);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetPwmMicroseconds(uint16_t* microseconds) {
|
int32_t SmartIo::GetPwmOutputMicroseconds(uint16_t* microseconds) {
|
||||||
if (currentMode != SmartIoMode::PwmOutput) {
|
*microseconds = setPwmOutputMicrosecondsValue;
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
return MRC_STATUS_SUCCESS;
|
||||||
}
|
|
||||||
|
|
||||||
int val = getSubscriber.Get();
|
|
||||||
|
|
||||||
// Get to 0-2, then scale to 0-4096;
|
|
||||||
*microseconds = val;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetAnalogInput(uint16_t* value) {
|
int32_t SmartIo::GetAnalogInput(uint16_t* value) {
|
||||||
if (currentMode != SmartIoMode::AnalogInput) {
|
int32_t valueInt;
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
int32_t status = MRC_SmartIO_GetAnalogInput(channel, &valueInt);
|
||||||
|
if (status == 0) {
|
||||||
|
*value = valueInt;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
int val = getSubscriber.Get();
|
|
||||||
|
|
||||||
*value = val;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::GetCounter(int32_t* value) {
|
int32_t SmartIo::GetCounter(int32_t* value) {
|
||||||
if (currentMode != SmartIoMode::SingleCounterFalling &&
|
int32_t valueInt;
|
||||||
currentMode != SmartIoMode::SingleCounterRising) {
|
int32_t status = MRC_SmartIO_GetCounter(channel, &valueInt);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
if (status == 0) {
|
||||||
|
*value = valueInt;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
int32_t val = getSubscriber.Get();
|
|
||||||
|
|
||||||
*value = val;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SetLedStart(int32_t start) {
|
int32_t SmartIo::SetLedStart(int32_t start) {
|
||||||
if (currentMode != SmartIoMode::AddressableLED) {
|
return MRC_SmartIO_SetLedStartIndex(channel, start);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
|
||||||
}
|
|
||||||
if (start < 0 || start >= HAL_ADDRESSABLE_LED_MAX_LEN) {
|
|
||||||
return HAL_PARAMETER_OUT_OF_RANGE;
|
|
||||||
}
|
|
||||||
ledoffsetPublisher.Set(start);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t SmartIo::SetLedLength(int32_t length) {
|
int32_t SmartIo::SetLedLength(int32_t length) {
|
||||||
if (currentMode != SmartIoMode::AddressableLED) {
|
return MRC_SmartIO_SetLedLength(channel, length);
|
||||||
return HAL_INCOMPATIBLE_STATE;
|
|
||||||
}
|
|
||||||
if (length < 0 || length >= HAL_ADDRESSABLE_LED_MAX_LEN) {
|
|
||||||
return HAL_PARAMETER_OUT_OF_RANGE;
|
|
||||||
}
|
|
||||||
ledcountPublisher.Set(length);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace wpi::hal
|
} // namespace wpi::hal
|
||||||
|
|||||||
@@ -7,48 +7,22 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "PortsInternal.hpp"
|
#include "PortsInternal.hpp"
|
||||||
|
#include "mrclib/SmartIO.h"
|
||||||
#include "wpi/hal/handles/DigitalHandleResource.hpp"
|
#include "wpi/hal/handles/DigitalHandleResource.hpp"
|
||||||
#include "wpi/nt/IntegerTopic.hpp"
|
|
||||||
|
|
||||||
namespace wpi::hal {
|
namespace wpi::hal {
|
||||||
|
|
||||||
constexpr int32_t kPwmDisabled = 0;
|
constexpr int32_t kPwmDisabled = 0;
|
||||||
constexpr int32_t kPwmAlwaysHigh = 0xFFFF;
|
constexpr int32_t kPwmAlwaysHigh = 0xFFFF;
|
||||||
|
|
||||||
enum class SmartIoMode {
|
|
||||||
DigitalInput = 0,
|
|
||||||
DigitalOutput = 1,
|
|
||||||
AnalogInput = 2,
|
|
||||||
PwmInput = 3,
|
|
||||||
PwmOutput = 4,
|
|
||||||
SingleCounterRising = 5,
|
|
||||||
SingleCounterFalling = 6,
|
|
||||||
AddressableLED = 13,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class PwmOutputPeriod {
|
|
||||||
k20ms = 0,
|
|
||||||
k10ms,
|
|
||||||
k5ms,
|
|
||||||
k2ms,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SmartIo {
|
struct SmartIo {
|
||||||
|
~SmartIo() noexcept;
|
||||||
uint8_t channel;
|
uint8_t channel;
|
||||||
std::string previousAllocation;
|
std::string previousAllocation;
|
||||||
SmartIoMode currentMode{SmartIoMode::DigitalInput};
|
MRC_SmartIOMode currentMode{MRC_SmartIOMode::MRC_SmartIOMode_DigitalInput};
|
||||||
wpi::nt::IntegerPublisher modePublisher;
|
uint16_t setPwmOutputMicrosecondsValue{0};
|
||||||
|
|
||||||
wpi::nt::IntegerPublisher setPublisher;
|
int32_t InitializeMode(MRC_SmartIOMode mode);
|
||||||
wpi::nt::IntegerSubscriber getSubscriber;
|
|
||||||
|
|
||||||
wpi::nt::IntegerPublisher periodSetPublisher;
|
|
||||||
wpi::nt::IntegerSubscriber periodGetSubscriber;
|
|
||||||
|
|
||||||
wpi::nt::IntegerPublisher ledcountPublisher;
|
|
||||||
wpi::nt::IntegerPublisher ledoffsetPublisher;
|
|
||||||
|
|
||||||
int32_t InitializeMode(SmartIoMode mode);
|
|
||||||
int32_t SwitchDioDirection(bool input);
|
int32_t SwitchDioDirection(bool input);
|
||||||
|
|
||||||
int32_t SetDigitalOutput(bool value);
|
int32_t SetDigitalOutput(bool value);
|
||||||
@@ -57,10 +31,9 @@ struct SmartIo {
|
|||||||
int32_t GetPwmInputMicroseconds(uint16_t* microseconds);
|
int32_t GetPwmInputMicroseconds(uint16_t* microseconds);
|
||||||
int32_t GetPwmInputPeriodMicroseconds(uint16_t* microseconds);
|
int32_t GetPwmInputPeriodMicroseconds(uint16_t* microseconds);
|
||||||
|
|
||||||
int32_t SetPwmOutputPeriod(PwmOutputPeriod period);
|
int32_t SetPwmOutputPeriod(MRC_PwmOutputPeriod period);
|
||||||
|
int32_t SetPwmOutputMicroseconds(uint16_t microseconds);
|
||||||
int32_t SetPwmMicroseconds(uint16_t microseconds);
|
int32_t GetPwmOutputMicroseconds(uint16_t* microseconds);
|
||||||
int32_t GetPwmMicroseconds(uint16_t* microseconds);
|
|
||||||
|
|
||||||
int32_t GetAnalogInput(uint16_t* value);
|
int32_t GetAnalogInput(uint16_t* value);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user