internal idle in uart.write / fs.read ?

Posted on
  • I need to read some data from a file (fs.read) and later on write a lot of data to uart.
    This takes some time, and I don't want to block other code during this.
    Is Espruino adding idles states internally, or do I need to add something like setTimeout(function(){...},1) inside the loops for reading from SD and writing to UART ?

  • fs.read() blocks.

    I don't think UART sending does, though (it'd be madness if it did - I haven't looked at the implementation here, but I suspect it's implemented similar to Arduino, where it sticks the text into a buffer, and uses interrupts to feed the data to the UART.)

  • Yes, @DrAzzy's right. fs.read will block, but when you send data it's stuck in an output buffer. That buffer is 128 characters, so as long as you send less than that, Espruino won't block.

    I don't think there are any functions to tell how full that output buffer is though, so you'll need to use setTimeout between calls to Serial.write to make sure you don't send data faster than the UART can take it.

    eg.

    function sendData() {
      var moreData = getMoreData();
      Serial1.write(moreData);
      setTimeout(sendData, moreData.length*10000/baudRate);
    }
    
  • There is a nice event in NodeMCU (the LUA-version for ESP8266).
    I've no idea, could it be done/helpful/whatever in Espruino, but you never know :-)

    uart:on("sent",function(evt)
          doSomething(evt) end
        end)
    
  • Hmm - yes, it's possible something like that could be implemented... Node.js has the drain event that could be similar. Serial.write could return false if the output buffer got over halfway full, and then the drain event could be called.

    I guess doing it that way would mean you could just pipe data to Serial.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

internal idle in uart.write / fs.read ?

Posted by Avatar for JumJum @JumJum

Actions