Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main/apps/app_setup/app_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ void AppSetup::onOpen()
_destroy_menu = true;
_worker = std::make_unique<ButtonWorker>();
}},
{"Wi-Fi",
[&]() {
_destroy_menu = true;
_worker = std::make_unique<WifiWorker>();
}},
},
},
{
Expand Down
185 changes: 185 additions & 0 deletions main/apps/app_setup/workers/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <hal/hal.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>

using namespace smooth_ui_toolkit::lvgl_cpp;
Expand Down Expand Up @@ -228,6 +229,160 @@ class ButtonWorker::ButtonConfigView {
bool _save_requested = false;
};

class WifiWorker::WifiConfigView {
public:
explicit WifiConfigView(const Hal::WifiStatus& initialStatus)
{
_panel = std::make_unique<Container>(lv_screen_active());
_panel->align(LV_ALIGN_CENTER, 0, 0);
_panel->setSize(466, 466);
_panel->setRadius(0);
_panel->setBorderWidth(0);
_panel->setPaddingAll(0);
_panel->setBgColor(lv_color_hex(0x000000));
_panel->setBgOpa(LV_OPA_COVER);
_panel->removeFlag(LV_OBJ_FLAG_SCROLLABLE);

_title_label = std::make_unique<Label>(_panel->get());
_title_label->setText("Wi-Fi");
_title_label->setTextFont(&MontserratSemiBold26);
_title_label->setTextColor(lv_color_hex(0xFFFFFF));
_title_label->align(LV_ALIGN_TOP_MID, 0, 52);

_row = std::make_unique<Container>(_panel->get());
_row->setSize(374, 119);
_row->align(LV_ALIGN_TOP_MID, 0, 100);
_row->setBgColor(lv_color_hex(0x4C4C4C));
_row->setBorderWidth(0);
_row->setShadowWidth(0);
_row->setRadius(60);
_row->setPaddingAll(0);
_row->setBgOpa(LV_OPA_TRANSP);

_switch_label = std::make_unique<Label>(_row->get());
_switch_label->setText("Wi-Fi");
_switch_label->setTextFont(&lv_font_montserrat_24);
_switch_label->setTextColor(lv_color_hex(0xFFFFFF));
_switch_label->align(LV_ALIGN_LEFT_MID, 36, 0);

_switch_widget = std::make_unique<Switch>(_row->get());
_switch_widget->setSize(80, 44);
_switch_widget->align(LV_ALIGN_RIGHT_MID, -36, 0);
_switch_widget->setValue(initialStatus.enabled);
_switch_widget->setBgColor(lv_color_hex(0x3A3A3A), LV_PART_MAIN);
_switch_widget->setBgOpa(LV_OPA_COVER, LV_PART_MAIN);
_switch_widget->setBorderWidth(0, LV_PART_MAIN);
_switch_widget->setRadius(LV_RADIUS_CIRCLE, LV_PART_MAIN);
lv_obj_set_style_bg_color(_switch_widget->get(), lv_color_hex(0xFFFFFF), LV_PART_KNOB);
lv_obj_set_style_bg_opa(_switch_widget->get(), LV_OPA_COVER, LV_PART_KNOB);
lv_obj_set_style_border_width(_switch_widget->get(), 0, LV_PART_KNOB);
lv_obj_set_style_radius(_switch_widget->get(), LV_RADIUS_CIRCLE, LV_PART_KNOB);
_switch_widget->setBgColor(lv_color_hex(0x53BD65), LV_PART_INDICATOR | LV_STATE_CHECKED);
_switch_widget->setBgOpa(LV_OPA_COVER, LV_PART_INDICATOR | LV_STATE_CHECKED);
_switch_widget->setBorderWidth(0, LV_PART_INDICATOR | LV_STATE_CHECKED);
_switch_widget->onValueChanged().connect([this](bool enabled) {
if (_syncing_switch) {
return;
}
_pending_enabled = enabled;
_has_pending = true;
});

_status_label = std::make_unique<Label>(_panel->get());
_status_label->setTextFont(&lv_font_montserrat_22);
_status_label->setTextColor(lv_color_hex(0xD0D0D0));
_status_label->setWidth(360);
_status_label->setTextAlign(LV_TEXT_ALIGN_CENTER);
_status_label->setLongMode(LV_LABEL_LONG_MODE_WRAP);
_status_label->align(LV_ALIGN_CENTER, 0, 30);

_ok_button = std::make_unique<Button>(_panel->get());
_ok_button->align(LV_ALIGN_CENTER, 0, 175);
_ok_button->setSize(374, 130);
_ok_button->setRadius(77);
_ok_button->setBorderWidth(0);
_ok_button->setShadowWidth(0);
_ok_button->setBgColor(lv_color_hex(0x4AD78C));
_ok_button->label().setText("OK");
_ok_button->label().setTextFont(&lv_font_montserrat_28);
_ok_button->label().setTextColor(lv_color_hex(0x0F5831));
_ok_button->label().align(LV_ALIGN_CENTER, 0, 0);
_ok_button->onClick().connect([this]() { _done_requested = true; });

updateStatus(initialStatus);
}

bool consumeEnabledChange(bool& enabled)
{
if (!_has_pending) {
return false;
}
enabled = _pending_enabled;
_has_pending = false;
return true;
}

bool consumeDoneRequested()
{
bool requested = _done_requested;
_done_requested = false;
return requested;
}

void updateStatus(const Hal::WifiStatus& status)
{
if (_switch_widget) {
_syncing_switch = true;
_switch_widget->setValue(status.enabled);
_syncing_switch = false;
}

if (!_status_label) {
return;
}

_status_label->setText(buildStatusText(status));
}

private:
std::string buildStatusText(const Hal::WifiStatus& status) const
{
switch (status.mode) {
case Hal::WifiMode::Off:
return "Off";
case Hal::WifiMode::Connecting:
if (!status.ssid.empty()) {
return "Connecting\n" + status.ssid;
}
return status.message.empty() ? "Connecting" : status.message;
case Hal::WifiMode::Connected:
if (!status.ipAddress.empty()) {
return "Connected\n" + status.ssid + "\n" + status.ipAddress;
}
return "Connected\n" + status.ssid;
case Hal::WifiMode::ConfigAp:
return "Setup AP\n" + status.apSsid + "\n192.168.4.1";
case Hal::WifiMode::BadgeAp:
return "Badge AP active";
case Hal::WifiMode::Error:
return status.message.empty() ? "Wi-Fi error" : status.message;
}
return status.message;
}

std::unique_ptr<Container> _panel;
std::unique_ptr<Label> _title_label;
std::unique_ptr<Container> _row;
std::unique_ptr<Label> _switch_label;
std::unique_ptr<Switch> _switch_widget;
std::unique_ptr<Label> _status_label;
std::unique_ptr<Button> _ok_button;
bool _syncing_switch = false;
bool _pending_enabled = false;
bool _has_pending = false;
bool _done_requested = false;
};

} // namespace setup_workers

BrightnessWorker::BrightnessWorker()
Expand Down Expand Up @@ -305,3 +460,33 @@ void ButtonWorker::update()
ButtonWorker::~ButtonWorker()
{
}

WifiWorker::WifiWorker()
{
mclog::tagInfo(_tag, "start wifi worker");

_last_status = GetHAL().getWifiStatus();
_view = std::make_unique<WifiConfigView>(_last_status);
}

void WifiWorker::update()
{
if (_view) {
bool enabled = false;
if (_view->consumeEnabledChange(enabled)) {
GetHAL().setWifiEnabled(enabled, true);
}

const auto status = GetHAL().getWifiStatus();
_view->updateStatus(status);
_last_status = status;

if (_view->consumeDoneRequested()) {
_is_done = true;
}
}
}

WifiWorker::~WifiWorker()
{
}
17 changes: 17 additions & 0 deletions main/apps/app_setup/workers/workers.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ class ButtonWorker : public WorkerBase {
Hal::ButtonConfig _applied_config;
};

/**
* @brief
*
*/
class WifiWorker : public WorkerBase {
public:
WifiWorker();
~WifiWorker();
void update() override;

private:
class WifiConfigView;

std::unique_ptr<WifiConfigView> _view;
Hal::WifiStatus _last_status;
};

/**
* @brief
*
Expand Down
56 changes: 52 additions & 4 deletions main/apps/common/status_bar/status_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,57 @@ class StatuBarGesture {

namespace status_bar_view {

constexpr int _module_y_ofs = 8;
constexpr int _wifi_icon_x_ofs = -51;
constexpr int _battery_icon_x_ofs = -12;
constexpr int _battery_level_x_ofs = 39;
constexpr int _battery_level_width = 44;

class Widget {
public:
virtual ~Widget() = default;
virtual void update() = 0;
};

class WifiIndicator : public Widget {
public:
WifiIndicator(lv_obj_t* parent, uint32_t colorPrimary)
{
_icon = std::make_unique<Label>(parent);
_icon->setText(LV_SYMBOL_WIFI);
_icon->setTextFont(&lv_font_montserrat_16);
_icon->setTextColor(lv_color_hex(colorPrimary));
_icon->align(LV_ALIGN_CENTER, _wifi_icon_x_ofs, _module_y_ofs);

update();
}

void update() override
{
const auto status = GetHAL().getWifiStatus();

switch (status.mode) {
case Hal::WifiMode::Connected:
_icon->setHidden(false);
_icon->setOpa(LV_OPA_COVER);
break;
case Hal::WifiMode::Connecting:
case Hal::WifiMode::ConfigAp:
case Hal::WifiMode::BadgeAp:
_icon->setHidden(false);
_icon->setOpa(LV_OPA_50);
break;
case Hal::WifiMode::Off:
case Hal::WifiMode::Error:
_icon->setHidden(true);
break;
}
}

private:
std::unique_ptr<Label> _icon;
};

class BatteryIcon {
public:
BatteryIcon(lv_obj_t* parent, uint32_t colorSecondary, uint32_t colorPrimary)
Expand Down Expand Up @@ -169,14 +214,16 @@ class Battery : public Widget {
public:
Battery(lv_obj_t* parent, uint32_t colorSecondary, uint32_t colorPrimary)
{
_battery_icon = std::make_unique<BatteryIcon>(parent, colorSecondary, colorPrimary);
_battery_icon->align(LV_ALIGN_CENTER, _battery_icon_x_ofs, _module_y_ofs);

_label_level = std::make_unique<Label>(parent);
_label_level->setText("");
_label_level->setTextColor(lv_color_hex(colorPrimary));
_label_level->setTextFont(&lv_font_montserrat_16);
_label_level->align(LV_ALIGN_RIGHT_MID, -78, 8);

_battery_icon = std::make_unique<BatteryIcon>(parent, colorSecondary, colorPrimary);
_battery_icon->align(LV_ALIGN_RIGHT_MID, -43, 8);
_label_level->setWidth(_battery_level_width);
_label_level->setTextAlign(LV_TEXT_ALIGN_LEFT);
_label_level->align(LV_ALIGN_CENTER, _battery_level_x_ofs, _module_y_ofs);

update();
}
Expand Down Expand Up @@ -209,6 +256,7 @@ class StatusBarView {
_panel->removeFlag(LV_OBJ_FLAG_SCROLLABLE);
_panel->onClick().connect([this]() { hide(); });

_widgets.push_back(std::make_unique<WifiIndicator>(_panel->get(), colorPrimary));
_widgets.push_back(std::make_unique<Battery>(_panel->get(), colorSecondary, colorPrimary));

_panel->setPos(0, _pos_y_hide);
Expand Down
2 changes: 2 additions & 0 deletions main/hal/hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void Hal::init()
rtc_init();
button_init();
fs_init();
wifi_init();
}

/* -------------------------------------------------------------------------- */
Expand All @@ -67,6 +68,7 @@ std::uint32_t Hal::millis()

void Hal::feedTheDog()
{
wifi_tick();
vTaskDelay(1);
}

Expand Down
31 changes: 31 additions & 0 deletions main/hal/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,33 @@ class Hal {
void setButtonConfig(ButtonConfig config, bool saveToSettings = false);
const ButtonConfig& getButtonConfig(bool loadFromSettings = false);

/* ---------------------------------- Wi-Fi --------------------------------- */
enum class WifiMode {
Off,
Connecting,
Connected,
ConfigAp,
BadgeAp,
Error,
};

struct WifiStatus {
bool enabled = false;
WifiMode mode = WifiMode::Off;
bool connected = false;
bool configMode = false;
bool badgeMode = false;
std::string ssid;
std::string ipAddress;
std::string apSsid;
std::string apUrl;
std::string message;
};

void setWifiEnabled(bool enabled, bool saveToSettings = true);
bool isWifiEnabled(bool loadFromSettings = false);
WifiStatus getWifiStatus();

/* ---------------------------------- Badge --------------------------------- */
bool loadBadgeImage(lv_obj_t* image);
bool loadNextBadgeImage(lv_obj_t* image);
Expand Down Expand Up @@ -285,6 +312,10 @@ class Hal {
void rtc_init();
void button_init();
void fs_init();
void wifi_init();
void wifi_tick();
void wifi_enter_badge_ap();
void wifi_exit_badge_ap();
};

Hal& GetHAL();
Expand Down
Loading
Loading