This article will introduce how to set system parameters through LUA scripts, such as common baud rates, buzzer settings, RTC settings, and backlight brightness. The relevant functions involved in this section are
1. Callback function for user touch modification control: on_control_notify(screen,control,value). This function has three parameters:
- screen: represents the screen ID
- control: represents the control number
- value: represents the control value.
2. Set the baud rate for serial port communication: uart_set_baudrate(baudrate).
- baudrate: represents the set baud rate value. 232/TTL supports 1200~961200bps, with a typical value of 115200bps. 485 supports 1200~115200bps, with a typical value of 9600bps.
3. Set the backlight brightness: set_backlight(level).
- level: represents the set backlight value, with a range of 0~100. The higher the value, the brighter the backlight brightness.
4. Set the buzzer beeping duration: beep(time).
- time: represents the length of time for the buzzer to sound, in milliseconds. It does not control the enabling/disabling of the buzzer.
5. Set the screen RTC time: set_date_time(year,month,day,hour,minute,second)
- year: indicates the year
- month: indicates the month
- day: indicates the day
- hour: indicates the hour
- minute: indicates the minute
- second: indicates the second
Note: For more LUA information, refer toLUA Script API Function Interfacesection and website: www.runoob.com/lua
Scope of application: M series, W series, X series, F series (firmware version >= V4.2.401.0)
Routine download link: "<> System Parameter Settings"(Click to jump)
3.1 Modify baud rate
Screen configuration
Add two button controls (ID1, ID2), and set the baud rates to 19200 and 115200 respectively; add a text control (ID14) to display the baud rate.
- Two button controls: The internal instructions are configured to have a mutually exclusive effect, and both operate in a set-reset style. Specifically, the initial state of ID1 button is pressed, while the initial state of ID2 button is unpressed
- Text control: The input method is system variable, and the system variable is baud rate

LUA script
When the user clicks the [19200] or [115200] button control, the on_control_notify(screen, control, value) callback function is triggered. Depending on which button is pressed, the uart_set_baudrate(baudrate) function is called to set the corresponding baud rate. The code snippet is shown below
--[[***************************************************************************
** Function name: on_control_notify
** Descriptions: 用户通过触摸修改控件后,执行此回调函数。
点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
注意:回调函数的参数和函数名固定不能修改
** Input value : screen 画面ID
control 控件ID
value 控件值(包括文本控件输入的值)
***************************************************************************--]]
function on_control_notify(screen,control,value)
......
if screen==0 and control==1 and value==1 --按下第0页、编号1按钮
then
uart_set_baudrate(19200) --设置波特率为19200
elseif screen==0 and control==2 and value==1 --按下第0页、编号2按钮
then
uart_set_baudrate(115200) --设置波特率为115200
end
......
endPSThe uart_get_baudrate() function is used to obtain the current baud rate. For example: local baudrate = uart_get_baudrate() ↩
Running effect
According to the code segment mentioned above, the running effect is as follows

3.2 Set Backlight
The adjustment of the backlight can be adjusted by sliding with the progress bar and slider
Screen Configuration
Add a slider control (ID4) and a progress bar control (ID3) in the screen as the control for the backlight. The relevant control property configurations are as follows
- Slider Control: Set the initial range of values to 20~100 (if the backlight adjustment is set to 0 or lower, the content of the screen will be unclear). The backlight level set by the LUA script supports 0-100
- Progress Bar Control: Set the 'Slide Notification' to be associated with the slider, and add the corresponding background image

LUA Script
When the user clicks/slides the slider, the backlight value and progress bar also change accordingly. The sliding process triggers the LUA script on_control_notify(screen,control,value), which calls set_backlight(level) to set the backlight value. The code segment is as follows
--[[***************************************************************************
** Function name: on_control_notify
** Descriptions: 用户通过触摸修改控件后,执行此回调函数。
点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
注意:回调函数的参数和函数名固定不能修改
** Input value : screen 画面ID
control 控件ID
value 控件值(包括文本控件输入的值)
***************************************************************************--]]
function on_control_notify(screen,control,value)
......
if screen==0 and control == 4 --滑动第0页、编号4滑动条
then
set_backlight(value) --设置背光为value
end
......
endPSThe .get_backlight function is used to obtain the current backlight value, for example: local cur_backlight = get_backlight() ↩
Run Preview
The virtual screen running effect here is only used as a demonstration of control changes. To actually adjust the backlight effect, you need to compile and download it to the physical screen for experience.
3.3 Set the buzzer to sound.
Screen configuration.
Place a button control (ID: 5), and select "Yes" for the event notification in the button properties. As shown below.

LUA script.
When the button is pressed, the event notification function on_contrl_notify(screen,control,value) is triggered. After the corresponding control is triggered, the beep(500) function is called, indicating that the buzzer sounds for 500ms. The code segment is shown below.
--[[***************************************************************************
** Function name: on_control_notify
** Descriptions: 用户通过触摸修改控件后,执行此回调函数。
点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
注意:回调函数的参数和函数名固定不能修改
** Input value : screen 画面ID
control 控件ID
value 控件值(包括文本控件输入的值)
***************************************************************************--]]
function on_control_notify(screen,control,value)
......
if screen==0 and control == 5 and value == 1 --滑动第0页、编号4滑动条
then
beep(500) --设置蜂鸣器鸣叫500ms
end
......
endRun the preview.
Download to the serial port screen, click the "sound" button, and hear a "beep" sound.
3.4 Set the RTC.
In the screen, place an RTC control, and set different times for two buttons. The relevant attribute configuration of the control is shown below.

LUA script.
Trigger the event notification function on_contrl_notify(screen,control,value). After the corresponding control is triggered, the set_date_time() function is called to set the RTC time. The code is shown below:
--[[***************************************************************************
** Function name: on_control_notify
** Descriptions: 用户通过触摸修改控件后,执行此回调函数。
点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
注意:回调函数的参数和函数名固定不能修改
** Input value : screen 画面ID
control 控件ID
value 控件值(包括文本控件输入的值)
***************************************************************************--]]
function on_control_notify(screen,control,value)
......
if screen==0 and control == 6 and value == 1 --滑动第0页、编号4滑动条
then
set_date_time(2019,1,1,1,1,1) --设置当前时间为2019/01/01 01:01:01
end
......
endPS. The `get_date_time` function is used to obtain the current RTC value. For example: `local cur_backlight = get_backlight()` ↩
Run the preview
and download it to the serial port screen. Click the "Time 1" or "Time 2" button to see the RTC time change on the left side. Note that the virtual screen does not support modifying the RTC control, as it is the time of the computer PC end
Leave a Reply