From a2fcf82a9d030f40698977ef419e2c8cc107e129 Mon Sep 17 00:00:00 2001 From: Dylan Benzi Date: Sun, 15 Nov 2026 20:18:45 +0000 Subject: [PATCH] add led matrix draw logic --- .gitmodules | 3 + FlightWall.cpp | 29 +++--- LEDMatrix.cpp | 171 ++++++++++++++++++++++++++++++++++++ Makefile | 12 +++ TrackedAircraft.h | 16 ++++ external/rpi-rgb-led-matrix | 1 + 6 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 .gitmodules create mode 100644 LEDMatrix.cpp create mode 100644 Makefile create mode 100644 TrackedAircraft.h create mode 160000 external/rpi-rgb-led-matrix diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1df2e93 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/rpi-rgb-led-matrix"] + path = external/rpi-rgb-led-matrix + url = https://github.com/hzeller/rpi-rgb-led-matrix.git diff --git a/FlightWall.cpp b/FlightWall.cpp index f711a59..88ce128 100644 --- a/FlightWall.cpp +++ b/FlightWall.cpp @@ -1,3 +1,5 @@ +#include "TrackedAircraft.h" +#include "LEDMatrix.cpp" #include "Network.cpp" #include "SBS1Decoder.cpp" #include @@ -21,21 +23,15 @@ class FlightWall { private: FlightWallOpts opts_; + // hex ident, all other info + std::unordered_map tracked; + double min_distance = INT_MAX; + std::string min_aircraft_icao; + TrackedAircraft min_distance_aircraft; + static constexpr double EARTH_RADIUS_M = 6371000.0; static constexpr double DEG_TO_RAD = M_PI / 180.0; - struct TrackedAircraft { - std::optional callsign; // Field 10 - std::optional altitude; // Field 11 (ft) - std::optional ground_speed; // Field 12 (knots) - std::optional track; // Field 13 (degrees) - std::optional latitude; // Field 14 - std::optional longitude; // Field 15 - std::optional vertical_rate; // Field 16 (ft/min) - std::optional emergency; // Field 19 - std::time_t last_seen; - }; - static double get_distance(double lat1, double lon1, double lat2, double lon2) { double lat1Rad = lat1 * DEG_TO_RAD; @@ -50,7 +46,9 @@ private: } public: - FlightWall(FlightWallOpts opts) : opts_(opts) {} + FlightWall(FlightWallOpts opts) : opts_(opts) { + LEDMatrix m(&min_distance_aircraft); + } int start() { Network n(opts_.server_ip, opts_.port); @@ -58,10 +56,6 @@ public: char buffer[1024]; std::string line; - // hex ident, all other info - std::unordered_map tracked; - double min_distance = INT_MAX; - std::string min_aircraft_icao; while (true) { ssize_t bytes_rec = recv(n.get_sock(), buffer, sizeof(buffer) - 1, 0); @@ -148,6 +142,7 @@ public: if (parsed_dist < min_distance) { min_distance = parsed_dist; min_aircraft_icao = parsed->hex_ident; + min_distance_aircraft = tracked.find(parsed->hex_ident)->second; } } } diff --git a/LEDMatrix.cpp b/LEDMatrix.cpp new file mode 100644 index 0000000..df58cb5 --- /dev/null +++ b/LEDMatrix.cpp @@ -0,0 +1,171 @@ +#include "TrackedAircraft.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 + +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 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 + 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(); + } +}; diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4921ef8 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +LED_DIR = external/rpi-rgb-led-matrix +CXXFLAGS += -I$(LED_DIR)/include +LDFLAGS += -L$(LED_DIR)/lib -lrgbmatrix -lrt -lm -lpthread + +app: main.cpp $(LED_DIR)/lib/librgbmatrix.a + $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS) + +$(LED_DIR)/lib/librgbmatrix.a: + $(MAKE) -C $(LED_DIR)/lib + +clean: + rm -rf app diff --git a/TrackedAircraft.h b/TrackedAircraft.h new file mode 100644 index 0000000..9647801 --- /dev/null +++ b/TrackedAircraft.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +struct TrackedAircraft { + std::optional callsign; // Field 10 + std::optional altitude; // Field 11 (ft) + std::optional ground_speed; // Field 12 (knots) + std::optional track; // Field 13 (degrees) + std::optional latitude; // Field 14 + std::optional longitude; // Field 15 + std::optional vertical_rate; // Field 16 (ft/min) + std::optional emergency; // Field 19 + std::time_t last_seen; +}; diff --git a/external/rpi-rgb-led-matrix b/external/rpi-rgb-led-matrix new file mode 160000 index 0000000..b4dc553 --- /dev/null +++ b/external/rpi-rgb-led-matrix @@ -0,0 +1 @@ +Subproject commit b4dc553312315a5a8e562189f20d4e303e28cc24