Lua Script: Difference between revisions
m →tpkhook |
|||
| (17 intermediate revisions by the same user not shown) | |||
| Line 13: | Line 13: | ||
return 0 | return 0 | ||
end | end | ||
Note: | Note:<BR>main loop run continually<BR>intensive loop in the main loop may affect the Smart LCD UI operations | ||
==== Advance structure with hooks ==== | ==== Advance structure with hooks ==== | ||
| Line 82: | Line 79: | ||
<span id="hmt-functions-summary"></span> | <span id="hmt-functions-summary"></span> | ||
== hmt Lua Functions Summary == | == hmt Lua Functions Summary == | ||
{| class="wikitable" | {| class="wikitable" | ||
|+ | |+ | ||
| Line 345: | Line 341: | ||
* '''0x080002:123''' | * '''0x080002:123''' | ||
|} | |} | ||
=== hmt.readvp32 === | === hmt.readvp32 === | ||
{| | {| | ||
| Line 408: | Line 405: | ||
* '''N32value:36.25''' | * '''N32value:36.25''' | ||
|} | |} | ||
=== hmt.writevp32 === | === hmt.writevp32 === | ||
{| | {| | ||
| Line 438: | Line 436: | ||
* '''0x080004:54321''' | * '''0x080004:54321''' | ||
|} | |} | ||
=== hmt.readvp64 === | === hmt.readvp64 === | ||
{| | {| | ||
| Line 532: | Line 531: | ||
* '''0x030000:123456789''' | * '''0x030000:123456789''' | ||
|} | |} | ||
=== hmt.readvpreg === | === hmt.readvpreg === | ||
{| | {| | ||
| Line 1,027: | Line 1,027: | ||
|} | |} | ||
== thread_object:getid == | === thread_object:getid === | ||
{| | {| | ||
|'''Function''' | |'''Function''' | ||
| Line 1,056: | Line 1,056: | ||
* get its id and handle value | * get its id and handle value | ||
|} | |} | ||
== UART0 functions == | |||
UART0 is the default command terminal of the Smart LCD. It may be RS-232C or UART(3.3V) depends on model. Lua print function also using UART0 for default output. Some of the Smart LCD equipped with a second terminal UART1. It may be RS-232C or UART(3.3V) depends on model. | |||
=== hmt.uartisempty === | |||
{| | |||
|'''Function''' | |||
|hmt.uartisempty() | |||
|- | |||
|'''Description''' | |||
|Check the UART0 buffer status | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Integer; Return 1 for buffer empty; 0 for data in buffer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local Uart0state = 0<BR>Uart0state = hmt.uartisempty()<BR>print(“Uart0state:”, Uart0state) | |||
| | |||
* define a local variable Uart0state and put 0 inside | |||
* copy the current UART is empty result to Uart0state | |||
* print to UART0 | |||
* it should find the following at the UART0 '''Uart0state:1''' (as there is no data received form UART0) | |||
|} | |||
=== hmt.uartclearbuf === | |||
{| | |||
|'''Function''' | |||
|hmt.uartclearbuf() | |||
|- | |||
|'''Description''' | |||
|Clear the UART0 buffer | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uartclearbuf()<BR>print(“Uart0state:”, hmt.uartisempty()) | |||
| | |||
* use command to clear the UART0 buffer | |||
* print to UART0 | |||
* it should find the following at the UART0 '''Uart0state:1''' (as there is no data send to the UART0) | |||
|} | |||
=== hmt.getchar === | |||
{| | |||
|'''Function''' | |||
|hmt.getchar() | |||
|- | |||
|'''Description''' | |||
|Receive one byte of data form UART0 buffer | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Integer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local getdata = 0<BR>getdata = hmt.getchar()<BR>print(“getdata:”, getdata) | |||
| | |||
* Send one byte of data 0x99 to the Smart LCD terminal | |||
* define a local variable getdata and put 0 inside | |||
* get one byte of data form the UART0 buffer and put it into getdata | |||
* print to UART0 | |||
* it should find the following at the UART0 '''getdata:153''' (0x99 is 153(d)) | |||
|} | |||
=== hmt.putchar === | |||
{| | |||
|'''Function''' | |||
|hmt.putchar(char) | |||
|- | |||
|'''Description''' | |||
|Send one byte of data to UART0 | |||
|- | |||
|'''Parameter''' | |||
|char is the byte of data to be sent (it should be a byte value, not string char) | |||
|- | |||
|'''Return Value''' | |||
|Integer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.putchar(0x41)<BR>hmt.putchar(66) | |||
| | |||
* it should find the following at the UART0 '''0x41 0x42''' (in hex) or '''AB''' (in ASCII) | |||
|} | |||
=== hmt.uartsendbytes === | |||
{| | |||
|'''Function''' | |||
|hmt.uartsendbytes(table, num) | |||
|- | |||
|'''Description''' | |||
|Send bytes of data to UART0 | |||
|- | |||
|'''Parameter''' | |||
|table is the data byte array to be send, num is the number of byte to be send | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local sendbuff = {0xab, 0xcd, 0xef}<BR>hmt.uartsendbytes(sendbuff, 3) | |||
| | |||
* define a local variable array and put 0xab, 0xcd, 0xef inside | |||
* use hmt.uartsendbytes to send 3 byte of local variable array to UART0 | |||
* it should find the following at the UART0 '''0xab 0xcd 0xef''' (in hex) | |||
|} | |||
=== hmt.uartlock === | |||
{| | |||
|'''Function''' | |||
|hmt.uartlock() | |||
|- | |||
|'''Description''' | |||
|Lock the UART0 before Lua send data to UART0 | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uartlock()<BR>hmt.putchar(0xaa)<BR>hmt.putchar(0x02)<BR>hmt.putchar(0x31)<BR>hmt.uartunlock() | |||
| | |||
* Lock the UART0 for Lua use | |||
* Send 0xaa to UART0 | |||
* Send 0x02 to UART0 | |||
* Send 0x31 to UART0 | |||
* it should find the following at the UART0 '''0xaa 0x02 0x31''' (in hex) | |||
* Release the UART0 for Smart LCD normal operation | |||
|} | |||
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uartlock() and hmt.uartunlock() should be used in pair. | |||
=== hmt.uartunlock === | |||
{| | |||
|'''Function''' | |||
|hmt.uartunlock() | |||
|- | |||
|'''Description''' | |||
|unlock the UART0 after Lua send data to UART0 | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uartlock()<BR>hmt.putchar(0xaa)<BR>hmt.putchar(0x02)<BR>hmt.putchar(0x31)<BR>hmt.uartunlock() | |||
| | |||
*Lock the UART0 for Lua use | |||
*Send 0xaa to UART0 | |||
* Send 0x02 to UART0 | |||
* Send 0x31 to UART0 | |||
* it should find the following at the UART0 '''0xaa 0x02 0x31''' (in hex) | |||
* Release the UART0 for Smart LCD normal operation | |||
|} | |||
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uartlock() and hmt.uartunlock() should be used in pair. | |||
== UART1 functions == | |||
UART1 is the extend terminal of some of the Smart LCD. It may be RS-232C or UART(3.3V) depends on model. | |||
=== hmt.uart1isempty === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1isempty() | |||
|- | |||
|'''Description''' | |||
|Check the UART1 buffer status | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Integer; Return 1 for buffer empty; 0 for data in buffer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local Uart1state = 0<BR>Uart1state = hmt.uart1isempty()<BR>print(“Uart1state:”, Uart1state) | |||
| | |||
* define a local variable Uart1state and put 0 inside | |||
* copy the current UART is empty result to Uart1state | |||
* print to UART0 | |||
* it should find the following at the UART0 '''Uart1state:1''' (as there is no data received form UART1) | |||
|} | |||
=== hmt.uart1clearbuf === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1clearbuf() | |||
|- | |||
|'''Description''' | |||
|Clear the UART1 buffer | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uart1clearbuf()<BR>print(“Uart1state:”, hmt.uart1isempty()) | |||
| | |||
* use command to clear the UART1 buffer | |||
* print to UART0 | |||
* it should find the following at the UART0 '''Uart1state:1''' (as there is no data send to the UART1) | |||
|} | |||
=== hmt.uart1getchar === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1getchar() | |||
|- | |||
|'''Description''' | |||
|Receive one byte of data form UART1 buffer | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Integer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local getdata = 0<BR>getdata = hmt.uart1getchar()<BR>print(“getdata:”, getdata) | |||
| | |||
* Send one byte of data 0xaa to the Smart LCD terminal | |||
* define a local variable getdata and put 0 inside | |||
* get one byte of data form the UART1 buffer and put it into getdata | |||
* print to UART0 | |||
* it should find the following at the UART0 '''getdata:170''' (0xaa is 170(d)) | |||
|} | |||
=== hmt.uart1putchar === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1putchar(char) | |||
|- | |||
|'''Description''' | |||
|Send one byte of data to UART1 | |||
|- | |||
|'''Parameter''' | |||
|char is the byte of data to be sent (it should be a byte value, not string char) | |||
|- | |||
|'''Return Value''' | |||
|Integer | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uart1putchar(0x61)<BR>hmt.uart1putchar(97) | |||
| | |||
* it should find the following at the UART1 '''0x61 0x62''' (in hex) or '''ab''' (in ASCII) | |||
|} | |||
=== hmt.uart1sendbytes === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1sendbytes(table, num) | |||
|- | |||
|'''Description''' | |||
|Send bytes of data to UART1 | |||
|- | |||
|'''Parameter''' | |||
|table is the data byte array to be send, num is the number of byte to be send | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|local sendbuff = {0xab, 0xcd, 0xef}<BR>hmt.uart1sendbytes(sendbuff, 3) | |||
| | |||
* define a local variable array and put 0xab, 0xcd, 0xef inside | |||
* use hmt.uart1sendbytes to send 3 byte of local variable array to UART1 | |||
* it should find the following at the UART1 '''0xab 0xcd 0xef''' (in hex) | |||
|} | |||
=== hmt.uart1lock === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1lock() | |||
|- | |||
|'''Description''' | |||
|Lock the UART1 before Lua send data to UART1 | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uart1lock()<BR>hmt.uart1putchar(0xaa)<BR>hmt.uart1putchar(0x02)<BR>hmt.uart1putchar(0x31)<BR>hmt.uart1unlock() | |||
| | |||
* Lock the UART1 for Lua use | |||
* Send 0xaa to UART1 | |||
* Send 0x02 to UART1 | |||
* Send 0x31 to UART1 | |||
* it should find the following at the UART1 '''0xaa 0x02 0x31''' (in hex) | |||
* Release the UART1 for Smart LCD normal operation | |||
|} | |||
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uart1lock() and hmt.uart1unlock() should be used in pair. | |||
=== hmt.uart1unlock === | |||
{| | |||
|'''Function''' | |||
|hmt.uart1unlock() | |||
|- | |||
|'''Description''' | |||
|unlock the UART1 after Lua send data to UART1 | |||
|- | |||
|'''Parameter''' | |||
|Nil | |||
|- | |||
|'''Return Value''' | |||
|Nil | |||
|- | |||
|'''Example''' | |||
| | |||
|} | |||
{| class="wikitable" | |||
|+ | |||
!Lua Script | |||
!Descriptions | |||
|- | |||
|hmt.uart1lock()<BR>hmt.uart1putchar(0xaa)<BR>hmt.uart1putchar(0x02)<BR>hmt.uart1putchar(0x31)<BR>hmt.uart1unlock() | |||
| | |||
*Lock the UART1 for Lua use | |||
*Send 0xaa to UART1 | |||
* Send 0x02 to UART1 | |||
* Send 0x31 to UART1 | |||
* it should find the following at the UART1 '''0xaa 0x02 0x31''' (in hex) | |||
* Release the UART1 for Smart LCD normal operation | |||
|} | |||
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uart1lock() and hmt.uart1unlock() should be used in pair. | |||
== Main Loop and Hooks == | == Main Loop and Hooks == | ||
| Line 1,112: | Line 1,542: | ||
!Descriptions | !Descriptions | ||
|- | |- | ||
|tpkhook = function(page, id, state)<BR>print(“PageID=”, | |tpkhook = function(page, id, state)<BR>print(“PageID=”, page,“ TPKID=”, id,“ state=”, state)<BR>return 0<br>end | ||
| | | | ||
* On page 0 touch the touch key (ID=2) | * On page 0 touch the touch key (ID=2) | ||
| Line 1,152: | Line 1,582: | ||
|} | |} | ||
== Example | == Example == | ||
=== Built an alarm clock function with Lua === | |||
set up a default alarm (alarmhour, alarmminute, alarmsecond) and alarm duration (alarmend)<br>continue checking the Smart LCD compare the RTC with the alarm; if match then sound the buzzer on the Smart LCD.<br>set up a touch key monitor, when it is touched, assign the alarm with value store in the smart LCD (those values, can be assign with the user interface) | set up a default alarm (alarmhour, alarmminute, alarmsecond) and alarm duration (alarmend)<br>continue checking the Smart LCD compare the RTC with the alarm; if match then sound the buzzer on the Smart LCD.<br>set up a touch key monitor, when it is touched, assign the alarm with value store in the smart LCD (those values, can be assign with the user interface) | ||
| Line 1,195: | Line 1,626: | ||
Note: “wait loop” is not allowed in the script! | Note: “wait loop” is not allowed in the script! | ||
== Reference | == Reference == | ||
=== hmt.crc16calc(table, num) === | |||
uint16_t const CRC16[256]={ | uint16_t const CRC16[256]={ | ||
/* 16: 8005 reflected */ | /* 16: 8005 reflected */ | ||
Latest revision as of 12:17, 28 September 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 Lua 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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)) |
|
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 |
| Parameter | 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 |
| Parameter | 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 |
| Parameter | 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)
hmt.readpage
| Function | hmt.readpage() |
| Description | Read the current display page id |
| Parameter | Nil |
| Return Value | Integer |
| Example |
| Lua Script | Descriptions |
|---|---|
| local pageid = 0 pageid = hmt.readpage() print(“pageid:”, pageid) |
|
hmt.delayms
| Function | hmt.delayms(time) |
| Description | delay routine |
| Parameter | time is the delay in ms |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.delaymy(200) |
|
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) |
| Parameter | 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) |
|
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 |
| Parameter | is=1 for disable; is=0 for enable(normal) |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.bypass(1)
hmt.bypass(0) |
|
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 |
| Parameter | 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) |
|
hmt.formatdisk2
| Function | hmt.formatdisk2() |
| Description | Format Smart LCD 2nd FLASH partition ("disk2" is the 2nd partition for Lua File I/O) |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.formatdisk2() |
|
hmt.inituart
| Function | hmt.inituart(baudrate,parity) |
| Description | Initialize baud rate and parity parameters of UART0 and UART1 |
| Parameter | 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) |
|
Thread functions
Other than Lua main loop, maximum two threads can be executed at the same time for multi-tasking purpose in TOPWAY Smart LCD Lua script environment. Threads can be control easily with suspend, resume and terminate command. It is recommended to add a delay inside the thread to avoid allocating the CPU continuously, which may affect the display performance.
hmt.createthread
| Function | hmt.createthread(function()) |
| Description | Create a thread and return a thread object |
| Parameter | a callback function |
| Return Value | If successful, return a thread object; If failed, return nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| FUN_PRINT_THREAD = function(arg) while(1) do print(arg) hmt.delayms(3000) end end Thread_object = hmt.createthread(FUN_PRINT_THREAD, “TOPWAY”) |
|
Note. Maximum two threads can be executed in lua_main
thread_object:suspend
| Function | thread_object:suspend() |
| Description | Suspend thread(pause thread execution); this function can only be called after a thread was created |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| FUN_PRINT_THREAD = function() while(1) do print(“THREAD IS RUNNING”) hmt.delayms(3000) end end Thread_object = hmt.createthread(FUN_PRINT_THREAD) Thread_object:suspend() |
|
thread_object:resume
| Function | thread_object:resume() |
| Description | Restart thread ; this function can only be called after a thread was created |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| FUN_PRINT_THREAD = function() while(1) do Thread_object:suspend() print(“THREAD IS RUNNING”) hmt.delayms(3000) end end Thread_object = hmt.createthread(FUN_PRINT_THREAD) Thread_object:resume() |
|
thread_object:terminate
| Function | thread_object:terminate() |
| Description | Terminate thread, terminate thread execution (this function can only be called after creating a thread) |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| FUN_PRINT_THREAD = function() while(1) do print(“THREAD IS RUNNING”) hmt.delayms(3000) end end Thread_object = hmt.createthread(FUN_PRINT_THREAD) local value = instance_thread:terminate() Thread_object = nil |
|
thread_object:getid
| Function | thread_object:getid() |
| Description | Get thread ID and handle (this function can only be called after creating a thread) |
| Parameter | Nil |
| Return Value | Return callback function id and callback handle value. If the handle value is 0, the thread execution is completed |
| Example |
| Lua Script | Descriptions |
|---|---|
| FUN_PRINT_THREAD = function() while(1) do print(“THREAD IS RUNNING”) hmt.delayms(3000) end end Thread_object = hmt.createthread(FUN_PRINT_THREAD) local id, handle = Thread_object:getid() print(“id=”,id) print(“handle=”,handle) |
|
UART0 functions
UART0 is the default command terminal of the Smart LCD. It may be RS-232C or UART(3.3V) depends on model. Lua print function also using UART0 for default output. Some of the Smart LCD equipped with a second terminal UART1. It may be RS-232C or UART(3.3V) depends on model.
hmt.uartisempty
| Function | hmt.uartisempty() |
| Description | Check the UART0 buffer status |
| Parameter | Nil |
| Return Value | Integer; Return 1 for buffer empty; 0 for data in buffer |
| Example |
| Lua Script | Descriptions |
|---|---|
| local Uart0state = 0 Uart0state = hmt.uartisempty() print(“Uart0state:”, Uart0state) |
|
hmt.uartclearbuf
| Function | hmt.uartclearbuf() |
| Description | Clear the UART0 buffer |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uartclearbuf() print(“Uart0state:”, hmt.uartisempty()) |
|
hmt.getchar
| Function | hmt.getchar() |
| Description | Receive one byte of data form UART0 buffer |
| Parameter | Nil |
| Return Value | Integer |
| Example |
| Lua Script | Descriptions |
|---|---|
| local getdata = 0 getdata = hmt.getchar() print(“getdata:”, getdata) |
|
hmt.putchar
| Function | hmt.putchar(char) |
| Description | Send one byte of data to UART0 |
| Parameter | char is the byte of data to be sent (it should be a byte value, not string char) |
| Return Value | Integer |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.putchar(0x41) hmt.putchar(66) |
|
hmt.uartsendbytes
| Function | hmt.uartsendbytes(table, num) |
| Description | Send bytes of data to UART0 |
| Parameter | table is the data byte array to be send, num is the number of byte to be send |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| local sendbuff = {0xab, 0xcd, 0xef} hmt.uartsendbytes(sendbuff, 3) |
|
hmt.uartlock
| Function | hmt.uartlock() |
| Description | Lock the UART0 before Lua send data to UART0 |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uartlock() hmt.putchar(0xaa) hmt.putchar(0x02) hmt.putchar(0x31) hmt.uartunlock() |
|
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uartlock() and hmt.uartunlock() should be used in pair.
hmt.uartunlock
| Function | hmt.uartunlock() |
| Description | unlock the UART0 after Lua send data to UART0 |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uartlock() hmt.putchar(0xaa) hmt.putchar(0x02) hmt.putchar(0x31) hmt.uartunlock() |
|
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uartlock() and hmt.uartunlock() should be used in pair.
UART1 functions
UART1 is the extend terminal of some of the Smart LCD. It may be RS-232C or UART(3.3V) depends on model.
hmt.uart1isempty
| Function | hmt.uart1isempty() |
| Description | Check the UART1 buffer status |
| Parameter | Nil |
| Return Value | Integer; Return 1 for buffer empty; 0 for data in buffer |
| Example |
| Lua Script | Descriptions |
|---|---|
| local Uart1state = 0 Uart1state = hmt.uart1isempty() print(“Uart1state:”, Uart1state) |
|
hmt.uart1clearbuf
| Function | hmt.uart1clearbuf() |
| Description | Clear the UART1 buffer |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uart1clearbuf() print(“Uart1state:”, hmt.uart1isempty()) |
|
hmt.uart1getchar
| Function | hmt.uart1getchar() |
| Description | Receive one byte of data form UART1 buffer |
| Parameter | Nil |
| Return Value | Integer |
| Example |
| Lua Script | Descriptions |
|---|---|
| local getdata = 0 getdata = hmt.uart1getchar() print(“getdata:”, getdata) |
|
hmt.uart1putchar
| Function | hmt.uart1putchar(char) |
| Description | Send one byte of data to UART1 |
| Parameter | char is the byte of data to be sent (it should be a byte value, not string char) |
| Return Value | Integer |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uart1putchar(0x61) hmt.uart1putchar(97) |
|
hmt.uart1sendbytes
| Function | hmt.uart1sendbytes(table, num) |
| Description | Send bytes of data to UART1 |
| Parameter | table is the data byte array to be send, num is the number of byte to be send |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| local sendbuff = {0xab, 0xcd, 0xef} hmt.uart1sendbytes(sendbuff, 3) |
|
hmt.uart1lock
| Function | hmt.uart1lock() |
| Description | Lock the UART1 before Lua send data to UART1 |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uart1lock() hmt.uart1putchar(0xaa) hmt.uart1putchar(0x02) hmt.uart1putchar(0x31) hmt.uart1unlock() |
|
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uart1lock() and hmt.uart1unlock() should be used in pair.
hmt.uart1unlock
| Function | hmt.uart1unlock() |
| Description | unlock the UART1 after Lua send data to UART1 |
| Parameter | Nil |
| Return Value | Nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| hmt.uart1lock() hmt.uart1putchar(0xaa) hmt.uart1putchar(0x02) hmt.uart1putchar(0x31) hmt.uart1unlock() |
|
Note. It is necessary to lock the UART for Lua access, if hmt.bypass is not enabled. Generally hmt.uart1lock() and hmt.uart1unlock() should be used in pair.
Main Loop and Hooks
luamain
| Function | luamain = function(void) |
| Description | A loop routine run continuously. Looping cycle 10ms(MIN) |
| Parameter | Nil |
| Return Value | nil |
| Example |
| Lua Script | Descriptions |
|---|---|
| luamain = function(void) print(“TOPWAY”) return 0 end |
|
tpkhook
| Function | tpkhook = function(page, id, state) |
| Description | A hook routine that will be trigged while any of the touch key status that defined in the Smart LCD UI is being changed |
| Parameter | page is the touched touch key’s PAGE; id is the touch key’s ID; state is the statues of the touch key (0 for released; 1 for touched; 2 for moved out) |
| Return Value | return 0 tell Smart LCD UI continue to execute the touch key predefined operations; return 1 tell Smart LCD UI not to execute the touch key predefined operations |
| Example |
| Lua Script | Descriptions |
|---|---|
| tpkhook = function(page, id, state) print(“PageID=”, page,“ TPKID=”, id,“ state=”, state) return 0 end |
|
pagechangehook
| Function | pagechangehook = function(pageid) |
| Description | A hook routine that will be trigged when page changed (before showing the page) |
| Parameter | pageid is the target PAGE ID |
| Return Value | return 0 tell Smart LCD UI continue to showing up the target page; return 1 tell Smart LCD UI staying at the previous page |
| Example |
| Lua Script | Descriptions |
|---|---|
| pagechangehook = function(pageid) print(“PageID=”, pageid) return 0 end |
|
Example
Built an alarm clock function with Lua
set up a default alarm (alarmhour, alarmminute, alarmsecond) and alarm duration (alarmend)
continue checking the Smart LCD compare the RTC with the alarm; if match then sound the buzzer on the Smart LCD.
set up a touch key monitor, when it is touched, assign the alarm with value store in the smart LCD (those values, can be assign with the user interface)
-- define variable(s)
Alarmhour = 0x7
alarmminute = 0x14
alarmsecond = 0x0
alarmend = 0
-- main loop
luamain = function(void)
if((hmt.readvpreg(0xffff13) == alarmhour) and (hmt.readvpreg(0xffff14) == alarmminute) and (hmt.readvpreg(0xffff15) == alarmsecond))
then
alarmend = hmt.gettick() + 60000
end
if(alarmend > hmt.gettick())then
local buzzercmd = {0x7a, 0x01, 0x01, 0x01, 0x05, 0x08}
hmt.runcmd(buzzercmd,6)
hmt.delayms(200)
end
return 0
end
-- touch key hook
tpkhook = function(page, id, state)
if((page == 1)and(id == 2)and(state == 1))then
alarmhour = hmt.readvp16(0x080002)
alarmminute = hmt.readvp16(0x080004)
alarmsecond = hmt.readvp16(0x080006)
end
if((page == 1)and(id == 4)and(state == 1))then
alarmend = hmt.gettick()
end
return 0
end
-- page hook
pagechangehook = function(pageid)
return 0
end
Note: “wait loop” is not allowed in the script!
Reference
hmt.crc16calc(table, num)
uint16_t const CRC16[256]={
/* 16: 8005 reflected */
0x0000,0xc0c1,0xc181,0x0140,0xc301,0x03c0,0x0280,0xc241,0xc601,0x06c0,0x0780,0xc741,0x0500,0xc5c1,0xc481,0x0440,
0xcc01,0x0cc0,0x0d80,0xcd41,0x0f00,0xcfc1,0xce81,0x0e40,0x0a00,0xcac1,0xcb81,0x0b40,0xc901,0x09c0,0x0880,0xc841,
0xd801,0x18c0,0x1980,0xd941,0x1b00,0xdbc1,0xda81,0x1a40,0x1e00,0xdec1,0xdf81,0x1f40,0xdd01,0x1dc0,0x1c80,0xdc41,
0x1400,0xd4c1,0xd581,0x1540,0xd701,0x17c0,0x1680,0xd641,0xd201,0x12c0,0x1380,0xd341,0x1100,0xd1c1,0xd081,0x1040,
0xf001,0x30c0,0x3180,0xf141,0x3300,0xf3c1,0xf281,0x3240,0x3600,0xf6c1,0xf781,0x3740,0xf501,0x35c0,0x3480,0xf441,
0x3c00,0xfcc1,0xfd81,0x3d40,0xff01,0x3fc0,0x3e80,0xfe41,0xfa01,0x3ac0,0x3b80,0xfb41,0x3900,0xf9c1,0xf881,0x3840,
0x2800,0xe8c1,0xe981,0x2940,0xeb01,0x2bc0,0x2a80,0xea41,0xee01,0x2ec0,0x2f80,0xef41,0x2d00,0xedc1,0xec81,0x2c40,
0xe401,0x24c0,0x2580,0xe541,0x2700,0xe7c1,0xe681,0x2640,0x2200,0xe2c1,0xe381,0x2340,0xe101,0x21c0,0x2080,0xe041,
0xa001,0x60c0,0x6180,0xa141,0x6300,0xa3c1,0xa281,0x6240,0x6600,0xa6c1,0xa781,0x6740,0xa501,0x65c0,0x6480,0xa441,
0x6c00,0xacc1,0xad81,0x6d40,0xaf01,0x6fc0,0x6e80,0xae41,0xaa01,0x6ac0,0x6b80,0xab41,0x6900,0xa9c1,0xa881,0x6840,
0x7800,0xb8c1,0xb981,0x7940,0xbb01,0x7bc0,0x7a80,0xba41,0xbe01,0x7ec0,0x7f80,0xbf41,0x7d00,0xbdc1,0xbc81,0x7c40,
0xb401,0x74c0,0x7580,0xb541,0x7700,0xb7c1,0xb681,0x7640,0x7200,0xb2c1,0xb381,0x7340,0xb101,0x71c0,0x7080,0xb041,
0x5000,0x90c1,0x9181,0x5140,0x9301,0x53c0,0x5280,0x9241,0x9601,0x56c0,0x5780,0x9741,0x5500,0x95c1,0x9481,0x5440,
0x9c01,0x5cc0,0x5d80,0x9d41,0x5f00,0x9fc1,0x9e81,0x5e40,0x5a00,0x9ac1,0x9b81,0x5b40,0x9901,0x59c0,0x5880,0x9841,
0x8801,0x48c0,0x4980,0x8941,0x4b00,0x8bc1,0x8a81,0x4a40,0x4e00,0x8ec1,0x8f81,0x4f40,0x8d01,0x4dc0,0x4c80,0x8c41,
0x4400,0x84c1,0x8581,0x4540,0x8701,0x47c0,0x4680,0x8641,0x8201,0x42c0,0x4380,0x8341,0x4100,0x81c1,0x8081,0x4040,
};
static __inline uint16_t rshiftu16(uint16_t value, int nb)
{
return (uint16_t)((value >> nb) & ~((( uint16_t) 0x8000) >> (nb-1)));
}
uint16_t crc16_calc(unsigned char *q, int len)
{
uint16_t crc = 0xffff;
while (len-- > 0)
crc=(rshiftu16(crc,8) ^ CRC16[(crc ^ *q++) & 0xff]);
return crc;
}