Lua Script: Difference between revisions
| Line 676: | Line 676: | ||
!Descriptions | !Descriptions | ||
|- | |- | ||
|local worktime = 0 | |local worktime = 0<br>worktime = hmt.gettick()<br>print(“worktime:”, worktime) | ||
worktime = hmt.gettick() | |||
print(“worktime:”, worktime) | |||
| | | | ||
* define a local variable worktime and put 0 inside | * define a local variable worktime and put 0 inside | ||
Revision as of 16:15, 24 February 2025
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) |
|
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)) |
|
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) |
|
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)) |
|
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) |
|
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) |
|
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)) |
|
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) |
|
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) |
|
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)) |
|
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) |
|
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)) |
|
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) |
|
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) |
|
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) |
|
Note. Return Value will loop back to 0(dec) after 4294966(dec)