Skip to content

Commit 503e5d7

Browse files
doc: update the file
1 parent 415c2c8 commit 503e5d7

File tree

3 files changed

+171
-12
lines changed

3 files changed

+171
-12
lines changed

docs/docs/12.lua/index.md

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,142 @@ title: Lua脚本
33
nav_order: 12
44
---
55

6-
## 显示配置
6+
{: .note-title }
7+
> wxTools Lua
8+
>
9+
> 基于Lua 5.4...
710
8-
| 参数 | 说明 |
9-
| ---- | ---- |
11+
Lua脚本功能,可以让用户通过编写Lua脚本,来实现一些自动化操作,比如定时发送数据、处理接收数据等。Lua脚本运行在独立的线程中,不会阻塞主界面。
12+
13+
## wxt_write
14+
15+
发送数据接口,这个接口是内部实现的,可以直接调用:
16+
17+
```lua
18+
wxt_write("Hello, wxTools!\n")
19+
```
20+
21+
> 使用该接口时,需要设备正常打开。
22+
23+
## wxt_sleep
24+
25+
睡眠接口,单位为毫秒:
26+
27+
```lua
28+
wxt_sleep(1000) -- 睡眠1秒
29+
```
30+
31+
## wxt_is_interruption_requested
32+
33+
检查脚本是否被请求中断,返回true或false:
34+
35+
```lua
36+
ret = wxt_is_interruption_requested()
37+
if ret then
38+
print("Script interrupted.")
39+
break
40+
end
41+
```
42+
43+
> 如果程序有循环操作,必须调用该接口判断是否被中断,否则无法停止脚本。
44+
45+
## wxt_read
46+
47+
接收数据回调函数,当有数据接收时会调用该函数,参数为接收到的数据字符串。这个函数必须再脚本中定义:
48+
49+
```lua
50+
function wxt_read(data)
51+
print("Received data: " .. data)
52+
end
53+
```
54+
55+
在实际应用中,还要确保脚本中有循环操作,否则该函数不会被调用。
56+
57+
```lua
58+
function wxt_read(data)
59+
print("Received data: " .. data)
60+
end
61+
62+
while true do
63+
-- 其他操作
64+
wxt_sleep(1000)
65+
66+
ret = wxt_is_interruption_requested()
67+
if ret then
68+
print("Script interrupted.")
69+
break
70+
end
71+
end
72+
```
73+
74+
## 程序实例
75+
76+
### 发送数据
77+
78+
```lua
79+
print("Start 'Write' demo...")
80+
81+
-- wxt_write: send a string to the device.
82+
-- The function is defined in wxTools inner Lua environment.
83+
wxt_write("Hello, wxTools!")
84+
```
85+
86+
### 定时发送数据
87+
88+
```lua
89+
print("Start 'Timer' demo...")
90+
91+
while true do
92+
print("Current time: " .. os.date("%H:%M:%S"))
93+
wxt_write(os.date("%H:%M:%S") .. "\n")
94+
95+
ret = wxt_is_interruption_requested()
96+
if ret then
97+
print("Script interrupted.")
98+
break
99+
end
100+
101+
wxt_sleep(1000) -- Sleep for 1000 milliseconds (1 second)
102+
end
103+
```
104+
105+
### 回显
106+
107+
```lua
108+
print("Start 'Echo' demo...")
109+
110+
function wxt_read(str)
111+
print("[Rx]:", str)
112+
wxt_write(str)
113+
end
114+
115+
while true do
116+
ret = wxt_is_interruption_requested()
117+
if ret then
118+
print("Script interrupted.")
119+
break
120+
end
121+
122+
wxt_sleep(1000) -- Sleep for 1000 milliseconds (1 second)
123+
end
124+
```
125+
126+
### 接收数据处理
127+
128+
```lua
129+
print("Start 'Read' demo...")
130+
131+
function wxt_read(str)
132+
print("wxt_read:", str)
133+
end
134+
135+
while true do
136+
ret = wxt_is_interruption_requested()
137+
if ret then
138+
print("Script interrupted.")
139+
break
140+
end
141+
142+
wxt_sleep(1000) -- Sleep for 1000 milliseconds (1 second)
143+
end
144+
```

src/Page/Tabs/Lua/LuaTab.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ void LuaTab::DoSetupUiControllers()
133133
void LuaTab::DoSetupUiTextCtrl()
134134
{
135135
auto *luaBoxSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Lua Script"));
136-
m_luaTextCtrl = new wxTextCtrl(luaBoxSizer->GetStaticBox(),
137-
wxID_ANY,
138-
wxEmptyString,
139-
wxDefaultPosition,
140-
wxDefaultSize,
141-
wxTE_MULTILINE | wxTE_DONTWRAP);
136+
m_luaTextCtrl = new wxStyledTextCtrl(luaBoxSizer->GetStaticBox(),
137+
wxID_ANY,
138+
wxDefaultPosition,
139+
wxDefaultSize,
140+
wxTE_MULTILINE | wxTE_DONTWRAP);
141+
DoSetupStyledTextCtrl(m_luaTextCtrl);
142142
luaBoxSizer->Add(m_luaTextCtrl, 1, wxEXPAND | wxALL, 0);
143143
m_luaTextCtrl->Bind(wxEVT_TEXT, &LuaTab::OnLuaTextCtrlChanged, this);
144144

@@ -392,13 +392,13 @@ void LuaTab::OnOpenDirButtonClicked(wxCommandEvent &)
392392

393393
void LuaTab::OnHelpButtonClicked(wxCommandEvent &event)
394394
{
395-
wxString url = "https://x-tools-author.github.io/wx-tools/";
395+
wxString url = "https://x-tools-author.github.io/wx-tools/docs/12.lua/";
396396
wxLaunchDefaultBrowser(url);
397397
}
398398

399399
void LuaTab::OnNewButtonClicked(wxCommandEvent &event)
400400
{
401-
wxTextEntryDialog dlg(this,
401+
wxTextEntryDialog dlg(nullptr,
402402
_("Enter the name of the new Lua script file:"),
403403
_("New Lua Script"),
404404
"LuaScript",
@@ -492,4 +492,26 @@ wxString LuaTab::GetCurrentLuaFilePath()
492492
}
493493

494494
return *clientData;
495+
}
496+
497+
void LuaTab::DoSetupStyledTextCtrl(wxStyledTextCtrl *textCtrl)
498+
{
499+
textCtrl->SetLexer(wxSTC_LEX_LUA);
500+
// 显示行号
501+
textCtrl->SetMarginType(0, wxSTC_MARGIN_NUMBER);
502+
textCtrl->SetKeyWords(0, "function end local if then else for while do return break");
503+
textCtrl->StyleSetForeground(wxSTC_LUA_DEFAULT, wxColour("#000000"));
504+
textCtrl->StyleSetForeground(wxSTC_LUA_COMMENT, wxColour("#008000"));
505+
textCtrl->StyleSetForeground(wxSTC_LUA_COMMENTLINE, wxColour("#008000"));
506+
textCtrl->StyleSetForeground(wxSTC_LUA_COMMENTDOC, wxColour("#808080"));
507+
textCtrl->StyleSetForeground(wxSTC_LUA_NUMBER, wxColour("#0000FF"));
508+
textCtrl->StyleSetForeground(wxSTC_LUA_STRING, wxColour("#A32121"));
509+
textCtrl->StyleSetForeground(wxSTC_LUA_CHARACTER, wxColour("#A32121"));
510+
textCtrl->StyleSetForeground(wxSTC_LUA_LITERALSTRING, wxColour("#A32121"));
511+
textCtrl->StyleSetForeground(wxSTC_LUA_PREPROCESSOR, wxColour("#800080"));
512+
textCtrl->StyleSetForeground(wxSTC_LUA_OPERATOR, wxColour("#000000"));
513+
textCtrl->StyleSetForeground(wxSTC_LUA_IDENTIFIER, wxColour("#000000"));
514+
textCtrl->StyleSetForeground(wxSTC_LUA_STRINGEOL, wxColour("#FF0000"));
515+
textCtrl->StyleSetForeground(wxSTC_LUA_WORD, wxColour("#0000FF"));
516+
textCtrl->StyleSetBold(wxSTC_LUA_WORD, true);
495517
}

src/Page/Tabs/Lua/LuaTab.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <wx/bmpbuttn.h>
1414
#include <wx/gbsizer.h>
1515
#include <wx/sizer.h>
16+
#include <wx/stc/stc.h>
1617
#include <wx/textctrl.h>
1718
#include <wx/wx.h>
1819

@@ -63,13 +64,14 @@ class LuaTab : public wxPanel
6364
void OnThreadInvokeWrite(wxThreadEvent &event);
6465

6566
wxString GetCurrentLuaFilePath();
67+
void DoSetupStyledTextCtrl(wxStyledTextCtrl *textCtrl);
6668

6769
private:
6870
LuaRunner *m_luaRunner{nullptr};
6971
wxBoxSizer *m_controllerBoxSizer;
7072
wxGridBagSizer *m_mainSizer;
7173

72-
wxTextCtrl *m_luaTextCtrl;
74+
wxStyledTextCtrl *m_luaTextCtrl;
7375
wxTextCtrl *m_logTextCtrl;
7476
wxComboBox *m_fileComboBox;
7577
wxBitmapButton *m_runButton;

0 commit comments

Comments
 (0)