9#include "TinyMaterialDesign/Core/Page.h"
10#include "TinyMaterialDesign/Core/Screen.h"
11#include "TinyMaterialDesign/Draw/Icons.h"
12#include "TinyMaterialDesign/Widgets/Button.h"
13#include "TinyMaterialDesign/Widgets/IconButton.h"
14#include "TinyMaterialDesign/Widgets/Keyboard.h"
15#include "TinyMaterialDesign/Widgets/Label.h"
16#include "TinyMaterialDesign/Widgets/ListItem.h"
17#include "TinyMaterialDesign/Widgets/Menu.h"
18#include "TinyMaterialDesign/Widgets/SettingRows.h"
19#include "TinyMaterialDesign/Widgets/TextField.h"
20#include "TinyMaterialDesign/Wifi/WifiControl.h"
25
26
27
28
35 void setNetwork(
const char* ssid,
int bars,
bool secure,
bool current) {
42 void draw(tinygpu::ISurface<RGB_T>& target)
override {
44 const Bounds& b =
this->bounds;
45 const RGB_T bg = current_ ? t.colors.secondaryContainer : t.colors.surface;
46 target.fillRect(toPx(b.x + 1), toPx(b.y), toPx(b.w - 2), toPx(b.h), bg);
47 tinygpu::IFont<RGB_T>& body = *t.typography.body;
48 const RGB_T fg = current_ ? t.colors.onSecondaryContainer : t.colors.onSurface;
49 std::string name = ssid_;
50 const int32_t maxW = b
.w - 64;
51 while (name.size() > 3 &&
static_cast<int32_t>(body.measureTextWidth(name.c_str())) > maxW) name.pop_back();
52 body.drawText(target,
static_cast<int16_t>(b.x + 10),
53 static_cast<int16_t>(b.centerY() -
static_cast<int32_t>(body.getHeight(1)) / 2), name.c_str(), fg,
55 if (secure_) drawLock(target, Bounds(b.right() - 48, b.centerY() - 6, 9, 12), t.colors.onSurfaceVariant);
56 drawSignalBars(target, Bounds(b.right() - 30, b.centerY() - 7, 18, 14), bars_, t.colors.primary, t.colors.outline);
59 bool onGesture(
const tinygpu::GestureEvent& event)
override {
60 if (!isTapGesture(event.type))
return false;
61 if (onSelect) onSelect();
69 bool current_ =
false;
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
118 :
Page<RGB_T>(title, drawWifi<RGB_T>), keyboard_(keyboard), screen_(screen), showPassword_(
"Show password") {}
128 cancelText_ = text !=
nullptr ? text :
"";
130 cancel_.setLabel(cancelText_.c_str());
136 void connect(
const char* ssid,
const char* password) {
138 password_.setText(password);
144 if (wifi_ ==
nullptr || scanning_)
return;
147 if (popupOpen_) fillPopup();
158 screen_.presentDialog(popup_);
166 title_.setOnBackground(
true);
167 this->addChild(title_);
169 status_.setOnBackground(
true);
170 this->addChild(status_);
173 ssid_.setMaxLength(32);
174 ssid_.setOnBackground(
true);
176 this->addChild(ssid_);
179 this->addChild(list_);
181 password_.setMaxLength(63);
182 password_.setPasswordMode(
true);
183 password_.setOnBackground(
true);
184 password_.onSubmit = [
this]() { connectFromFields(); };
186 this->addChild(password_);
187 showPassword_.onChange = [
this](
bool v) { password_.setPasswordMode(!v); };
188 this->addChild(showPassword_);
191 connect_.onClick = [
this]() {
198 this->addChild(connect_);
199 cancel_ = Button<RGB_T>(Bounds(), cancelText_.c_str(), ButtonVariant::kOutlined);
200 cancel_.onClick = [
this]() {
202 if (onDone) onDone();
204 this->addChild(cancel_);
205 this->addChild(spacer_);
208 for (
int i = 0; i <
kMaxNetworks; ++i) rows_[i].onSelect = [
this, i]() { select(i); };
211 popup_.onOutsideTap = [
this]() { closePopup(); };
217 if (wifi_ !=
nullptr && ssid_.text().empty()) ssid_.setText(wifi_
->ssid().c_str());
221 this->setScrollOffset(0);
228 if (popupOpen_) closePopup();
233 bool tick(uint32_t nowMs)
override {
234 if (wifi_ ==
nullptr || nowMs - lastTickMs_ < 250)
return false;
236 bool changed =
false;
237 if (scanning_ && wifi_->scanDone(networks_)) {
239 if (networks_.size() > kMaxNetworks) networks_.resize(kMaxNetworks);
240 if (popupOpen_) fillPopup();
244 if (state != lastState_) {
250 if (doneAtMs_ != 0 &&
static_cast<int32_t>(nowMs - doneAtMs_) >= 0) {
252 if (onDone) onDone();
254 if (changed) refreshStatus();
263 bool scanning_ =
false;
264 bool popupOpen_ =
false;
266 uint32_t lastTickMs_ = 0;
267 uint32_t doneAtMs_ = 0;
268 std::vector<WifiNetwork> networks_;
269 std::string cancelText_ =
"Cancel";
271 Label<RGB_T> title_, status_, spacer_;
275 Button<RGB_T> connect_, cancel_;
283 keyboard_.manage(field);
284 std::function<
void()> showKeyboard = field.onFocus;
285 field.onFocus = [
this, showKeyboard, &field]() {
286 if (showKeyboard) showKeyboard();
287 this->scrollToChild(field);
288 screen_.invalidate();
294 const Bounds& b =
this->bounds;
295 const int count =
static_cast<
int>(networks_.size());
297 const int32_t maxH = b
.h - 40;
298 const int32_t h = std::min<int32_t>((count + 1) * rowH, maxH - maxH % rowH);
300 popup_.onOutsideTap = [
this]() { closePopup(); };
301 const std::string current =
303 for (
int i = 0; i < count; ++i) {
306 rows_[i].bounds = popup_.itemRect(i, rowH);
307 popup_.addItem(rows_[i]);
309 scanAgain_.setTitle(scanning_ ?
"Scanning ..." : count == 0 ?
"No networks - scan again" :
"Scan again");
310 scanAgain_.bounds = popup_.itemRect(count, rowH);
311 popup_.addItem(scanAgain_);
312 popup_.setTheme(
this->theme());
313 screen_.invalidate();
318 screen_.dismissDialog();
321 void select(
int index) {
322 if (index >=
static_cast<
int>(networks_.size()))
return;
324 ssid_.setText(networks_[index].ssid.c_str());
325 password_.setText(
"");
326 if (networks_[index].secure) {
343 void connectFromFields() {
344 if (wifi_ ==
nullptr || ssid_.text().empty())
return;
348 this->setScrollOffset(0);
349 wifi_
->connect(ssid_.text().c_str()
, password_.text().c_str()
);
354 void refreshStatus() {
356 if (wifi_ ==
nullptr) {
357 status_.setText(
"No WiFi available");
360 const ColorScheme<RGB_T>& c =
this->theme().colors;
364 snprintf(buf,
sizeof(buf),
"Connected to %s (%s)", wifi_
->ssid().c_str(), wifi_
->ip().c_str());
365 status_.setColor(c.primary);
368 snprintf(buf,
sizeof(buf),
"Connecting to %s ...", wifi_
->ssid().c_str());
369 status_.setColor(c.onSurfaceVariant);
372 snprintf(buf,
sizeof(buf),
"Could not connect to %s: check the password", wifi_
->ssid().c_str());
373 status_.setColor(c.error);
376 snprintf(buf,
sizeof(buf), scanning_ ?
"Not connected - scanning for networks ..."
377 :
"Not connected: select a network");
378 status_.setColor(c.onSurfaceVariant);
381 status_.setText(buf);
382 connect_.setLabel(connected() ?
"Disconnect" :
"Connect");
383 screen_.invalidate();
387 const Bounds& b =
this->bounds;
388 constexpr int32_t margin = 8;
389 const int32_t x = b
.x + margin;
390 const int32_t w = b
.w - margin -
Container<RGB_T>::kScrollbarTouchWidth;
404 cancel_.visible = !cancelText_.empty();
405 if (cancel_.visible) {
406 const int32_t half = (w - 8) / 2;
408 cancel_.bounds =
Bounds(x + half + 8
, y
, half
, 32
);
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
On-screen QWERTY keyboard that drives one text input at a time.
Definition: Keyboard.h:44
Single line of themed, non-interactive text.
Definition: Label.h:34
Tappable row: optional leading icon + title, with a selected state.
Definition: ListItem.h:22
One page (tab) of an app: a scrollable Container with a title and an icon for the navigation,...
Definition: Page.h:28
Owns a set of widgets, draws them, and routes gesture events to whichever one should handle each even...
Definition: Screen.h:71
Caption on the left, Switch on the right; tapping the row toggles it.
Definition: SettingRows.h:138
Single-line text input box.
Definition: TextField.h:41
The WiFi of a device as WifiLoginPage uses it: scan, connect and the stored credentials....
Definition: WifiControl.h:50
virtual std::string ip()
The IP address while connected.
Definition: WifiControl.h:68
State
Definition: WifiControl.h:52
virtual void connect(const char *ssid, const char *password)=0
Connects in the background; the credentials are stored when it works.
virtual std::string ssid()=0
The network that is connected or that is being connected to.
virtual bool startScan()=0
Starts a scan in the background.
virtual void disconnect()=0
WiFi selection and login as a Page: the state of the connection, the SSID with a button that opens th...
Definition: WifiLoginPage.h:113
bool tick(uint32_t nowMs) override
Polls the scan and the connection: true if the page changed.
Definition: WifiLoginPage.h:233
const std::vector< WifiNetwork > & networks() const
The networks of the last scan.
Definition: WifiLoginPage.h:162
void onHide() override
The page is hidden (another page is shown).
Definition: WifiLoginPage.h:226
void showNetworks()
Opens the pop-up list of the networks (as the button next to the SSID).
Definition: WifiLoginPage.h:151
void startScan()
Starts a new scan.
Definition: WifiLoginPage.h:143
void onShow() override
The page becomes visible.
Definition: WifiLoginPage.h:216
WifiLoginPage(Keyboard< RGB_T > &keyboard, Screen< RGB_T > &screen, const char *title="WiFi")
Definition: WifiLoginPage.h:117
std::function< void()> onDone
The user is done: connected or cancelled.
Definition: WifiLoginPage.h:121
void build() override
Creates the widgets (called once by PageNavigator::begin()).
Definition: WifiLoginPage.h:164
void connect(const char *ssid, const char *password)
Fills in the fields and connects (as the Connect button).
Definition: WifiLoginPage.h:136
static constexpr int kMaxNetworks
Definition: WifiLoginPage.h:115
void setCancelText(const char *text)
Definition: WifiLoginPage.h:127
void setWifi(WifiControl *wifi)
Definition: WifiLoginPage.h:123
One network in a list: name, a lock for a secured network and the signal bars; tapping the row calls ...
Definition: WifiLoginPage.h:30
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: WifiLoginPage.h:59
static constexpr int32_t kHeight
Definition: WifiLoginPage.h:32
std::function< void()> onSelect
Definition: WifiLoginPage.h:33
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: WifiLoginPage.h:42
void setNetwork(const char *ssid, int bars, bool secure, bool current)
Definition: WifiLoginPage.h:35
Definition: AnchorLayout.h:4
TypographyRole
Definition: Label.h:11
int wifiBars(int rssi)
Signal strength as 0-4 bars (see drawSignalBars()).
Definition: WifiControl.h:20
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t w
Definition: Bounds.h:24
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t h
Definition: Bounds.h:25
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
Material Design 3 color roles.
Definition: MaterialColors.h:24
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50
A WiFi network found by a scan.
Definition: WifiControl.h:11
std::string ssid
Definition: WifiControl.h:12
int rssi
Signal strength in dBm (e.g. -45 strong, -85 weak).
Definition: WifiControl.h:14
bool secure
Needs a password.
Definition: WifiControl.h:16