Files
allwpilib/hal/src/main/native/sim/PWM.cpp

130 lines
3.8 KiB
C++
Raw Normal View History

// 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.
2025-11-07 19:56:21 -05:00
#include "wpi/hal/PWM.h"
#include <algorithm>
#include <cmath>
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
2016-07-02 23:19:14 -07:00
#include "PortsInternal.h"
#include "mockdata/PWMDataInternal.h"
2025-11-07 19:57:55 -05:00
#include "wpi/hal/handles/HandlesInternal.h"
2025-11-07 20:00:05 -05:00
using namespace wpi::hal;
2025-11-07 20:00:05 -05:00
namespace wpi::hal::init {
void InitializePWM() {}
2025-11-07 20:00:05 -05:00
} // namespace wpi::hal::init
extern "C" {
2025-01-30 18:59:34 -08:00
HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
const char* allocationLocation,
int32_t* status) {
2025-11-07 20:00:05 -05:00
wpi::hal::init::CheckInit();
2016-06-30 21:39:09 -07:00
2025-01-30 18:59:34 -08:00
if (channel < 0 || channel >= kNumPWMChannels) {
*status = RESOURCE_OUT_OF_RANGE;
2025-11-07 20:00:05 -05:00
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
return HAL_kInvalidHandle;
2016-06-30 21:39:09 -07:00
}
uint8_t origChannel = static_cast<uint8_t>(channel);
2016-06-30 21:39:09 -07:00
if (origChannel < kNumPWMHeaders) {
channel += kNumDigitalChannels; // remap Headers to end of allocations
} else {
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
2016-06-30 21:39:09 -07:00
}
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
&handle, status);
2016-06-30 21:39:09 -07:00
if (*status != 0) {
if (port) {
2025-11-07 20:00:05 -05:00
wpi::hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
2025-11-07 20:00:05 -05:00
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
2016-06-30 21:39:09 -07:00
port->channel = origChannel;
2016-06-30 21:39:09 -07:00
SimPWMData[origChannel].initialized = true;
2016-06-30 21:39:09 -07:00
// Disable output.
HAL_SetPWMPulseTimeMicroseconds(handle, 0, status);
2018-03-03 01:56:49 -08:00
port->previousAllocation = allocationLocation ? allocationLocation : "";
2016-06-30 21:39:09 -07:00
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
2016-06-30 21:39:09 -07:00
if (port == nullptr) {
return;
}
SimPWMData[port->channel].initialized = false;
2016-06-30 21:39:09 -07:00
digitalChannelHandles->Free(pwmPortHandle, HAL_HandleEnum::PWM);
}
HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
return channel < kNumPWMChannels && channel >= 0;
}
2016-06-30 21:39:09 -07:00
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) {
return;
}
SimPWMData[port->channel].simDevice = device;
}
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t value, int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
2016-06-30 21:39:09 -07:00
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].pulseMicrosecond = value;
}
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
2016-06-30 21:39:09 -07:00
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return SimPWMData[port->channel].pulseMicrosecond;
}
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].outputPeriod = period;
}
} // extern "C"