InfiniTime/src/displayapp/screens/Weather.cpp

151 lines
5.4 KiB
C++
Raw Normal View History

2021-06-09 17:44:49 -04:00
#include "Weather.h"
#include <lvgl/lvgl.h>
2021-06-24 18:18:56 -04:00
#include <components/ble/weather/WeatherService.h>
2021-06-09 17:44:49 -04:00
#include "../DisplayApp.h"
#include "Label.h"
#include "Version.h"
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/brightness/BrightnessController.h"
#include "components/datetime/DateTimeController.h"
#include "drivers/Watchdog.h"
#include "components/ble/weather/WeatherData.h"
using namespace Pinetime::Applications::Screens;
2021-06-24 18:18:56 -04:00
Weather::Weather(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::WeatherService& weather)
2021-06-09 17:44:49 -04:00
: Screen(app),
dateTimeController {dateTimeController},
2021-06-24 18:18:56 -04:00
weatherService(weather),
2021-06-09 17:44:49 -04:00
screens {app,
0,
{[this]() -> std::unique_ptr<Screen> {
2021-06-24 18:18:56 -04:00
return CreateScreenTemperature();
2021-06-09 17:44:49 -04:00
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreen2();
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreen3();
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreen4();
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreen5();
}},
Screens::ScreenListModes::UpDown} {
}
Weather::~Weather() {
lv_obj_clean(lv_scr_act());
}
bool Weather::Refresh() {
if (running) {
screens.Refresh();
}
return running;
}
bool Weather::OnButtonPushed() {
running = false;
return true;
}
bool Weather::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
return screens.OnTouchEvent(event);
}
2021-06-24 18:18:56 -04:00
std::unique_ptr<Screen> Weather::CreateScreenTemperature() {
2021-06-09 17:44:49 -04:00
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
2021-06-24 18:18:56 -04:00
Controllers::WeatherData::Temperature current = weatherService.GetCurrentTemperature();
2021-06-09 17:44:49 -04:00
lv_label_set_text_fmt(label,
2021-06-24 18:18:56 -04:00
"#FFFF00 Temperature#\n\n"
"#444444 %hd%%#°C \n\n"
"#444444 %hd#\n"
"%llu\n"
"%lu\n",
current.temperature,
current.dewPoint,
current.timestamp,
current.expires);
2021-06-09 17:44:49 -04:00
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
return std::unique_ptr<Screen>(new Screens::Label(0, 5, app, label));
}
std::unique_ptr<Screen> Weather::CreateScreen2() {
// uptime
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
2021-06-24 18:18:56 -04:00
lv_label_set_text_fmt(label, "#444444 Date# %02d\n", dateTimeController.Day());
2021-06-09 17:44:49 -04:00
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
return std::unique_ptr<Screen>(new Screens::Label(1, 4, app, label));
}
std::unique_ptr<Screen> Weather::CreateScreen3() {
lv_mem_monitor_t mon;
lv_mem_monitor(&mon);
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
lv_label_set_text_fmt(label,
" #444444 frag# %d%%\n"
2021-06-24 18:18:56 -04:00
" #444444 free# %d",
2021-06-09 17:44:49 -04:00
mon.used_pct,
2021-06-24 18:18:56 -04:00
mon.frag_pct);
2021-06-09 17:44:49 -04:00
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
return std::unique_ptr<Screen>(new Screens::Label(2, 5, app, label));
}
bool sortById(const TaskStatus_t& lhs, const TaskStatus_t& rhs) {
return lhs.xTaskNumber < rhs.xTaskNumber;
}
std::unique_ptr<Screen> Weather::CreateScreen4() {
TaskStatus_t tasksStatus[7];
2021-06-24 18:18:56 -04:00
lv_obj_t* infoTask = lv_table_create(lv_scr_act(), nullptr);
2021-06-09 17:44:49 -04:00
lv_table_set_col_cnt(infoTask, 3);
lv_table_set_row_cnt(infoTask, 8);
lv_obj_set_pos(infoTask, 10, 10);
lv_table_set_cell_value(infoTask, 0, 0, "#");
lv_table_set_col_width(infoTask, 0, 50);
lv_table_set_cell_value(infoTask, 0, 1, "Task");
lv_table_set_col_width(infoTask, 1, 80);
lv_table_set_cell_value(infoTask, 0, 2, "Free");
lv_table_set_col_width(infoTask, 2, 90);
auto nb = uxTaskGetSystemState(tasksStatus, 7, nullptr);
std::sort(tasksStatus, tasksStatus + nb, sortById);
for (uint8_t i = 0; i < nb; i++) {
lv_table_set_cell_value(infoTask, i + 1, 0, std::to_string(tasksStatus[i].xTaskNumber).c_str());
lv_table_set_cell_value(infoTask, i + 1, 1, tasksStatus[i].pcTaskName);
if (tasksStatus[i].usStackHighWaterMark < 20) {
std::string str1 = std::to_string(tasksStatus[i].usStackHighWaterMark) + " low";
lv_table_set_cell_value(infoTask, i + 1, 2, str1.c_str());
} else {
lv_table_set_cell_value(infoTask, i + 1, 2, std::to_string(tasksStatus[i].usStackHighWaterMark).c_str());
}
}
return std::unique_ptr<Screen>(new Screens::Label(3, 5, app, infoTask));
}
std::unique_ptr<Screen> Weather::CreateScreen5() {
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
lv_label_set_text_static(label,
"Software Licensed\n"
"under the terms of\n"
"the GNU General\n"
"Public License v3\n"
"#444444 Source code#\n"
"#FFFF00 https://github.com/#\n"
"#FFFF00 JF002/InfiniTime#");
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
return std::unique_ptr<Screen>(new Screens::Label(4, 5, app, label));
2021-06-24 17:43:30 -04:00
}