From d05144fb7e8aefbd1f6b0865746b10284c6a5ce3 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Wed, 5 Aug 2026 14:55:56 +0200 Subject: [PATCH] It works!!! --- src/core/gfx.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/gfx.hpp | 4 +++ src/core/units.hpp | 5 ++++ src/main.cpp | 22 ++++++++++++-- 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/core/gfx.cpp create mode 100644 src/core/gfx.hpp create mode 100644 src/core/units.hpp diff --git a/src/core/gfx.cpp b/src/core/gfx.cpp new file mode 100644 index 0000000..71441d0 --- /dev/null +++ b/src/core/gfx.cpp @@ -0,0 +1,74 @@ +#include "config.hpp" +#include "hal/draw.hpp" +#include +#include +#include + +struct color_stop { + double value; + draw::color_t color; +}; + +draw::color_t interpolateColor(double value, + const std::array &stops) { + if (value <= stops.front().value) + return stops.front().color; + + if (value >= stops.back().value) + return stops.back().color; + + for (size_t i = 0; i < stops.size() - 1; ++i) { + if (value >= stops[i].value && value <= stops[i + 1].value) { + + double t = + (value - stops[i].value) / (stops[i + 1].value - stops[i].value); + + draw::color_t c0 = stops[i].color; + draw::color_t c1 = stops[i + 1].color; + + draw::color_t result; + result.r = static_cast(std::round(c0.r + t * (c1.r - c0.r))); + result.g = static_cast(std::round(c0.g + t * (c1.g - c0.g))); + result.b = static_cast(std::round(c0.b + t * (c1.b - c0.b))); + + return result; + } + } + + // Fallback + return stops.back().color; +} + +#ifdef METRIC +draw::color_t get_altitude_color(uint32_t value) { + static constexpr std::array stops = {{ +#ifdef METRIC + {0, {220, 90, 20}}, + {150, {245, 115, 25}}, + {300, {250, 150, 30}}, + {600, {245, 180, 20}}, + {1200, {225, 200, 20}}, + {1800, {175, 215, 30}}, + {2400, {100, 225, 50}}, + {3000, {40, 210, 110}}, + {6000, {40, 175, 215}}, + {9000, {50, 110, 240}}, + {12000, {140, 50, 220}} +#endif +#ifndef METRIC + {0, {220, 90, 20}}, // Brown/Dark Orange + {500, {245, 115, 25}}, // Orange + {1000, {250, 150, 30}}, // Light Orange + {2000, {245, 180, 20}}, // Yellow-Orange + {4000, {225, 200, 20}}, // Yellow + {6000, {175, 215, 30}}, // Yellow-Green + {8000, {100, 225, 50}}, // Light Green + {10000, {40, 210, 110}}, // Teal/Cyan + {20000, {40, 175, 215}}, // Cyan-Blue + {30000, {50, 110, 240}}, // Blue + {40000, {140, 50, 220}} // Purple +#endif + }}; + return interpolateColor(value, stops); +} +#endif \ No newline at end of file diff --git a/src/core/gfx.hpp b/src/core/gfx.hpp new file mode 100644 index 0000000..c77cf1f --- /dev/null +++ b/src/core/gfx.hpp @@ -0,0 +1,4 @@ +#pragma once +#include "hal/draw.hpp" +#include +draw::color_t get_altitude_color(uint32_t value); \ No newline at end of file diff --git a/src/core/units.hpp b/src/core/units.hpp new file mode 100644 index 0000000..f3982d8 --- /dev/null +++ b/src/core/units.hpp @@ -0,0 +1,5 @@ +#pragma once +template +static inline T feet_to_meters(T ft) { + return static_cast(ft * 0.3048); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 3403de0..a52e385 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "core/color.h" #include "core/config.hpp" #include "core/const.hpp" +#include "core/units.hpp" #include "hal/draw.hpp" #include "hal/hal.hpp" #include "hal/net.hpp" @@ -12,6 +13,7 @@ #include #include "core/global.hpp" #include "core/geo.hpp" +#include "core/gfx.hpp" int main() { if (hal::init() != 0) { @@ -76,12 +78,28 @@ int main() { nlohmann::json aircraft = net::http_get_json(std::format("{}/aircraft.json", base_url)); if (aircraft!= nullptr) { for(nlohmann::json &craft : aircraft["aircraft"]) { + + uint32_t altitude; + try { + if(craft["alt_baro"].is_string()) { + altitude = 0; + } else { + altitude =craft["alt_baro"].get(); + #ifdef METRIC + altitude = feet_to_meters(altitude); + #endif + } + } catch(...) { + std::clog << YELLOW"[WARNING]" RESET " aircraft with no alt data detected!\n"; + altitude = 0; + } + draw::color_t color= get_altitude_color(altitude); try { const auto [x_offset,y_offset] = get_offset_from_station(craft["lat"].get(), craft["lon"].get()); const uint16_t x_center = middle_x+((x_offset/1000)*pixels_per_km); const uint16_t y_center = middle_y+((y_offset/1000)*pixels_per_km); - draw::pixel(x_center, y_center, {0xff,0x68,0x00}); - draw::rhombus(x_center, y_center, 8/*TODO: remove magic*/,{0xff,0x68,0x00}); + draw::pixel(x_center, y_center, color); + draw::rhombus(x_center, y_center, 8/*TODO: remove magic*/,color); } catch(...) { std::clog << YELLOW"[WARNING]" RESET " aircraft with no lat/lon data detected!\n"; }