You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • This is a different wording of the facts @AkosLukacs points out in post #7.

    The problem is that blockly does not think event driven. Javascript is a single threaded executed... and wait - by constantly checking time until time arrives - just cannot exist in such an environment, and - worse - in Espruino, it hogs the processor until 'snippet' is completed. Never ever implement something like in second code block of #1 post. Even the while loop hogs the processor... it comes from the outdated main loop concept (erroneous reestablished and 'hardened' in the public mind by Arduino - so retro), a concept that was introduced in machine control with digital logic a good 50 years ago... derived from / emulating the even much older electro mechanical 'Schrittschaltwerk' - automatic stepping mechanism. The automatic stepping mechanism is a list of unconditioned and conditioned steps that are executed one after the other over and over again. It is easy to understand but extremely limited...

    (If you are using ESP8266, your processor reboots constantly... because the Wifi stack on it has an interrupt cycle of a given number of micro seconds to take care of urgent communication work, and when it does not get the control of the processor, it resets itself. This means in other words that the javascript executed on an even can only take so much that it still fits into this interrupt cycle window... From the total processor time only a percentage is available to other code / application code / Espruino code, and this code has to finish within a given number of microseconds. The other percentage is used by the Wifi stack).

    What actually happens in the blockly code is the following:

    • create the while loop with true executing this code:
      • crate an entry in the 'time table' - so to speak - that after 1 second execute the following code:
        • turn the led off
        • create an entry in the time table hat after 1 second executed the following code:
          • nothing

    Creating the entry in the time table takes practically no time -returns immediately - to executed next statement, and now y0u understand that the code creates tons of execution deferred pieces of code... until it rans out of space in the time table... (paraphrased), and never ever even 'gets the time' to work on these scheduled blocks of code...

    SetInterval and SetTimeout is not a waiting kind of thing but a scheduling thing scheduling a piece of code to be executed at scheduled time (now plus delay), is executed immediately with control returning immediately.

    Try something like that in blockly:

    • create a boolean variable b that you set to true.
    • create a function f that includes the following:
      • wait (blockly's incorrect notion of schedule following code to be executed after wait time
      • turn the led on / off
      • if query the boolean variable b, and if true:
        • invoke 'yourself' - the function f
    • invoke the function f - to get it all started

    Invoking the function f you even can - should - do with a wait, because you want to let the upload finish - because it is also a Javascript snippet(s) execution.

    • create a boolean variable b that you set to true.
    • create a function f that includes the following:
      • wait (blockly's incorrect notion of schedule following code to be executed after given time)
      • turn the led on / off
      • if query the boolean variable b, and if true:
        • invoke 'yourself' - the function f
    • wait (blockly's incorrect notion of schedule following code to be executed after given time)
    • invoke the function f - to get it all started (after upload completed)

    The sequence you create this logic in blockly is to create the function first, and then the other code:

    • create a function f - a named block of code - that includes the following:

      • wait (blockly's incorrect notion of schedule following code to be executed after given time)
      • turn the led on / off
      • if query the boolean variable b, and if true:
        • invoke 'yourself' - the function f

    • create a boolean variable b that you set to true.

    • wait (blockly's incorrect notion of schedule following code to be executed after given time)

    • invoke the function f - to get it all started (after upload completed)

    I'm sure you have some other logic that controls boolean variable b and as well some variable code that determines whether the led should be turned on or off...

    @Gordon, I guess it is time to thing of something more Espruino / JS / Event driven and 'ban' the (inferior and in reality never used concept of) wait (by killing processor cycles by just looping around and checking time)... for examle: scheduleOnce and scheduleRepeated as an (right hand) expression returning the handle and with the look of the while/for loop or if/else to make clear the block structure. (Something on your already tall pile... may be there are takers in the forum...). I wanted to use blockly in its current from, but it got so cumbersome that learning JS and coding in a language in text from is what just works better.

About

Avatar for allObjects @allObjects started