Is it not using inheritance?

Posted on
  • I don't even have my board yet, but when it arrives I plan to extend the DS18B20 module.

    Now I'm looking at the source code at http://www.espruino.com/modules/DS18B20.js and I'm wondering if when I create multiple thermometers:

    var sensor1 = require("DS18B20").connect(A1);
    
    var sensor2 = require("DS18B20").connect(A2);
    
    var sensor3 = require("DS18B20").connect(A13);
    

    Is it somehow wasting memory because each object have it's own copy of getTemp() method? Or is that not the case and that's the way to do it.

  • Hi. That's a very good point. Espruino can use inheritance, but as you say I'm not using it in the modules. As it is, the 'getTemp' method will be redefined for each call to connect.

    It'd be a really good idea to re-write them in such a way that the functions could be re-used. Could you paste up what you think a better version of the DS18B20 module would be? It'd be nice to re-arrange the existing modules while there aren't that many of them :)

  • The main thing I want to change with this module is to make it work with multiple thermometers connected to single 1-Wire bus. It would also be nice to support the programmable alerts on the sensor, but I'm not sure if I know how to do it at the moment. It's something I want to play around with and maybe I'll work it out if I'm lucky.

    As to the modules in general - is it the same system as in node.js?

  • Yes, that'd be good - although it'd be nice if you could add that to the existing library? Alerts should be possible too (using setWatch if it works by pulling the line low?).

    The modules themselves should be a cut down version of node.js (using the exports var) so you'd hope that most existing (simple) modules will just work with Espruino. As far as distribution goes, for now that is handled by the Espruino website via the Web IDE (and when I get time I plan to add support to the website for loading proper node.js modules).

    For developing yourself there's a bit of info here: http://forum.espruino.com/conversations/373/#comment3917

  • Thanks Gordon. If I come up with something good enough then naturally the plan is it's going back to Espruino (that is if you accept it).

  • Great - thanks!

  • @graf: do you have an update about this topic? I'm also interested in this sensor alarm issue.

  • Alarms weren't added, however the module was modified to allow multiple sensors to be connected to a single wire pretty easily, and it allows reading and writing of the scratchpad, so alarms can be set pretty easily.

    Having said that, you can't do the 'alarm search' to get devices with alarms set, so it's a bit useless at the moment.

    What are you trying to do though? It seems like in most cases you could implement the alarm functionality just by reading the temperature repeatedly and checking it in Espruino?

  • At the moment, that's exactly what i'm doing. Reading the temperature of every device and checking it with min/max value.

    I just browse and search for some issues about the DS18B20 library and found this topic. I thought, maybe after 7 month, there are some news. But i will follow that issue to simplify the program structure in future.

  • Actually it turns out it's a simple thing to add - I had to rush out a 1v69 release because of a serious regression, so I stuck it in.

    var ow = new OneWire(A1);
    var sensor = require("DS18B20").connect(ow);
    sensor.setAlarm(20,30)
    // then: sensor.searchAlarm() will return a list of sensor addresses that have the alarm set
    
  • sounds very nice blushed

  • @possmann sorry I got distracted by other things and never finished this module properly, but as Gordon said it shouldn't be too difficult now - unless you want to handle alarms as events, in which case I'm not able to help :)

    As a side note - if you don't need alarms and don't mind hacky solutions then you can use write/read scratchpad functions to persistently store your data in these unused registers.

  • @Gordon: i test the new version :-) and it work's very well!
    @graf: no problem, gordon did a really great job!

  • @gordon: i have a small question. the alert system work's but there is one issue i did not expect.

    Currently i use this small test code for the DS18B20 sensor:

    var ow = new OneWire(B8);
    var sensor = require("DS18B20").connect(ow);
    sensor.setAlarm(25, 30);
    
    setInterval(function() {
      console.log(sensor.getTemp(), sensor.searchAlarm());
    }, 1000);
    

    The alert for 30° works fine. If the value is more than 30°, the alert starts. But the low value is works strange. If the value is under 26°, the alert starts. I expected, if the value is lower than 25.

    Here the debug output (i delete all redundant lines)

    26.3125 []
    26.25 []
    26.1875 []
    26.125 []
    26.0625 []
    26 []
    25.9375 [
      "288728080600007a"
    ]
    

    Do you have an idea?

  • I'm afraid I don't know - I just feed those values into the DS18B20. I guess you'd have to look at the Datasheet.... I could modify the code to subtract one from the low value.

  • I guess, it's not the right way to subtract it :-) I can also handle it, in my script, that's not the problem.

    I will try it on my SparkCore again, if there is the same issue. I had a working project with the same library and i also used the alerts. I remember, that the alarms works fine... but i'm not quite sure. I will report it as soon as possible.

  • @Gordon: so now i have a comparison.

    This is the output from the SparkCore which also use the DallasTemperature library (https://github.com/tomdeboer/SparkCoreDallasTemperature).

    value 0 -> alarm off
    value 1 -> alarm on
    -----------------------------------------
    Temp for Sensor A: 26.25  ->  1
    Temp for Sensor A: 26.31  ->  1
    Temp for Sensor A: 25.56  ->  1
    Temp for Sensor A: 24.56  ->  0
    Temp for Sensor A: 23.75  ->  0
    Temp for Sensor A: 23.00  ->  0
    Temp for Sensor A: 22.31  ->  0
    Temp for Sensor A: 21.75  ->  0
    Temp for Sensor A: 21.31  ->  0
    Temp for Sensor A: 20.81  ->  1
    Temp for Sensor A: 20.44  ->  1
    Temp for Sensor A: 20.06  ->  1
    Temp for Sensor A: 19.75  ->  1
    Temp for Sensor A: 19.44  ->  1
    

    I set the temperature range between 20° and 25°. So we have the same issue. I guess it's a bug or feature of the sensor :-) It seems that the alarm recognize only floor values.

    Additionally i read the specification and found the definition:
    (http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf -> page 4 at the last paragraph)

    If the measured temperature is lower than or equal to TL or higher
    than or equal to TH, an alarm condition exists and an alarm flag is
    set inside the DS18B20

    Hmmm, but what would be the right way now? Somehow, I am not happy with a subtraction of the minimal value, but that would be a simple and quick solution.

    What do you think?

  • I don't actually see a problem with subtracting one... I'm not sure it would cause a problem anywhere

  • @Gordon: well, i'm fine with this solution. If i find something about this issue, i will inform you. I can't believe that other people doesn't have this problem neither.

  • My suspicion is that the device is only comparing the integer part of the temperature to the values.

  • i suspect that too :-(

  • Right, I've just changed this - so next time you flash your code, the alarm will happen at the correct point.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Is it not using inheritance?

Posted by Avatar for graf @graf

Actions