• ...the IDE is aware of the function

    yes, but more things happen and the context is different then when this shows up on Espruino when coming through console... The this through the console has the object / context of the 'console listener' - JS interpreter - on Espruino.

    Also, when a module is uploaded, the variable exports pointing to an empty object {} is already in the context when Espruino executes the arriving module code. In other words using exports and populating it with properties is the usual - a-la node.js - use in a module 'definition'. Assigning a new object, such as a 'class' - in object-oriented terms, is making the variable to point to a different object while loosing a previously added property... and this IS the cause (among others) that things did or do not work for you.

    I noticed

    Just performing a send creates the error.

    I assume you are not trying to send the module, do you?

    You send only your application code - that uses your modules - to Espruino board in the IDE. The module you store in the modules folder of your local sandbox. The use of the module in your application code is detected by the upload function of the IDE on the presence of require("..."). On encountering it, the upload function of the IDE goes for the module hunt. It tries to find it in your local / sandbox module folder or on the web - espruino.com/modules/... - when it is a plain file name - and at the url - when the module name is a complete url - and grabs the source code - and uploads it as source code to Espruino... and stores it in the Modules cache... During the process it may also minify it if needed and told so to save space (leave more to variable space, since minifying makes things shorter, and because Espruino runs off of the source code as is - for the most part - in its RAM, more RAM is left for variables...)

    Your application code using - for example, the GrovRelay - looks something like this:

    var GrovRelay = require("GroveRelay");
    var r = new GroveRelay([B3]);
    ...
    

    After upload, Espruino has a (global) variable r (or global.r in the global scope of its execution context that points to an instance of the GroveRelay. Since thru console global things in the Espruino execution context - like all other JavaScript functions and key words, because it is an interpreter - are accessible by just mentioning the variable name, you can enter in the console r.on(); and your relay turns on. ;)

    Now you go back to your modules, and they will work...

    Stick this code into your modules folder:

    function TestLED(pin) {
      this.pin = pin;
      pinMode(this.pin,"output");
      this.off();
    };
    TestLED.prototype.on = function() {
        this.pin.set();
    };
    TestLED.prototype.off = function() {
        this.pin.reset()
    };
    exports = TestLED;
    };
    

    Then upload the following application code:

    var TestLED = require("TestLED");
    var testLED1pin = B3;
    
    var testLED1 = new TestLED(testLED1pin);
    
    setInterval(function(){
      testLed1.on();
      setTimeout(function(){
        testLED1.off();
      },500)
    },1000);
    

    ...and you have your elaborate ;-) blinking example... blink frequency: 1Hz, duty cycle: 50%

    (should it not work... then there may be a typo... code proof read but not executed on Espruino yet...)

  • Sat 2018.09.08

    Thank you for the detailed explanation. That content confirms what I thought I understood.

    re: 'I assume you are not trying to send the module'

    I'm following this pp at:

    http://www.espruino.com/Writing+Modules

    "To test out your module, we'd suggest that you copy it verbatim into the right-hand side of the Web IDE, with the line var exports={}; at the top, and at the bottom, where you use the module, write exports.myfunction()
    instead of require('MOD123').myfunction()."

    If I understand the above correctly, I placed those fifteen lines of code between, and was able to create one instance. It was when I attempted to create the accessor, the error occurred.

    Don't have much time this evening, but I'll try your code snippets as you suggest and, as the above pp suggests.

About

Avatar for allObjects @allObjects started