• Took a quick look at the MFRC522 module -http://www.espruino.com/modules/MFRC522­.js. Unfortunately, the 'class' - or the constructor function is not exposed so that you could access the prototype (easily) and follow the instructions you mention by links in your last post how to overwrite... BUT, there is a way, and Espruio - aka @Gordon - implemented the code to access the prototype when you have an instance.

    In the module you find the this code:

    exports.connect = function(spi, cs) {
      var m = new MFRC522(spi, cs);
      m.init();
      return m;
    };
    

    connect function which says new to the constructor function

    function MFRC522(spi, cs) {
      this.spi = spi;
      this.cs = cs;
    }
    

    and returns m - an instance of 'MFRC522'.

    To get to the prototype of the constructor function, you use the instance that mfrc522Module.connect returns and Object.getPrototypeOf(instance);

    The complete code:

    var mfrc522Module = require("MFRC522");
    var mfrc522Instance = mfrc522Module.connect(...);
    var mfr522Prototype = Object.getPrototypeOf(mfrc522Instance);
    mfr522Prototype.req = function(data) {  
      this.w(R.COMMAND, PCD.IDLE);
      this.w(R.COMIEN, 0x80 | 0x77);
      this.w(R.COMIRQ, 0x7F); // clear IRQs
      this.w(R.FIFOLEVEL, 0x80); // clear fifo
      this.w(R.FIFODATA, data); // write request
      this.w(R.COMMAND, PCD.TRANSCIEVE);
      this.w(R.BITFRAMING, this.r(R.BITFRAMING)|0x80); // start TX
      //while (!(this.r(R.COMIRQ)&0x31)); // wait doesn't seem to be needed
      this.w(R.BITFRAMING, this.r(R.BITFRAMING)&~0x80); // stop TX
      var err = this.r(R.ERROR);
      if (err) throw new Error("MFRC522 Request Error "+err);
      var fifo = this.r(R.FIFOLEVEL);
      return this.ra(R.FIFODATA,fifo);
    };
    

    I intentionally separated module from connect to show that module is an object as well which has only one method (or function) - .connect(...) to create and return a 'connected' representation of the sensor MRF522.

    In the code above - which is an exact copy out of the module - you can apply just any change you want...

    Now some other good news about JavaScript: you do not need to overwrite the prototype, you just can 'overwrite' by attaching a method (function) to the instance:

    var mfrc522Module = require("MFRC522");
    var mfrc522Instance = mfrc522Module.connect(...);
    mfrc522Instance.req = function(data) {  
      this.w(R.COMMAND, PCD.IDLE);
      this.w(R.COMIEN, 0x80 | 0x77);
      .....
      .....
      .....
      return this.ra(R.FIFODATA,fifo);
    };
    

    Depending how the module was minified, none of the above may work because some of the references / values may have gotten a different name or are gone completely. We though can check that by looking at the minified version - http://www.espruino.com/modules/MFRC522.­min.js.

    The relevant code section is this:

    ...b.prototype.req=function(a){this.w(2,­0);this.w(4,247);this.w(8,127);this.w(20­,128);this.w(18,a);this.w(2,12);this.w(2­6,this.r(26)|128);this.w(26,this.r(26)&-­129);if(a=this.r(12))throw Error("MFRC522 Request Error "+a);a=this.r(20);return this.ra(18,a)};...
    

    You recognize the ...prototype.req=function(... part easily, and unfortunately it confirms - compared with the un-minified source - that the constants of the constant object R are folded into the code and therefore constant object R could be dropped to save variable space / memory and thus has not have to be and is not anymore present...

    Again: BUT, do not give up...

    Because the module mechanism is available locally as well, copy the non-minified code of the module into your Espruino Web IDE sandbox under the name MFRC522Robin.js. When you go to Settings - Project of the Web IDE, you can specify a sandbox 'home' directory by clicking the Select Directory for Sandbox and make a new one - I usually call it something like _sbx in a directory of my choice. The directory is created with multiple sub directories for different purposes. One is for local modules. In that modules folder you copy the module and apply changes to your taste... Of course, because we named the module file copy MFRC522Robin.js, you have to require it with the modified name

    var mfrc522Module = require("MFRC522Robin");
    

    I guess from here you can take it where you want it to have to be... :}

    Btw, I got it running pretty nicely myself a while ago as you can take from the conversation save() with modules where .connect() initializes device (such as RFID reader MFRC522).

  • Thr 2017.01.26

    @allObjects post #8

    Your detailed response was well written and was easy to grasp. One is able to observe you have a complete understanding of the subject matter and you do have a knack for the ability to get into the mind of the reader. Following along was easy, and the reader will appreciate that, spending the additional time to format the content in order to make it an easy read.

    Bravo, Well Done.

    You might consider adding the post #8 section as a followup to your link in the same post.

    but wait, there's more . . . post #9

    Appreciate the observation on recognizing the 'Gem' statement from post #5 I firmly believe that which I stated.

About

Avatar for allObjects @allObjects started