Start a new topic
Solved

while statement v0.38 (bug or feature?)

I am a little confused about the while statement. What can you do with it?

So far as i can see, only its own counter inc. or dec. can be used like this:(Instruction)

 

while(n0.val<100)
{
n0.val++
}

This is only a little timer without documented time.

Because no external events (GUI/Timers) can be used while the while-loop runs.

It is particularly fatal that any timers on the page stops while the while loop runs. 



The while clause is a programming block that exits when the defining condition is met.


while(condition)

{

// program block

//

}


I can put many statements in that block that will continue to run in sequence while the condition exists.

consider the variable user_quits.val = 0


while(user_quits.val = 0)

{

// do a boatload of things


}


So if the way a user quits is to touch a hotspot and the event code in the hotspot is user_quits.val=1,

or as in a game, until 500 points is reached and a clause

if(points.val>=500)

{

user_quits.val=1

}

Or perhaps user_quits.val is triggered by the RTC clock when 18:00h is reached.


the while statement is a powerful tool user by programmers.

With regards to your code 

while(n0.val<100)

{

n0.val++

You certainly have choices of what you put inside your code.  Your block chose to count and nothing else.  So I wouldn't expect it do accomplish anything other than exactly what you told it to do.  The expected behaviour of the while statement is predictable.  Until the condition of n0 being less than 100 no longer exists ... continue counting.   Consider this snippet:

while(n0.val<100)

{

}
what do you suppose happens.  Nothing!, and it will continue to do nothing as there is no escape.  That would be detrimental to the HMI, and the effect will only seem as if the Nextion is bricked.  But it is NOT bricked, it is executing your commands exactly as it was instructed to do.  After some time at 48Mhz, continuing its branch to restart the nothingness loop without any pauses it might even begin to overheat and potentially fry itself out.  So it is assumed that the programmer is aware of any consequences of what they are programming and the effects it will have on the systems they are programming.
while(n0.val<100)
{
delay(100)
}

Similar case wait 100ms repeat - not the same kind of race cycle as very few of the 48 million instruction cycles for that second will ever get used.  But again no escape, and a continuous loop.
if(rtc4=59)
{
if(rtc5>45)
{
while(rtc4=59)
{
delay(50)
}

}

}

click m0,1

Now the above code when executed will pause until almost exactly On-The-Hour before executing the commands that are stored in the Hotspot m0's Touch Pressed Event.  Perhaps some type of surprise or "chime" will execute.  Perhaps a signal sent out through GPIO.  Perhaps it will be a printh command that will trigger the connecting MCU to execute some special function.


Is the while command fatal?  Does it hijack?  It's more like demanding the MCU.  You will do this { //code block } and only that, while(condition) still exists.  It can be used wisely creating awesome results, or I suppose it can be used to fry a Nextion (ps: not covered under warranty) if placed in the wrong hands.


Hallo Patrick, 


thanks for your replay.

I think, you have misunderstand me. Thats my mistake.

I now what a while-loop is. I love while-loop since many years and in many languages.

The question is not, what can i do with a while loop, rather what can i do with a while-loop in NEXTION Script language because of all the limitations.

In your example.

  

while(user_quits.val == 0)
{
// do a boatload of things

} 

How should the condition be true? 

From MCU? - no serial command executed while the  while-loop runs

From GUI? - no GUI Inputs allowed in while loop. doevents only for refresh GUI (outputs)

From Timer? - Timer stops while the  while-loop runs


So far i can see, the contition can only manipulate within the while-loop;-(

 

The while-loop is a powerful tool, thats correct.

Maybe you have an example of the meaningful use in NEXTIOIN Script language.


MCUs as with many modern CPUs have Interrupts.  Interrupts "interrupt" the code when the priority of the triggering interrupt is greater than the priority where it is at now.  So we know that there are no real user defined software interrupts that you can program from within the Nextion Editor, so mostly I see the while command relying on external influences.  I imagine the touch screen triggers a hardware interrupt.  Does the touch screen have a higher priority - test and find out.  If it does, then the pressed event will occur.  If your while is inside a touch event, then the equal priority may not be enough to trigger.  Communications can be hardware driven, when the Nextion is put to sleep, comms still operate.  Is it a hardware driven interrupt, test and find out.  Without the firmware source it is hard to say what the interrupt priorities were programmed at (and we know we aren't going to get the source code to the firmware).  So trial and error and deduction through observed behaviour.


I should be able to create (untested as of yet)

sleep=1

while(rtc3 < 8)

{

// sleep for a minute

delay(60000)

}

sleep=0

and put the Nextion to bed until 8 in the morning.  (might need to be modified slightly)


Or a potential use of the command if we were monitoring temp and adding it to a waveform

while(va0.val<50)

{

// tell my MCU to get the temp and upload it with custom MCU command 36

printh 24

// wait for MCU to complete its upload to va0.val and add value to waveform

delay(100);

// wait remainder of minute until next cycle

delay(59850);

}

at this point (untested), we perhaps get the MCU to update va0 and the waveform every minute

when the temp goes past 50C, we might want to check the condition if the house is on fire.


The condition is tested by the while loop.  It does not mean that you are not confined to the while loop

Code inside of hotspot events Pressed and Released are up to you.  Your va0 value could be altered inside any one of those events.  The while command has purpose, you merely haven't found a purpose that you can put it to good use yet.  And any statement that can be created with a while statement can be built with a for loop and if statements.


while(va0.val<50)

{

// run code in m0 Pressed

click m0,1

delay(100)

// run code in m0 Released

click m0,0

delay(100)

}


I would refrain from referring to a few Instructions as a Scripting Language.  In that manner, it is not a script language but rather a few shortcuts that save a bit of time now and again.  As you keep trying to have it fit in your mind as though it were a scripting language, it keeps not conforming to your definition of what a scripting language should achieve and certainly will not work as per your expectations.  It really is a simple API with a few shortcuts.  The real programming gets done on a connecting MCU.

The flow of the code on the Nextion is FIXED, and does not really allow for a scripting language.  The components are data record structures and not objects.


When the Nextion boots up, it initializes the "program",  Starting with the default main page page 0


BEGIN

- performs instructions in the Preinitialize event of page 0,

- dump all, clear memory, prep for new page

- fetches and displays page 0 as per design, then

- performs the instructions in the Postinitialize Event.


LOOP:

- After that it waits for commands from a few sources:

  • Timer Event - low priority as it is a software timer and not a hardware timer
  • text commands coming in over serial
                - carry out command as per incoming instruction
                - is the attribute altered default to auto update, if so update component
  • pressed/released events from the touch panel
                - at x,y what component occupies (compare x,y,w,h data definitions for the components)
                - does this component have a touch pressed event defined? if so run it
                - when touch is released, does a release event exist? if so run it
  • Does the event code contain a page command?  If so start over with requested page

And that is pretty much the Nextion.  You can somewhat jump from point to point a little (not too much or you will create a stack overflow), but it is always limited to the main confines of the data structures.

Yes, the click command works in while-loop. Thats good and can be very helpful. Thanks for that.


That works

 

while(c1.val>0)
{
  delay=100
  click c0,0  //attention, press event not sets the val!
  delay=100
  click c1,1  //only in release event the val sets to 1
}
t0.txt="OK"

 >> The real programming gets done on a connecting MCU

Sure, but my intention is, that the MCU only sends display values to the display and receive command from Touch-Buttons etc.

But this discussion does not belong here.

As most sensors values come in from pins on the MCU first, it makes more sense that the MCU does the processing and the display is just that - a display.  When we program headless systems (servers, MCUs and portions of the desktop system), the processing is generally done on the MCU and the display is used to reflect the current state of affairs.  As an Human Input Device, then we display what is necessary to obtain the desired human input.  When using the Display for purposes outside of intended usage, even if innovative in a new found purpose - doesn't make it a bug when the extended purpose and new usage fails to work as it is outside of the intended scope.

Edwin Croissant has summarized all the arguments, better than I can in my bad English.


http://support.iteadstudio.com/support/discussions/topics/11000005749/page/2?url_locale=


That's exactly what i think.

Thanks for your help and a good sunday.


Reading the updated Nextion Instruction Set - combining doevents inside a while loop helps provide updates/refreshes as the while statements are looping (helps avoid the bricked look)


while(n0.val<100)

{
n0.val++
doevents
}

I will mark this topic as solved.