You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • The structure looks pretty tidy - although I'd really recommend doing:

    Sensor.prototype.getSensorType = function ...
    Sensor.prototype.getSensorAddress = function ...
    // etc
    

    Instead of your Sensor.prototype = {. It's probably more personal opinion, but it seems more pleasant than having to redefine constructor.

    You've also defined getters like getSensorAddress, which is fine - however you then do a = this.getSensorAddress;. That'll return the function itself, rather than executing it and getting the result... You might also want to put a var before a, so a isn't defined as a global variable.

    In your code getSensorResult returns immediately, so while you get a value in getSensorReading you're not actually returning it. For that, you'd need to use a callback:

      getSensorResult:function (callback) {
        // ...
        setTimeout(function (e) { callback(that.getSensorReading()); }, w);
      },
    

    Then you can use it with:

    ph.getSensorResult(function(value) {
      console.log(value);
    });
    

    Hope that helps!

  • Hi @Gordon The reason why I chose Sensor.prototype = { was, at the time, I didn't care for the repetitiveness of writing Sensor.prototype. However, thats nothing that copy and paste can't fix :). As you recommended, I switched to Sensor.prototype.getSensorType = function. I can appreciate the readability and the code seems to flow a little better and makes sense to me.

  • @Gordon Its been 4 months since I did anything with my project. My computer crashed and I lost all of my code referring to this post. I copied and pasted my code from this post and tried to implement your suggestions again.

    I am trying to pass in the value of this.address and execute the functions instead of returning the function it self.

    However, as you explained, I am returning the function itself rather than executing it and getting the result. For example, in

    Sensor.prototype.updateResTemp
    
    console.log(a + " " + c);
    

    returns:

    function () {
    return this.address; //Get Sensor address
    } T,19.5

    Code from this thread and memory:

    I2C1.setup({scl:b6, sda:b7});
    
    function Sensor (theType, theAddress) {
      this.type = theType;   //i.e. PH
      this.address = theAddress;  //i2c Address
      this.sensorResult = 0; //Store sensor result
      this.cmdTable = {
        "Calibrate" : {  //List of Calibration commands and timeout value.
          "clear" : { "cmd" : "Cal,Clear",      "wait" : 300  },
          "mid"   : { "cmd" : "Cal,mid,7.00",   "wait" : 1300 },
          "low"   : { "cmd" : "Cal,low,4.00",   "wait" : 1300 },
          "high"  : { "cmd" : "Cal,high,10.00", "wait" : 1300 },
          "query" : { "cmd" : "Cal,?",          "wait" : 300  }
        },
        "Information" : {  //Device Information
        },
        "LED" : {  //Enable / Disable or Query the LEDs
          "L0" : { "cmd" : "L,0", "wait" : 300 },
          "L1" : { "cmd" : "L,1", "wait" : 300 },
          "L?" : { "cmd" : "L,?", "wait" : 300 }
        },
        "Reading" : {  //Takes a single reading
          "R" : { "cmd" : "R", "wait" : 1000 } //Takes a single temperature compensated reading
        },
        "Serial"      : {  //Switch back to UART mode
        },
        "Sleep"       : {  //Enter low power sleep mode
        },
        "Status"      : {  //Retrieve status information
        },
        "Temperature" : {  //Set or Query the temperature compensation
          "T"  : { "cmd" : "T,19.5", "wait" : 300 },  //Where the temperature is any value; floating point, or int, in ASCII form
          "T?" : { "cmd" : "T,?",   "wait" : 300 }  //Query the set temerature
        },
        "Factory"     : {  //Factory reset
        },
      };
    }
    
    Sensor.prototype.getSensorType = function () {
      return this.type; //Get Sensor type
    };
    
    Sensor.prototype.getSensorAddress = function () {
      return this.address; //Get Sensor address
    };
    
    Sensor.prototype.getSensorReading = function() {
        var a = this.getSensorAddress;
        var d = I2C1.readFrom(a, 7);
        return d;
    };
    
    Sensor.prototype.getSensorResult = function () {
        var a = this.address;
        var c = this.cmdTable.Reading.R.cmd;
        var w = this.cmdTable.Reading.R.wait;
        var that = this;
    
        //I2C1.writeTo(a, c);
    
        setTimeout(function (e) { callback(that.getSensorReading()); }, w);
    };
    
    Sensor.prototype.storeSensorResult = function () {
    };
    
    Sensor.prototype.updateResTemp = function (temp) {
        var a = this.getSensorAddress;
        var c = this.cmdTable.Temperature.T.cmd;
        var w = this.cmdTable.Reading.R.wait;
        var that = this;
    
        console.log(a + " " + c);
    
        //I2C1.writeTo(99, "T,19.5");
    
        I2C1.writeTo(a, c);
        setTimeout(function (e) { that.getSensorReading(); }, w);
    };
    
    var ph = new Sensor("ph", 0x63);
    
    ph.updateResTemp(90.5);
    
About

Avatar for Gordon @Gordon started