The trick is, do not touch those global variables as far as possible. Let them do their magic in the background. if the page is already loaded, then simply update the buttons directly. The new state will automatically be stored to the global variables on page exit. Especially when you pack multiple values into one global variable this is a lot easier, too.
An example of how to load two numbers with 16 bit resolution from 1 global variable, and how to store 4 button states in another one can be found here: https://github.com/MMMZZZZ/Syntherrupter/blob/95e63a165aa0b0aaff50a823802cf4c302f6e164/Syntherrupter_Nextion/Source_as_Text/Lightsaber.txt#L147
In the page exit event is the code that saves them to the global variables: https://github.com/MMMZZZZ/Syntherrupter/blob/95e63a165aa0b0aaff50a823802cf4c302f6e164/Syntherrupter_Nextion/Source_as_Text/Lightsaber.txt#L222
(Source code as text generated from the Nextion file with this tool: https://unofficialnextion.com/t/convert-nextion-to-text/ )
This trick has one limitation though: It does not work if you have to modify something unrelated on another page. Now what does unrelated mean? Imagine you want to keep the colors of multiple textboxes on different pages identical. Then you simply use the same global variable for all those components instead of writing code to sync them. And magically you have less RAM usage and can stick with the easier code from above.
In case this is really not possible, then of course you have to directly modify the data in the global variables. And good coding practices say you shouldn’t write two different codes for doing the same thing, so it would be great if you could use that same code to update the values even while the page is loaded.
My solution would be to put the whole load/save code in the touch press and touch release event of a component that’s invisible. Then you can f.ex. load all states from the global variables with click loadSave,0
, and save all of them with click loadSave,1
. The only thing to remember is that you have to do those click
events at the very end of your preinit event or in the postinit event, because they force the screen to be updated. I currently have no good example of this - and not the time to write one. Hope it helps anyways.
One note about text: text is expensive; it needs lots of ram (1 Byte per character). Often you don’t have to display completely random texts but only switch between texts that are already known. in this case you can create local strings with those texts and assign them a number (string1, string2, string3). Then, in your global variable you only store the string number which only costs you 4 bytes or less - no matter how long your strings are. On page preinit of course, you load the right string into the component based on the number.
This keeps the whole string data inside of the local memory, and doesn’t occupy valuable memory on other pages. The value of the local strings is set in the Editor (so they will always have that value when the page loads).
If however it is a completely unknown string, there’s no such trick.
Edit (since this post isn‘t already long enough): I personally prefer to keep the whole GUI stuff running on the Nextion itself. Meaning I don‘t use the Arduino to transfer data from one page to another. Nextion runs the GUI, no more, no less. Arduino (or any other microcontroller) does the control stuff. Reading sensors, etc. This keeps the time consuming communication between both devices at a minimum. Nextion only talks to Arduino if the user changes settings, and Arduino only talks to Nextion if some monitoring needs to be updated.
Kind regards,
Max