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 15:05:56 -08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
#include "../PortsInternal.h"
|
|
|
|
|
#include "AddressableLEDDataInternal.h"
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::hal;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::init {
|
2019-11-17 15:05:56 -08:00
|
|
|
void InitializeAddressableLEDData() {
|
|
|
|
|
static AddressableLEDData sad[kNumAddressableLEDs];
|
2025-11-07 20:00:05 -05:00
|
|
|
::wpi::hal::SimAddressableLEDData = sad;
|
2025-07-21 21:52:10 -07:00
|
|
|
static AddressableLEDDataBuffer buf;
|
2025-11-07 20:00:05 -05:00
|
|
|
::wpi::hal::SimAddressableLEDDataBuffer = &buf;
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
AddressableLEDData* wpi::hal::SimAddressableLEDData;
|
|
|
|
|
AddressableLEDDataBuffer* wpi::hal::SimAddressableLEDDataBuffer;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
|
|
|
|
void AddressableLEDData::ResetData() {
|
|
|
|
|
initialized.Reset(false);
|
2025-07-21 21:52:10 -07:00
|
|
|
start.Reset(0);
|
|
|
|
|
length.Reset(0);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
void AddressableLEDDataBuffer::SetData(int32_t start, int32_t len,
|
|
|
|
|
const HAL_AddressableLEDData* d) {
|
|
|
|
|
if ((start + len) > HAL_kAddressableLEDMaxLength) {
|
|
|
|
|
len = HAL_kAddressableLEDMaxLength - start;
|
|
|
|
|
}
|
|
|
|
|
if (len <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-11-17 15:05:56 -08:00
|
|
|
{
|
|
|
|
|
std::scoped_lock lock(m_dataMutex);
|
2025-07-21 21:52:10 -07:00
|
|
|
std::memcpy(&m_data[start], d, len * sizeof(d[0]));
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
data(reinterpret_cast<const uint8_t*>(d), len * sizeof(d[0]));
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
int32_t AddressableLEDDataBuffer::GetData(int32_t start, int32_t len,
|
|
|
|
|
HAL_AddressableLEDData* d) {
|
|
|
|
|
if ((start + len) > HAL_kAddressableLEDMaxLength) {
|
|
|
|
|
len = HAL_kAddressableLEDMaxLength - start;
|
|
|
|
|
}
|
|
|
|
|
if (len <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-11-17 15:05:56 -08:00
|
|
|
std::scoped_lock lock(m_dataMutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (d) {
|
2025-07-21 21:52:10 -07:00
|
|
|
std::memcpy(d, &m_data[start], len * sizeof(d[0]));
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-11-17 15:05:56 -08:00
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2020-07-04 10:10:43 -07:00
|
|
|
|
2019-11-17 15:05:56 -08:00
|
|
|
void HALSIM_ResetAddressableLEDData(int32_t index) {
|
|
|
|
|
SimAddressableLEDData[index].ResetData();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
int32_t HALSIM_GetAddressableLEDData(int32_t start, int32_t length,
|
2019-11-17 15:05:56 -08:00
|
|
|
struct HAL_AddressableLEDData* data) {
|
2025-07-21 21:52:10 -07:00
|
|
|
return SimAddressableLEDDataBuffer->GetData(start, length, data);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
void HALSIM_SetAddressableLEDData(int32_t start, int32_t length,
|
|
|
|
|
const struct HAL_AddressableLEDData* data) {
|
|
|
|
|
SimAddressableLEDDataBuffer->SetData(start, length, data);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
|
|
|
|
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, AddressableLED##CAPINAME, \
|
|
|
|
|
SimAddressableLEDData, LOWERNAME)
|
|
|
|
|
|
|
|
|
|
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
|
2025-07-21 21:52:10 -07:00
|
|
|
DEFINE_CAPI(int32_t, Start, start)
|
2019-11-17 15:05:56 -08:00
|
|
|
DEFINE_CAPI(int32_t, Length, length)
|
|
|
|
|
|
|
|
|
|
#undef DEFINE_CAPI
|
2025-07-21 21:52:10 -07:00
|
|
|
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
|
|
|
|
HAL_SIMCALLBACKREGISTRY_DEFINE_CAPI_NOINDEX( \
|
|
|
|
|
TYPE, HALSIM, AddressableLED##CAPINAME, SimAddressableLEDDataBuffer, \
|
|
|
|
|
LOWERNAME)
|
2019-11-17 15:05:56 -08:00
|
|
|
|
|
|
|
|
DEFINE_CAPI(HAL_ConstBufferCallback, Data, data)
|
|
|
|
|
|
|
|
|
|
#define REGISTER(NAME) \
|
|
|
|
|
SimAddressableLEDData[index].NAME.RegisterCallback(callback, param, \
|
|
|
|
|
initialNotify)
|
|
|
|
|
|
|
|
|
|
void HALSIM_RegisterAddressableLEDAllCallbacks(int32_t index,
|
|
|
|
|
HAL_NotifyCallback callback,
|
|
|
|
|
void* param,
|
|
|
|
|
HAL_Bool initialNotify) {
|
|
|
|
|
REGISTER(initialized);
|
2025-07-21 21:52:10 -07:00
|
|
|
REGISTER(start);
|
2019-11-17 15:05:56 -08:00
|
|
|
REGISTER(length);
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|