espruino / espruino-tools

Node module to allow command-line access to Espruino
26 stars 5 forks source link

Sending inline module fails silently on command line #6

Open andrewn opened 9 years ago

andrewn commented 9 years ago

Hi,

I'm writing an application that uses espruinotool to send code to a Pico when the app starts.

The code I'm using is source.js in this gist . I declare the CAP1188 module but in this example I'm not using it, instead I have a setInterval at the bottom that should flash the onboard LED on/off.

I send the file to the Pico using espruinotool --verbose --port /dev/cu.usbmodem1411 /Users/andrew/Projects/oss/ucl-sensors/sensorama/espruino/source.js espruino-tools and the output is here in terminal-output.log.

There do not seem to be any errors in the log output but the LED on the Pico does not flash.

I've also tried using the --minify flag and this has the same (non) result.

Sending the code using the Espruino Web IDE flashes the LED as expected. Sending the code via the CLI to an Espruino v1.3b also flashes the LED.

Any ideas what I'm doing wrong?

gfwilliams commented 9 years ago

I'm not sure it's you. I think the problem might be that node's serial port implementation is a bit more efficient than the Web IDE's, and it sends the data to Espruino much more quickly.

There's a bit of an issue with the USB serial port right now (on Pico and Espruino) in that it can stall data coming in for around 5-10ms, but after that the host computer actually throws it away. I've got a fix planned for it as part of: https://github.com/espruino/Espruino/issues/221

In your code I'd guess the issue is that you're defining a huge function and then executing it (for the closure), and it takes enough time to execute that a lot of what comes after gets lost.

You could hackily fix it for now by applying the following patch to the EspruinoTools repo:

--- EspruinoTools/core/codeWriter.js
+++ EspruinoTools/core/codeWriter.js
@@ -30,9 +30,15 @@

       console.log("Sending... "+data);
       var full = "echo(0);\n" + data + "\necho(1);\n";
-      Espruino.Core.Serial.write(full);
-
-      if (callback) setTimeout(callback, 1000); // TODO: proper callback when send finished?
+      
+      var lines = full.split("\n");
+      function w() {
+        var l = lines.shift();
+        Espruino.Core.Serial.write(l+"\n");
+        if (lines.length>0) setTimeout(w, 10);
+        else  if (callback) setTimeout(callback, 200);
+      }
+      w();     
     };
     var sendSerial = realSendSerial;

That'll delay sending each line and should make it work fine.

... but hopefully a proper fix on the Pico side will be coming soon.

andrewn commented 9 years ago

Thanks for getting back to me so quickly!

That does send the code to the Pico, but now espruinotool sometimes does not exit. It just hangs.

gfwilliams commented 9 years ago

That's strange - is there any way to reproduce it, or is it random?

andrewn commented 9 years ago

It only happens when I send source.js as per the original gist and the CAP1188 breakout is plugged in.

I think that USB.println() is being called by the sensor and Espruino is then blocking meaning that espruinotool is not disconnecting? Adding a timeout before calling USB.println() fixes this issue.

gfwilliams commented 9 years ago

Ahh - sorry, I just read this again and realised what the issue probably is - I'd guess that the data isn't read read from USB and so Espruino blocks - in fact that could have had a lot to do with the problem in the first place. Hopefully it'll be quite an easy fix once I figure out why the data isn't getting read.