Created
May 29, 2021 16:18
-
-
Save iso2022jp/5d2fac7dff641d9d425e5fea532a0c7b to your computer and use it in GitHub Desktop.
M5Stack Core2 で Wi-Fi に接続する GUI(入力辛い系)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5Core2.h> | |
#include <WiFi.h> | |
//--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2 | |
void selectWiFi(String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel) { | |
auto& d = M5.Lcd; | |
d.fillScreen(BLACK); | |
d.setTextColor(WHITE); | |
d.setTextSize(2); | |
auto center = [&](int32_t x, int32_t y, int32_t cx, int32_t cy, const char *string) { | |
d.setCursor(x + (cx - d.textWidth(string)) / 2 , y + (cy - d.fontHeight()) / 2); | |
d.print(string); | |
}; | |
d.drawRect(0, 220, 100, 20, WHITE); | |
d.drawRect(110, 220, 100, 20, WHITE); | |
d.drawRect(220, 220, 100, 20, WHITE); | |
center(0, 220, 100, 20, "Select"); | |
center(110, 220, 100, 20, "OK"); | |
center(220, 220, 100, 20, "Rescan"); | |
d.drawFastHLine(0, 19, 320, WHITE); | |
center(0, 0, 320, 20, "Wi-Fi Setup"); | |
bool drawn = false; | |
auto scan = [&] { | |
d.fillRect(0, 20, 320, 200, BLACK); | |
d.setCursor(0, 20); | |
d.printf("Scanning..."); | |
drawn = false; | |
WiFi.scanNetworks(true); | |
}; | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
scan(); | |
auto selected = 0; | |
auto count = 0; | |
while (true) { | |
if (!drawn) { | |
count = WiFi.scanComplete(); | |
if (count != WIFI_SCAN_RUNNING) { | |
d.fillRect(0, 20, 320, 200, BLACK); | |
d.setCursor(0, 20); | |
switch (count) { | |
case WIFI_SCAN_FAILED: | |
d.println("Scaning failed."); | |
break; | |
case 0: | |
d.println("No networks found."); | |
break; | |
default: | |
for (auto i = 0; i < count ; ++i) { | |
d.setCursor(20, 20 + i * 20); | |
d.println(WiFi.SSID(i)); | |
} | |
selected = 0; | |
center(0, 20 , 20, 20, "*"); | |
break; | |
} | |
drawn = true; | |
} | |
} | |
loop(); | |
if (M5.BtnA.wasPressed()) { | |
if (drawn && count > 0) { | |
selected = (selected + 1) % count; | |
d.fillRect(0, 20, 20, 200, BLACK); | |
center(0, 20 + 20 * selected, 20, 20, "*"); | |
} | |
} | |
if (M5.BtnB.wasPressed()) { | |
if (WiFi.getNetworkInfo(selected, ssid, encType, rssi, bssid, channel)) { | |
return; | |
} | |
} | |
if (M5.BtnC.wasPressed()) { | |
if (WiFi.scanComplete() != WIFI_SCAN_RUNNING) { | |
scan(); | |
} | |
} | |
} | |
} | |
String promptPassword() { | |
auto& d = M5.Lcd; | |
auto FROM = ' '; | |
auto TO = '~'; | |
d.fillScreen(BLACK); | |
d.setTextColor(WHITE); | |
d.setTextSize(2); | |
d.setTextWrap(false); | |
auto center = [&](int32_t x, int32_t y, int32_t cx, int32_t cy, const char *string) { | |
d.setCursor(x + (cx - d.textWidth(string)) / 2 , y + (cy - d.fontHeight()) / 2); | |
d.print(string); | |
}; | |
d.drawRect(0, 220, 100, 20, WHITE); | |
d.drawRect(110, 220, 100, 20, WHITE); | |
d.drawRect(220, 220, 100, 20, WHITE); | |
center(0, 220, 100, 20, "<"); | |
center(110, 220, 100, 20, "Enter"); | |
center(220, 220, 100, 20, ">"); | |
d.drawFastHLine(0, 19, 320, WHITE); | |
center(0, 0, 320, 20, "Wi-Fi Setup"); | |
d.setCursor(20, 60); | |
d.print("Password:"); | |
d.drawFastHLine(20, 101, 280, WHITE); | |
String password; | |
char selected = 'a'; | |
d.drawRect(-20, 140, 40, 40, WHITE); | |
d.drawRect(20, 140, 40, 40, WHITE); | |
d.drawRect(60, 140, 40, 40, WHITE); | |
d.drawRect(100, 140, 40, 40, WHITE); | |
d.drawRect(140, 140, 40, 40, WHITE); | |
d.drawRect(180, 140, 40, 40, WHITE); | |
d.drawRect(220, 140, 40, 40, WHITE); | |
d.drawRect(260, 140, 40, 40, WHITE); | |
d.drawRect(300, 140, 40, 40, WHITE); | |
auto drawPad = [&] { | |
d.setTextSize(3); | |
for (auto i = -4; i <= 4; ++i) { | |
d.fillRect(141 + i * 40, 141, 38, 38, i ? BLACK : WHITE); | |
d.setTextColor(i ? WHITE : BLACK); | |
char c = selected + i; | |
if (c < FROM) { | |
c += (TO - FROM + 1); | |
} | |
if (c > TO) { | |
c -= (TO - FROM + 1); | |
} | |
if (c == ' ') { | |
c = '»'; | |
} | |
center(141 + i * 40, 141, 38, 38, String(c).c_str()); | |
} | |
}; | |
// 0123456789 | |
// abcdefg | |
// hijklmnop | |
// qrstuvwxyz | |
// ABCDEFG | |
// HIJKLMNOP | |
// QRSTUVWXYZ | |
// !"#$%&'()* | |
// +,-./:;<=> | |
// ?@[\]^_`{|}~ | |
drawPad(); | |
while (true) { | |
loop(); | |
if (M5.BtnA.wasPressed()) { | |
if (selected == FROM) { | |
selected = TO; | |
} else { | |
--selected; | |
} | |
drawPad(); | |
} | |
if (M5.BtnB.wasPressed()) { | |
if (selected == ' ') { | |
return password; | |
} | |
password += selected; | |
d.setTextSize(2); | |
d.setCursor(20, 80); | |
d.setTextColor(WHITE); | |
for (auto i = 0; i < password.length(); ++i) { | |
d.print('*'); | |
} | |
} | |
if (M5.BtnC.wasPressed()) { | |
if (selected == TO) { | |
selected = FROM; | |
} else { | |
++selected; | |
} | |
drawPad(); | |
} | |
} | |
} | |
bool attempt(const char* ssid, const char* password) { | |
auto& d = M5.Lcd; | |
d.fillScreen(BLACK); | |
d.setTextColor(WHITE); | |
d.setTextSize(2); | |
d.setTextWrap(false); | |
auto center = [&](int32_t x, int32_t y, int32_t cx, int32_t cy, const char *string) { | |
d.setCursor(x + (cx - d.textWidth(string)) / 2 , y + (cy - d.fontHeight()) / 2); | |
d.print(string); | |
}; | |
d.drawFastHLine(0, 19, 320, WHITE); | |
center(0, 0, 320, 20, "Wi-Fi Setup"); | |
d.setCursor(20, 60); | |
d.print("Connecting..."); | |
if (password) { | |
WiFi.begin(ssid, password); | |
} else { | |
WiFi.begin(ssid); | |
} | |
return WiFi.waitForConnectResult() == WL_CONNECTED; | |
} | |
void setup() { | |
M5.begin(); | |
WiFi.persistent(false); | |
auto connected = false; | |
do { | |
String ssid; | |
uint8_t encType; | |
int32_t rssi; | |
uint8_t* bssid; | |
int32_t channel; | |
selectWiFi(ssid, encType, rssi, bssid, channel); | |
if (encType != WIFI_AUTH_OPEN) { | |
auto password = promptPassword(); | |
connected = attempt(ssid.c_str(), password.c_str()); | |
} else { | |
connected = attempt(ssid.c_str(), nullptr); | |
} | |
} while (!connected); | |
auto& d = M5.Lcd; | |
d.fillScreen(BLACK); | |
d.setTextColor(WHITE); | |
d.setTextSize(2); | |
d.setTextWrap(false); | |
d.setCursor(20, 20); | |
d.print("OK!"); | |
} | |
void loop() { | |
M5.update(); | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment