Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending inline module fails silently on command line #6

Open
andrewn opened this issue May 13, 2015 · 5 comments
Open

Sending inline module fails silently on command line #6

andrewn opened this issue May 13, 2015 · 5 comments

Comments

@andrewn
Copy link

andrewn commented May 13, 2015

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
Copy link
Member

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: espruino/Espruino#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
Copy link
Author

andrewn commented May 13, 2015

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
Copy link
Member

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

@andrewn
Copy link
Author

andrewn commented May 14, 2015

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants