add led matrix draw logic
This commit is contained in:
@@ -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
|
||||||
+12
-17
@@ -1,3 +1,5 @@
|
|||||||
|
#include "TrackedAircraft.h"
|
||||||
|
#include "LEDMatrix.cpp"
|
||||||
#include "Network.cpp"
|
#include "Network.cpp"
|
||||||
#include "SBS1Decoder.cpp"
|
#include "SBS1Decoder.cpp"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
@@ -21,21 +23,15 @@ class FlightWall {
|
|||||||
private:
|
private:
|
||||||
FlightWallOpts opts_;
|
FlightWallOpts opts_;
|
||||||
|
|
||||||
|
// hex ident, all other info
|
||||||
|
std::unordered_map<std::string, TrackedAircraft> 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 EARTH_RADIUS_M = 6371000.0;
|
||||||
static constexpr double DEG_TO_RAD = M_PI / 180.0;
|
static constexpr double DEG_TO_RAD = M_PI / 180.0;
|
||||||
|
|
||||||
struct TrackedAircraft {
|
|
||||||
std::optional<std::string> callsign; // Field 10
|
|
||||||
std::optional<int> altitude; // Field 11 (ft)
|
|
||||||
std::optional<double> ground_speed; // Field 12 (knots)
|
|
||||||
std::optional<double> track; // Field 13 (degrees)
|
|
||||||
std::optional<double> latitude; // Field 14
|
|
||||||
std::optional<double> longitude; // Field 15
|
|
||||||
std::optional<int> vertical_rate; // Field 16 (ft/min)
|
|
||||||
std::optional<bool> emergency; // Field 19
|
|
||||||
std::time_t last_seen;
|
|
||||||
};
|
|
||||||
|
|
||||||
static double get_distance(double lat1, double lon1, double lat2,
|
static double get_distance(double lat1, double lon1, double lat2,
|
||||||
double lon2) {
|
double lon2) {
|
||||||
double lat1Rad = lat1 * DEG_TO_RAD;
|
double lat1Rad = lat1 * DEG_TO_RAD;
|
||||||
@@ -50,7 +46,9 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FlightWall(FlightWallOpts opts) : opts_(opts) {}
|
FlightWall(FlightWallOpts opts) : opts_(opts) {
|
||||||
|
LEDMatrix m(&min_distance_aircraft);
|
||||||
|
}
|
||||||
|
|
||||||
int start() {
|
int start() {
|
||||||
Network n(opts_.server_ip, opts_.port);
|
Network n(opts_.server_ip, opts_.port);
|
||||||
@@ -58,10 +56,6 @@ public:
|
|||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
std::string line;
|
std::string line;
|
||||||
|
|
||||||
// hex ident, all other info
|
|
||||||
std::unordered_map<std::string, TrackedAircraft> tracked;
|
|
||||||
double min_distance = INT_MAX;
|
|
||||||
std::string min_aircraft_icao;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
ssize_t bytes_rec = recv(n.get_sock(), buffer, sizeof(buffer) - 1, 0);
|
ssize_t bytes_rec = recv(n.get_sock(), buffer, sizeof(buffer) - 1, 0);
|
||||||
@@ -148,6 +142,7 @@ public:
|
|||||||
if (parsed_dist < min_distance) {
|
if (parsed_dist < min_distance) {
|
||||||
min_distance = parsed_dist;
|
min_distance = parsed_dist;
|
||||||
min_aircraft_icao = parsed->hex_ident;
|
min_aircraft_icao = parsed->hex_ident;
|
||||||
|
min_distance_aircraft = tracked.find(parsed->hex_ident)->second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+171
@@ -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 <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();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
struct TrackedAircraft {
|
||||||
|
std::optional<std::string> callsign; // Field 10
|
||||||
|
std::optional<int> altitude; // Field 11 (ft)
|
||||||
|
std::optional<double> ground_speed; // Field 12 (knots)
|
||||||
|
std::optional<double> track; // Field 13 (degrees)
|
||||||
|
std::optional<double> latitude; // Field 14
|
||||||
|
std::optional<double> longitude; // Field 15
|
||||||
|
std::optional<int> vertical_rate; // Field 16 (ft/min)
|
||||||
|
std::optional<bool> emergency; // Field 19
|
||||||
|
std::time_t last_seen;
|
||||||
|
};
|
||||||
+1
Submodule external/rpi-rgb-led-matrix added at b4dc553312
Reference in New Issue
Block a user