You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • @PeterS, you get the syntax error info also on the right hand side - in the editor pane. I guess you noticed that by now. Sorry that we did not think of the issue entering the example in the console (right hand side). What you also may have found out is that you can enter everything on the left side, it is just not as convenient. For given DHTH22 sensor example, you would have to enter on the left side following code before you enter the example code (with lines 3..40 being the non-minified, source code of the DHTD22 module from http://www.espruino.com/modules/DHT22.js­) :

    Modules.addCached("DHTD22",function(){
    
    function DHT22(pin) {
      this.pin = pin;
    }
    
    DHT22.prototype.read = function (cb, n) {
      if (!n) n=10;
      var d = ""; 
      var ht = this;
      pinMode(ht.pin); // set pin state to automatic
      digitalWrite(ht.pin, 0);
      this.watch = setWatch(function(t) {
        d+=0|(t.time-t.lastTime>0.00005);
      }, ht.pin, {edge:'falling',repeat:true} );
      setTimeout(function() {pinMode(ht.pin,'input_pullup');},1);
      setTimeout(function() {
        clearWatch(ht.watch);
        delete ht.watch;
        var cks = 
            parseInt(d.substr(2,8),2)+
            parseInt(d.substr(10,8),2)+
            parseInt(d.substr(18,8),2)+
            parseInt(d.substr(26,8),2);
        if (cks&&((cks&0xFF)==parseInt(d.substr(34,­8),2))) {
          cb({ 
            raw : d,
            rh : parseInt(d.substr(2,16),2)*0.1,
            temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[1­8])
          });
        } else {
          if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
          else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1});
        }
      }, 50);
    };
    
    exports.connect = function(pin) {
        return new DHT22(pin);
    };
    
    });
    

    ...because something like this happens when you upload your code on the right hand side with the Upload to Board button:

    The upload looks *FIRST* through your code (with regular expression) to find all require.("...") patterns. For each "...." module reference, the code is pulling from internet or local modules folder and 'shoved the down the throat' of Espruino over the same channel as when you enter stuff on the left side. As *SECOND AND LAST*, the actual application code is uploaded - again - over the same channel.

    You just cannot obviously see that because before sending any code, the uploader turns echo in console of and turns it on again after completion of upload. But when you use the Up Arrow key in the console (left hand side) after an upload just happened, you see the last complete JavaScript expression/line that was uploaded... which proves, that the uploader just did what I described above.

    (You may have read post #8 and post #18 of the conversation about simple explanation how to save code that espruino run on start? which explains these things in more detail. - Luckily, the complexity of sequencing of nonblocking/asynchronous thins or callback-hell where Espruino moves on in the code before things are done is made easy: [Promise]()s is available on Espruino and makes writing event driven applications with Espruino even more a breeze.)

    To complete the exercise of this post - proof that we can enter all in the left side and succeed - we paste now the example in the left hand side (for convenience the example code is repeated here):

    var ow = new OneWire(A1);
    var sensor = require("DS18B20").connect(ow);
    setInterval(function() {
      sensor.getTemp(function (temp) {
        console.log("Temp is "+temp+"°C"); 
      });
    }, 1000);
    

    There is no real mystery, just pur(e)3.co.uk smartness... 'pure smartness' - I just made that up. No clue where pur in http://pur3.co.uk stands for... may be IT IS pure - with the E facing backwards as some do when obfuscating their password or user id... Cats puRR, may be in 3(D)...)... only @Gordon may shed some light on pur3... if he thinks we can handle it... sorry: ...if I can handle it... ;-)

About

Avatar for allObjects @allObjects started