You are reading a single comment by @possmann 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.

About

Avatar for possmann @possmann started