Skip to content

Commit 0809b8b

Browse files
authored
Merge pull request #252 from currentc57/master
nextion_ez - Improved functionality and documentation
2 parents e954f6e + 415efc5 commit 0809b8b

File tree

6 files changed

+263
-121
lines changed

6 files changed

+263
-121
lines changed

libraries/community/p1/All/nextion_ez/README.md

+26-21
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,63 @@ Full documentation on the Arduino Easy Nextion Library and protocol, as well as
1313

1414
If you find this library useful, please consider supporting the author of the original Easy Nextion Library, Thanasis Seitanis at: [[email protected]](https://paypal.me/seithan)
1515

16-
1716
**NOTE**: `.HMI` files for Nextion Editor are also included in the demo folder.
1817

1918
## The public methods
2019
- `start()`
2120
- `writeNum()`
2221
- `writeStr()`
22+
- `writeByte()`
23+
- `pushCmdArg()`
2324
- `sendCmd()`
2425
- `addWave()`
2526
- `readNum()`
2627
- `readStr()`
28+
- `readByte()`
2729
- `cmdAvail()`
2830
- `getCmd()`
29-
- `getSubCmd()`
30-
- `readByte()`
31+
- `getCmdLen()`
3132
- `getCurrentPage()`
3233
- `getLastPage()`
34+
- `setCurrentPage()`
35+
- `setLastPage()`
36+
37+
**NOTE** previous version had a `getSubCmd` method that exactly duplicated the functionality of `readByte`, it has been removed. If you used `getSubCmd` in your code, just replace it with `readByte`
3338

3439
In order for the object to update the Id of the current page, you must write the Preinitialize Event of every page: `printh 23 02 50 XX` , where `XX` the id of the page in HEX.
3540
Your code can then read the current page and previous page using the `getCurrentPage()` and `getLastPage()` methods.
3641

3742
Standard Easy Nextion Library commands are sent from the Nextion display with `printh 23 02 54 XX` , where `XX` is the id for the command in HEX.
38-
Your code should call the `listen()` method frequently to check for new commands from the display. You can then use the `getAvail`, `getCmd()` and `getSubCmd` methods to parse any commands.
43+
Your code should call the `listen()` method frequently to check for new commands from the display. You can then use the `cmdAvail`, `getCmd()` and `readByte()` methods to parse any commands.
3944

4045
example:
41-
```
42-
PRI callCommand(_cmd) 'parse the 1st command byte and decide how to proceed
46+
```spin
47+
PUB main()
48+
nextion.listen ' need to run this to check for incoming data from the Nextion
49+
if nextion.cmdAvail > 0 ' has the nextion sent a command?
50+
callCommand(nextion.getCmd) ' get the command byte and see parse it
51+
52+
PRI callCommand(_cmd) ' parse the 1st command byte and decide how to proceed
4353
case _cmd
44-
"T" : 'standard Easy Nextion Library commands start with "T"
45-
nx_sub := nextion.getSubCmd ' so we need the second byte to know what function to call
46-
callTrigger(nx_sub)
54+
"T" : ' standard Easy Nextion Library commands start with "T"
55+
callTrigger(readByte) ' so we need the second byte to know what function to call
56+
' custom commands can be added by expanding this case statement
4757
48-
PRI callTrigger(_triggerId) 'use the 2nd command byte from nextion and call associated function
58+
PRI callTrigger(_triggerId) ' use the 2nd command byte from nextion and call associated function
4959
case _triggerId
5060
$00 :
51-
trigger00
61+
trigger00 ' the orginal Arduino library uses numbered trigger functions
5262
$01 :
5363
trigger01
5464
$02 :
55-
trigger02
56-
$03 :
57-
trigger03
58-
$04 :
59-
trigger04
65+
runCount ' but since we are parsing ourselves, we can call any method we want
6066
```
6167

6268
**NOTE**: This Spin object requires the use of a custom version of FullDuplexSerial.spin called FullDuplexSerialAvail.spin that adds a function to return the number of bytes in the rx_buffer
6369

6470
**NOTE**: Spin on the Proppeller 1 chip has a limit of 64 of items in a case statement. For complexe Nextion systems use of custom commands in addition to the "T" command can be used to avoid this limit. Alternatively case statements can be nested inside another structure.
6571
Example:
66-
```
72+
```spin
6773
PRI callTrigger(_triggerId) 'use the 2nd command byte from nextion and call associated function
6874
if _triggerId < $40
6975
case _triggerId
@@ -116,10 +122,9 @@ A local Number component n0 on page1 can be accessed by page1.n0 or n0, but ther
116122

117123

118124
## Compatibility
119-
* Propeller (spin version in P1 folder)
120-
* Propeller2 (spin2 version in P2 folder)
121-
122-
## Releases:
125+
* Propeller (https://github.com/parallaxinc/propeller spin version in P1 folder)
126+
* Propeller2 (https://github.com/parallaxinc/propeller spin2 version in P2 folder)
127+
* Arduino (https://github.com/currentc57/nextion_ez)
123128

124129

125130
## Licence

libraries/community/p1/All/nextion_ez/demo/nextion_ez_demo.spin

+37-21
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@
66

77
'' E-mail..... [email protected]
88
'' Started.... 17 JUN 2022
9-
'' Updated.... 19 JUN 2022
9+
'' Updated.... 04 JUL 2022
1010
''
1111
'' =================================================================================================
1212
{{
1313
A simple demonstration of how to use the nextion_ez_p1 object with the Nextion display
1414
15+
This demo uses two serial connections. One is for communicating with the Nextion (or simulator),
16+
the other to send debug data to a serial terminal.
17+
18+
The Nextion demo project has 2 pages.
19+
The first page has 2 buttons, 1 number, 1 float and 1 text field
20+
The Run button is dual state and when depressed it will cause
21+
the Propeller To change the text field and increment the number and float fields.
22+
The Page1 button will cause the Propeller to request a change to page1
23+
24+
The second page has 1 button, 1 slider, 1 gauge, 1 progress and 1 waveform
25+
The slider is located on the far right of the page
26+
Moving the slider will cause the Propeller to retrieve its position
27+
and use that value to update the progress bar, gauge and waveform.
28+
The Page0 button will cause the Propeller to request a change to page0
29+
1530
NOTE: HMI files for Nextion Editor are also included in the demo folder.
1631
}}
1732

@@ -22,7 +37,7 @@ CON
2237

2338
NX_TX_PIN = 0
2439
NX_RX_PIN = 1
25-
NX_BAUD = 9_600
40+
NX_BAUD = 115_200
2641

2742
DB_TX_PIN = 30
2843
DB_RX_PIN = 31
@@ -51,7 +66,7 @@ PUB main
5166
repeat
5267
waitcnt(clkfreq / 25 + cnt)
5368

54-
if nextion.getCurrentPage <> currentPage
69+
if nextion.getCurrentPage <> currentPage 'has the Nextion page changed?
5570
lastPage := nextion.getLastPage
5671
currentPage := nextion.getCurrentPage
5772

@@ -66,74 +81,75 @@ PUB main
6681
serial.hex(nextion.readNum(STRING("dp")), 2)
6782
serial.Tx(CR)
6883

69-
nextion.listen
70-
if nextion.cmdAvail > 0
71-
nx_cmd := nextion.getCmd
84+
nextion.listen ' need to run this to check for incoming data from the Nextion
85+
if nextion.cmdAvail > 0 ' has the nextion sent a command? '
86+
nx_cmd := nextion.getCmd ' get the command byte
7287

7388
'data to serial terminal to demonstrate what is returned
7489
serial.Str(STRING("nextion command = "))
7590
serial.hex(nx_cmd, 2)
7691
serial.Tx(CR)
7792

78-
callCommand(nx_cmd)
93+
callCommand(nx_cmd) ' let's see what command we received
7994

8095
if run_count == true
8196
disp_value++
82-
nextion.writeNum(STRING("x0.val"), disp_value)
83-
nextion.writeNum(STRING("n0.val"), disp_value)
97+
nextion.writeNum(STRING("x0.val"), disp_value) ' update the nextion number
98+
nextion.writeNum(STRING("n0.val"), disp_value) ' and float fields on page0
8499

85100
PRI callCommand(_cmd) 'parse the 1st command byte and decide how to proceed
86101
case _cmd
87102
"T" : 'standard Easy Nextion Library commands start with "T"
88-
nx_sub := nextion.getSubCmd ' so we need the second byte to know what function to call
103+
nx_sub := nextion.readByte ' so we need the second byte to know what function to call
89104

90105
'data to serial terminal to demonstrate what is returned
91106
serial.Str(STRING("nextion subcommand = "))
92107
serial.hex(nx_sub, 2)
93108
serial.Tx(CR)
94109

95-
callTrigger(nx_sub)
110+
callTrigger(nx_sub) ' now we call the associated function
96111

97112
PRI callTrigger(_triggerId) 'use the 2nd command byte from nextion and call associated function
98113
case _triggerId
99114
$00 :
100-
trigger00
115+
trigger00 ' the orginal Arduino library uses numbered trigger functions
101116
$01 :
102117
trigger01
103118
$02 :
104-
trigger02
119+
runCount ' but since we are parsing ourselves, we can call any method we want
105120
$03 :
106121
trigger03
107122
$04 :
108123
trigger04
109124

110125
PRI trigger00
111-
nextion.sendCmd(STRING("page 1"))
126+
nextion.sendCmd(STRING("page 1")) ' nextion commands can have their arguments in the string we send
112127

113128
PRI trigger01
114-
nextion.sendCmd(STRING("page 0"))
129+
nextion.pushCmdArg(0) ' or up to 16 arguments can pe passed via a stack
130+
nextion.sendCmd(STRING("page")) ' this allows the easy use of variables and constants
115131

116-
PRI trigger02
132+
PRI runCount
117133
run_count := NOT run_count
118134
if run_count
119-
nextion.writeStr(STRING("t0.txt"), STRING("Running"))
135+
nextion.writeStr(STRING("t0.txt"), STRING("Running")) ' we can update nextion text attributes with writeStr
120136
else
121137
nextion.writeStr(STRING("t0.txt"), STRING("Stopped"))
122-
nextion.readStr(STRING("t0.txt"), @txt)
138+
nextion.readStr(STRING("t0.txt"), @txt) ' and we can read text attributes with readStr
123139

124140
'data to serial terminal to demonstrate what is returned
125141
serial.str(STRING("t0.txt = "))
126142
serial.str(@txt)
127143
serial.tx(CR)
128144

129145
PRI trigger03 | slidder, wave, guage
130-
slidder := nextion.readNum(STRING("h0.val"))
146+
slidder := nextion.readNum(STRING("h0.val")) ' number attributes can be read with readNum
131147
guage := slidder * 36 / 10
132148
wave := slidder * 255 / 100
133149

134-
nextion.writeNum(STRING("j0.val"), slidder)
150+
nextion.writeNum(STRING("j0.val"), slidder) ' and number attributes an be updated on the nextion with writeNum
135151
nextion.writeNum(STRING("z0.val"), guage)
136-
nextion.addWave(1, 0, wave)
152+
nextion.addWave(1, 0, wave) ' the addWave method makes it easy to add to a nextion waveform
137153

138154
'data to serial terminal to demonstrate what is returned
139155
serial.str(STRING("h0.val = "))

libraries/community/p1/All/nextion_ez/nextion_ez.spin

+59-18
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,27 @@
2626
If you find this library useful, please consider supporting the author of the original
2727
Easy Nextion Library, Thanasis Seitanis at: [[email protected]](https://paypal.me/seithan)
2828
29-
Differences between the Arduino library and Spin object:
29+
Differences between the Arduino library and Spin object:
3030
1) The Arduino implementation automatically calls trigger functions, stored in a separate file,
3131
in response to Nextion commands.
32-
This object provides the methods cmdAvail(), getCmd(). getSubCmd() and readByte()
32+
This object provides the methods cmdAvail(), getCmd(), and readByte()
3333
to retreave the command packets sent from the Nextion.
3434
3535
2) The Arduino C++ library uses a single overloaded function writeStr() to send commands and
3636
update string values on the Nextion.
3737
This object uses separate methods sendCmd() and writeStr().
3838
39-
3) The the equivilent of the Arduino NextionListen() function has been named listen()
39+
3) A argument fifo has been added to allow a new method pushCmdArg() that can be used to
40+
provide a variable number of arguments to sendCmd().
41+
42+
4) The the equivilent of the Arduino NextionListen() function has been named listen()
4043
in this implementation.
4144
42-
4) This object adds a method called addWave() to address the special syntax of the
45+
5) This object adds a method called addWave() to create a quick and easy interface to the
4346
Nextion waveform add command.
4447
45-
5) In this object the currentPageId and lastCurrentPageId variables can be accessed with the
46-
methods getCurrentPage() and getLastPage()
48+
6) In this object the currentPageId and lastCurrentPageId variables can be accessed with the
49+
methods getCurrentPage(), getLastPage(), setCurrentPage() and setLastPage()
4750
4851
}}
4952

@@ -55,9 +58,11 @@ VAR
5558
long current_page_id
5659
long last_current_page_id
5760
byte cmd
58-
byte sub_cmd
5961
byte cmd_len
6062
byte cmd_avail
63+
long cmd_fifo[16]
64+
byte cmd_fifo_head
65+
byte cmd_fifo_tail
6166

6267
OBJ
6368
_nextion : "FullDuplexSerialAvail" 'a special version of FullDuplexSerial that provides an available method like Arduino and FullDuplexSerial
@@ -101,15 +106,53 @@ PUB writeStr(ptr_component, ptr_txt) 'send a string value t
101106
repeat 3
102107
_nextion.tx($FF)
103108

109+
PUB writeByte(val) 'send raw data byte (not ASCII formated) to Nextion
110+
{{
111+
Main purpose and usage is for sending the raw data required by the addt command
112+
where we need to write raw bytes to serial
113+
114+
example: nextion.writeByte(0)
115+
}}
116+
_nextion.tx(val)
117+
118+
PUB pushCmdArg(argument) 'load the argument FIFO with numeric arguments that are to be sent with the command using sendCmd()
119+
{{
120+
Used to load the argument FIFO with numeric arguments that are to be sent with the command using sendCmd()
121+
example: to send the command "page 1" to the nextion
122+
nextion.pushCmdArg(1)
123+
nextion.sendCmd(STRING("page"))
124+
}}
125+
cmd_fifo[cmd_fifo_head] := argument
126+
cmd_fifo_head++
127+
if cmd_fifo_head > 15
128+
cmd_fifo_head := 0
104129

105-
PUB sendCmd(ptr_command) 'send a command to nextion
130+
PUB sendCmd(ptr_command) | count, x, argument 'send a command to nextion
106131
{{
107132
send a command to nextion
108133
ptr_command should be a pointer to a string containing the command to be sent
109134
110135
example: nextion.sendCmd(STRING("page 0"))
111136
}}
137+
if(cmd_fifo_head < cmd_fifo_tail)
138+
count := (cmd_fifo_head + 16) - cmd_fifo_tail
139+
else
140+
count := cmd_fifo_head - cmd_fifo_tail
141+
112142
_nextion.str(ptr_command)
143+
144+
if(count > 0)
145+
_nextion.tx(" ")
146+
x := 0
147+
repeat count
148+
if x > 0
149+
_nextion.tx(",") 'only need commas between arguments, not between command and 1st argument
150+
argument := cmd_fifo[cmd_fifo_tail]
151+
_nextion.dec(argument)
152+
cmd_fifo_tail++
153+
if(cmd_fifo_tail > 15)
154+
cmd_fifo_tail := 0
155+
113156
repeat 3
114157
_nextion.tx($FF)
115158

@@ -327,33 +370,31 @@ PUB listen | _char, _time, _ms, _len, _cmdFound, _cmd 'check for incoming s
327370
last_current_page_id := current_page_id
328371
current_page_id := _nextion.rx
329372

330-
"T" :
331-
cmd_avail := true 'normal Easy Nextion Library style 2 byte commands start with "T"
332-
cmd := _cmd ' so we pull both bytes and hold them for the main code
333-
sub_cmd := _nextion.rx
334-
335-
OTHER : 'custom commands can be variable length, we pull just the first and leave the rest for main code to deal with
373+
OTHER : 'commands can be variable length, we pull just the first and leave the rest for main code to deal with
336374
cmd_avail := true
337375
cmd := _cmd
338376
return
339377

340378
PUB getCurrentPage : _page 'returns the current page id
341379
return current_page_id
342380

381+
PUB setCurrentPage(_page) 'sets the current page id
382+
current_page_id := _page
383+
343384
PUB getLastPage : _page 'returns the previous page id
344385
return last_current_page_id
345386

346-
PUB cmdAvail : _avail 'returns the number of commands in the buffer
387+
PUB setLastPage(_page) 'sets the previous page id
388+
last_current_page_id := _page
389+
390+
PUB cmdAvail : _avail 'returns true if commands in the buffer
347391
_avail := cmd_avail
348392
cmd_avail := false
349393
return
350394

351395
PUB getCmd : _cmd 'returns the 1st command byte
352396
return cmd
353397

354-
PUB getSubCmd : _sub 'returns the 2nd command byte
355-
return sub_cmd
356-
357398
PUB getCmdLen : _len 'returns the number of command bytes (for use in custom commands)
358399
return cmd_len
359400

0 commit comments

Comments
 (0)