#include "TrackedAircraft.h" #include "assets/logos/logos.h" #include "external/rpi-rgb-led-matrix/include/graphics.h" #include "external/rpi-rgb-led-matrix/include/led-matrix.h" #include #include #include #include #include #include #include #include #include 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 = nullptr; FrameCanvas *offscreen = nullptr; std::vector display_text; Text flight_operator = {"", 1, 14, 0, WHITE}; Text flight_number = {"", 1, 21, 0, WHITE}; Text altitude = {"", 40, 0, 0, WHITE}; Text speed = {"", 40, 7, 0, WHITE}; Text track = {"", 40, 14, 0, WHITE}; Text vertical_speed = {"", 40, 21, 0, WHITE}; const char *ga_logo_path = ""; const char *ufo_logo_path = ""; std::vector ga_logo; std::vector ufo_logo; const uint8_t *current_logo = nullptr; TrackedAircraft *current_aircraft; static std::vector load_logo(const char *path) { std::vector data; if (!path || !*path) { return data; } std::ifstream f(path, std::ios::binary); data.assign(std::istreambuf_iterator(f), {}); if (data.size() != LOGO_SIZE * LOGO_SIZE * 3) { data.clear(); } return data; } static const uint8_t *find_logo(const std::string &code) { for (int i = 0; i < LOGO_COUNT; i++) { if (code == LOGOS[i].code) { return LOGOS[i].rgb; } } return nullptr; } void draw_logo(int x, int y) { if (!current_logo) { return; } for (int row = 0; row < LOGO_SIZE; row++) { for (int col = 0; col < LOGO_SIZE; col++) { const uint8_t *px = current_logo + (row * LOGO_SIZE + col) * 3; offscreen->SetPixel(x + col, y + row, px[0], px[1], px[2]); } } } 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 get_callsign_structure(std::string callsign) { std::string operator_or_reg; std::string flight_number; int i = 0; while (i < static_cast(callsign.size()) && !std::isdigit(static_cast(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; matrix_options.brightness = 50; 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"); } offscreen = canvas->CreateFrameCanvas(); display_text = { {"ALT", 21, 0, 0, WHITE}, {"SPD", 21, 7, 0, WHITE}, {"TRK", 21, 14, 0, WHITE}, {"VS", 27, 21, 0, WHITE}, }; ga_logo = load_logo(ga_logo_path); ufo_logo = load_logo(ufo_logo_path); current_aircraft = t; } ~LEDMatrix() { delete canvas; } void draw_display_text() { offscreen->Clear(); draw_logo(1, 1); for (Text t : display_text) { rgb_matrix::DrawText(offscreen, font, t.x, t.y + font.baseline(), t.color, NULL, t.text.c_str(), t.spacing); } rgb_matrix::DrawText(offscreen, font, flight_number.x, flight_number.y + font.baseline(), flight_number.color, NULL, flight_number.text.c_str(), flight_number.spacing); rgb_matrix::DrawText(offscreen, font, flight_operator.x, flight_operator.y + font.baseline(), flight_operator.color, NULL, flight_operator.text.c_str(), flight_operator.spacing); rgb_matrix::DrawText(offscreen, font, altitude.x, altitude.y + font.baseline(), altitude.color, NULL, altitude.text.c_str(), altitude.spacing); rgb_matrix::DrawText(offscreen, font, speed.x, speed.y + font.baseline(), speed.color, NULL, speed.text.c_str(), speed.spacing); rgb_matrix::DrawText(offscreen, font, track.x, track.y + font.baseline(), track.color, NULL, track.text.c_str(), track.spacing); rgb_matrix::DrawText(offscreen, font, vertical_speed.x, vertical_speed.y + font.baseline(), vertical_speed.color, NULL, vertical_speed.text.c_str(), vertical_speed.spacing); offscreen = canvas->SwapOnVSync(offscreen); } void draw() { if (current_aircraft->callsign && !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(0, 4); current_logo = ga_logo.empty() ? nullptr : ga_logo.data(); // if other flight number } else { flight_operator.text = op; flight_number.text = num.substr(0, 4); current_logo = find_logo(op); if (!current_logo && !ufo_logo.empty()) { current_logo = ufo_logo.data(); } } } 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(); } };