Lua Script

From Smart LCD
Revision as of 16:34, 24 February 2025 by Usr255 (talk | contribs) (hmt.crc16calc)

Smart LCD Lua Script Support

Lua script is supported in some of the TOPWAY Smart LCD to enhance the flexibility. Engineer may refine the Smart LCD functionality with script. The provided Lua functionality support most of the features of Lua 5.3 except OS operation. (please refer to lua.net for Lua 5.3 details)It also extended with hmt library for Smart LCD variables access, function control and value return etc.

Lua Script Structure

Basic structure

-- define variable(s)
Script_Variable01 = 0x00

-- main loop
luamain = function(void)

return 0
end

Note:

  • main loop run continually
  • intensive loop in the main loop may affect the Smart LCD UI operations

Advance structure with hooks

-- define variable(s)
Script_Variable01 = 0x00

-- main loop
luamain = function(void)

return 0
end

-- touch key hook
tpkhook = function(page, id, state)
return 0
end

-- page hook
pagechangehook = function(pageid)

return 0
end

Note:

  • main loop run continually
  • intensive loop in the main loop may affect the Smart LCD UI operations
  • hooks share the MCU time with the main loop.
  • looping or long delay will affect the main loop
  • “wait loop” is not allowed in the hooks.

Smart LCD VP variables

Mnemonic Name Memory Limit Address Range
VP_STR String Variable 1024(MAX)*(127+1)byte 0x000000~0x01FF80
VP_N16 16Bit Integer Variable 32512(MAX)*(2)byte 0x080000~0x08FDFE
VP_N32 32Bit Integer Variable 16128(MAX)*(4)byte 0x020000~0x02FEFC
VP_N64 64Bit Integer Variable 7936(MAX)*(8)byte 0x030000~0x03F7F8
VP_SYS System Register Variable 256(MAX)*(1)byte 0xFFFF00~0xFFFFFF

Note. Please also refer to Smart LCD handbook for VP variables details

hmt Functions Summary

Category Function Name Descriptions
VP

read/write

hmt.readvpstr(addr) Read VP_STR value
hmt.writevpstr(addr, string) Write VP_STR value
hmt.readvp16(addr) Read VP_16 value
hmt.writevp16(addr, value) Write VP_16 value
hmt.readvp32(addr) Read VP_32 value
hmt.readvp32f(addr) Read VP_32 value (floating point value)
hmt.writevp32(addr, value) Write VP_32 value
hmt.readvp64(addr) Read VP_64 value
hmt.readvp64f(addr) Read VP_64 value (floating point value)
hmt.writevp64(addr, value) Write VP_64 value
hmt.readvpreg(addr) Read VP_SYS value
hmt.writevpreg(addr, value) Write VP_SYS value
System hmt.changepage(pageid) Display a pre-stored PAGE
hmt.readpage() Read Page ID
hmt.gettick() Read system time
hmt.delayms(time) Delay in ms(*1)
hmt.runcmd(table, num) Execute terminal command (without packet header and taill)
hmt.bypass(is) Disable Smart LCD terminal command execution (I/O); is=1 for disable; is=0 for enable(normal)
hmt.crc16calc(table, num) Generate a CRC value
hmt.formatdisk2() Format module flash second partition (*4)
hmt.inituart(baudrate,parity) Set baud rate and parity parameters of A and B (*4)
Thread hmt.createthread(function()) Create a thread and return a thread object (*4)
suspend() Suspend thread (*4)
resume() Resume thread (*4)
terminate() Terminate thread (*4)
getid() Get thread ID and handle (*4)
UART0 hmt.uartisempty() Check for UART0 buffer; 1 for empty; 0 for not empty
hmt.uartclearbuf() Clear UART0 buffer
hmt.getchar() Read a byte form UART0 buffer
hmt.putchar(char) Send a byte to UART0
hmt.uartsendbytes(table, num) Send a num of byte to UART0
hmt.uartlock() Lock UART0 before Lua sending data to UART0 (*2, *3)
hmt.uartunlock() Unlock UART0 after Lua sending data to UART0 (*2, *3)
UART1 hmt.uart1isempty() Check for UART1 buffer; 1 for empty; 0 for not empty
hmt.uart1clearbuf() Clear UART1 buffer
hmt.uart1getchar() Read a byte form UART1 buffer
hmt.uart1putchar(char) Send a byte to UART1
hmt.uart1sendbytes(table, num) Send a num of byte to UART1
hmt.uart1lock() Lock UART1 before sending data to UART1 (*2, *3)
hmt.uart1unlock() Unlock UART1 after sending data to UART1 (*2, *3)

Note.
*1. long delay may affect the Smart LCD normal operation.
*2. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled.
*3. Generally hmt.uartlock() and hmt.uartunlock() should be used in pair.
*4. Selected model only

VP read/write Functions

hmt.readvpstr

Function hmt.readvpstr(addr)
Description Read VP_STR value
Paramete addr is the VP_STR address (*1)
Return Value String
Example
Lua Script Descriptions
local strvalue = ““
strvalue = hmt.readvpstr(0x000080)
print(“strvalue=”, strvalue)
  • Put “TOPWAY” into VP_STR 0x000080 via SmartLCD UI
  • define a local variable strvalue and put ““ inside
  • copy value in VP_STR at 0x000080 to strvalue
  • print to UART0
  • it should find the following at the UART0
  • strvalue=TOPWAY

hmt.writevpstr

Function hmt.writevpstr(addr, string)
Description Write a value to VP_STR
Paramete addr is the VP_STR address (*1), string is the value to be written
Return Value nil
Example
Lua Script Descriptions
local strvalue = “ShenZhen”
hmt.writevpstr(0x000080, strvalue)
print(“0x000080:”, hmt.readvpstr(0x000080))
  • define a local variable strvalue and put “ShenZhen” inside
  • copy the strvalue to VP_STR at 0x000080
  • print to UART0
  • it should find the following at the UART0
  • 0x000080:ShenZhen

hmt.readvp16

Function hmt.readvp16(addr)
Description Read VP_N16 value
Paramete addr is the VP_N16 address (*1)
Return Value Integer
Example
Lua Script Descriptions
local N16value = 0
N16value = hmt.readvp16(0x080002)
print(“N16value:”, N16value)
  • Put 24 into VP_N16 0x080002 via SmartLCD UI
  • define a local variable N16value and put 0 inside
  • copy value in VP_N16 at 0x080002 to N16value
  • print to UART0
  • it should find the following at the UART0
  • N16value:24

hmt.writevp16

Function hmt.writevp16(addr, value)
Description Write a value to VP_N16
Paramete addr is the VP_N16 address(*1), value is the value to be written
Return Value nil
Example
Lua Script Descriptions
local N16value = 123
hmt.writevp16(0x080002, N16value)
print(“0x080002:”, hmt.readvp16(0x080002))
  • define a local variable N16value and put 123 inside
  • copy the N16value to VP_N16 at 0x080002
  • print to UART0
  • it should find the following at the UART0
  • 0x080002:123

hmt.readvp32

Function hmt.readvp32(addr)
Description Read VP_N32 value
Paramete addr is the VP_N32 address (*1)
Return Value Integer
Example
Lua Script Descriptions
local N32value = 0
N32value = hmt.readvp32(0x020004)
print(“N32value:”, N32value)
  • Put 1108410368 into VP_N32 0x020004 via SmartLCD UI
  • define a local variable N32value and put 0 inside
  • copy value in VP_N32 at 0x020004 to N32value
  • print to UART0
  • it should find the following at the UART0
  • N32value:1108410368

hmt.readvp32f

Function hmt.readvp32f(addr)
Description Read VP_N32 value with floating value inside
Paramete addr is the VP_N32 address (*1)
Return Value Floating point
Example
Lua Script Descriptions
local N32value = 0.0
N32value = hmt.readvp32f(0x020004)
print(“N32value:”, N32value)
  • Put a floating point value 36.25 into VP_N32 0x020004 via SmartLCD UI
  • define a local variable N32value and put 0.0 inside
  • copy floating point value in VP_N32 at 0x020004 to N32value
  • print to UART0
  • it should find the following at the UART0
  • N32value:36.25

hmt.writevp32

Function hmt.writevp32(addr, value)
Description Write a value to VP_N32
Paramete addr is the VP_N32 address(*1), value is the value to be written
Return Value nil
Example
Lua Script Descriptions
local N32value = 54321
hmt.writevp32(0x080004, N32value)
print(“0x080004:”, hmt.readvp32(0x080004))
  • define a local variable N32value and put 54321 inside
  • copy the N32value to VP_N32 at 0x080004
  • print to UART0
  • it should find the following at the UART0
  • 0x080004:54321

hmt.readvp64

Function hmt.readvp64(addr)
Description Read VP_N64 value
Paramete addr is the VP_N64 address (*1)
Return Value Integer
Example
Lua Script Descriptions
local N64value = 0
N32value = hmt.readvp64(0x030000)
print(“N64value:”, N64value)
  • Put 4638029965185179976 into VP_N64 0x030000 via SmartLCD UI\
  • define a local variable N64value and put 0 inside
  • copy value in VP_N64 at 0x030000 to N64value
  • print to UART0
  • it should find the following at the UART0
  • N64value: 4638029965185179976

hmt.readvp64f

Function hmt.readvp64f(addr)
Description Read VP_N64 value with floating value inside
Paramete addr is the VP_N64 address (*1)
Return Value Floating point
Example
Lua Script Descriptions
local N64value = 0.0
N64value = hmt.readvp64f(0x030000)
print(“N64value:”, N64value)
  • Put a floating point value 118.37 into VP_N64 0x030000 via SmartLCD UI
  • define a local variable N64value and put 0.0 inside
  • copy floating point value in VP_N64 at 0x030000 to N64value
  • print to UART0
  • it should find the following at the UART0
  • N64value:118.37

hmt.writevp64

Function hmt.writevp64(addr, value)
Description Write a value to VP_N64
Paramete addr is the VP_N64 address(*1), value is the value to be written
Return Value nil
Example
Lua Script Descriptions
local N64value = 123456789
hmt.writevp64(0x030000, N64value)
print(“0x030000:”, hmt.readvp64(0x030000))
  • define a local variable N64value and put 123456789 inside
  • copy the N64value to VP_N64 at 0x030000
  • print to UART0
  • it should find the following at the UART0
  • 0x030000:123456789

hmt.readvpreg

Function hmt.readvpreg(addr)
Description Read Smart LCD system register value (VP_SYS) value
Paramete ddr is the VP_SYS address (*1)
Return Value Integer
Example
Lua Script Descriptions
local Regvalue = 0
Regvalue = hmt.readvpreg(0xFFFF20)
print(“Regvalue:”, Regvalue)
  • define a local variable Regvalue and put 0 inside
  • copy value in VP_SYS at 0xFFFF20 to Regvalue
  • print to UART0
  • it should find the following at the UART0
  • Regvalue:8

hmt.writevpreg

Function hmt.writevpreg(addr, value)
Description Write a value to Smart LCD system register value (VP_SYS) value
Paramete addr is the VP_SYS address(*1), value is the value to be written
Return Value nil
Example
Lua Script Descriptions
local Regvalue = 18
hmt.writevpreg(0xFFFF20, Regvalue)
print(“0xFFFF20:”, hmt.readvpreg(0xFFFF20))
  • define a local variable Regvalue and put 18 inside
  • copy the Regvalue to VP_SYS at 0xFFFF20
  • print to UART0
  • it should find the following at the UART0
  • 0xFFFF20:18

Note: *1. Please refer to Smart LCD VP variables section.

System Functions

hmt.changepage

Function hmt.changepage(pageid)
Description Change the Smart LCD UI display PAGE
Paramete pageid is the target display PAGE to be shown
Return Value Nil
Example
Lua Script Descriptions
hmt.changepage(0x02)
  • UI show up the PAGE 2

hmt.readpage

Function hmt.readpage()
Description Read the current display page id
Paramete Nil
Return Value Integer
Example
Lua Script Descriptions
local pageid = 0
pageid = hmt.readpage()
print(“pageid:”, pageid)
  • SmartLCD UI is showing PAGE 3
  • define a local variable pageid and put 0 inside
  • copy the current display page id to pageid
  • print to UART0
  • it should find the following at the UART0
  • pageid:3

hmt.gettick

Function hmt.gettick()
Description Read the system running time in ms
Paramete Nil
Return Value Integer
Example
Lua Script Descriptions
local worktime = 0
worktime = hmt.gettick()
print(“worktime:”, worktime)
  • define a local variable worktime and put 0 inside
  • copy the current system running time to worktime
  • print to UART0
  • it should find the following at the UART0
  • worktime:334520  (The system ran for about 334520ms)

Note. Return Value will loop back to 0(dec) after 4294966(dec)

hmt.readpage

Function hmt.readpage()
Description Read the current display page id
Paramete Nil
Return Value Integer
Example
Lua Script Descriptions
local pageid = 0
pageid = hmt.readpage()
print(“pageid:”, pageid)
  • SmartLCD UI is showing PAGE 3
  • define a local variable pageid and put 0 inside
  • copy the current display page id to pageid
  • print to UART0
  • it should find the following at the UART0
  • pageid:3

hmt.delayms

Function hmt.delayms(time)
Description delay routine
Paramete time is the delay in ms
Return Value Nil
Example
Lua Script Descriptions
hmt.delaymy(200)
  • system will do nothing for 200ms and jump and go on

Note. long delay may affect the Smart LCD normal operation.

hmt.runcmd

Function hmt.runcmd(table, num)
Description Send a command directly as sending command to the Smart LCD terminal (no command head and command tail)
Paramete table is the command byte array to be send, num is the number of byte to be send
Return Value Integer
Example
Lua Script Descriptions
local cmdtab = {0x5F, 0x04}
hmt.runcmd(cmdtab, 2)
  • define a local variable array and put 0x5F, 0x04 inside
  • use hmt.runcmd to send 2 byte of local variable array to Smart LCD
  • it will adjust the backlight of the display to dim down

Note. 0x5F is backlight control command; please also refer to Smart LCD handbook for command details

hmt.bypass

Function hmt.bypass(is)
Description Disable Smart LCD terminal command execution (I/O), And prevent data stream corruption while Lua accessing the UART0
Paramete is=1 for disable; is=0 for enable(normal)
Return Value Nil
Example
Lua Script Descriptions
hmt.bypass(1)

hmt.bypass(0)

  • Disable Smart LCD terminal command execution
  • Send AA 5F 03 CC 33 C3 3C to Smart LCD terminal to dim down the backlight
  • No response
  • Enable Smart LCD terminal command execution
  • Send AA 5F 03 CC 33 C3 3C to Smart LCD terminal to dim down the backlight
  • Display dim down accordingly

Note. 0x5F is backlight control command; please also refer to Smart LCD handbook for command details

hmt.crc16calc

Function hmt.crc16calc(table, num)
Description Generate a CRC value form given num of bytes
Paramete table is the byte array to be input, num is the number of byte to be taken form the table (see appendix for reference)
Return Value Integer
Example
Lua Script Descriptions
local padcmd = {0x82, 0x00, 0x08, 0x05}
local crcreturn = 0
crcreturn = hmt.crc16calc(padcmd, 4)
print(“crcreturn:”, crcreturn)
  • define a local variable array and put 0x82, 0x00, 0x08, 0x05 inside
  • define a local variable and put 0 inside
  • calculate a CRC value of the 4 bytes form the array
  • print to UART0
  • it should find the following at the UART0
  • crcreturn:6134316

hmt.formatdisk2

Function hmt.formatdisk2()
Description Format Smart LCD 2nd FLASH partition ("disk2" is the 2nd partition for Lua File I/O)
Paramete Nil
Return Value Nil
Example
Lua Script Descriptions
hmt.formatdisk2()
  • Format Smart LCD 2nd FLASH partition.
  • All the files in this partition will be eased.

hmt.inituart

Function hmt.inituart(baudrate,parity)
Description Initialize baud rate and parity parameters of UART0 and UART1
Paramete Baudrate: 0=1200bps; 1=2400bps; 2=4800bps; 3=9600bps; 4=19200bps; 5=38400bps; 6=57600bps; 7=115200bps

Parity: 0=none; 1=odd; 2=even

Return Value Nil
Example
Lua Script Descriptions
hmt.inituart(7,0)
  • Set the baud rate of UART0 and UART1 to 115200, 8N1