You are reading a single comment by @DrAzzy and its replies. Click here to read the full conversation.
  • Modularized.

    I posted the module - does this work?

    SPI1.setup({ miso:B4, sck:B3, baud:1000000 });
    var max=require("http://drazzy.com/espruino/­MAX31855.js").connect(SPI1,C0);
    
    console.log(max.getTemp());
    
    

    Module code:

    exports.connect = function(spi,cs) {
      return new MAX31855(spi,cs);
    };
    
    function MAX31855(spi,cs) {
      this.spi=spi;
      this.cs=cs;
    }
    
    MAX31855.prototype.getTemp = function () {
      var d = SPI1.send("\0\0\0\0",C0); 
      var trtn={};
      trtn.temp=(d.charCodeAt(0)<<6 | d.charCodeAt(1)>>2)*0.25;
      if (d.charCodeAt(1) & 1)  {
        var fault = (d.charCodeAt(3) & 7);
        switch (fault) {
          case 1:
            trtn.fault="No probe detected";
          case 3:
            trtn.fault="Probe shorted to Ground";
          case 4:
            trtn.fault="Probe shorted to VCC";
          default:
            trtn.fault=fault;
        }
      }
      return trtn;
    };
    
  • @DrAzzy: just a little code review question: why do you store this.spi / this.cs inside the constructor? And why not use this.spi / this.cs in the getTemp?

    I would have expected this:

    MAX31855.prototype.getTemp = function () {
        var d = this.spi.send("\0\0\0\0", this.cs);
        ...
    }
    

    Otherwise i don't understand the constructor.

  • Thank you for putting this code into a module. I have been trying to understand OOP Javascript and just brought a book to try to wrap my head around it. Looking over this code as well as the comment from possman, It now fairly clear the way it works. Having scratched my head over this for a couple of weeks and now the light bulb in my brain turns on!

    I did try the code and it partially works. It does report temperature correctly but the error reporting does not work correctly now. It only reports case 4 under all fault conditions. I will try to debug this and repost code later today.

About

Avatar for DrAzzy @DrAzzy started