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

spstr can be replaced by substr to get the value. Using hex encoded values e.g. with same size each gets rid of the delimiter. Works very well in case the value range isn’t too different and even the 3-char hex string is shorter and faster to index.

00002304606908C0AF0D20F00F411713A14114214314416718A1AD1D01F321621723A25D2802A32C62E92EA2EB2EC2ED2EE2EF2F031333635937C3C23C33E640942C44F47214314214113A1170F40F00D20AF08C069046023000
0,35,70,105,140,175,210,240,244,279,314,321,322,323,324,359,394,429,464,499,534,535,570,605,640,675,710,745,746,747,748,749,750,751,752,787,822,857,892,962,963,998,1033,1068,1103,1138,323,322,321,314,279,244,240,210,175,140,105,70,35,0

Hi,
I made a cnc lathe control pannel with nextion hmi and arduino due. And i’m trying to simplify my code too.
User can draw part using control pannel.
It’s look like in the picture.

Here is a video which shows how to draw part.
https://youtu.be/gJ8Yoy6v4c0?list=PLW9VonyAm3aFcd6lUvJKMxmIL2QO_59sY&t=55

User can enter 30 dimentions. I wrote similar code 30 times because of i could not define array variable.
There are more then 10000 lines in drawing page. Is there any way to define array variable or something like it.