/
StatusBar.cpp
180 lines (150 loc) · 5.22 KB
/
StatusBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/************************************************
* Status bar : status icons
*************************************************/
#include "StatusBar.h"
#include "Network.h"
/****
* callbacks
***/
static void stepClicked( lv_obj_t *, lv_event_t event ){
if(event == LV_EVENT_CLICKED){
Serial.println("RAZ podo");
ttgo->bma->resetStepCounter();
gui->updateStepCounter();
}
}
static void wifiClicked( lv_obj_t *, lv_event_t event ){
if(event == LV_EVENT_CLICKED){
Serial.println("WIFI clicked");
if( network.isActive() ){
/* Nothing on way : we can disconnect */
network.disconnect();
} else if( network.getStatus() == Network::net_status_t::WIFI_NOT_CONNECTED ||
network.getStatus() == Network::net_status_t::WIFI_FAILED ){
/* Not connected : we can connect */
network.connect();
} else
Serial.println("Network is processing");
}
}
/*****
* GUI
*****/
LV_IMG_DECLARE(foot_16px);
StatusBar::StatusBar( lv_obj_t *parent, const lv_obj_t *cloned ) :
Container( parent, cloned )
{
this->addStyle( mainStyle ); // Copy gui style
/***
* Customize style of the bar
***/
this->setBgOpacity( LV_OPA_40 );
this->setSize( LV_HOR_RES, BARHEIGHT );
/***
* Step counter related
***/
/* Create of button above step counter to reset it */
this->stepButton = new Button( mainStyle, this );
this->stepButton->setLayout( LV_LAYOUT_ROW_MID ); // child are horizontally aligned
this->stepButton->setSize( this->getWidth() / 2, this->getHeight() );
this->stepButton->Align( LV_ALIGN_IN_LEFT_MID ); // it is itself aligned on the left
this->stepButton->setBgOpacity( LV_OPA_0 );
/* Only an image of a foot */
this->stepIcon = new Image( this->stepButton );
this->stepIcon->Set( &foot_16px );
this->stepIcon->setClickable( false ); // Pass click to the parent
this->stepCounter = new Label( this->stepButton );
this->stepCounter->setText( "??????" );
this->stepCounter->setClickable( false ); // Pass click to the parent
this->stepButton->attachEventHandler( stepClicked );
this->updateStepCounter(); // Ensure the counter is not "????" :)
/***
* Battery related
*
* No need for a button around as no action is possible
* so using "classical" alignment
***/
this->batPercent = new Label( this ); // Battery value
this->batPercent->setText( "100%" );
this->batPercent->Align( LV_ALIGN_IN_RIGHT_MID );
this->batPercent->AutoRealign();
this->batIcon = new Image( this ); // corresponding icon
this->batIcon->seTexttFont( &lv_font_montserrat_16 );
this->batIcon->Set( LV_SYMBOL_BATTERY_FULL );
this->batIcon->Align( LV_ALIGN_OUT_LEFT_MID, this->batPercent->getMyself(), -5);
/***
* Wifi
*
* Set it clickable to enable/disable WiFi networking
****/
this->wifiIcon = new Image( this );
this->wifiIcon->seTexttFont( &lv_font_montserrat_16 );
this->wifiIcon->Set( LV_SYMBOL_WIFI );
this->wifiIcon->Recolor( LV_COLOR_WHITE ); // Apply the color
this->wifiIcon->Align( LV_ALIGN_OUT_LEFT_MID, this->batIcon, -15 );
this->wifiIcon->setIntensity();
this->wifiIcon->setClickable( true );
this->wifiIcon->attachEventHandler( wifiClicked );
}
void StatusBar::updateStepCounter( void ){
this->stepCounter->setText( String(ttgo->bma->getCounter()).c_str() );
}
void StatusBar::updateBatteryLevel( void ){
this->batPercent->setText( (String(ttgo->power->getBattPercentage())+'%').c_str() );
this->updateBatteryIcon( Gui::LV_ICON_UNKNOWN ); // Rethink the icon
}
void StatusBar::updateBatteryIcon( Gui::lv_icon_battery_t index ){
lv_color_t color = LV_COLOR_WHITE; // Default color
if( index == Gui::LV_ICON_UNKNOWN && ttgo->power->isChargeing() ) // Are we charging ?
index = Gui::LV_ICON_CHARGE;
if( index == Gui::LV_ICON_CHARGE )
color = LV_COLOR_BLUE;
else if( index >= Gui::LV_ICON_CALCULATION ){ // Determine as per battery level
int level = ttgo->power->getBattPercentage();
if(level > 95){
color = LV_COLOR_GREEN;
index = Gui::LV_ICON_BAT_FULL;
} else if(level > 80){
color = LV_COLOR_GREEN;
index = Gui::LV_ICON_BAT_3;
} else if(level > 45)
index = Gui::LV_ICON_BAT_2;
else if(level > 20){
color = LV_COLOR_ORANGE;
index = Gui::LV_ICON_BAT_1;
} else {
color = LV_COLOR_RED;
index = Gui::LV_ICON_BAT_EMPTY;
}
}
this->batIcon->Recolor( color ); // Apply the color
static const char *icons[] = {LV_SYMBOL_BATTERY_EMPTY, LV_SYMBOL_BATTERY_1, LV_SYMBOL_BATTERY_2, LV_SYMBOL_BATTERY_3, LV_SYMBOL_BATTERY_FULL, LV_SYMBOL_CHARGE};
this->batIcon->Set( icons[index] ); // And the icon
}
void StatusBar::updateNetwork( void ){
switch( network.getStatus() ){
case Network::net_status_t::WIFI_CONNECTING :
this->wifiIcon->Recolor(LV_COLOR_ORANGE);
break;
case Network::Network::net_status_t::WIFI_FAILED :
this->wifiIcon->Recolor(LV_COLOR_RED);
break;
case Network::Network::net_status_t::WIFI_CONNECTED :
this->wifiIcon->Recolor(LV_COLOR_LIME);
break;
case Network::net_status_t::WIFI_MQTT :
this->wifiIcon->Recolor(LV_COLOR_BLUE);
break;
default :
this->wifiIcon->Recolor(LV_COLOR_WHITE);
}
}
/*
* Utilities
*/
static void cbUpdBat( lv_task_t *tsk ){
((StatusBar *)(tsk->user_data))->updateBatteryLevel();
}
void StatusBar::initAutomation( void ){
this->upd_bat_task = lv_task_create( cbUpdBat, 30000, LV_TASK_PRIO_LOWEST, this );
}