Skip to content

Commit 21ce468

Browse files
authored
Merge pull request #255 from currentc57/master
nextion_ez pushCmdArg() more general and fixed nextion baud
2 parents 45ccd98 + e946d5b commit 21ce468

File tree

6 files changed

+192
-63
lines changed

6 files changed

+192
-63
lines changed
0 Bytes
Binary file not shown.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'' =================================================================================================
22
''
3-
'' File....... nextion_ez_p1.spin
3+
'' File....... nextion_ez_demo.spin
44
'' Purpose....
55
'' Author..... Charles Current
66

@@ -127,7 +127,7 @@ PRI trigger00
127127

128128
PRI trigger01
129129
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
130+
nextion.sendCmd(STRING("page ")) ' this allows the easy use of variables and constants
131131

132132
PRI runCount
133133
run_count := NOT run_count

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

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
methods getCurrentPage(), getLastPage(), setCurrentPage() and setLastPage()
5050
5151
}}
52-
52+
'------------------------------------------------------------------------------
5353
CON
5454
SERIAL_MODE = %0000
55-
ERROR_NUM = 777777
56-
55+
ERROR_NUM = 777777
56+
'------------------------------------------------------------------------------
5757
VAR
5858
long current_page_id
5959
long last_current_page_id
@@ -63,23 +63,23 @@ VAR
6363
long cmd_fifo[16]
6464
byte cmd_fifo_head
6565
byte cmd_fifo_tail
66-
66+
'------------------------------------------------------------------------------
6767
OBJ
6868
_nextion : "FullDuplexSerialAvail" 'a special version of FullDuplexSerial that provides an available method like Arduino and FullDuplexSerial
69-
69+
'------------------------------------------------------------------------------
7070
PUB start(rxPin, txPin, baud) 'Must be run before using object
7171
{{
7272
Must be run before using object
7373
Will start a new serial object in it's own cog using the pins and rate provided
7474
}}
7575
_nextion.start(rxPin, txPin, SERIAL_MODE, baud)
7676
waitcnt(clkfreq / 100 + cnt) 'wait for serial to init
77-
77+
'------------------------------------------------------------------------------
7878
PUB writeNum(ptr_component, num) 'send a numeric value to nextion
7979
{{
8080
send a numeric value to nextion
8181
ptr_component should be a pointer to a string that names the object and attribute to receive the new value
82-
_num is the value to assign to the object.attribute
82+
num is the value to assign to the object.attribute
8383
8484
example: nextion.writeNum(STRING("j0.val"), number)
8585
}}
@@ -89,7 +89,7 @@ PUB writeNum(ptr_component, num) 'send a numeric value
8989
repeat 3
9090
_nextion.tx($FF)
9191

92-
92+
'------------------------------------------------------------------------------
9393
PUB writeStr(ptr_component, ptr_txt) 'send a string value to nextion
9494
{{
9595
send a string value to nextion
@@ -105,7 +105,7 @@ PUB writeStr(ptr_component, ptr_txt) 'send a string value t
105105
_nextion.tx($22)'double quote
106106
repeat 3
107107
_nextion.tx($FF)
108-
108+
'------------------------------------------------------------------------------
109109
PUB writeByte(val) 'send raw data byte (not ASCII formated) to Nextion
110110
{{
111111
Main purpose and usage is for sending the raw data required by the addt command
@@ -114,23 +114,27 @@ PUB writeByte(val) 'send raw data byte (n
114114
example: nextion.writeByte(0)
115115
}}
116116
_nextion.tx(val)
117-
117+
'------------------------------------------------------------------------------
118118
PUB pushCmdArg(argument) 'load the argument FIFO with numeric arguments that are to be sent with the command using sendCmd()
119119
{{
120-
Used to load the argument FIFO with numeric arguments that are to be sent with the command using sendCmd()
120+
Used to load the argument FIFO with numeric arguments that are to be sent with the command using sendCmd().
121+
This allows easier creation of methods that take a variable argument.
122+
if using FIFO, the command string must include any required spaces or commas before the first FIFO argument
123+
121124
example: to send the command "page 1" to the nextion
122125
nextion.pushCmdArg(1)
123-
nextion.sendCmd(STRING("page"))
126+
nextion.sendCmd(STRING("page "))
124127
}}
125128
cmd_fifo[cmd_fifo_head] := argument
126129
cmd_fifo_head++
127130
if cmd_fifo_head > 15
128131
cmd_fifo_head := 0
129-
132+
'------------------------------------------------------------------------------
130133
PUB sendCmd(ptr_command) | count, x, argument 'send a command to nextion
131134
{{
132-
send a command to nextion
135+
Send a single string command to nextion.
133136
ptr_command should be a pointer to a string containing the command to be sent
137+
if using FIFO, the command string must include any required spaces or commas before the first FIFO argument
134138
135139
example: nextion.sendCmd(STRING("page 0"))
136140
}}
@@ -142,7 +146,6 @@ PUB sendCmd(ptr_command) | count, x, argument 'sen
142146
_nextion.str(ptr_command)
143147

144148
if(count > 0)
145-
_nextion.tx(" ")
146149
x := 0
147150
repeat count
148151
if x > 0
@@ -156,7 +159,7 @@ PUB sendCmd(ptr_command) | count, x, argument 'sen
156159
repeat 3
157160
_nextion.tx($FF)
158161

159-
162+
'------------------------------------------------------------------------------
160163
PUB addWave(id, channel, val) 'Add single value to a Nextion waveform channel
161164
{{
162165
Add single value to a Nextion waveform channel
@@ -175,7 +178,7 @@ PUB addWave(id, channel, val) 'Add single value to a N
175178
repeat 3
176179
_nextion.tx($FF)
177180

178-
181+
'------------------------------------------------------------------------------
179182
PUB readStr(ptr_component, ptr_return) : status | _char, _pos, _time, _ms, _ffCount, _end 'Read a string value from nextion, will return a 1 if successful or -1 on error
180183
{{
181184
Read a string value from nextion, will return a 1 if successful or -1 on error
@@ -241,7 +244,7 @@ PUB readStr(ptr_component, ptr_return) : status | _char, _pos, _time, _ms, _ffCo
241244
return
242245

243246
return 1
244-
247+
'------------------------------------------------------------------------------
245248
PUB readNum(ptr_component) : num | _time, _ms, _ffCount, _end, _char, _count, _numBuff[4] 'read a numeric value from nextion, returns number value or 777777 on error
246249
{{
247250
Read a numeric value from nextion, returns number value or 777777 on error
@@ -314,10 +317,28 @@ PUB readNum(ptr_component) : num | _time, _ms, _ffCount, _end, _char, _count, _n
314317

315318
return
316319

317-
PUB readByte : _char 'read a byte from serial buffer (for use in custom commannds)
320+
PUB readByte : _char 'read a byte from serial buffer
321+
{{
322+
Read a single byte from the serial buffer.
323+
324+
This is used to retrieve additional commmand bytes for parsing. The first command byte is pulled from the buffer by listen() and is then
325+
available via getCmd() method. Any additional command bytes can be pulled from the buffer via readByte().
326+
327+
example:
328+
PUB main()
329+
nextion.listen ' need to run this to check for incoming data from the Nextion
330+
if nextion.cmdAvail() > 0 ' has the nextion sent a command?
331+
callCommand(nextion.getCmd()) ' get the 1st command byte and see parse it
332+
333+
PRI callCommand(_cmd) ' parse the 1st command byte and decide how to proceed
334+
case _cmd
335+
"T" : ' standard Easy Nextion Library commands start with "T"
336+
callTrigger(readByte()) ' so we need the second byte to know what function to call
337+
' custom commands can be added by expanding this case statement
338+
}}
318339
_char := _nextion.rxTime(100) 'if timeout (-1) return error (-1)
319340
return
320-
341+
'------------------------------------------------------------------------------
321342
PUB listen | _char, _time, _ms, _len, _cmdFound, _cmd 'check for incoming serial data from nextion, must be run frequently to respond to events
322343
{{
323344
Check for incoming serial data from nextion, must be run frequently to respond to events
@@ -374,31 +395,76 @@ PUB listen | _char, _time, _ms, _len, _cmdFound, _cmd 'check for incoming s
374395
cmd_avail := true
375396
cmd := _cmd
376397
return
377-
398+
'------------------------------------------------------------------------------
378399
PUB getCurrentPage : _page 'returns the current page id
379-
return current_page_id
400+
{{
401+
This method is provided as a way to read the methods current_page_id variable.
380402
403+
In order for the object to update the Id of the current page, you must write in the Preinitialize Event of every page: printh 23 02 50 XX ,
404+
where XX the id of the page in HEX. Your code can then read the current page and previous page using the getCurrentPage() and getLastPage() methods.
405+
}}
406+
return current_page_id
407+
'------------------------------------------------------------------------------
381408
PUB setCurrentPage(_page) 'sets the current page id
382-
current_page_id := _page
409+
{{
410+
This method is provided as a way to write the methods current_page_id variable.
383411
412+
In some cases it is usefull to be able to change the methods current_page_id and/or last_current_page_id variables.
413+
}}
414+
current_page_id := _page
415+
'------------------------------------------------------------------------------
384416
PUB getLastPage : _page 'returns the previous page id
385-
return last_current_page_id
417+
{{
418+
This method is provided as a way to read the methods last_current_page_id variable.
386419
420+
In order for the object to update the Id of the current page, you must write in the Preinitialize Event of every page: printh 23 02 50 XX ,
421+
where XX the id of the page in HEX. Your code can then read the current page and previous page using the getCurrentPage() and getLastPage() methods.
422+
}}
423+
return last_current_page_id
424+
'------------------------------------------------------------------------------
387425
PUB setLastPage(_page) 'sets the previous page id
388-
last_current_page_id := _page
426+
{{
427+
This method is provided as a way to write the methods last_current_page_id variable.
389428
429+
In some cases it is usefull to be able to change the methods current_page_id and/or last_current_page_id variables.
430+
}}
431+
last_current_page_id := _page
432+
'------------------------------------------------------------------------------
390433
PUB cmdAvail : _avail 'returns true if commands in the buffer
434+
{{
435+
After calling the listen() method, this method is used to see if there is a pending command from the Nextion.
436+
437+
example:
438+
PUB main()
439+
nextion.listen ' need to run this to check for incoming data from the Nextion
440+
if nextion.cmdAvail() > 0 ' has the nextion sent a command?
441+
callCommand(nextion.getCmd()) ' get the 1st command byte and see parse it
442+
}}
391443
_avail := cmd_avail
392444
cmd_avail := false
393445
return
394-
446+
'------------------------------------------------------------------------------
395447
PUB getCmd : _cmd 'returns the 1st command byte
448+
{{
449+
After calling the listen() method, and verifying that a command is available,
450+
this method is used to see retrieve the 1st command byte.
451+
452+
example:
453+
PUB main()
454+
nextion.listen ' need to run this to check for incoming data from the Nextion
455+
if nextion.cmdAvail() > 0 ' has the nextion sent a command?
456+
callCommand(nextion.getCmd()) ' get the 1st command byte and see parse it
457+
}}
396458
return cmd
397-
459+
'------------------------------------------------------------------------------
398460
PUB getCmdLen : _len 'returns the number of command bytes (for use in custom commands)
461+
{{
462+
This method will return the number of bytes in the command buffer.
463+
This could be useful for creating custom commands of variable length.
464+
}}
399465
return cmd_len
400-
401-
con { license }
466+
'------------------------------------------------------------------------------
467+
CON { license }
402468

403469
{{
404470
0 Bytes
Binary file not shown.

libraries/community/p2/All/nextion_ez/demo/nextion_ez_demo.spin2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'' =================================================================================================
22
''
3-
'' File....... nextion_ez_p2.spin2
3+
'' File....... nextion_ez_demo.spin2
44
'' Purpose....
55
'' Author..... Charles Current
66

@@ -122,7 +122,7 @@ PRI trigger00()
122122

123123
PRI trigger01()
124124
nextion.pushCmdArg(0) ' or up to 16 arguments can pe passed via a stack
125-
nextion.sendCmd(STRING("page")) ' this allows the easy use of variables and constants
125+
nextion.sendCmd(STRING("page ")) ' this allows the easy use of variables and constants
126126

127127
PRI runCount()
128128
run_count := NOT run_count

0 commit comments

Comments
 (0)