Skip to content

Commit 7767ce0

Browse files
authored
Merge pull request #3 from Forairaaaaa/main
add app set wifi
2 parents 1780664 + bb3e6f1 commit 7767ce0

15 files changed

Lines changed: 1228 additions & 49 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.16)
77

88

99
# add_definitions(-DMC_USE_CUSTOM_CONF)
10+
# add_definitions(-DNO_BOOT_PLAY_SHIT)
1011

1112

1213
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

main/apps/app_ir/app_ir.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file app_ir.cpp
33
* @author Forairaaaaa
44
* @brief http://gitlab.m5stack.com/Forairaaaaa/stamps3_keypad_factory_test/blob/master/code/src/factory_test/ir/ir_test.cpp
5-
* @version 0.1
5+
* @version 0.6
66
* @date 2023-09-20
77
*
88
* @copyright Copyright (c) 2023
@@ -34,6 +34,7 @@ void AppIR::onCreate()
3434
_data.hal->canvas()->setFont(FONT_REPL);
3535
_data.hal->canvas()->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
3636
_data.hal->canvas()->setTextSize(1);
37+
_data.hal->canvas()->setTextScroll(false);
3738
ir_wrap_init();
3839
}
3940

main/apps/app_repl/app_repl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file app_repl.cpp
33
* @author Forairaaaaa
44
* @brief
5-
* @version 0.1
5+
* @version 0.6
66
* @date 2023-09-20
77
*
88
* @copyright Copyright (c) 2023
@@ -105,15 +105,15 @@ void AppREPL::_update_input()
105105
int cursor_x = _canvas->getCursorX();
106106
int cursor_y = _canvas->getCursorY();
107107

108-
if (cursor_x - _canvas->fontWidth() < 0)
108+
if (cursor_x - FONT_REPL_WIDTH < 0)
109109
{
110110
// Last line
111-
cursor_y -= _canvas->fontHeight();
112-
cursor_x = _canvas->width() - _canvas->fontWidth();
111+
cursor_y -= FONT_REPL_HEIGHT;
112+
cursor_x = _canvas->width() - FONT_REPL_WIDTH;
113113
}
114114
else
115115
{
116-
cursor_x -= _canvas->fontWidth();
116+
cursor_x -= FONT_REPL_WIDTH;
117117
}
118118

119119
spdlog::info("new cursor {} {}", cursor_x, cursor_y);

main/apps/app_set_wifi/app_set_wifi.cpp

Lines changed: 224 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,250 @@
22
* @file app_set_wifi.cpp
33
* @author Forairaaaaa
44
* @brief
5-
* @version 0.1
5+
* @version 0.6
66
* @date 2023-09-20
77
*
88
* @copyright Copyright (c) 2023
99
*
1010
*/
1111
#include "app_set_wifi.h"
1212
#include "spdlog/spdlog.h"
13+
#include "../utils/wifi_connect_wrap/wifi_connect_wrap.h"
14+
#include "../utils/sntp_wrap/sntp_wrap.h"
1315

1416

1517
using namespace MOONCAKE::APPS;
1618

1719

20+
#define _keyboard _data.hal->keyboard()
21+
#define _canvas _data.hal->canvas()
22+
#define _canvas_update _data.hal->canvas_update
23+
#define _canvas_clear() _canvas->fillScreen(THEME_COLOR_BG)
24+
25+
26+
void AppSetWiFi::_update_input()
27+
{
28+
// spdlog::info("{} {}", _keyboard->keyList().size(), _data.last_key_num);
29+
30+
// If changed
31+
if (_keyboard->keyList().size() != _data.last_key_num)
32+
{
33+
// If key pressed
34+
if (_keyboard->keyList().size() != 0)
35+
{
36+
// Update states and values
37+
_keyboard->updateKeysState();
38+
39+
// If enter
40+
if (_keyboard->keysState().enter)
41+
{
42+
// New line
43+
_canvas->print(" \n");
44+
45+
_update_state();
46+
47+
// Reset buffer
48+
_data.repl_input_buffer = "";
49+
}
50+
51+
// If space
52+
else if (_keyboard->keysState().space)
53+
{
54+
_canvas->print(' ');
55+
_data.repl_input_buffer += ' ';
56+
}
57+
58+
// If delete
59+
else if (_keyboard->keysState().del)
60+
{
61+
if (_data.repl_input_buffer.size())
62+
{
63+
// Pop input buffer
64+
_data.repl_input_buffer.pop_back();
65+
66+
// Pop canvas display
67+
int cursor_x = _canvas->getCursorX();
68+
int cursor_y = _canvas->getCursorY();
69+
70+
if (cursor_x - FONT_REPL_WIDTH < 0)
71+
{
72+
// Last line
73+
cursor_y -= FONT_REPL_HEIGHT;
74+
cursor_x = _canvas->width() - FONT_REPL_WIDTH;
75+
}
76+
else
77+
{
78+
cursor_x -= FONT_REPL_WIDTH;
79+
}
80+
81+
spdlog::info("new cursor {} {}", cursor_x, cursor_y);
82+
83+
_canvas->setCursor(cursor_x, cursor_y);
84+
_canvas->print(" ");
85+
_canvas->setCursor(cursor_x, cursor_y);
86+
}
87+
}
88+
89+
// Normal chars
90+
else
91+
{
92+
for (auto& i : _keyboard->keysState().values)
93+
{
94+
// spdlog::info("{}", i);
95+
96+
_canvas->print(i);
97+
_data.repl_input_buffer += i;
98+
}
99+
}
100+
101+
_canvas_update();
102+
103+
// Update last key num
104+
_data.last_key_num = _keyboard->keyList().size();
105+
}
106+
else
107+
{
108+
// Reset last key num
109+
_data.last_key_num = 0;
110+
}
111+
}
112+
}
113+
114+
115+
void AppSetWiFi::_update_cursor()
116+
{
117+
if ((millis() - _data.cursor_update_time_count) > _data.cursor_update_period)
118+
{
119+
// Get cursor
120+
int cursor_x = _canvas->getCursorX();
121+
int cursor_y = _canvas->getCursorY();
122+
123+
// spdlog::info("cursor {} {}", cursor_x, cursor_y);
124+
125+
_canvas->print(_data.cursor_state ? '_' : ' ');
126+
_canvas->setCursor(cursor_x, cursor_y);
127+
_canvas_update();
128+
129+
_data.cursor_state = !_data.cursor_state;
130+
_data.cursor_update_time_count = millis();
131+
}
132+
}
133+
134+
135+
void AppSetWiFi::_update_state()
136+
{
137+
if (_data.current_state == state_init)
138+
{
139+
_canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
140+
_canvas->printf("WiFi SSID:\n");
141+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
142+
_canvas_update();
143+
144+
_data.current_state = state_wait_ssid;
145+
}
146+
147+
else if (_data.current_state == state_wait_ssid)
148+
{
149+
_canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
150+
_canvas->printf("WiFi Password:\n");
151+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
152+
_canvas_update();
153+
154+
_data.wifi_ssid = _data.repl_input_buffer;
155+
_data.current_state = state_wait_password;
156+
spdlog::info("wifi ssid set: {}", _data.wifi_ssid);
157+
}
158+
159+
else if (_data.current_state == state_wait_password)
160+
{
161+
_data.wifi_password = _data.repl_input_buffer;
162+
_data.current_state = state_connect;
163+
spdlog::info("wifi password set: {}", _data.wifi_password);
164+
}
165+
166+
if (_data.current_state == state_connect)
167+
{
168+
_canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG);
169+
_canvas->printf("WiFi config:\n- %s\n- %s\nConnecting...\n", _data.wifi_ssid.c_str(), _data.wifi_password.c_str());
170+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
171+
_canvas_update();
172+
173+
174+
wifi_connect_wrap_config(_data.wifi_ssid.c_str(), _data.wifi_password.c_str());
175+
wifi_connect_wrap_connect();
176+
177+
if (wifi_connect_wrap_is_wifi_connect_success() != 0)
178+
{
179+
_canvas->setTextColor(TFT_GREENYELLOW, THEME_COLOR_BG);
180+
_canvas->printf("Connected\nSNTP init...");
181+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
182+
_canvas_update();
183+
184+
sntp_warp_init();
185+
_data.hal->setSntpAdjusted(true);
186+
187+
_canvas->setTextColor(TFT_GREENYELLOW, THEME_COLOR_BG);
188+
_canvas->printf("Done");
189+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
190+
_canvas_update();
191+
}
192+
else
193+
{
194+
_canvas->setTextColor(TFT_RED, THEME_COLOR_BG);
195+
_canvas->printf("Failed");
196+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
197+
_canvas_update();
198+
}
199+
200+
wifi_connect_wrap_disconnect();
201+
202+
_data.current_state = state_wait_quit;
203+
}
204+
}
205+
206+
18207
void AppSetWiFi::onCreate()
19208
{
20209
spdlog::info("{} onCreate", getAppName());
21210

22211
// Get hal
23-
_data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal *>();
212+
_data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal*>();
213+
}
214+
215+
216+
void AppSetWiFi::onResume()
217+
{
218+
ANIM_APP_OPEN();
219+
220+
_canvas_clear();
221+
_canvas->setTextScroll(true);
222+
_canvas->setBaseColor(THEME_COLOR_BG);
223+
_canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG);
224+
_canvas->setFont(FONT_REPL);
225+
_canvas->setTextSize(FONT_SIZE_REPL);
226+
_canvas->setCursor(0, 0);
227+
228+
_data.current_state = state_init;
229+
_update_state();
24230
}
25231

26232

27233
void AppSetWiFi::onRunning()
28234
{
29-
destroyApp();
235+
if (_data.current_state != state_wait_quit)
236+
_update_input();
237+
_update_cursor();
238+
239+
if (_data.hal->homeButton()->pressed())
240+
{
241+
_data.hal->playNextSound();
242+
spdlog::info("quit set wifi");
243+
destroyApp();
244+
}
245+
}
246+
247+
248+
void AppSetWiFi::onDestroy()
249+
{
250+
_canvas->setTextScroll(false);
30251
}

main/apps/app_set_wifi/app_set_wifi.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file app_set_wifi.h
33
* @author Forairaaaaa
44
* @brief
5-
* @version 0.1
5+
* @version 0.6
66
* @date 2023-09-20
77
*
88
* @copyright Copyright (c) 2023
@@ -26,16 +26,42 @@ namespace MOONCAKE
2626
class AppSetWiFi : public APP_BASE
2727
{
2828
private:
29+
enum State_t
30+
{
31+
state_init = 0,
32+
state_wait_ssid,
33+
state_wait_password,
34+
state_connect,
35+
state_wait_quit,
36+
};
37+
2938
struct Data_t
3039
{
3140
HAL::Hal* hal = nullptr;
41+
42+
int last_key_num = 0;
43+
std::string repl_input_buffer;
44+
bool is_caps = false;
45+
char* value_buffer = nullptr;
46+
47+
uint32_t cursor_update_time_count = 0;
48+
uint32_t cursor_update_period = 500;
49+
bool cursor_state = false;
50+
51+
State_t current_state = state_init;
52+
std::string wifi_ssid;
53+
std::string wifi_password;
3254
};
3355
Data_t _data;
56+
void _update_input();
57+
void _update_cursor();
58+
void _update_state();
3459

3560
public:
3661
void onCreate() override;
37-
void onResume() override { ANIM_APP_OPEN(); }
62+
void onResume() override;
3863
void onRunning() override;
64+
void onDestroy() override;
3965
};
4066

4167
class AppSetWiFi_Packer : public APP_PACKER_BASE

0 commit comments

Comments
 (0)