Valor 6800 1.0
Loading...
Searching...
No Matches
CANdleSensor.h
1
2#pragma once
3
4#include <bitset>
5#include <iostream>
6#include <memory>
7#include <string>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include <ctre/phoenix6/CANcoder.hpp>
13#include <ctre/phoenix6/core/CoreCANdle.hpp>
14#include <frc/TimedRobot.h>
15
16#include "valkyrie/sensors/BaseSensor.h"
17
18namespace valor {
19
26class CANdleSensor : public valor::BaseSensor<int>, public ctre::phoenix6::hardware::core::CoreCANdle {
27 public:
28 static constexpr frc::Color8Bit VALOR_GOLD{0xEE, 0xA8, 0x00};
29 static constexpr frc::Color8Bit VALOR_PURPLE{0xFF, 0x00, 0xFF};
30 static constexpr frc::Color8Bit RED{0xFF, 0x00, 0x00};
31 static constexpr frc::Color8Bit ORANGE{0xFF, 0x30, 0x00};
32 static constexpr frc::Color8Bit GREEN{0x00, 0xFF, 0x00};
33 static constexpr frc::Color8Bit BLUE{0x00, 0x00, 0xFF};
34 static constexpr frc::Color8Bit LIGHT_BLUE{0xFF, 0x00, 0xF9};
35 static constexpr frc::Color8Bit WHITE{0xFF, 0xFF, 0xFF};
36 static constexpr frc::Color8Bit OFF{0x00, 0x00, 0x00};
37
38 enum Priority { LOW, MEDIUM, HIGH };
39
46 static frc::Color8Bit cancoderMagnetHealthGetter(ctre::phoenix6::hardware::CANcoder* cancoder);
47
57 CANdleSensor(frc::TimedRobot& robot, int ledCount, int segments, int canID, std::string canbus = "baseCAN");
58
59 void setBrightness(double brightness, bool saveImmediately = true);
60 void setStripType(ctre::phoenix6::signals::StripTypeValue stripType, bool saveImmediately = true);
61
68 void setLED(int led, frc::Color8Bit color);
69
77 void setColor(int segment, frc::Color8Bit color, Priority priority = LOW);
78
85 void setColor(frc::Color8Bit color, Priority priority = LOW);
86
95 template <typename T>
96 void setAnimation(int segment, frc::Color8Bit color, Priority priority = LOW) {
97 if (priority < currentPriority)
98 return;
99 currentPriority = priority;
100 if (!segments[segment].animationSlot) {
101 for (size_t i = 0; i < slots.size(); i++) {
102 if (!slots.test(i)) {
103 segments[segment].animationSlot = i;
104 slots.set(i);
105 break;
106 }
107 }
108 }
109 auto controlRequest = std::make_unique<T>(segments[segment].startLed, segments[segment].endLed);
110 if constexpr (requires(T& t) { t.Color; })
111 controlRequest->Color = color;
112 controlRequest->Slot = segments[segment].animationSlot.value();
113 segments[segment].controlRequest = std::move(controlRequest);
114 }
115
123 template <typename T>
124 void setAnimation(frc::Color8Bit color, Priority priority = LOW) {
125 if (priority < currentPriority)
126 return;
127 currentPriority = priority;
128 slots.reset();
129 slots.set(0);
130 segments[0].animationSlot = 0;
131 for (size_t i = 1; i < segments.size(); i++) {
132 segments[i].animationSlot.reset();
133 segments[i].controlRequest.reset();
134 }
135 auto controlRequest = std::make_unique<T>(segments[1].startLed, segments.back().endLed);
136 controlRequest->Slot = 0;
137 if constexpr (requires(T& t) { t.Color; })
138 controlRequest->Color = color;
139 segments[0].controlRequest = std::move(controlRequest);
140 }
141
149
153 void reset() override;
154
155 private:
159 struct Segment {
160 std::unique_ptr<ctre::phoenix6::controls::ControlRequest> controlRequest;
161 std::optional<int> animationSlot;
162 int startLed;
163 int endLed;
164 };
165
166 Priority currentPriority;
167 std::vector<Segment> segments;
172 void calculate() override;
173
174 std::bitset<8> slots;
176 ctre::phoenix6::configs::CANdleConfiguration config;
177};
178} // namespace valor
Abstract class that all Valor sensors should implement.
Definition BaseSensor.h:27
Sensor for controlling the CANdle and associated LEDs.
Definition CANdleSensor.h:26
void clearAnimation(int segment)
Clears any active animation for a specific segment.
void setAnimation(frc::Color8Bit color, Priority priority=LOW)
Set an animation for all segments.
Definition CANdleSensor.h:124
CANdleSensor(frc::TimedRobot &robot, int ledCount, int segments, int canID, std::string canbus="baseCAN")
Construct a new CANdleSensor object.
static frc::Color8Bit cancoderMagnetHealthGetter(ctre::phoenix6::hardware::CANcoder *cancoder)
Get the health status of a CANcoder magnet.
void setAnimation(int segment, frc::Color8Bit color, Priority priority=LOW)
Set an animation for a specific segment.
Definition CANdleSensor.h:96
void setColor(frc::Color8Bit color, Priority priority=LOW)
Set the color for all segments.
void setColor(int segment, frc::Color8Bit color, Priority priority=LOW)
Set the color of a specific segment.
void reset() override
Resets the CANdle device.
void setLED(int led, frc::Color8Bit color)
Set the color of a specific LED.
T ReadLog(std::string_view field, const T &defaultValue={})
Read a value from NetworkTables for the given field.
Definition Loggable.h:343