blk_box_lib/include/blk_box_drivers/leds.hpp
2026-04-01 22:11:48 -05:00

112 lines
2.7 KiB
C++

#ifndef LEDS_H
#define LEDS_H
#include <stdint.h>
const uint32_t LED_COUNT = 21;
const uint32_t LED_SHAPE_COUNT = 4;
const uint32_t LED_INDICATOR_COUNT = 17;
// We will store 7 LEDs worth of data in the RMT peripheral,
// so we can update all the LEDs in 3 transactions.
#define RMT_LED_SYMBOLS 7
// Each LED requires 24 RMT symbols (1 for each bit)
#define RMT_SYMBOLS_PER_LED 24
// 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)
class LEDColor {
private:
uint32_t color;
constexpr LEDColor(uint32_t color) : color(color) {}
public:
constexpr LEDColor(uint8_t r, uint8_t g, uint8_t b)
: color((static_cast<uint32_t>(r) << 16)
| (static_cast<uint32_t>(g) << 8)
| static_cast<uint32_t>(b)) {}
constexpr static LEDColor from_rgb(uint8_t r, uint8_t g, uint8_t b) {
return LEDColor(r, g, b);
}
static const LEDColor OFF;
static const LEDColor RED;
static const LEDColor RED_STRONG;
static const LEDColor ORANGE;
static const LEDColor ORANGE_STRONG;
static const LEDColor YELLOW;
static const LEDColor YELLOW_STRONG;
static const LEDColor GREEN;
static const LEDColor GREEN_STRONG;
static const LEDColor BLUE;
static const LEDColor BLUE_STRONG;
static const LEDColor PINK;
static const LEDColor PINK_STRONG;
static const LEDColor WHITE;
static const LEDColor WHITE_STRONG;
inline constexpr uint32_t value() const { return color; }
};
/// One of the shapes on the shape display.
enum class ShapeLed {
Shape1Led = 0,
Shape2Led = 1,
Shape3Led = 2,
Shape4Led = 3,
};
/// One of the indicator LEDs.
enum class IndicatorLED {
MODULE_SSEG = 4,
GAME_SSEG = 5,
TFT = 6,
MIC = 7,
IR_LED = 8,
SPEAKER = 9,
RFID = 10,
KEYPAD = 11,
LCD = 12,
S4 = 13,
S3 = 14,
S2 = 15,
S1 = 16,
B4 = 17,
B3 = 18,
B2 = 19,
B1 = 20,
};
/// @brief Initializes the indicator LEDs
void init_leds();
class LEDController {
private:
uint32_t led_colors[LED_COUNT];
static void set_led(uint32_t led, uint32_t color);
public:
/// Sets the color of an indicator LED.
///
/// Call `flush()` to send the data to the LEDs.
static void set_indicator(IndicatorLED led, LEDColor color) {
set_led(static_cast<uint32_t>(led), color.value());
}
/// Sets the color of an indicator LED.
///
/// Call `flush()` to send the data to the LEDs.
static void set_shape(ShapeLed led, LEDColor color) {
set_led(static_cast<uint32_t>(led), color.value());
}
/// Outputs the data to the leds.
static void flush();
/// Clears the LEDs
static void clear();
};
#endif // LEDS_H