UI 原型

(尽管最终设计没采用,但还是 po 出来,对不齐还是字体的锅)

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
初始化页面:
+----+------------------------+
|init|mac: |
|dhcp|[XX][XX][XX][XX][XX][XX]|
|stic| |
|ping|net info setted |
+----+------------------------+

DHCP 页面:
+----+------------------------+
|init|DHCP process 0 |
|dhcp|Callback: IP assigned! |
|stic|DHCP IP Leased Time: |
|ping|10 Sec |
+----+------------------------+

状态页面:
+----+------------------------+
|init| ip mask gateway dns |
|dhcp|[192].[168].[112].[ 1] |
|stic| |
|ping| |
+----+------------------------+

Ping 页面:
+----+------------------------+
|init|[008].[008].[008].[008] |
|dhcp|ping 8.8.8.8 |
|stic|1 icmp_seq=4 ttl=107 |
|ping|2 icmp_seq=4 ttl=107 |
+----+------------------------+

代码(Arduino)

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <ESPping.h>
#include <M5Cardputer.h>
#include <WiFi.h>

// DEBUG ONLY
#include <esp_wifi.h>
const char *test_ssid = "OMBC-NTP";

#define RGB888_TO_RGB565(r, g, b) \
(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3))

uint16_t Flipper_Orange = RGB888_TO_RGB565(0xFF, 0x82, 0x00);

enum PageState {
STATE_MENU,
STATE_INIT,
STATE_DHCP,
STATE_PING,
STATE_SETTING
};

byte currentMac[6];
bool HasMacReady = false;
String MacAddress = "";
String pingTarget = "8.8.8.8";

PageState currentPage = STATE_MENU;
int cursor = 0;
const int menuCount = 4;
const char *menuItems[] = {"Initialize", "DHCP", "Ping", "Setting"};

void RandomMacGen() {
for (int i = 0; i < 6; i++) {
currentMac[i] = random(0, 256);
}
currentMac[0] = (currentMac[0] & 0xFE) | 0x02;

WiFi.mode(WIFI_STA);
esp_wifi_set_mac(WIFI_IF_STA, currentMac);
HasMacReady = true;
}

void PrintMac(byte *mac) {
M5Cardputer.Display.setCursor(15, 40);
for (int i = 0; i < 6; i++) {
if (mac[i] < 16)
M5Cardputer.Display.print("0");
M5Cardputer.Display.print(mac[i], HEX);
if (i < 5)
M5Cardputer.Display.print(":");
}
M5Cardputer.Display.print("\n");
}

String macToString() {
String res = "";
for (int i = 0; i < 6; i++) {
if (currentMac[i] < 16)
res += "0";
res += String(currentMac[i], HEX);
if (i < 5)
res += ":";
}
res.toUpperCase();
return res;
}

void connectToOpenAP(const char *ssid) {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setCursor(15, 20);
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.println("OMBC DEBUG");

WiFi.begin(ssid, NULL);

int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 18) {
delay(500);
attempts++;
}

if (WiFi.status() == WL_CONNECTED) {
M5Cardputer.Display.setCursor(5, 45);
M5Cardputer.Display.printf("IP: %s", WiFi.localIP().toString().c_str());
M5Cardputer.Display.setCursor(5, 65);
M5Cardputer.Display.printf("MS: %s", WiFi.subnetMask().toString().c_str());
M5Cardputer.Display.setCursor(5, 85);
M5Cardputer.Display.printf("GW: %s", WiFi.gatewayIP().toString().c_str());
} else {
M5Cardputer.Display.setTextColor(RED);
M5Cardputer.Display.println("E: DHCP FAILED");
}
}

void doPing(const char *target) {
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.setCursor(5, 50);
for (int i = 0; i < 4; i++) {
bool success = Ping.ping(target, 1);
if (success) {
int avg_time = Ping.averageTime();
M5Cardputer.Display.printf("Success: %dms\n", avg_time);
} else {
M5Cardputer.Display.println("Timed out");
}
delay(500);
}
}

void drawMenuPage() {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setTextSize(2);
for (int i = 0; i < menuCount; i++) {
int y = 20 + (i * 25);
if (i == cursor) {
M5Cardputer.Display.fillRect(5, y - 2, 230, 22, BLACK);
M5Cardputer.Display.setTextColor(Flipper_Orange);
} else {
M5Cardputer.Display.setTextColor(BLACK);
}
M5Cardputer.Display.setCursor(15, y);
M5Cardputer.Display.print(menuItems[i]);
}
}

void drawInitPage() {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setCursor(15, 20);
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.println("Init W5500 Module");
if (HasMacReady) {
M5Cardputer.Display.setCursor(15, 40);
M5Cardputer.Display.println(MacAddress);
}
}

void drawDHCPPage() {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setCursor(15, 20);
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.println("DHCP Request");
}

void drawPingTestPage() {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setCursor(15, 20);
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.printf("TGT: %s", pingTarget.c_str());
}

void drawSettingPage() {
M5Cardputer.Display.fillScreen(Flipper_Orange);
M5Cardputer.Display.setTextSize(2);
M5Cardputer.Display.setCursor(15, 20);
M5Cardputer.Display.setTextColor(BLACK);
M5Cardputer.Display.println("W5500 Settings");
}

void handleMenuSelect(int index) {
switch (index) {
case 0:
currentPage = STATE_INIT;
drawInitPage();
break;
case 1:
currentPage = STATE_DHCP;
drawDHCPPage();
break;
case 2:
currentPage = STATE_PING;
drawPingTestPage();
break;
case 3:
currentPage = STATE_SETTING;
drawSettingPage();
break;
}
}

void setup() {
auto cfg = M5.config();
M5Cardputer.begin(cfg, true);
M5Cardputer.Display.setRotation(1);
drawMenuPage();
}

void loop() {
M5Cardputer.update();

if (M5Cardputer.Keyboard.isChange() && M5Cardputer.Keyboard.isPressed()) {
if (M5Cardputer.Keyboard.isKeyPressed('`')) {
currentPage = STATE_MENU;
drawMenuPage();
return;
}

if (currentPage == STATE_MENU) {
if (M5Cardputer.Keyboard.isKeyPressed(';')) {
cursor = (cursor > 0) ? cursor - 1 : menuCount - 1;
drawMenuPage();
} else if (M5Cardputer.Keyboard.isKeyPressed('.')) {
cursor = (cursor < menuCount - 1) ? cursor + 1 : 0;
drawMenuPage();
} else if (M5Cardputer.Keyboard.isKeyPressed(KEY_ENTER)) {
handleMenuSelect(cursor);
}
} else if (currentPage == STATE_INIT) {
if (M5Cardputer.Keyboard.isKeyPressed('i') && !HasMacReady) {
RandomMacGen();
PrintMac(currentMac);
MacAddress = macToString();
}
} else if (currentPage == STATE_DHCP) {
if (M5Cardputer.Keyboard.isKeyPressed(KEY_ENTER) && HasMacReady) {
connectToOpenAP("OMBC-NTP");
}
} else if (currentPage == STATE_PING) {
if (M5Cardputer.Keyboard.isKeyPressed('p')) {
doPing(pingTarget.c_str());
}
}
}
}

实测

因为买的 W5500 模块还没到,所以这里用非 Adv CP 走的 DEBUG MODE。

测试