Files
flightwall/LEDMatrix.cpp
T
2026-11-15 20:18:45 +00:00

172 lines
5.0 KiB
C++

#include "TrackedAircraft.h"
#include "external/rpi-rgb-led-matrix/include/graphics.h"
#include "external/rpi-rgb-led-matrix/include/led-matrix.h"
#include <getopt.h>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <vector>
using namespace rgb_matrix;
class LEDMatrix {
private:
struct Text {
std::string text;
int x = 0;
int y = 0;
int spacing = 0;
Color color{255, 255, 255};
};
Color WHITE{255, 255, 255};
Color BLACK{0, 0, 0};
Color FLOOD = BLACK;
RGBMatrix::Options matrix_options;
rgb_matrix::RuntimeOptions runtime_opt;
const char *bdf_6x12 = "./external/rpi-rgb-led-matrix/fonts/6x12.bdf";
rgb_matrix::Font font;
RGBMatrix *canvas;
std::vector<Text> display_text;
Text flight_operator = {"", 3, 17, 0, WHITE};
Text flight_number = {"", 3, 24, 0, WHITE};
Text altitude = {"FT", 41, 3, 0, WHITE};
Text speed = {"KTS", 41, 10, 0, WHITE};
Text track = {"DEG", 41, 17, 0, WHITE};
Text vertical_speed = {"VS", 41, 24, 0, WHITE};
TrackedAircraft *current_aircraft;
static bool parseColor(Color *c, const char *str) {
return sscanf(str, "%hhu,%hhu,%hhu", &c->r, &c->g, &c->b) == 3;
}
static bool FullSaturation(const Color &c) {
return (c.r == 0 || c.r == 255) && (c.g == 0 || c.g == 255) &&
(c.b == 0 || c.b == 255);
}
static std::pair<std::string, std::string>
get_callsign_structure(std::string callsign) {
std::string operator_or_reg;
std::string flight_number;
int i = 0;
while (!std::isdigit(callsign[i])) {
i++;
}
operator_or_reg = callsign.substr(0, i);
flight_number = callsign.substr(i);
return {operator_or_reg, flight_number};
}
public:
LEDMatrix() {};
LEDMatrix(TrackedAircraft *t) {
matrix_options.cols = 64;
runtime_opt.gpio_slowdown = 2;
// load font from bdf file
if (!font.LoadFont(bdf_6x12)) {
throw std::runtime_error("Couldn't load font");
}
// create canvas
canvas = RGBMatrix::CreateFromOptions(matrix_options, runtime_opt);
if (canvas == NULL) {
throw std::runtime_error("Canvas creation failed");
}
display_text = {
{"ALT", 24, 3, 0, WHITE},
{"SPD", 24, 10, 0, WHITE},
{"TRK", 24, 17, 0, WHITE},
{"FPM", 24, 24, 0, WHITE},
};
current_aircraft = t;
}
~LEDMatrix() { delete canvas; }
void draw_display_text() {
for (Text t : display_text) {
rgb_matrix::DrawText(canvas, font, t.x, t.y + font.baseline(), t.color,
NULL, t.text.c_str(), t.spacing);
}
rgb_matrix::DrawText(canvas, font, flight_number.x, flight_number.y + font.baseline(), flight_number.color,
NULL, flight_number.text.c_str(), flight_number.spacing);
rgb_matrix::DrawText(canvas, font, flight_operator.x, flight_operator.y + font.baseline(), flight_operator.color,
NULL, flight_operator.text.c_str(), flight_operator.spacing);
rgb_matrix::DrawText(canvas, font, altitude.x, altitude.y + font.baseline(), altitude.color,
NULL, altitude.text.c_str(), altitude.spacing);
rgb_matrix::DrawText(canvas, font, speed.x, speed.y + font.baseline(), speed.color,
NULL, speed.text.c_str(), speed.spacing);
rgb_matrix::DrawText(canvas, font, track.x, track.y + font.baseline(), track.color,
NULL, track.text.c_str(), track.spacing);
rgb_matrix::DrawText(canvas, font, vertical_speed.x, vertical_speed.y + font.baseline(), vertical_speed.color,
NULL, vertical_speed.text.c_str(), vertical_speed.spacing);
}
void draw() {
if (!current_aircraft->callsign->empty()) {
std::string current_callsign = *current_aircraft->callsign;
auto [op, num] = get_callsign_structure(current_callsign);
// if general November reg
if (op == "N") {
flight_operator.text = "GA";
flight_number.text = num.substr(num.length(), 4);
// if other flight number
} else {
flight_operator.text = op;
flight_number.text = num.substr(num.length(), 4);
}
}
if (*current_aircraft->altitude) {
int current_altitude = *current_aircraft->altitude;
// if over 10k feet show flight level
if (current_altitude >= 10'000) {
altitude.text = std::to_string(current_altitude).substr(0, 3);
// otherwise show full alt
} else {
altitude.text = std::to_string(current_altitude);
}
}
if (*current_aircraft->ground_speed) {
int current_ground_speed = *current_aircraft->ground_speed;
speed.text = std::to_string(current_ground_speed);
}
if (*current_aircraft->track) {
int current_track = *current_aircraft->track;
track.text = std::to_string(current_track);
}
if (*current_aircraft->vertical_rate) {
int current_vertical_speed = *current_aircraft->vertical_rate;
vertical_speed.text = std::to_string(current_vertical_speed);
}
draw_display_text();
}
};