2020-05-02 08:16:57 -04:00
|
|
|
#include "FirmwareUpdate.h"
|
2020-11-15 10:49:36 -05:00
|
|
|
#include <lvgl/lvgl.h>
|
|
|
|
#include "components/ble/BleController.h"
|
2020-05-02 08:16:57 -04:00
|
|
|
#include "../DisplayApp.h"
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
FirmwareUpdate::FirmwareUpdate(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Ble& bleController)
|
|
|
|
: Screen(app), bleController {bleController} {
|
2020-05-02 08:16:57 -04:00
|
|
|
|
2021-04-18 11:44:40 -04:00
|
|
|
lv_obj_t * backgroundLabel = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP);
|
|
|
|
lv_obj_set_size(backgroundLabel, 240, 240);
|
|
|
|
lv_obj_set_pos(backgroundLabel, 0, 0);
|
|
|
|
lv_label_set_text_static(backgroundLabel, "");
|
|
|
|
|
2020-10-04 07:53:11 -04:00
|
|
|
titleLabel = lv_label_create(lv_scr_act(), nullptr);
|
2020-05-02 08:16:57 -04:00
|
|
|
lv_label_set_text(titleLabel, "Firmware update");
|
|
|
|
lv_obj_set_auto_realign(titleLabel, true);
|
2020-10-04 07:53:11 -04:00
|
|
|
lv_obj_align(titleLabel, nullptr, LV_ALIGN_IN_TOP_MID, 0, 50);
|
2020-05-02 08:16:57 -04:00
|
|
|
|
2020-10-04 07:53:11 -04:00
|
|
|
bar1 = lv_bar_create(lv_scr_act(), nullptr);
|
2020-05-02 08:16:57 -04:00
|
|
|
lv_obj_set_size(bar1, 200, 30);
|
2020-10-04 07:53:11 -04:00
|
|
|
lv_obj_align(bar1, nullptr, LV_ALIGN_CENTER, 0, 0);
|
2020-05-02 08:16:57 -04:00
|
|
|
lv_bar_set_anim_time(bar1, 10);
|
|
|
|
lv_bar_set_range(bar1, 0, 100);
|
|
|
|
lv_bar_set_value(bar1, 0, LV_ANIM_OFF);
|
|
|
|
|
2020-10-04 07:53:11 -04:00
|
|
|
percentLabel = lv_label_create(lv_scr_act(), nullptr);
|
2020-05-02 08:16:57 -04:00
|
|
|
lv_label_set_text(percentLabel, "");
|
|
|
|
lv_obj_set_auto_realign(percentLabel, true);
|
|
|
|
lv_obj_align(percentLabel, bar1, LV_ALIGN_OUT_TOP_MID, 0, 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
FirmwareUpdate::~FirmwareUpdate() {
|
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FirmwareUpdate::Refresh() {
|
2021-04-18 13:28:14 -04:00
|
|
|
switch (bleController.State()) {
|
2020-06-01 03:22:54 -04:00
|
|
|
default:
|
|
|
|
case Pinetime::Controllers::Ble::FirmwareUpdateStates::Idle:
|
|
|
|
case Pinetime::Controllers::Ble::FirmwareUpdateStates::Running:
|
2021-04-18 13:28:14 -04:00
|
|
|
if (state != States::Running)
|
2020-06-01 03:22:54 -04:00
|
|
|
state = States::Running;
|
|
|
|
return DisplayProgression();
|
|
|
|
case Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated:
|
2021-04-18 13:28:14 -04:00
|
|
|
if (state != States::Validated) {
|
2020-06-01 03:22:54 -04:00
|
|
|
UpdateValidated();
|
|
|
|
state = States::Validated;
|
|
|
|
}
|
|
|
|
return running;
|
|
|
|
case Pinetime::Controllers::Ble::FirmwareUpdateStates::Error:
|
2021-04-18 13:28:14 -04:00
|
|
|
if (state != States::Error) {
|
2020-06-01 03:22:54 -04:00
|
|
|
UpdateError();
|
|
|
|
state = States::Error;
|
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FirmwareUpdate::DisplayProgression() const {
|
2020-05-02 08:16:57 -04:00
|
|
|
float current = bleController.FirmwareUpdateCurrentBytes() / 1024.0f;
|
|
|
|
float total = bleController.FirmwareUpdateTotalBytes() / 1024.0f;
|
|
|
|
int16_t pc = (current / total) * 100.0f;
|
|
|
|
sprintf(percentStr, "%d %%", pc);
|
|
|
|
lv_label_set_text(percentLabel, percentStr);
|
|
|
|
|
|
|
|
lv_bar_set_value(bar1, pc, LV_ANIM_OFF);
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2020-06-01 03:22:54 -04:00
|
|
|
void FirmwareUpdate::UpdateValidated() {
|
|
|
|
lv_label_set_recolor(percentLabel, true);
|
|
|
|
lv_label_set_text(percentLabel, "#00ff00 Image Ok!#");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FirmwareUpdate::UpdateError() {
|
|
|
|
lv_label_set_recolor(percentLabel, true);
|
|
|
|
lv_label_set_text(percentLabel, "#ff0000 Error!#");
|
|
|
|
}
|