Skip to content

Commit 3b9913a

Browse files
committed
feature/startup/fire: Add CardKb input function
Signed-off-by: lbuque <[email protected]>
1 parent 86d167c commit 3b9913a

File tree

4 files changed

+207
-20
lines changed

4 files changed

+207
-20
lines changed

m5stack/modules/startup/fire/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def next(self):
2929
def current(self):
3030
return self._apps[self._id]
3131

32+
def prev(self):
33+
self._id = (self._id - 1) % len(self._apps)
34+
return self._apps[self._id]
35+
3236

3337
class AppBase:
3438
def __init__(self) -> None:

m5stack/modules/startup/fire/apps/app_list.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,31 @@ def __init__(self, x, y, w, h, color, fill_c, parent=M5.Lcd) -> None:
2626
self._parent = parent
2727
self.set_pos(self._x, self._y)
2828

29+
def set_x(self, x):
30+
self._x = x
31+
self._parent.fillRect(self._x, self._y, self._w, self._h, self._fill_c)
32+
self._parent.drawRect(self._x, self._y, self._w, self._h, self._color)
33+
34+
def get_y(self):
35+
return self._y
36+
37+
def set_y(self, y):
38+
self._y = y
39+
self._parent.fillRect(self._x, self._y, self._w, self._h, self._fill_c)
40+
self._parent.drawRect(self._x, self._y, self._w, self._h, self._color)
41+
2942
def set_pos(self, x, y):
3043
self._x = x
3144
self._y = y
3245
self._parent.fillRect(self._x, self._y, self._w, self._h, self._fill_c)
3346
self._parent.drawRect(self._x, self._y, self._w, self._h, self._color)
3447

48+
def set_color(self, color, fill_c):
49+
self._color = color
50+
self._fill_c = fill_c
51+
self._parent.fillRect(self._x, self._y, self._w, self._h, self._fill_c)
52+
self._parent.drawRect(self._x, self._y, self._w, self._h, self._color)
53+
3554

3655
class FileList:
3756
def __init__(self, dir, suffix=".py") -> None:

m5stack/modules/startup/fire/apps/settings.py

Lines changed: 143 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from ..app import AppBase, generator, AppSelector
22
import M5
3-
from M5 import Widgets
43
from widgets.image import Image
54
from widgets.label import Label
65
import esp32
@@ -19,28 +18,37 @@
1918
SETTING_SELECTED_IMG,
2019
BAR1_IMG,
2120
)
21+
from unit import KeyCode
22+
from .app_list import Rectangle
2223

2324

2425
class WiFiSetting(AppBase):
2526
def __init__(self, icos: dict, data=None) -> None:
27+
self._wifi = data
2628
self._lcd = icos
2729

2830
def on_launch(self):
2931
self.get_data()
32+
self._option = 0
33+
self.focus = True
3034

3135
def on_view(self):
32-
origin_x = 4
33-
origin_y = 4
36+
self._origin_x = 4
37+
self._origin_y = 4
3438

3539
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)
3741
self._bg_img.set_size(312, 108)
3842
self._bg_img.set_src(SETTING_WIFI_IMG)
3943

44+
self._rect0 = Rectangle(
45+
self._origin_x + 96, self._origin_y + 7, 144, 26, 0xFEFEFE, 0xFEFEFE, self._lcd
46+
)
47+
4048
self._ssid_label = Label(
4149
"ssid",
42-
origin_x + 98,
43-
origin_y + 12,
50+
self._origin_x + 98,
51+
self._origin_y + 12,
4452
w=144,
4553
font_align=Label.LEFT_ALIGNED,
4654
fg_color=0x000000,
@@ -51,24 +59,27 @@ def on_view(self):
5159
self._ssid_label.setLongMode(Label.LONG_DOT)
5260
self._ssid_label.setText(self.ssid)
5361

54-
self._pwd_label = Label(
62+
self._psk_label = Label(
5563
"pwd",
56-
origin_x + 98,
57-
origin_y + 12 + 35,
64+
self._origin_x + 98,
65+
self._origin_y + 12 + 35,
5866
w=144,
5967
font_align=Label.LEFT_ALIGNED,
6068
fg_color=0x000000,
6169
bg_color=0xFEFEFE,
6270
font=MontserratMedium16.FONT,
6371
parent=self._lcd,
6472
)
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("")
6778

6879
self._server_label = Label(
6980
"server",
70-
origin_x + 98,
71-
origin_y + 12 + 35 + 34,
81+
self._origin_x + 98,
82+
self._origin_y + 12 + 35 + 34,
7283
w=144,
7384
font_align=Label.LEFT_ALIGNED,
7485
fg_color=0x000000,
@@ -79,6 +90,14 @@ def on_view(self):
7990
self._server_label.setLongMode(Label.LONG_DOT)
8091
self._server_label.setText(self.server)
8192

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+
82101
def on_ready(self):
83102
pass
84103

@@ -88,6 +107,112 @@ def on_hide(self):
88107
def on_exit(self):
89108
pass
90109

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+
91216
def get_data(self):
92217
self.nvs = esp32.NVS("uiflow")
93218
self.ssid = self.nvs.get_str("ssid0")
@@ -107,7 +232,7 @@ def set_data(self):
107232
if self.psk != self.psk_tmp:
108233
self.psk = self.psk_tmp
109234
self.nvs.set_str("pswd0", self.psk)
110-
print("set new ssid: ", self.ssid)
235+
print("set new psk: ", self.psk)
111236
is_save = True
112237
if self.server != self.server_tmp:
113238
self.server = self.server_tmp
@@ -309,6 +434,10 @@ def on_hide(self):
309434
def on_exit(self):
310435
M5.Lcd.drawImage(SETTING_UNSELECTED_IMG, 5 + 62 * 0, 0)
311436

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+
312441
async def _btna_event_handler(self, fw):
313442
pass
314443

m5stack/modules/startup/fire/framework.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from .app import AppBase, AppSelector
2-
3-
try:
4-
import uasyncio as asyncio
5-
except ImportError:
6-
import asyncio
7-
2+
import uasyncio as asyncio
83
import M5
94
import gc
5+
from machine import I2C, Pin
6+
from unit import CardKB, KeyCode
7+
8+
9+
class KeyEvent:
10+
key = 0
11+
status = False
1012

1113

1214
class Framework:
@@ -46,6 +48,14 @@ async def run(self):
4648
break
4749
self._launcher.start()
4850
# asyncio.create_task(self.gc_task())
51+
52+
self.i2c0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
53+
self._kb_status = False
54+
if 0x5F in self.i2c0.scan():
55+
self._kb = CardKB(self.i2c0)
56+
self._event = KeyEvent()
57+
self._kb_status = True
58+
4959
while True:
5060
M5.update()
5161
if M5.BtnA.wasClicked():
@@ -68,6 +78,31 @@ async def run(self):
6878
asyncio.create_task(app._btnc_event_handler(self))
6979
await asyncio.sleep_ms(100)
7080

81+
if self._kb_status:
82+
if self._kb.is_pressed():
83+
M5.Speaker.tone(3500, 50)
84+
self._event.key = self._kb.get_key()
85+
self._event.status = False
86+
await self.handle_input(self._event)
87+
88+
async def handle_input(self, event: KeyEvent):
89+
if event.key is KeyCode.KEYCODE_RIGHT:
90+
app = self._app_selector.current()
91+
app.stop()
92+
app = self._app_selector.next()
93+
app.start()
94+
event.status = True
95+
if KeyCode.KEYCODE_LEFT == event.key:
96+
app = self._app_selector.current()
97+
app.stop()
98+
app = self._app_selector.prev()
99+
app.start()
100+
event.status = True
101+
if event.status == False:
102+
app = self._app_selector.current()
103+
if hasattr(app, "_kb_event_handler"):
104+
await app._kb_event_handler(event, self)
105+
71106
async def gc_task(self):
72107
while True:
73108
gc.collect()

0 commit comments

Comments
 (0)