1
1
from ..app import AppBase , generator , AppSelector
2
2
import M5
3
- from M5 import Widgets
4
3
from widgets .image import Image
5
4
from widgets .label import Label
6
5
import esp32
19
18
SETTING_SELECTED_IMG ,
20
19
BAR1_IMG ,
21
20
)
21
+ from unit import KeyCode
22
+ from .app_list import Rectangle
22
23
23
24
24
25
class WiFiSetting (AppBase ):
25
26
def __init__ (self , icos : dict , data = None ) -> None :
27
+ self ._wifi = data
26
28
self ._lcd = icos
27
29
28
30
def on_launch (self ):
29
31
self .get_data ()
32
+ self ._option = 0
33
+ self .focus = True
30
34
31
35
def on_view (self ):
32
- origin_x = 4
33
- origin_y = 4
36
+ self . _origin_x = 4
37
+ self . _origin_y = 4
34
38
35
39
self ._bg_img = Image (use_sprite = False , parent = self ._lcd )
36
- self ._bg_img .set_pos (origin_x , origin_y )
40
+ self ._bg_img .set_pos (self . _origin_x , self . _origin_y )
37
41
self ._bg_img .set_size (312 , 108 )
38
42
self ._bg_img .set_src (SETTING_WIFI_IMG )
39
43
44
+ self ._rect0 = Rectangle (
45
+ self ._origin_x + 96 , self ._origin_y + 7 , 144 , 26 , 0xFEFEFE , 0xFEFEFE , self ._lcd
46
+ )
47
+
40
48
self ._ssid_label = Label (
41
49
"ssid" ,
42
- origin_x + 98 ,
43
- origin_y + 12 ,
50
+ self . _origin_x + 98 ,
51
+ self . _origin_y + 12 ,
44
52
w = 144 ,
45
53
font_align = Label .LEFT_ALIGNED ,
46
54
fg_color = 0x000000 ,
@@ -51,24 +59,27 @@ def on_view(self):
51
59
self ._ssid_label .setLongMode (Label .LONG_DOT )
52
60
self ._ssid_label .setText (self .ssid )
53
61
54
- self ._pwd_label = Label (
62
+ self ._psk_label = Label (
55
63
"pwd" ,
56
- origin_x + 98 ,
57
- origin_y + 12 + 35 ,
64
+ self . _origin_x + 98 ,
65
+ self . _origin_y + 12 + 35 ,
58
66
w = 144 ,
59
67
font_align = Label .LEFT_ALIGNED ,
60
68
fg_color = 0x000000 ,
61
69
bg_color = 0xFEFEFE ,
62
70
font = MontserratMedium16 .FONT ,
63
71
parent = self ._lcd ,
64
72
)
65
- self ._pwd_label .setLongMode (Label .LONG_DOT )
66
- self ._pwd_label .setText ("*" * 20 )
73
+ self ._psk_label .setLongMode (Label .LONG_DOT )
74
+ if len (self .psk ):
75
+ self ._psk_label .setText ("*" * 20 )
76
+ else :
77
+ self ._psk_label .setText ("" )
67
78
68
79
self ._server_label = Label (
69
80
"server" ,
70
- origin_x + 98 ,
71
- origin_y + 12 + 35 + 34 ,
81
+ self . _origin_x + 98 ,
82
+ self . _origin_y + 12 + 35 + 34 ,
72
83
w = 144 ,
73
84
font_align = Label .LEFT_ALIGNED ,
74
85
fg_color = 0x000000 ,
@@ -79,6 +90,14 @@ def on_view(self):
79
90
self ._server_label .setLongMode (Label .LONG_DOT )
80
91
self ._server_label .setText (self .server )
81
92
93
+ self ._option_views = generator (
94
+ (
95
+ (0 , self ._select_ssid_option ),
96
+ (1 , self ._select_psk_option ),
97
+ (2 , self ._select_server_option ),
98
+ )
99
+ )
100
+
82
101
def on_ready (self ):
83
102
pass
84
103
@@ -88,6 +107,112 @@ def on_hide(self):
88
107
def on_exit (self ):
89
108
pass
90
109
110
+ async def _kb_event_handler (self , event , fw ):
111
+ if event .key == KeyCode .KEYCODE_ENTER :
112
+ event .status = True
113
+ self .focus = True
114
+ self ._option , view_fn = next (self ._option_views )
115
+ view_fn ()
116
+ self .set_data ()
117
+
118
+ if self .focus is False :
119
+ return
120
+
121
+ if event .key == KeyCode .KEYCODE_ESC :
122
+ self .ssid_tmp = self .ssid
123
+ self .psk_tmp = self .psk
124
+ self .server_tmp = self .server
125
+ self ._select_default_option ()
126
+ self .focus = False
127
+ event .status = True
128
+ self .set_data ()
129
+
130
+ if event .key == KeyCode .KEYCODE_BACKSPACE and self ._option in (0 , 1 , 2 ):
131
+ if self ._option == 0 :
132
+ self .ssid_tmp = self .ssid_tmp [:- 1 ]
133
+ self ._ssid_label .setText (self .ssid_tmp )
134
+ elif self ._option == 1 :
135
+ if self .psk_tmp == self .psk and len (self .psk ):
136
+ self .psk_tmp = ""
137
+ else :
138
+ self .psk_tmp = self .psk_tmp [:- 1 ]
139
+ self ._psk_label .setText (self .psk_tmp )
140
+ elif self ._option == 2 :
141
+ self .server_tmp = self .server_tmp [:- 1 ]
142
+ self ._server_label .setText (self .server_tmp )
143
+ event .status = True
144
+ elif event .key >= 0x20 and event .key <= 126 :
145
+ if self ._option == 0 :
146
+ self .ssid_tmp += chr (event .key )
147
+ self ._ssid_label .setText (self .ssid_tmp )
148
+ elif self ._option == 1 :
149
+ if self .psk_tmp == self .psk and len (self .psk ):
150
+ self .psk_tmp = ""
151
+ else :
152
+ self .psk_tmp += chr (event .key )
153
+ self ._psk_label .setText (self .psk_tmp )
154
+ elif self ._option == 2 :
155
+ self .server_tmp += chr (event .key )
156
+ self ._server_label .setText (self .server_tmp )
157
+ event .status = True
158
+
159
+ def _select_default_option (self ):
160
+ self ._bg_img .refresh ()
161
+ self ._ssid_label .setTextColor (0x000000 , 0xFEFEFE )
162
+ self ._psk_label .setTextColor (0x000000 , 0xFEFEFE )
163
+ self ._server_label .setTextColor (0x000000 , 0xFEFEFE )
164
+ self ._ssid_label .setText (self .ssid_tmp )
165
+ if len (self .psk_tmp ) is 0 :
166
+ self ._psk_label .setText ("" )
167
+ else :
168
+ self ._psk_label .setText ("*" * 20 )
169
+ self ._server_label .setText (self .server_tmp )
170
+
171
+ def _select_ssid_option (self ):
172
+ # self._bg_img.set_src(SETTING_WIFI_IMG)
173
+ self ._rect0 .set_color (0xFEFEFE , 0xFEFEFE )
174
+ self ._rect0 .set_pos (self ._origin_x + 98 , self ._origin_y + 7 )
175
+ self ._rect0 .set_color (0xDCDDDD , 0xDCDDDD )
176
+ self ._ssid_label .setTextColor (0x000000 , 0xDCDDDD )
177
+ self ._psk_label .setTextColor (0x000000 , 0xFEFEFE )
178
+ self ._server_label .setTextColor (0x000000 , 0xFEFEFE )
179
+ self ._ssid_label .setText (self .ssid_tmp )
180
+ if len (self .psk_tmp ) is 0 :
181
+ self ._psk_label .setText ("" )
182
+ else :
183
+ self ._psk_label .setText ("*" * 20 )
184
+ self ._server_label .setText (self .server_tmp )
185
+
186
+ def _select_psk_option (self ):
187
+ # self._bg_img.set_src(SETTING_WIFI_IMG)
188
+ self ._rect0 .set_color (0xFEFEFE , 0xFEFEFE )
189
+ self ._rect0 .set_pos (self ._origin_x + 98 , self ._origin_y + 7 + 36 )
190
+ self ._rect0 .set_color (0xDCDDDD , 0xDCDDDD )
191
+ self ._ssid_label .setTextColor (0x000000 , 0xFEFEFE )
192
+ self ._psk_label .setTextColor (0x000000 , 0xDCDDDD )
193
+ self ._server_label .setTextColor (0x000000 , 0xFEFEFE )
194
+ self ._ssid_label .setText (self .ssid_tmp )
195
+ if len (self .psk_tmp ) is 0 :
196
+ self ._psk_label .setText ("" )
197
+ else :
198
+ self ._psk_label .setText ("*" * 20 )
199
+ self ._server_label .setText (self .server_tmp )
200
+
201
+ def _select_server_option (self ):
202
+ # self._bg_img.set_src(SETTING_WIFI_IMG)
203
+ self ._rect0 .set_color (0xFEFEFE , 0xFEFEFE )
204
+ self ._rect0 .set_pos (self ._origin_x + 98 , self ._origin_y + 7 + 36 + 36 )
205
+ self ._rect0 .set_color (0xDCDDDD , 0xDCDDDD )
206
+ self ._ssid_label .setTextColor (0x000000 , 0xFEFEFE )
207
+ self ._psk_label .setTextColor (0x000000 , 0xFEFEFE )
208
+ self ._server_label .setTextColor (0x000000 , 0xDCDDDD )
209
+ self ._ssid_label .setText (self .ssid_tmp )
210
+ if len (self .psk_tmp ) is 0 :
211
+ self ._psk_label .setText ("" )
212
+ else :
213
+ self ._psk_label .setText ("*" * 20 )
214
+ self ._server_label .setText (self .server_tmp )
215
+
91
216
def get_data (self ):
92
217
self .nvs = esp32 .NVS ("uiflow" )
93
218
self .ssid = self .nvs .get_str ("ssid0" )
@@ -107,7 +232,7 @@ def set_data(self):
107
232
if self .psk != self .psk_tmp :
108
233
self .psk = self .psk_tmp
109
234
self .nvs .set_str ("pswd0" , self .psk )
110
- print ("set new ssid : " , self .ssid )
235
+ print ("set new psk : " , self .psk )
111
236
is_save = True
112
237
if self .server != self .server_tmp :
113
238
self .server = self .server_tmp
@@ -309,6 +434,10 @@ def on_hide(self):
309
434
def on_exit (self ):
310
435
M5 .Lcd .drawImage (SETTING_UNSELECTED_IMG , 5 + 62 * 0 , 0 )
311
436
437
+ async def _kb_event_handler (self , event , fw ):
438
+ await self ._wlan_app ._kb_event_handler (event , fw )
439
+ self ._lcd .push (self ._origin_x , self ._origin_y )
440
+
312
441
async def _btna_event_handler (self , fw ):
313
442
pass
314
443
0 commit comments