core/utils.js
function getEspruinoPrompt(callback) {
    if (Espruino.Core.Terminal!==undefined &&
        Espruino.Core.Terminal.getTerminalLine()==">") {
      console.log("Found a prompt... great!");
      return callback();
    }

    var receivedData = "";
    var prevReader = Espruino.Core.Serial.startListening(function (readData) {
      var bufView = new Uint8Array(readData);
      for(var i = 0; i < bufView.length; i++) {
        receivedData += String.fromCharCode(bufView[i]);
      }
      if (receivedData[receivedData.length-1] == ">") {
        if (receivedData.substr(-6)=="debug>") {
          console.log("Got debug> - sending Ctrl-C to break out and we'll be good");
          Espruino.Core.Serial.write('\x03');
        } else {
          if (receivedData == "\r\n=undefined\r\n>")
            receivedData=""; // this was just what we expected - so ignore it

          console.log("Received a prompt after sending newline... good!");
          clearTimeout(timeout);
          nextStep();
        }
      }
    });
    // timeout in case something goes wrong...
    var hadToBreak = false;
    var timeout = setTimeout(function() {
      console.log("Got "+JSON.stringify(receivedData));
      // if we haven't had the prompt displayed for us, Ctrl-C to break out of what we had
      console.log("No Prompt found, got "+JSON.stringify(receivedData[receivedData.length-1])+" - issuing Ctrl-C to try and break out");
      Espruino.Core.Serial.write('\x03');
      hadToBreak = true;
      timeout = setTimeout(function() {
        console.log("Still no prompt - issuing another Ctrl-C");
        Espruino.Core.Serial.write('\x03');
        nextStep();
      },500);
    },500);
    // when we're done...
    var nextStep = function() {
      // send data to console anyway...
      if(prevReader) prevReader(receivedData);
      receivedData = "";
      // start the previous reader listening again
      Espruino.Core.Serial.startListening(prevReader);
      // call our callback
      if (callback) callback(hadToBreak);
    };
    // send a newline, and we hope we'll see '=undefined\r\n>'
    Espruino.Core.Serial.write('\n');
  }
function getEspruinoPrompt(callback) {
    if (Espruino.Core.Terminal!==undefined &&
        Espruino.Core.Terminal.getTerminalLine()==">") {
      console.log("Found a prompt... great!");
      return callback();
    }

    var receivedData = "";
    var prevReader = Espruino.Core.Serial.startListening(function (readData) {
      var bufView = new Uint8Array(readData);
      for(var i = 0; i < bufView.length; i++) {
        receivedData += String.fromCharCode(bufView[i]);
      }
      if (receivedData[receivedData.length-1] == ">") {
        if (receivedData.substr(-6)=="debug>") {
          console.log("Got debug> - sending Ctrl-C to break out and we'll be good");
          Espruino.Core.Serial.write('\x03');
        } else {
          if (receivedData == "\r\n=undefined\r\n>")
            receivedData=""; // this was just what we expected - so ignore it

          console.log("Received a prompt after sending newline... good!");
          clearTimeout(timeout);
          nextStep();
        }
      }
    });
    // timeout in case something goes wrong...
    var hadToBreak = false;
    var timeout = setTimeout(function() {
      console.log("Got "+JSON.stringify(receivedData));
      // if we haven't had the prompt displayed for us, Ctrl-C to break out of what we had
      console.log("No Prompt found, got "+JSON.stringify(receivedData[receivedData.length-1])+" - issuing Ctrl-C to try and break out");
      Espruino.Core.Serial.write('\x03');
      hadToBreak = true;
      timeout = setTimeout(function() {
        console.log("Still no prompt - issuing another Ctrl-C");
        Espruino.Core.Serial.write('\x03');
        nextStep();
      },500);
    },500);
    // when we're done...
    var nextStep = function() {
      // send data to console anyway...
      if(prevReader) prevReader(receivedData);
      receivedData = "";
      // start the previous reader listening again
      Espruino.Core.Serial.startListening(prevReader);
      // call our callback
      if (callback) callback(hadToBreak);
    };
    // send a newline, and we hope we'll see '=undefined\r\n>'
    Espruino.Core.Serial.write('\n');
  }
function getEspruinoPrompt ( callback ) { if ( Espruino . Core . Terminal !== undefined && Espruino . Core . Terminal . getTerminalLine ( ) == ">" ) { console . log ( "Found a prompt... great!" ) ; return callback ( ) ; } var receivedData = "" ; var prevReader = Espruino . Core . Serial . startListening ( function ( readData ) { var bufView = new Uint8Array ( readData ) ; for ( var i = 0 ; i < bufView . length ; i ++ ) { receivedData += String . fromCharCode ( bufView [ i ] ) ; } if ( receivedData [ receivedData . length - 1 ] == ">" ) { if ( receivedData . substr ( - 6 ) == "debug>" ) { console . log ( "Got debug> - sending Ctrl-C to break out and we'll be good" ) ; Espruino . Core . Serial . write ( '\x03' ) ; } else { if ( receivedData == "\r\n=undefined\r\n>" ) receivedData = "" ; // this was just what we expected - so ignore it console . log ( "Received a prompt after sending newline... good!" ) ; clearTimeout ( timeout ) ; nextStep ( ) ; } } } ) ; // timeout in case something goes wrong... var hadToBreak = false ; var timeout = setTimeout ( function ( ) { console . log ( "Got " + JSON . stringify ( receivedData ) ) ; // if we haven't had the prompt displayed for us, Ctrl-C to break out of what we had console . log ( "No Prompt found, got " + JSON . stringify ( receivedData [ receivedData . length - 1 ] ) + " - issuing Ctrl-C to try and break out" ) ; Espruino . Core . Serial . write ( '\x03' ) ; hadToBreak = true ; timeout = setTimeout ( function ( ) { console . log ( "Still no prompt - issuing another Ctrl-C" ) ; Espruino . Core . Serial . write ( '\x03' ) ; nextStep ( ) ; } , 500 ) ; } , 500 ) ; // when we're done... var nextStep = function ( ) { // send data to console anyway... if ( prevReader ) prevReader ( receivedData ) ; receivedData = "" ; // start the previous reader listening again Espruino . Core . Serial . startListening ( prevReader ) ; // call our callback if ( callback ) callback ( hadToBreak ) ; } ; // send a newline, and we hope we'll see '=undefined\r\n>' Espruino . Core . Serial . write ( '\n' ) ; }
Try and get a prompt from Espruino - if we don't see one, issue Ctrl-C
and hope it comes back. Calls callback with first argument true if it
had to Ctrl-C out
Try and get a prompt from Espruino - if we don t see one issue Ctrl - C and hope it comes back . Calls callback with first argument true if it had to Ctrl - C out