core/utils.js
function executeExpression(expressionToExecute, callback) {
    var receivedData = "";
    var hadDataSinceTimeout = false;

    function getProcessInfo(expressionToExecute, callback) {
      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]);
        }
        // check if we got what we wanted
        var startProcess = receivedData.indexOf("< <<");
        var endProcess = receivedData.indexOf(">> >", startProcess);
        if(startProcess >= 0 && endProcess > 0){
          // All good - get the data!
          var result = receivedData.substring(startProcess + 4,endProcess);
          console.log("Got "+JSON.stringify(receivedData));
          // strip out the text we found
          receivedData = receivedData.substr(0,startProcess) + receivedData.substr(endProcess+4);
          // Now stop time timeout
          if (timeout) clearInterval(timeout);
          timeout = "cancelled";
          // Do the next stuff
          nextStep(result);
        } else if (startProcess >= 0) {
          // we got some data - so keep waiting...
          hadDataSinceTimeout = true;
        }
      });

      // when we're done...
      var nextStep = function(result) {
        // start the previous reader listing again
        Espruino.Core.Serial.startListening(prevReader);
        // forward the original text to the previous reader
        if(prevReader) prevReader(receivedData);
        // run the callback
        callback(result);
      };

      var timeout = undefined;
      // Don't Ctrl-C, as we've already got ourselves a prompt with Espruino.Core.Utils.getEspruinoPrompt
      Espruino.Core.Serial.write('\x10print("<","<<",JSON.stringify('+expressionToExecute+'),">>",">")\n',
                                 undefined, function() {
        // now it's sent, wait for data
        var maxTimeout = 10; // seconds - how long we wait if we're getting data
        var minTimeout = 2; // seconds - how long we wait if we're not getting data
        var pollInterval = 500; // milliseconds
        var timeoutSeconds = 0;
        if (timeout != "cancelled")
          timeout = setInterval(function onTimeout(){
          timeoutSeconds += pollInterval/1000;
          // if we're still getting data, keep waiting for up to 10 secs
          if (hadDataSinceTimeout && timeoutSeconds<maxTimeout) {
            hadDataSinceTimeout = false;
          } else if (timeoutSeconds > minTimeout) {
            // No data yet...
            // OR we keep getting data for > maxTimeout seconds
            clearInterval(timeout);
            console.warn("No result found for "+JSON.stringify(expressionToExecute)+" - just got "+JSON.stringify(receivedData));
            nextStep(undefined);
          }
        }, pollInterval);
      });
    }

    if(Espruino.Core.Serial.isConnected()){
      Espruino.Core.Utils.getEspruinoPrompt(function() {
        getProcessInfo(expressionToExecute, callback);
      });
    } else {
      console.error("executeExpression called when not connected!");
      callback(undefined);
    }
  }
function executeExpression(expressionToExecute, callback) {
    var receivedData = "";
    var hadDataSinceTimeout = false;

    function getProcessInfo(expressionToExecute, callback) {
      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]);
        }
        // check if we got what we wanted
        var startProcess = receivedData.indexOf("< <<");
        var endProcess = receivedData.indexOf(">> >", startProcess);
        if(startProcess >= 0 && endProcess > 0){
          // All good - get the data!
          var result = receivedData.substring(startProcess + 4,endProcess);
          console.log("Got "+JSON.stringify(receivedData));
          // strip out the text we found
          receivedData = receivedData.substr(0,startProcess) + receivedData.substr(endProcess+4);
          // Now stop time timeout
          if (timeout) clearInterval(timeout);
          timeout = "cancelled";
          // Do the next stuff
          nextStep(result);
        } else if (startProcess >= 0) {
          // we got some data - so keep waiting...
          hadDataSinceTimeout = true;
        }
      });

      // when we're done...
      var nextStep = function(result) {
        // start the previous reader listing again
        Espruino.Core.Serial.startListening(prevReader);
        // forward the original text to the previous reader
        if(prevReader) prevReader(receivedData);
        // run the callback
        callback(result);
      };

      var timeout = undefined;
      // Don't Ctrl-C, as we've already got ourselves a prompt with Espruino.Core.Utils.getEspruinoPrompt
      Espruino.Core.Serial.write('\x10print("<","<<",JSON.stringify('+expressionToExecute+'),">>",">")\n',
                                 undefined, function() {
        // now it's sent, wait for data
        var maxTimeout = 10; // seconds - how long we wait if we're getting data
        var minTimeout = 2; // seconds - how long we wait if we're not getting data
        var pollInterval = 500; // milliseconds
        var timeoutSeconds = 0;
        if (timeout != "cancelled")
          timeout = setInterval(function onTimeout(){
          timeoutSeconds += pollInterval/1000;
          // if we're still getting data, keep waiting for up to 10 secs
          if (hadDataSinceTimeout && timeoutSeconds<maxTimeout) {
            hadDataSinceTimeout = false;
          } else if (timeoutSeconds > minTimeout) {
            // No data yet...
            // OR we keep getting data for > maxTimeout seconds
            clearInterval(timeout);
            console.warn("No result found for "+JSON.stringify(expressionToExecute)+" - just got "+JSON.stringify(receivedData));
            nextStep(undefined);
          }
        }, pollInterval);
      });
    }

    if(Espruino.Core.Serial.isConnected()){
      Espruino.Core.Utils.getEspruinoPrompt(function() {
        getProcessInfo(expressionToExecute, callback);
      });
    } else {
      console.error("executeExpression called when not connected!");
      callback(undefined);
    }
  }
function executeExpression ( expressionToExecute , callback ) { var receivedData = "" ; var hadDataSinceTimeout = false ; function getProcessInfo ( expressionToExecute , callback ) { 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 ] ) ; } // check if we got what we wanted var startProcess = receivedData . indexOf ( "< <<" ) ; var endProcess = receivedData . indexOf ( ">> >" , startProcess ) ; if ( startProcess >= 0 && endProcess > 0 ) { // All good - get the data! var result = receivedData . substring ( startProcess + 4 , endProcess ) ; console . log ( "Got " + JSON . stringify ( receivedData ) ) ; // strip out the text we found receivedData = receivedData . substr ( 0 , startProcess ) + receivedData . substr ( endProcess + 4 ) ; // Now stop time timeout if ( timeout ) clearInterval ( timeout ) ; timeout = "cancelled" ; // Do the next stuff nextStep ( result ) ; } else if ( startProcess >= 0 ) { // we got some data - so keep waiting... hadDataSinceTimeout = true ; } } ) ; // when we're done... var nextStep = function ( result ) { // start the previous reader listing again Espruino . Core . Serial . startListening ( prevReader ) ; // forward the original text to the previous reader if ( prevReader ) prevReader ( receivedData ) ; // run the callback callback ( result ) ; } ; var timeout = undefined ; // Don't Ctrl-C, as we've already got ourselves a prompt with Espruino.Core.Utils.getEspruinoPrompt Espruino . Core . Serial . write ( '\x10print("<","<<",JSON.stringify(' + expressionToExecute + '),">>",">")\n' , undefined , function ( ) { // now it's sent, wait for data var maxTimeout = 10 ; // seconds - how long we wait if we're getting data var minTimeout = 2 ; // seconds - how long we wait if we're not getting data var pollInterval = 500 ; // milliseconds var timeoutSeconds = 0 ; if ( timeout != "cancelled" ) timeout = setInterval ( function onTimeout ( ) { timeoutSeconds += pollInterval / 1000 ; // if we're still getting data, keep waiting for up to 10 secs if ( hadDataSinceTimeout && timeoutSeconds < maxTimeout ) { hadDataSinceTimeout = false ; } else if ( timeoutSeconds > minTimeout ) { // No data yet... // OR we keep getting data for > maxTimeout seconds clearInterval ( timeout ) ; console . warn ( "No result found for " + JSON . stringify ( expressionToExecute ) + " - just got " + JSON . stringify ( receivedData ) ) ; nextStep ( undefined ) ; } } , pollInterval ) ; } ) ; } if ( Espruino . Core . Serial . isConnected ( ) ) { Espruino . Core . Utils . getEspruinoPrompt ( function ( ) { getProcessInfo ( expressionToExecute , callback ) ; } ) ; } else { console . error ( "executeExpression called when not connected!" ) ; callback ( undefined ) ; } }
Return the value of executing an expression on the board
Return the value of executing an expression on the board