LUA script API function interface

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

LUA script API function interface

This document is applicable to the M series, W series, X series, and F series (firmware version >= V4.2.401.0). The parameters "screen" and "control" in the API functions represent the target screen ID and target configuration control ID, respectively. Each API will not be introduced individually in the following text.

1.1 Control Attribute Class

1.1.1 change_screen(screen)

Switch to the specified screen

1.1.2 change_child_screen (screen)

Switch to a child screen, such as a dialog box

1.1.3 change_screen_effect(screen,effect)

Switch to the specified screen using an animation effect

  • effect: animation effect. 0 - no animation, 1 - left to right, 2 - right to left, 3 - top to bottom, 4 - bottom to top, 5 - fade in/fade out

1.1.4 set_slide_screen(slide,round,start_screen,end_screen)

Set the window sliding mode

  • slide: 0 - disable sliding, 1 - slide left to right, 2 - slide up and down
  • round: 1 - Loop mode, the last page can be scrolled to the first page
  • start_screen: Start screen
  • end_screen: End screen

1.1.5 set_button_notify_release()

Set button release notification mode, the window can also be scrolled on the touch button

1.1.6 get_current_screen()

Get current screen ID

1.1.7 set_value(screen, control, value)

Set control value

  • value: Control value

Button control: value=1 - pressed, 0 - released

Text control: value - integer or decimal

Progress bars, sliders, gauges, sliding selection controls, and more can also be set up

1.1.8 get_value(screen,control)

Gets the value of a control, such as buttons, text, progress bars, sliders, gauges, etc.

1.1.9 set_visiable(screen,control,visiable)

Sets whether a control is visible

  • visiable: 0-hidden, 1-displayed

1.1.10 set_enable(screen,control,enable)

Sets whether a control is touchable

  • enable: 0-touch disabled, 1-touch enabled

1.1.11 set_fore_color(screen,control,color)

Sets the foreground color of a control, such as the text color of a text control or the display color of a progress bar

  • color: RGB value, 2 bytes, RGB565

1.1.12 set_back_color(screen,control,color)

Set the background color of controls, such as the background color of text controls and progress bars

  • color: RGB value, 2 bytes, RGB565

1.1.13 set_text(screen,control,text)

Set the display content (string) of controls, such as text controls and QR code controls

  • text: string type

1.1.14 set_text_roll(screen,control, speed)

Set the scrolling speed of text controls from right to left

  • speed: number of pixels per second. Set to 0 to stop scrolling

1.1.15 set_text_flicker (screen,control, cycle)

Set the flickering cycle of text controls

  • cycle: flickering cycle, unit: 10ms. Set to 0 to stop flickering

1.1.16 get_text(screen,control)

Get control string content (string), text control, QR code control, etc.

1.1.17 set_options (screen, control, options)

Set the content of the sliding selection control

  • options: Option content, in string format, with each item separated by a semicolon ';'

Example: set_options (screen,control, 'Option 1;Option 2;Option 3;')

1.1.18 set_screen_range(screen, control, start_screen,end_screen, round)

Set the sliding mode of the sub-screen window, supported by W series, not supported by F and M series

  • start_screen: Starting screen
  • end_screen: Ending screen
  • round: 1 loop mode, the last page can slide to the first page

1.1.19 play_animation(screen, control)

Set animation control to play

1.1.20 stop_animation(screen, control)

Set animation control to stop

1.1.21 set_history_graph_value(screen, control, channel0,..,channel7)

Set the channel values of the historical curve, fill in the parameters according to the number of channels

  • channe0: value of channel 0
  • …
  • channe7: value of channel 7

For example, if there are 4 channels: set_history_graph_value(screen, control, 1, 2, 3, 4)

1.1.22 set_history_graph_direction(screen, control, direction)

Set the refresh direction of the historical curve. By default, the refresh direction is from right to left

  • direction: 0-from right to left, 1-from left to right

1.1.23 set_scroll_position(screen, control, mode)

Set the page turning mode for historical curves

  • mode: page turning mode. 2-next page, 3-previous page, 4-last page, 5-first page

1.2 Record control

1.2.1 record_set_event(screen,control,eventid)

Alarm type - trigger alarm

  • eventid: associated with the alarm parsing ID in the data record control of the alarm type

1.2.2 record_reset_event(screen,control,eventid)

Alarm type - clear alarm

  • eventid: associated with the alarm parsing ID in the data record control of the alarm type

1.2.3 record_add(screen,control,record)

Add a record at the end

  • record: record is a string
例如:record_add(screen,control, “item1;item2;item3;”)

1.2.4 record_insert(screen, control, position, record)

Inserts a record at the specified position

  • position: The insertion position index
  • record: The record is a string

1.2.5 record_clear(screen, control)

Clears record data

1.2.6 record_setoffset(screen, control, offset)

Sets the scroll display position

  • offset: The current display is positioned to this index

1.2.7 record_get_count(screen, control)

Gets the number of records

例如:local recordCnt = record_get_count(0, 1) —假设画面0、控件1位数据记录控件

1.2.8 record_read(screen, control, position)

Read a record and return the string

  • position: the row number to be read. The index starts from 0, with 0 representing the first row of data

1.2.9 record_modify(screen,control, position ,record)

Modify a record

  • position: the row number to be modified, the index starts from 0, with 0 representing the first row of data
  • record: the new modification content

1.2.10 record_delete(screen,control,position)

Delete a record

  • position: the row number to be deleted. The index starts from 0, with 0 representing the first row of data

1.2.11 record_select(screen,control,position)

Select a record

  • position: the row number to be selected. The index starts from 0, with 0 representing the first row of data

1.2.12 record_export(screen,control)

Export records to SD card/U disk

1.3 Common callback functions

1.3.1 on_init()

This callback function is called immediately after the system loads the LUA script file and is usually used to perform initialization operations.

1.3.2 on_systick()

This callback function is automatically called by the system every 1 second.

1.3.3 on_control_notify(screen,control,value)

This callback function is executed after the user touches and modifies a control. Clicking a button control, modifying a text control, or modifying a slider will trigger this control

  • value: is of numeric type. If you need to obtain the string value of a text control, use get_text(screen,control)

1.3.4 on_screen_change(screen)

This callback function is executed when the screen needs to be switched, with screen being the target screen.

Note that this function internally calls `change_screen` and will not execute `on_screen_change` in a nested manner.

1.3.5 `on_press(state,x,y)`

This callback function is executed when the user clicks on the touch screen.

  • `state`: touch state, 0-released, 1-pressed, 2-continuously pressed
  • `x`: touch X coordinate
  • `y`: touch Y coordinate

1.3.6 `on_usb_inserted(driver)`

This callback function is executed when a USB drive is inserted

  • `dirver`: drive letter of the USB drive

1.3.7 `on_usb_removed()`

This callback function is executed when a USB drive is removed

1.3.8 `on_sd_inserted(dir)`

SD Card Insertion Notification

  • dir: Drive Letter Path

1.3.9 on_sd_removed()

SD Card Removal Notification

1.3.10 on_screen_sliding(screen, control)

Used to determine the target screen ID when a finger slides. Supported by the W series, but not supported by the F and M series.

Returns the ID of the screen to switch to. There are four parameters: left, right, up, and down, specifying the page IDs for left, right, up, and down directions. If it is -1, sliding in that direction is not allowed.

  • screen: The currently displayed screen.
  • control: The current page, the ID of the control that is being slid on.

1.4 Drawing Functions

1.4.1 on_draw(screen)

When the display content of the interface needs to be updated, the system automatically calls this function. Users add custom drawing operations in this function. The content drawn by the user is superimposed on the screen content control. This function is a system callback function, and users should not call it directly

This function is triggered in the following situations:

  • Dynamic refresh of animations, video playback, and RTC time display on the interface
  • User interaction with screen controls
  • Updating controls through LUA scripts or serial port commands
  • Executing redraw

In summary, any change on the interface will trigger this callback function.

1.4.2 on_pre_draw(screen)

The content drawn by the user is at the bottom layer of the screen, and the currently drawn screen needs to be transparent without a background image

The relevant description is the same as above

1.4.3 redraw()

Sends a redraw request, triggering the execution of on_draw

1.4.4 screen_shoot(filename,x,y,width,height,quality)

Capture the screen window range and store it to the specified file path. Supported by W series, not supported by F and M series.

  • filename: Path for storing the image file.
  • x: Starting x-coordinate of the screenshot.
  • y: Starting y-coordinate of the screenshot.
  • width: Width of the screenshot.
  • height: Height of the screenshot.
  • quality: JPEG image quality, default is 95.

Example: screen_shoot('b:/shoot.jpg', 0, 0, 480, 272, 95).

1.4.5 set_pen_color(color)

Set the color of the brush, RGB565, used to specify the color of lines, rectangles, circles, etc.

  • color: RGB565 color value.

1.4.6 draw_line(x0,y0,x1,y1,width)

Draw a straight line

  • x0,y0: Starting point coordinates
  • x1,y1: End point coordinates
  • width: The thickness of the line, 1~10

1.4.7 draw_rect(x0,y0,x1,y1,fill)

Draw a rectangle

  • x0,y0: Top left corner coordinates
  • x1,y1: Bottom right corner coordinates
  • fill: 0 for no filling, 1 for filling

1.4.8 draw_rect_alpha(x0,y0,x1,y1,alpha)

Draw a solid semi-transparent rectangle, not supported by the F series

  • x0,y0: Top left corner coordinates
  • x1, y1: lower right corner coordinates
  • alpha: transparency 0 (fully transparent) ~ 255 (opaque)

1.4.9 draw_circle(x, y, r, fill)

Draw a circle

  • x, y: center coordinates of the circle
  • r: radius of the circle
  • fill: 0 (no filling), 1 (filling)

1.4.10 draw_ellipse(x0, y0, x1, y1, fill)

Draw an ellipse

  • x0, y0: upper left corner coordinates
  • x1, y1: lower right corner coordinates
  • fill: 0 (no filling), 1 (filling)

1.4.11 load_image (image_id, frame_id)

Loads the specified image into memory, typically used in on_init, sacrificing startup speed but making the running process smoother. Supported by the W series, not supported by the F and M series

  • image_id: ID of the image resource
  • frame_id: corresponding icon, can be set to a frame ID, other images are fixed to 0

1.4.12 draw_image(image_id, frame_id, dstx, dsty, width, height, srcx, srcy)

Draws an image

  • image_id: ID of the image resource
  • frame_id: corresponding icon, can be set to a frame ID, other images are fixed to 0
  • dstx: X coordinate for image display
  • dsty: Y coordinate for image display
  • width: width of image display
  • height: height of image display
  • srcx: Image cropping X coordinate
  • srcy: Image cropping Y coordinate

1.4.13 draw_image_file(filename,dstx,dsty,width,height,srcx,srcy)

Draws an image. This method does not cache images and has low efficiency. Supported by W series, not supported by F and M series

  • filename: Image file, supports JPEG/PNG
  • dstx: Image display X coordinate
  • dsty: Image display Y coordinate
  • width: Image display width
  • height: Image display height
  • srcx: Image cropping X coordinate
  • srcy: Image cropping Y coordinate

1.4.14 load_surface (filename)

Load an image to a layer. Supported by W series, M series >= V6.1.241.00 (cropping display supported), not supported by F series.

  • filename: image file, supports JPEG/PNG.

Example: surface = load_surface("c:/test.jpg")

When the layer is no longer used, it needs to be destroyed by calling destroy_surface. Otherwise, it will cause memory leaks.

1.4.15 destroy_surface(surface)

Destroy the layer. Supported by W series, M series >= V6.1.241.00, not supported by F series.

  • surface: layer resource pointer.

1.4.16 draw_surface(surface, dstx, dsty, width, height, srcx, srcy)

Draw the layer. Compared to draw_image_file, this method is more efficient. Supported by W series, M series >= V6.1.241.00 (cropping display supported), not supported by F series.

  • surface: layer resource pointer.
  • dstx: X coordinate of image display.
  • dsty: Y coordinate of image display
  • width: width of the displayed image [optional]
  • height: Display height of the image [optional]
  • srcx: X-coordinate for image cropping [optional]
  • srcy: Y coordinate for image cropping [optional]

Example:

  1. Tile display: draw_surface(surface, dstx, dsty)
  2. Zoom display: draw_surface(surface, dstx, dsty, width, height)
  3. Crop display: draw_surface(surface, dstx, dsty, width, height, srcx, srcy)

1.4.17 draw_text(text,x,y,w,h,font,color,align)

Display text

  • text: String
  • x: Display X coordinate
  • y: Display Y coordinate
  • w: Display width
  • h: Display height
  • font: Font number
  • color: Color (RGB565)
  • align: Alignment

bit0~bit1: Horizontal alignment, 0 for left alignment, 1 for center alignment, 2 for right alignment

bit2~bit3: Vertical alignment, 0 for top alignment, 1 for center alignment, 3 for bottom alignment

1.5 MODBUS protocol access

To access variables defined in MODBUS/PLC protocols in LUA, the following variable access interface is required

The interface prefixed with "mb_" is dedicated to the MODBUS protocol. Firmware version requirements: W series >= 3.0.590.0, F series >= 4.1.401.0, M series >= V6.1.59.00

1.5.1 get_variant(name)

Get the value of the protocol variable

  • name: variable name
例如:get_variant(“Variable1”)

1.5.2 set_variant(name,value)

Set the value of the protocol variable

  • name: variable name
  • value: the value to be assigned to the variable
例如:set_variant(“Variable1”,12345)

1.5.3 mb_set_timeout (timeout)

Set the slave response timeout

  • timeout: range 1~255, in 10 millisecond units

1.5.4 mb_read_coil_01 (slave,addr,quantity)

01 function code, read coil, returns a byte array when successful, with 8 coils per byte, returns nil on failure

  • slave: slave address
  • addr: variable address
  • quantity: number of coils read

1.5.5 mb_read_input_02(slave,addr,quantity)

02 function code, read discrete input, returns byte array on success, one byte for 8 inputs, returns nil on failure

  • slave: slave address
  • addr: variable address
  • quantity: number of discrete input registers read

1.5.6 mb_read_reg_03(slave,addr,quantity)

03 function code, read hold register, returns WORD array on success, returns nil on failure

  • slave: slave address
  • addr: variable address
  • quantity: number of save registers read

1.5.7 mb_read_input_reg_04(slave,addr,quantity)

04 function code, read input register, return WORD array on success, return nil on failure

  • slave: slave address
  • addr: variable address
  • quantity: number of input registers read

1.5.8 mb_write_coil_05 (slave,addr,status)

05 function code, write single coil, return true on success, return false on failure

  • slave: slave address
  • addr: variable address
  • status: 1 for ON, 0 for OFF

1.5.9 mb_write_reg_06 (slave,addr,reg)

06 function code, write single holding register, return true on success, return false on failure

  • slave: slave address
  • addr: variable address
  • reg: register value

1.5.10 mb_write_coil_15 (slave,addr,quantity,coils)

15 function code, write multiple coils, returns true on success, false on failure

  • slave: slave address
  • addr: variable address
  • coils: byte array, one byte contains 8 coils, subscript index starts from 0

1.5.11 mb_write_reg_16 (slave,addr,regs)

16 function code, write multiple holding registers, returns true on success, false on failure

  • slave: slave address
  • addr: variable address
  • regs: Register WORD array, with subscript index starting from 0

W-type supported, F-series and M-series not supported

1.6.1 get_wifi_cfg()

Returns 4 parameters

例如:wifi_mode, secumode, ssid, password = get_wifi_cfg()
  • wifi_mode: Wireless network mode. 0-Disable wireless network, 1-Wireless network card mode, 2-AP hotspot mode
  • secumode: Encryption mode 0-AUTO (default), 1-WEP, 2-WPAPSK, 3-WPAPSK2
  • ssid: Wireless network name
  • password: Wireless network password

1.6.2 set_wifi_cfg(wifi_mode, secumode, ssid, password)

Sets WIFI configuration, with parameter descriptions as above

1.6.3 on_wifi_callback(state,reason)

When the network status changes,

1.6.4 get_network_state()

will be called to obtain the network status, returning a parameter

例如:state = get_network_state()

status bit description

  • bit0 - wireless network connection
  • bit1 - wired network connection
  • bit2 - whether connected to the server
  • bit3 - whether there is a client connected

1.6.5 set_network_cfg(dhcp, ipaddr, netmask, gateway, dns)

dhcp enables DHCP, 0 disables, 1 enables. Only when disabled, the following parameters are valid

  • ipaddr: static IP
  • netmask: mask
  • gateway: subnet mask
  • dns: domain name server

1.6.6 get_network_cfg()

returns five parameters, with descriptions as above

例如:dhcp, ipaddr, netmask, gateway, dns = get_network_cfg()

1.6.7 save_network_cfg()

saves network settings and reconnects to the network

Note: Modifying network configuration, server parameters, etc. requires calling this function to save and take effect, as shown below

......
set_network_cfg(dhcp, ipaddr, netmask, gateway, dns)
save_network_cfg()
......

1.6.8 set_network_service_cfg(wificom, mode, port, server_addr)

sets network service parameters

  • wificom: defaults to 0, when 1 enables transparent transmission mode (i.e., wireless serial port screen)
  • mode: 0 disables network service, 1 client mode, 2 server mode
  • port: service port, defaults to 5050
  • server_addr: Server address (when acting as a client)

1.6.9 get_network_service_cfg()

Returns 4 parameters, with descriptions as above

wificom, mode, port, server_addr = get_network_service_cfg()

1.6.10 scan_ap()

Scans for wireless access points and returns the number of access points

ap_count = scan_ap()

1.6.11 get_ap_info(index)

Gets information about a specified access point

  • index: Serial number of the WIFI, as follows
local ap_cnt = scan_ap()
for i = 1, ap_cnt 
do
    local ssid, security, quality = get_ap_info(i - 1)
    record_add(sc_scanwifi, 1, ssid..";"..quality..";")
end
  • index Access point index, starting from 0
  • ssid: Access point name
  • security: Encryption method
  • quality: signal quality

1.6.12 client_send_data(packet)

Send a packet through the client's SOCKET to the server

  • packet: the sent byte array packet, with indices starting from 0
function my_sendToserver()
    local send_buff = {}    
    send_buff[0] = 0x00
    send_buff[1] = 0x01
    send_buff[2] = 0x02
    send_buff[3] = 0x03
    send_buff[4] = 0x04
    send_buff[5] = 0x05
    send_buff[6] = 0x06
    send_buff[7] = 0x07
    send_buff[8] = 0x08
    send_buff[9] = 0x09
    client_send_data(send_buff)
end
--用户通过触摸修改控件后,执行此回调函数。
--点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
    ......
    if screen == sc_tcp
    then 
        if control == 3 and value == 0
        then
            my_sendToserver()
        end
    end
    ......
end

1.6.13 server_send_data(clinet_id, packet)

Send a packet to the client through the server's SOCKET

  • clinet_id: the target client ID
  • packet: the sent byte array packet, with indices starting from 0
function my_sendToClien()
    local send_buff = {}    
    send_buff[0] = 0x00
    send_buff[1] = 0x01
    send_buff[2] = 0x02
    send_buff[3] = 0x03
    send_buff[4] = 0x04
    send_buff[5] = 0x05
    send_buff[6] = 0x06
    send_buff[7] = 0x07
    send_buff[8] = 0x08
    send_buff[9] = 0x09
    server_send_data(1,send_buff)
end
--用户通过触摸修改控件后,执行此回调函数。
--点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
    ......
    if screen == sc_tcp
    then 
        if control == 3 and value == 0
        then
            my_sendToClien()
        end
    end
    ......
end

1.6.14 on_client_recv_data(packet)

When the client's SOCKET receives data, the system automatically calls back this function.

  • packet: the received byte array packet, with indices starting from 0

PS: The data received by the screen from the server may not be a complete frame of packet data. Here, it is still necessary to perform packet gluing and packet splitting, and then extract a complete frame of data for processing

function on_client_recv_data(packet)
    local client_rec = ''
    for i= 1,#(packet) do
            client_rec = client_rec .." ".. string.format("%02X",packet[i])
    end

    --简单显示报文数据
    set_text(sc_tcp, 1, 'cnt('..recv_cnt..') = '..client_rec)
end

​

1.6.15 on_server_recv_data(clinet_id, packet)

This function is automatically called back by the system when the server SOCKET receives data

  • clinet_id: client ID
  • packet: received byte array packet, with subscripts starting from 0

PS: The data received by the screen from the server may not be a complete frame of message data. Here, it is still necessary to perform packet splicing and packet division, and then extract a complete frame of data for processing

The processing method is similar to that of on_client_recv_data.

1.6.16 http_request(taskid, uri, method, content_type, postdata)

Sends an HTTP request to the server

  • taskid: request task number, set arbitrarily
  • uri: resource path
  • method: method, 0 for GET, 1 for POST

The following parameters are only required for the POST method

  • content_type: Data type, such as json, xml, text, etc.
  • postdata: POST data

1.6.17 on_http_response(taskid,response)

HTTP response

  • taskid: Response task ID, matched with http_request
  • response: Response data

1.6.18 http_download (taskid,uri,savepath)

Download files using HTTP protocol

  • taskid: Request task ID, set arbitrarily
  • uri: Resource path
  • savepath: Storage location

1.6.19 http_download_bigfile(taskid,uri ,savepath)

Download large files using HTTP protocol

Parameter description as above

1.6.20 on_http_download (taskid, status)

Download response

taskid: Response task ID, matched with http_download

status: Download status: 0 for download failure, 1 for download success but storage failure, 2 for download and storage success

1.6.21 udp_create(port)

  • port: Port number

Create UDP socket and bind to service port

例如:sockfd = udp_create(12345)

1.6.22 udp_close(sockfd)

Close UDP socket

  • sockfd: Socket, returned by udp_create(port)

1.6.23 udp_recvfrom(sockfd)

Receiving UDP datagram

  • ret=-1: indicates an error occurred, ret=0 indicates no data, other values indicate data length
  • ip, port: sender's IP and port
  • packet is the datagram, of type table, with indices starting from 0
ret,ip,port,packet = udp_recvfrom(sockfd)

1.6.24 udp_sendto(sockfd, ip, port, packet)

Sending UDP datagram

  • sockfd: UDP socket
  • ip, port: receiver's IP and port
  • packet: datagram, of type table, with indices starting from 0

1.6.25 get_wifi_mac()

Gets the MAC address of the WIFI module and returns a string

local mac_addr = get_wifi_mac()

1.6.26 get_ethernet_mac()

Gets the MAC address of the Ethernet and returns a string

local mac_addr = get_ethernet_mac()

1.7 Timer

1.7.1 start_timer(timer_id, timeout, countdown, repeat)

Starts the timer, and the system automatically calls on_timer after timeout

  • timer_id: Timer ID, 0~31
  • timeout: Timeout period, in milliseconds
  • countdown: 0 for countup, 1 for countdown
  • repeat: Number of repetitions, 0 for infinite repetition

1.7.2 stop_timer(timer_id)

Stops the timer

  • timer_id: Timer ID, 0~31

1.7.3 on_timer(timer_id)

Timer timeout callback function

  • timer_id: Timer ID, 0~31

1.7.4 get_timer_value(timer_id)

Gets the current timing time of the timer, in milliseconds

  • timer_id: Timer ID, 0~31

1.8 Serial port

Serial port API function of LUA script, more commonly used in free serial port protocols, detailed DEMO, refer toLUA tutorial 10-Custom Serial Port Commands.zip

1.8.1 uart_send_data(packet)

Sends data through the serial port

  • packet: Byte array, subscripts start from 0
function uart_doorState(state)
    local door_buff = {}    
    door_buff[0] = 0x5A
    door_buff[1] = 0x5A
    door_buff[2] = 0x07
    door_buff[3] = 0x82
    door_buff[4] = 0x00
    door_buff[5] = 0x01
    door_buff[6] = state

    uart_send_data(door_buff)
end
--用户通过触摸修改控件后,执行此回调函数。
--点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
    ......
    if screen == sc_door
    then 
        if control == 1
        then
            uart_doorState(value)
        end
    end
    ......
end

1.8.2 uart_set_timeout(timeout, timeout_inter)

Set UART receive timeout

  • timeout: total receive timeout
  • timeout_inter: byte interval timeout
常用于自由串口协议(uart_free_protocol = 1),eg:uart_set_timeout(0, 0)

1.8.3 uart_set_baudrate(baudrate)

  • baudrate: baud rate, supports 2400bps ~ 961200bps
设置波特率,如uart_set_baudrate(9600)

1.8.4 uart_get_baudrate()

Get baud rate, returns baud rate value

local baudrate = uart_get_baudrate()

1.8.5 on_uart_recv_data(packet)

UART receive data callback function, there are two ways to trigger this function:

☆ Use custom serial port commands: The format is EE B5 [custom data] FF FC FF FF

☆ Use free UART protocol: define global variable uart_free_protocol = 1 in LUA script

  • packet: Received byte data

1.8.6 uart_setup(baudrate,parity,stopbit,databits)

UART parameter setting

  • baudrate: Baud rate value
  • parity: 0-No parity, 1-Odd parity, 2-Even parity
  • stopbit: stopbit=0, 1 stop bit; stopbit=1, 1.5 stop bits
  • databits: Data bits 5~8

1.9 CAN Interface

1.9.1 canbus_open(index,baudrate,listen_mode,loop_back)

Open CANBUS Interface

  • index: Index number 0~1
  • baudrate: Baud rate (unit: K), optional 125, 250, 500, 1000
  • listen_mode: Read-only mode
  • loop_back: Loopback (self-test)

1.9.2 canbus_close(index)

Close CANBUS interface

  • index: Index number 0~1

1.9.3 canbus_write(index,identifier,dlc,rtr,ide,data)

Send CAN message

  • index: Index number 0~1
  • identifier: Message ID
  • dlc: Data length
  • rtr: Remote frame
  • ide: Extended frame
  • data: data, in table format

1.9.4 on_canbus_recv(index,identifier,dlc,rtr,ide,data)

CAN message callback function, automatically invoked by the system upon receiving a message

  • index: index number 0~1
  • identifier: message ID
  • dlc: data length
  • rtr: remote frame
  • ide: extended frame
  • data: data, in table format

1.10 Audio and Video

W series, F series, and M series all support audio playback, while W series and M series both support video playback, and F type does not support it

1.10.1 play_sound(filename)

Play the specified sound file, such as playing the on-screen audio file

  • filename: path
屏内路径播放:
W系列:play_sound(‘a:/sounds/welcome.wav’)
M、F系列:play_sound(‘3:/sounds/welcome.wav’)

1.10.2 stop_sound()

Stop playing

1.10.3 on_audio_callback (state)

Callback notification for sound playback end

  • state is reserved and unused.

1.10.4 set_volume(level)

Set volume

  • level: volume level 0~100. 0 is mute, 100 is maximum volume

1.10.5 get_volume()

Get volume, return value 0-100

1.10.6 play_video(pathname,repeat)

Play video, not applicable to M/F/X series

pathname is the video path

repeat is the number of repetitions, 0 for unlimited repetitions,

Play on-screen video: play_video('a:/Videos/1.mp4', 0)

1.10.7 pause_video()

Pause video playback, not supported by F series

1.10.8 resume_video()

Resume video playback, not supported by F series

1.10.9 stop_video()

Stop video playback, not supported by F series

1.10.10 play_video(file,left,top,width,height)

Play video, M/X series, F series/IoT type not supported

file: file path

left: starting coordinate x

top: starting coordinate y

width: width of video display

height: height of video display

1.10.11 on_video_notify(msg, v1, v2)

Video playback callback function

msg: 1-playing, 0-finished playing

v1: current playback progress, current played duration, in seconds

v2: total playback progress, total duration of current video, in seconds

1.10.12 set_av_channel(channel)

Switch AV channel. Not supported for M series, IoT type, and F series.

channel = 0, switch to channel 1.

channel = 1, switch to channel 2.

1.11 Read and write FLASH memory.

The screen provides 128K user FLASH, which can be used to store configuration parameters.

Firmware version requirements: W series >= 3.0.590.0, F series >= 4.1.401.0, M series >= V6.1.59.00.

1.11.1 write_flash(addr,data)

Write user FLASH data. addr is the write address, and data is a byte array with subscripts starting from 0.

1.11.2 read_flash(addr,length)

Read user FLASH data. addr is the write address, and length is the number of bytes to read. The return type is a byte array with subscripts starting from 0.

data = read_flash(addr,length)

1.11.3 write_flash_string(addr,str)

Write a string to the specified FLASH address

1.11.4 read_flash_string(addr)

Reads a string from the specified FLASH address. Returns the string on success, and nil on failure.

str = read_flash_string(addr)

1.11.5 flush_flash()

The system performs cache optimization for FLASH write operations to improve write efficiency.

The flush_flash operation immediately writes data to FLASH.

1.12 File System Operations

File system read and write interfaces, dedicated to the M series. Some functions are used for IoT purposes, and all unspecified parts are proprietary to the M series.

1.12.1 list_dir(path)

Traverses the files and folders under the specified directory. Returns true on success, and false on failure.

  • path: File path.
  • filename: File name.
  • type: 0-folder, 1-file
  • fsize: File size

Returns the contents of the folder through the following callback function

on_list_dir(path,filename,type,fsize)

1.12.2 file_open(path,mode)

Opens a file, returns true on success, false on failure

  • path - File path
  • mode - Open mode, as follows
FA_OPEN_EXISTINGFA_READFA_WRITEFA_CREATE_NEWFA_CREATE_ALWAYSFA_OPEN_ALWAYS
0x000x010x020x040x080x10

For example:

打开文件用于读取:file_open(path, 0x01)
创建文件用于写入:file_open(path, 0x02|0x08)

1.12.3 file_close()

Close the file. Returns true on success and false on failure

1.12.4 file_size()

Get the current file size and return the number of bytes

local file_fd = file_open(path, 0x01)
local filesize = file_size()

1.12.5 file_seek(offset)

Position the file reading position, return true if successful, false if failed

  • offset: file offset position

1.12.6 file_read(count)

Read file content, return table array if successful, nil if failed

  • count: number of bytes read, maximum 2048 bytes

1.12.7 file_write(data)

Write file content, return true if successful, false if failed

  • data: table array to be written, index starts from 0, maximum 2048 bytes written at once

1.12.8 file_delete(path)

  • path: file path to be deleted

1.12.9 file_copy(src_path, dst_path)

  • src_path: source file path
  • dst_path: destination file path

1.12.10 start_copy_file (from,to)

Note: This function is only used by IoT type!!!

File copy

  • from: source path
  • to: destination path

During the file copy process, the system automatically calls back the file copy progress notification

on_copy_file_process(status,filesize,transfersize)

1.12.11 on_copy_file_process(status,filesize,transfersize)

Note: This function is shared by IoT type/M series!!!

File copy progress, used in conjunction with file_copy(src_path, dst_path). When calling file_copy, the on_copy_file_process function is automatically called back

  • status: status.0-failed, 1-copying, 2-copied
  • filesize: size of the file being copied
  • transfersize: number of bytes currently being copied in the callback

1.13 Other

1.13.1 set_backlight(level)

Set backlight brightness

  • level: brightness level 0~100, 0 is the darkest, 100 is the brightest

1.13.2 get_backlight()

Get brightness percentage, return a value, total 100 levels

如:level = get_backlight()

1.13.3 set_language(lang)

Set current language option

  • lang: language index, starting from 0. If using English prompt keyboard, or set to 0x80
例如:设置语言1(索引0)
中文提示键盘:set_language(0)
英文提示键盘:set_language(0x80|0)

1.13.4 get_language()

Get current language option

lang: Returns the language index, starting from 0. If using English prompts for keyboard input, the language item or 0x80 will be obtained

例如:假设当前语言1(索引0)cur_lang = get_language()
中文提示键盘:cur_lang  = 0
英文提示键盘:cur_lang  = 0x80

1.13.5 set_wakeup_mode(mode)

Wakeup mode setting, can be combined settings, supported by W series, not supported by M series and F series

  • mode: 0x1 for single-click wakeup, 0x2 for double-click wakeup, 0x4 for serial port wakeup

1.13.6 sleepmode(on)

  • on: Equal to 1 to enter

Enter sleep low-power mode, after which screen functions will no longer operate and only touch can be used for wakeup

1.13.7 standbymode(on)

Enter standby low-power mode, the screen's serial port can operate normally. Supported by W series, not supported by M series and F series

1.13.8 beep(time)

Buzzer beeping duration

  • time: unit: milliseconds

1.13.9 get_tick_count()

Gets the runtime since power-on, in units of 10ms.

32-bit counter, overflows to zero after approximately 49 days and restarts timing.

1.13.10 get_date_time()

Gets the current date and time, returning 7 parameters

year,mon,day,hour,min,sec,week = get_date_time()
  • year: year
  • mon: month
  • day: day
  • hour: hour
  • min: minute
  • sec: second
  • week: week x, Sunday is 0

1.13.11 set_date_time (year, mon, day, hour, min, sec)

Set current date and time

  • year: year
  • mon: month
  • day: day
  • hour: hour
  • min: minute
  • sec: second
set_date_time(year,mon,day,hour,min,sec)

1.13.12 upgrade_logo (url)

Update boot LOGO via USB flash drive, supported by W series, not supported by F series and M series

  • url: image path
function on_usb_inserted(driver)
    upgrade_logo(driver..'logo.jpeg')
end

1.13.13 gpio_set_in (pin)

Set the PIN to input mode, supported by W series and M series, not supported by F series

1.13.14 gpio_set_out (pin)

Set the PIN to output mode, supported by W series and M series, not supported by F series

1.13.15 gpio_set_value (pin, value)

Set the output PIN to (high level 1/low level 0), supported by W series and M series, not supported by F series

1.13.16 gpio_get_value (pin)

Get the input PIN level (high level 1/low level 0), supported by W series and M series, not supported by F series

1.13.18 feed_dog ()

If an operation within the script takes more than 5 seconds, a dog feed is required

Not supported by W series, supported by F series and M series.

1.13.19 get_pixel(x,y)

Gets the RGB565 value of the corresponding coordinates

Supported by W series and M series, not supported by F series.

1.13.20 refresh_screen()

Refreshes the screen

1.13.21 get_version()

Gets the firmware version number and returns a string

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.