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.
|
2019-11-17 16:39:38 -08:00
|
|
|
|
|
|
|
|
#include "hal/AddressableLED.h"
|
|
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
2019-12-14 21:15:32 -08:00
|
|
|
#include <FRC_FPGA_ChipObject/fpgainterfacecapi/NiFpga_HMB.h>
|
|
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
#include "ConstantsInternal.h"
|
|
|
|
|
#include "DigitalInternal.h"
|
2019-11-18 15:25:04 -08:00
|
|
|
#include "HALInitializer.h"
|
2021-05-01 13:22:08 -07:00
|
|
|
#include "HALInternal.h"
|
2019-11-17 16:39:38 -08:00
|
|
|
#include "PortsInternal.h"
|
2020-10-03 12:21:03 -04:00
|
|
|
#include "hal/AddressableLEDTypes.h"
|
2019-11-17 16:39:38 -08:00
|
|
|
#include "hal/ChipObject.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
#include "hal/handles/LimitedHandleResource.h"
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
struct AddressableLED {
|
|
|
|
|
std::unique_ptr<tLED> led;
|
|
|
|
|
void* ledBuffer;
|
|
|
|
|
size_t ledBufferSize;
|
|
|
|
|
int32_t stringLength = 1;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
static LimitedHandleResource<
|
|
|
|
|
HAL_AddressableLEDHandle, AddressableLED, kNumAddressableLEDs,
|
|
|
|
|
HAL_HandleEnum::AddressableLED>* addressableLEDHandles;
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace hal::init {
|
2019-11-17 16:39:38 -08:00
|
|
|
void InitializeAddressableLED() {
|
|
|
|
|
static LimitedHandleResource<HAL_AddressableLEDHandle, AddressableLED,
|
|
|
|
|
kNumAddressableLEDs,
|
|
|
|
|
HAL_HandleEnum::AddressableLED>
|
|
|
|
|
alH;
|
|
|
|
|
addressableLEDHandles = &alH;
|
|
|
|
|
}
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace hal::init
|
2019-11-17 16:39:38 -08:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
HAL_AddressableLEDHandle HAL_InitializeAddressableLED(
|
|
|
|
|
HAL_DigitalHandle outputPort, int32_t* status) {
|
2019-11-18 15:25:04 -08:00
|
|
|
hal::init::CheckInit();
|
|
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
auto digitalPort =
|
|
|
|
|
hal::digitalChannelHandles->Get(outputPort, hal::HAL_HandleEnum::PWM);
|
|
|
|
|
|
|
|
|
|
if (!digitalPort) {
|
2019-11-18 15:25:04 -08:00
|
|
|
// If DIO was passed, channel error, else generic error
|
|
|
|
|
if (getHandleType(outputPort) == hal::HAL_HandleEnum::DIO) {
|
|
|
|
|
*status = HAL_LED_CHANNEL_ERROR;
|
|
|
|
|
} else {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
}
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (digitalPort->channel >= kNumPWMHeaders) {
|
|
|
|
|
*status = HAL_LED_CHANNEL_ERROR;
|
2019-11-17 16:39:38 -08:00
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto handle = addressableLEDHandles->Allocate();
|
|
|
|
|
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
|
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led.reset(tLED::create(status));
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
addressableLEDHandles->Free(handle);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->writeOutputSelect(digitalPort->channel, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
addressableLEDHandles->Free(handle);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->ledBuffer = nullptr;
|
|
|
|
|
led->ledBufferSize = 0;
|
|
|
|
|
|
|
|
|
|
uint32_t session = led->led->getSystemInterface()->getHandle();
|
|
|
|
|
|
2019-12-14 21:15:32 -08:00
|
|
|
*status = NiFpga_OpenHostMemoryBuffer(session, "HMB_0_LED", &led->ledBuffer,
|
|
|
|
|
&led->ledBufferSize);
|
2019-11-17 16:39:38 -08:00
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
addressableLEDHandles->Free(handle);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_FreeAddressableLED(HAL_AddressableLEDHandle handle) {
|
|
|
|
|
addressableLEDHandles->Free(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAddressableLEDOutputPort(HAL_AddressableLEDHandle handle,
|
|
|
|
|
HAL_DigitalHandle outputPort,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto digitalPort =
|
|
|
|
|
hal::digitalChannelHandles->Get(outputPort, hal::HAL_HandleEnum::PWM);
|
|
|
|
|
|
|
|
|
|
if (!digitalPort) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->writeOutputSelect(digitalPort->channel, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
|
|
|
|
|
int32_t length, int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 13:22:08 -07:00
|
|
|
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
|
2019-11-17 16:39:38 -08:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2021-05-01 13:22:08 -07:00
|
|
|
hal::SetLastError(status, "LED length must be less than or equal to " +
|
|
|
|
|
wpi::Twine(HAL_kAddressableLEDMaxLength) +
|
|
|
|
|
". " + wpi::Twine(length) + " was requested");
|
2019-11-17 16:39:38 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->strobeReset(status);
|
|
|
|
|
|
|
|
|
|
while (led->led->readPixelWriteIndex(status) != 0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->writeStringLength(length, status);
|
|
|
|
|
|
|
|
|
|
led->stringLength = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static_assert(sizeof(HAL_AddressableLEDData) == sizeof(uint32_t),
|
|
|
|
|
"LED Data must be 32 bit");
|
|
|
|
|
|
|
|
|
|
void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle,
|
|
|
|
|
const struct HAL_AddressableLEDData* data,
|
|
|
|
|
int32_t length, int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 13:22:08 -07:00
|
|
|
if (length > led->stringLength || length < 0) {
|
2019-11-17 16:39:38 -08:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2021-05-01 13:22:08 -07:00
|
|
|
hal::SetLastError(status, "Data length must be less than or equal to " +
|
|
|
|
|
wpi::Twine(led->stringLength) + ". " +
|
|
|
|
|
wpi::Twine(length) + " was requested");
|
2019-11-17 16:39:38 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::memcpy(led->ledBuffer, data, length * sizeof(HAL_AddressableLEDData));
|
|
|
|
|
|
|
|
|
|
asm("dmb");
|
|
|
|
|
|
|
|
|
|
led->led->strobeLoad(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAddressableLEDBitTiming(HAL_AddressableLEDHandle handle,
|
|
|
|
|
int32_t lowTime0NanoSeconds,
|
|
|
|
|
int32_t highTime0NanoSeconds,
|
|
|
|
|
int32_t lowTime1NanoSeconds,
|
|
|
|
|
int32_t highTime1NanoSeconds,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->writeLowBitTickTiming(1, highTime0NanoSeconds / 25, status);
|
|
|
|
|
led->led->writeLowBitTickTiming(0, lowTime0NanoSeconds / 25, status);
|
|
|
|
|
led->led->writeHighBitTickTiming(1, highTime1NanoSeconds / 25, status);
|
|
|
|
|
led->led->writeHighBitTickTiming(0, lowTime1NanoSeconds / 25, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetAddressableLEDSyncTime(HAL_AddressableLEDHandle handle,
|
|
|
|
|
int32_t syncTimeMicroSeconds,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->writeSyncTiming(syncTimeMicroSeconds, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_StartAddressableLEDOutput(HAL_AddressableLEDHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->strobeStart(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_StopAddressableLEDOutput(HAL_AddressableLEDHandle handle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto led = addressableLEDHandles->Get(handle);
|
|
|
|
|
if (!led) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
led->led->strobeAbort(status);
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|