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 "frc/AddressableLED.h"
|
|
|
|
|
|
|
|
|
|
#include <hal/AddressableLED.h>
|
2019-11-20 23:13:39 -05:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2019-11-20 14:48:14 -08:00
|
|
|
#include <hal/HALBase.h>
|
|
|
|
|
#include <hal/PWM.h>
|
|
|
|
|
#include <hal/Ports.h>
|
2019-11-17 16:39:38 -08:00
|
|
|
|
|
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2019-11-20 14:48:14 -08:00
|
|
|
AddressableLED::AddressableLED(int port) {
|
|
|
|
|
int32_t status = 0;
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2019-11-20 14:48:14 -08:00
|
|
|
m_pwmHandle = HAL_InitializePWMPort(HAL_GetPort(port), &status);
|
|
|
|
|
wpi_setHALErrorWithRange(status, 0, HAL_GetNumPWMChannels(), port);
|
|
|
|
|
if (m_pwmHandle == HAL_kInvalidHandle) {
|
|
|
|
|
return;
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 14:48:14 -08:00
|
|
|
m_handle = HAL_InitializeAddressableLED(m_pwmHandle, &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
if (m_handle == HAL_kInvalidHandle) {
|
|
|
|
|
HAL_FreePWMPort(m_pwmHandle, &status);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
2019-11-20 23:13:39 -05:00
|
|
|
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AddressableLEDs, port + 1);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 14:48:14 -08:00
|
|
|
AddressableLED::~AddressableLED() {
|
|
|
|
|
HAL_FreeAddressableLED(m_handle);
|
2019-11-17 16:39:38 -08:00
|
|
|
int32_t status = 0;
|
2019-11-20 14:48:14 -08:00
|
|
|
HAL_FreePWMPort(m_pwmHandle, &status);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetLength(int length) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDLength(m_handle, length, &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static_assert(sizeof(AddressableLED::LEDData) == sizeof(HAL_AddressableLEDData),
|
|
|
|
|
"LED Structs MUST be the same size");
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetData(wpi::ArrayRef<LEDData> ledData) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(),
|
|
|
|
|
&status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetData(std::initializer_list<LEDData> ledData) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(),
|
|
|
|
|
&status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetBitTiming(units::nanosecond_t lowTime0,
|
|
|
|
|
units::nanosecond_t highTime0,
|
|
|
|
|
units::nanosecond_t lowTime1,
|
|
|
|
|
units::nanosecond_t highTime1) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDBitTiming(
|
|
|
|
|
m_handle, lowTime0.to<int32_t>(), highTime0.to<int32_t>(),
|
|
|
|
|
lowTime1.to<int32_t>(), highTime1.to<int32_t>(), &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetSyncTime(units::microsecond_t syncTime) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDSyncTime(m_handle, syncTime.to<int32_t>(), &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::Start() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StartAddressableLEDOutput(m_handle, &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::Stop() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StopAddressableLEDOutput(m_handle, &status);
|
|
|
|
|
wpi_setHALError(status);
|
|
|
|
|
}
|
2019-11-29 15:16:57 -08:00
|
|
|
|
|
|
|
|
void AddressableLED::LEDData::SetHSV(int h, int s, int v) {
|
2020-04-06 02:08:52 -04:00
|
|
|
SetLED(Color::FromHSV(h, s, v));
|
2019-11-29 15:16:57 -08:00
|
|
|
}
|