Wiki source code of 09 Lua Editor

Last modified by Jim on 2023/08/11 09:10

Hide last authors
Hunter 3.1 1 Lua script is only supported on HMI+, i series, ie series, ig series or models whose upgrade OS version to HMI V2.0 and above;
Leo Wei 1.1 2
3 (% style="text-align:center" %)
Joey 2.1 4 [[image:HMI Lua Editor user manual_html_90c08e4cdcf4210c.png||height="484" width="500" class="img-thumbnail"]]
Leo Wei 1.1 5
6
7 (% style="text-align:center" %)
Joey 2.1 8 [[image:HMI Lua Editor user manual_html_674bb6778b0f003.png||height="487" width="500" class="img-thumbnail"]]
Leo Wei 1.1 9
Hunter 3.1 10 = **Lua Editor** =
Leo Wei 1.1 11
12 The editor is divided into two areas, one is the script file area, and the other is the script editing area.
13
14 (% style="text-align:center" %)
Joey 2.1 15 [[image:HMI Lua Editor user manual_html_34da990820a7a4e5.png||height="208" width="400" class="img-thumbnail"]]
Leo Wei 1.1 16
Stone Wu 5.1 17 == Script file area ==
Leo Wei 1.1 18
19 The script file area is located on the left side, named after the Lua, It will be referred to as the Lua node hereinafter.
20
Hunter 3.1 21 **Lua Node**
Leo Wei 1.1 22
Stone Wu 5.1 23 * Create new file: right-click the Lua node to select new file, and enter a name in pop-up window, (the name starts with an underscore or letter);
24 * Refresh list: the file edited (new/deleted/renamed) by the user externally, the configuration is not refreshed. In this case this function needs to be used to synchronize changes;
Leo Wei 1.1 25
Jim 6.1 26 (% class="box infomessage" %)
27 (((
Stone Wu 5.1 28 **✎Note: **
Leo Wei 1.1 29
Stone Wu 5.1 30 * There is no script file for the newly created project. Following two new files will automatically generate.
31 * Script_BG.lua: is the global background script file, which includes initialization and timing functions
32 * Script_Screen.lua: is a screen script file, and each screen can have initialization, timing, and close functions
33 * At any time, as long as there is a new operation, the software will judge whether the above two files exist, and which one is missing will be automatically filled.
Jim 6.1 34 )))
Leo Wei 1.1 35
Hunter 3.1 36 **Lua Child Node**
Leo Wei 1.1 37
38 After creating a new file, expand the Lua node to display several script files. The following is referred to as child nodes.
39
40 (% style="text-align:center" %)
Joey 2.1 41 [[image:HMI Lua Editor user manual_html_fc10128583a230da.png||height="143" width="200" class="img-thumbnail"]]
Leo Wei 1.1 42
Stone Wu 5.1 43 * Child node color description:
44 * Red: encrypted file
45 * Yellow: read-only file
46 * Green: existed file or refresh the list to turn white to green
47 * White: The newly created file, and the project will turn green after refreshing, protecting or reopening the project
Leo Wei 1.1 48
Stone Wu 5.1 49 **Delete:** right-click the child node to delete. The deleted node will be placed in the recycle bin.
Leo Wei 1.1 50
Stone Wu 5.1 51 **Rename: **right-click child node to rename.
Leo Wei 1.1 52
Stone Wu 5.1 53 **Encryption:** right-click the child node, select encryption. Each file can be set with an independent password(length 1-6, only uppercase/lowercase letters and numbers are accepted).
Leo Wei 1.1 54
Stone Wu 5.1 55 * Encryption mode: When this mode is turned on, you need to verify the password to view and edit.
56 * Read-only mode: This mode is opened without verifying the password, if you want to edit, you must first release the protection
Leo Wei 1.1 57
Stone Wu 5.1 58 **Unprotect:** Right-click on the child node, select Unprotect, enter the password to release.
Leo Wei 1.1 59
Jim 6.1 60 (% class="box infomessage" %)
61 (((
Leo Wei 1.1 62 **✎Note: **If user forgets the password, the script file can never be recovered, the software will not storage any passwords, developers need to keep the password by themselves carefully.
Jim 6.1 63 )))
Leo Wei 1.1 64
Stone Wu 5.1 65 == Script editor ==
Leo Wei 1.1 66
67 The script editor is divided into three blocks: the upper part is the toolbar, the middle part is the editing area, and the lower part is the output area.
68
Hunter 3.1 69 **Tool Bar**
Leo Wei 1.1 70
71 Open any script file, the toolbar is as follows:
72
73 (% style="text-align:center" %)
Joey 2.1 74 [[image:HMI Lua Editor user manual_html_59f8bacea4239441.png||height="29" width="279" class="img-thumbnail"]]
Leo Wei 1.1 75
76 Save, cut, copy, paste, word address, bit address, zoom in, zoom out, display spaces and tabs, grammar check.
77
78 Find and replace, undo and redo and more functions see shortcut keys manual.
79
Hunter 3.1 80 **Editor Area**
Leo Wei 1.1 81
Stone Wu 5.1 82 * **Highlight color:** Lua's built-in keywords has a different color with the Lua interface functions. The functions written by users will not be highlighted
83 * **Auto-complete:** Lua function auto-complete prompts, Lua chunk auto-complete (such as if xx then end), also supports the completion of all the words in this script file. Use the Enter key to select candidates words, use the Tab key to turn off automatic completion.
84 * **Automatic indentation: **The tab key can add 4 spaces
85 * **Calling prompt:** only available for Lua interface functions
86 * **Hovering hint:** for example the mouse stays near the o letter position of demo() , it will display the prototype of the function
87 * **(6)** **Shortcut keys:** In order to facilitate users, various shortcut keys have been added. Detailed description: Configure Lua shortcut keys.doc
88 * **Interface document: **The scripts listed in the interface document will be distinguished by the highlighted color in the editing area. You can check whether the interface is called incorrectly by judging the color change. Detailed description: Lua interface manual.
Leo Wei 1.1 89
Hunter 3.1 90 **Output Area**
Leo Wei 1.1 91
92 Click the syntax button in the toolbar to perform a grammar check. If the script is written incorrectly, it will be displayed in the window. Double-click the prompt to locate the "near" line of the error.
93
Hunter 3.1 94 = **Lua Object** =
Leo Wei 1.1 95
96 Lua objects are mainly used to call functions and complete user interface interactions. Normally, functions should not be written in objects. Next, we will explain how to use the objects.
97
98 (% style="text-align:center" %)
Joey 2.1 99 [[image:HMI Lua Editor user manual_html_a9a41fb25f413fec.png||height="317" width="150" class="img-thumbnail"]]
Leo Wei 1.1 100
Stone Wu 5.1 101 == How to use Lua button ==
Leo Wei 1.1 102
103 Find the custom part->LuaScript on the right side, drag and draw to generate a part. Double-click the part to pop up a dialog box.
104
105 (% style="text-align:center" %)
Joey 2.1 106 [[image:HMI Lua Editor user manual_html_14bab1899c9ccc62.png||height="267" width="400" class="img-thumbnail"]]
Leo Wei 1.1 107
108 Click Lua object will trigger two actions, that is, rising and falling scripts;
109
Stone Wu 5.1 110 * **Falling script**: after the button is pressed, the code chunk of the area is executed;
111 * **Rising script**: after the button is raised, the code chunk in the area is executed;
112 * **Timing script**: periodically execute the code.
Leo Wei 1.1 113
Jim 7.1 114 (% class="box infomessage" %)
115 (((
Stone Wu 5.1 116 **✎Note: **
Leo Wei 1.1 117
Stone Wu 5.1 118 * The timing script is not commonly used because it is similar to the screen background script function. It is not recommended to write and call function here. It should be written in Script_Screen.lua, which is convenient for editing, searching, and replacing operations.
119 * It is not allowed to write endless loops or call functions that will loop enlessly in the widget. Once used, the entire screen will be stuck, and any buttons will no longer be used. We have to restart the screen to restore.
Jim 7.1 120 )))
Leo Wei 1.1 121
Hunter 3.1 122 = **Platform** =
Leo Wei 1.1 123
Stone Wu 5.1 124 == File header identification ==
Leo Wei 1.1 125
126 Any new file will automatically generate a line of code, as shown below:
127
128 (% style="text-align:center" %)
Joey 2.1 129 [[image:HMI Lua Editor user manual_html_40558d3555455933.png||height="116" width="270" class="img-thumbnail"]]
Leo Wei 1.1 130
131 The format of this variable: file name_limits = number
132
133 The file name comes from the newly created file name (without suffix):
134
135 (% style="text-align:center" %)
Joey 2.1 136 [[image:HMI Lua Editor user manual_html_52fb935bcc76d7ad.png||height="113" width="203" class="img-thumbnail"]]
Leo Wei 1.1 137
Stone Wu 5.1 138 * **_limits: **Fixed format, cannot be modified
139 * **Description**:
140 * **Tens digit=1**: indicates that the function or global variable defined in this file can be used in the background code file (Script_BG.lua). If it is 0, it is not available.
141 * **Ones digit=1**: indicates that the functions or global variables defined in this file can be used in the screen code file (Script_Screen.lua) or LuaScript components. If it is 0, it is not available.
Leo Wei 1.1 142
Jim 8.1 143 (% class="box infomessage" %)
144 (((
Leo Wei 1.1 145 **✎Note: **If you rename the file manually, you must modify the limits in the file accordingly, otherwise the script file will not take effect.
Jim 8.1 146 )))
Leo Wei 1.1 147
Stone Wu 5.1 148 == Built-In script file ==
Leo Wei 1.1 149
150 As mentioned above, the first new build will automatically generate two script files. These two files cannot be deleted and renamed, which will be explained in detail here.
151
Stone Wu 5.1 152 **Script_BG.lua** is a global background script file, which includes initialization and polling functions
Leo Wei 1.1 153
154 (% style="text-align:center" %)
Joey 2.1 155 [[image:HMI Lua Editor user manual_html_5eb1ad5776f18955.png||height="148" width="200" class="img-thumbnail"]]
Leo Wei 1.1 156
Stone Wu 5.1 157 * we_bg_init(): global initialization, that is, execute once when power on.
158 * we_bg_poll(): Global polling, that is, the cycle is repeated after power on.
Leo Wei 1.1 159
Hunter 3.1 160 Script_Screen.lua is a screen script file, and each screen has an initialization, polling, and closing functions
Leo Wei 1.1 161
162 (% style="text-align:center" %)
Joey 2.1 163 [[image:HMI Lua Editor user manual_html_e6807eb0da0eedd4.png||height="231" width="300" class="img-thumbnail"]]
Leo Wei 1.1 164
Stone Wu 5.1 165 * we_scr_init_0(): execute this function when screen 0 is initialized
166 * we_scr_poll_0(): when in picture 0, the function is executed repeatedly
167 * we_scr_close_0(): execute this function when screen 0 is closed
Leo Wei 1.1 168
169 The above is the initialization function of screen 0. For screen 100, you can replace 0 with 100. You can add your own function: function we_scr_init_100() end
170
Jim 9.1 171 (% class="box infomessage" %)
172 (((
Leo Wei 1.1 173 **✎Note: **The above function has a fixed name. If it is modified, it will not cause crash, but the function can be found.
Jim 9.1 174 )))
Leo Wei 1.1 175
Stone Wu 5.1 176 **Example:** After the screen 0 initialization function is changed to we_scR_init_0(), the script will not be executed when screen 0 is initialized, and then no script will be executed, and nothing will be done because the "we_scr_init_0" function cannot be found.
Leo Wei 1.1 177
Stone Wu 5.1 178 == Script type (operating mechanism) ==
Leo Wei 1.1 179
180 There are two types of scripts:
181
Stone Wu 5.1 182 * Active: script automatically executed by HMI
183 * Passive: Script executed only after user trigger
Leo Wei 1.1 184
185 The active types includes:
186
Stone Wu 5.1 187 * Global _bg_ related functions in Script_BG.lua
188 * Screen _scr_ related functions in Script_Screen.lua
Leo Wei 1.1 189
190 Polling script in Lua object
191
Stone Wu 5.1 192 The passive type scripts include: The rising and falling script in Lua object.