From fe6ba39d7493de621b6ae3a3d5c0e710a0c2adb4 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Wed, 24 Apr 2024 22:21:39 -0500 Subject: [PATCH] Get WeatherHelper working again --- src/displayapp/WeatherHelper.cpp | 41 +++++++++++++++++------------- src/displayapp/screens/Weather.cpp | 35 +++++++++++++++---------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/displayapp/WeatherHelper.cpp b/src/displayapp/WeatherHelper.cpp index 686d1334..3242821b 100644 --- a/src/displayapp/WeatherHelper.cpp +++ b/src/displayapp/WeatherHelper.cpp @@ -25,27 +25,31 @@ #include #include #include + using namespace Pinetime::Applications; //Linear gradient temperature color calculator :) - + int16_t WeatherHelper::RoundTemperature(int16_t temp) { return temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); } + std::tuple rgb565to888(int r, int g, int b) { + return std::tuple( + ( r * 527 + 23 ) >> 6, + ( g * 259 + 33 ) >> 6, + ( b * 527 + 23 ) >> 6 + ); + } const char* WeatherHelper::floatToRgbHex(lv_color_t rgb) { + + std::tuple tuple = rgb565to888(LV_COLOR_GET_R(rgb), LV_COLOR_GET_G(rgb), LV_COLOR_GET_B(rgb)); char *rgbHex = new char[7]; - snprintf(rgbHex, 7, "%02X%02X%02X", LV_COLOR_GET_R(rgb), LV_COLOR_GET_G(rgb), LV_COLOR_GET_B(rgb)); + snprintf(rgbHex, 7, "%02X%02X%02X", std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple)); return rgbHex; } lv_color_t hexToFloat(int rgb) { - //uint16_t r = ((rgb >> 16) & 0xFF); - //uint16_t g = ((rgb >> 8) & 0xFF); - //int16_t b = (rgb & 0xFF); - //NRF_LOG_INFO("hexToFloat: %i, %i, %i", r, g, b); - lv_color_t outputLv = lv_color_hex3(rgb); - NRF_LOG_INFO("output lv struct: %i, %i, %i", LV_COLOR_GET_R(outputLv), LV_COLOR_GET_G(outputLv), LV_COLOR_GET_B(outputLv)); - return outputLv; + return lv_color_hex(rgb); } float normalize(float value) { @@ -57,16 +61,19 @@ using namespace Pinetime::Applications; return value; } } - const lv_color_t lerp(lv_color_t pointA, lv_color_t pointB, float normalValue) { + // reference: https://dev.to/ndesmic/linear-color-gradients-from-scratch-1a0e - //std::tuple lerp(std::tuple pointA, std::tuple pointB, float normalValue) { + const lv_color_t lerp(lv_color_t pointA, lv_color_t pointB, float normalValue) { + std::tuple pointAtuple = rgb565to888(LV_COLOR_GET_R(pointA), LV_COLOR_GET_G(pointA), LV_COLOR_GET_B(pointA)); + std::tuple pointBtuple = rgb565to888(LV_COLOR_GET_R(pointB), LV_COLOR_GET_G(pointB), LV_COLOR_GET_B(pointB)); + NRF_LOG_INFO("Normal value: %f", normalValue); - auto redA = LV_COLOR_GET_R(pointA); - auto redB = LV_COLOR_GET_R(pointB); - auto greenA = LV_COLOR_GET_G(pointA); - auto greenB = LV_COLOR_GET_G(pointB); - auto blueA = LV_COLOR_GET_B(pointA); - auto blueB = LV_COLOR_GET_B(pointB); + auto redA = std::get<0>(pointAtuple); + auto redB = std::get<0>(pointBtuple); + auto greenA = std::get<1>(pointAtuple); + auto greenB = std::get<1>(pointBtuple); + auto blueA = std::get<2>(pointAtuple); + auto blueB = std::get<2>(pointBtuple); int outputRed = (redA + (redB - redA) * normalValue); int outputGreen = (greenA + (greenB - greenA) * normalValue); diff --git a/src/displayapp/screens/Weather.cpp b/src/displayapp/screens/Weather.cpp index 6758fb5a..109ab717 100644 --- a/src/displayapp/screens/Weather.cpp +++ b/src/displayapp/screens/Weather.cpp @@ -9,9 +9,9 @@ #include "displayapp/InfiniTimeTheme.h" using namespace Pinetime::Applications::Screens; - -namespace { /* +namespace { + lv_color_t TemperatureColor(int16_t temperature) { if (temperature <= 0) { // freezing return Colors::blue; @@ -22,7 +22,7 @@ namespace { } return Colors::orange; // normal } - */ + uint8_t TemperatureStyle(int16_t temperature) { if (temperature <= 0) { // freezing @@ -33,9 +33,10 @@ namespace { return LV_TABLE_PART_CELL6; } return LV_TABLE_PART_CELL5; // normal - } -} +} + } + */ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService) : settingsController {settingsController}, weatherService {weatherService} { @@ -135,9 +136,8 @@ void Weather::Refresh() { lv_label_set_text_fmt(temperature, "%d°%c", WeatherHelper::RoundTemperature(temp), tempUnit); lv_label_set_text_fmt(minTemperature, "%d°", WeatherHelper::RoundTemperature(minTemp)); - //color = WeatherHelper::floatToRgbHex(WeatherHelper::TemperatureColor(maxTemp)); lv_label_set_text_fmt(maxTemperature, "%d°", WeatherHelper::RoundTemperature(maxTemp)); - //delete[] color; + } else { lv_label_set_text(icon, ""); lv_label_set_text(condition, ""); @@ -157,8 +157,14 @@ void Weather::Refresh() { for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { int16_t maxTemp = optCurrentForecast->days[i].maxTemperature; int16_t minTemp = optCurrentForecast->days[i].minTemperature; - lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(maxTemp)); - lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(minTemp)); + + auto color = WeatherHelper::TemperatureColor(maxTemp); + lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, color); + lv_table_set_cell_type(forecast, 2, i, LV_TABLE_PART_CELL5); + + color = WeatherHelper::TemperatureColor(minTemp); + lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, color); + lv_table_set_cell_type(forecast, 3, i, LV_TABLE_PART_CELL6); if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp); @@ -183,11 +189,12 @@ void Weather::Refresh() { maxPadding[0] = '\0'; minPadding[diff] = '\0'; } - auto color = WeatherHelper::floatToRgbHex(WeatherHelper::TemperatureColor(minTemp)); - lv_table_set_cell_value_fmt(forecast, 2, i, "# #%s %s%d", color, maxPadding, maxTemp); - color = WeatherHelper::floatToRgbHex(WeatherHelper::TemperatureColor(maxTemp)); - lv_table_set_cell_value_fmt(forecast, 3, i, "# #%s %s%d", color, minPadding, minTemp); - delete[] color; + //auto color = WeatherHelper::TemperatureColor(maxTemp); + //lv_obj_set_style_local_text_color(forecast, 2, i, color); + lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding, maxTemp); + //color = WeatherHelper::TemperatureColor(minTemp); + //lv_obj_set_style_local_text_color(forecast, 3, i, color); + lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding, minTemp); } } else { for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {