Unofficial Nextion/TJC User Forum

Is it even possible to simplify my code? To avoid crashes?

Hello

I wrote a weather forecast application based on an ESP32 and NX8084K050_011.

In total 1172 images for 61 Icons:
46 animated weather icons
15 static weather icons

Serial commands are kept minimal by using variables:
First row: “vdsc0” … “vdsc6” for 7 images
Second row: “vhsc0” … “vhsc7” for 8 images
Values: -30 to +30 (negative: night, positive: day, zero: blank)
Only on changing page or changing icon will send new value (50-500/day)

Two timer cycle in 75ms:
“tmdsc” for first row, responsible for 7 icons
“tmhsc” for second row, responsible for 8 icons

Timer “tmdsc” has 2873 lines of code
Timer “tmhsc” has 3278 lines of code

On each timer event cycle as folowed:
Timer “tmdsc”: tmdsc.txt
Timer “tmhsc”: tmhsc.txt

I have tried add more(15 in total) timer but has no effect. Program Code has still the same line amount or more.

Is it even possible to simplify my code? Less lines? Any suggestions?

I’m thinking of switching to a raspberry pi without a nextion display…

Img1: nex1.png
Img2: nex2.png

Its very difficult to work with parts of a program , is it possible to get a copy of the hmi file to run in the simulator.

Obviously, the “official” Nextion staff is reading here and Patrick felt challenged…

Thanks for sharing the post from the blog. I guess I wouldn’t have seen it without.
I’m not sure how to assume this now… Do I feel embarrassed or proud that this is now on a blog?
Well I think this is a perfect example for creating “arrays”. I would definitely never have thought of it and I would like to see this as a tutorial to create arrays.

Thanks to everyone who took the time to take a closer look!

1 Like

Almost forgot to share a picture…

3 Likes

You definitely do not have to feel ashamed, the Documentation on Arrays in Nextion is unbelivable minimalistic and if you do not have much experience with it there is no way to use them without running into more problems than you solve.

As you say your post inspired the guys over at nextion to finally talk about the arrays they are using, its a win win

You gotta let that sink… The “simplest” way to implement something as trivial as an array of integers is to write a >200 character long string, that’ll be split into dozens of parts, one of which gets converted into an integer. And that entire split and convert action happens on every single access to the “array”.

I’d be highly interested in seeing some benchmarks on this. Quite ironical that this solution comes from Patrick. He’s always the first guy to remember everyone on every occasion how limited (and thus precious) Nextions resources are, how inefficient strings are compared to integers, etc.

Btw I do think one could write a more efficient and string-less version of this. It is however a bit more work. For every one of the ~60 values of the array, create a variable component and assign the value to it. The variable name doesn’t matter but they got to be in the right order. Then you can use the b array to access all of them. All you need to do is know the offset of the first variable so you end up with something like b[34+i].val. 60 variables equals 240 bytes of RAM usage which shouldn’t be a problem.
The big advantage of this solution is that no expensive string loading, processing and converting is required. It’s all integer as it should be. The disadvantage is that you have to create 60 variables. As for the RAM usage I’m not sure if the 200 character string occupies “user” RAM while being processed or not so I can’t compare it on that level.

Kind regards,
Max

The 60 integer variables will use much more RAM than the string. If I remember correctly, each numeric variable componentuses 16 bytes to carry a payload of 4 bytes. The string solution is thus more efficient, especially when you have only 1, 2, or 3 digits. Even with the added separator, the storage will not exceed 4 bytes per value.

To add to this while working with xmodem CRC16 I used a string to hold the characters from 32 up so it could be searched and I found that 4 times less was used per character in strings as in number variables.

Number components do take a lot more RAM, yes. But at least in my editor (currently 1.62.1) and for the NX8048T050 target I just verified that a variable takes up 4 bytes. No more no less. The local memory usage went from 20 to 24 bytes.
Curious to hear if I‘m missing something.

Edit: @paulvk true, you only have 32bit variables which aren‘t memory efficient for storing 8 or 16bit values. I guess it depends a lot on the implementation how efficient the string based alternative is - space and speedwise.
If you take advantage of the fact that 1 character can encode 8 bits of data, it is for sure more compact than the variable. Speedwise IMO you can‘t beat an integer only solution but not sure. Requires testing!

Regards,
Max


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).