3. For the third step, we tidy up the module code with some (extra) documentation.
It is very useful to include in the module code - as comment - some terse documentation about the usage of the module, see example in DS18B20 temperature sensor module. The embedded module documentation/information is at the beginning in block comment.
The usage example is taken from current code. The code to be and to be used as a module -
lines 5..65 - looks like this:
// TemperatureDevTest2.js
require("DS18B20"); // used by the dynamically loaded module
Modules.addCached("Temperature", function() {
/* Copyright (c) 2017 ... */
/*
Module to wrap application of DS18B20 temp sensor module in Temperature class
` ` `
var oneWire = new OneWire(B8);
var Temperature = require("Temperature");
var ts1 = new Temperature
( "office" // name / location of temperatre sensor 1
, oneWire
, null // addr on one-wire currently not implemented / used
, 5000 // every 5 secs make a read (to keep it not booring)
, "F" // preferred unit is Fahrenheit
);
var temp = 0;
setTimeout() { function() {
var tempTemp = ts1.getTemp(); // get temperature,...
if (tempTemp != temp) { // ... if changed from last time,...
temp = tempTemp; // ... hold on to it and...
console.log(new Date() + ": " + temp; // ...print it;...
} }, 10000); // ...every ten seconds // ...do this every 10 seconds
` ` `
*/
// Temperature 'class'/prototype definition
var Temperature = function // module (class), for multiple instances
( name // name hinting room / location of sensor\
, oneWire // one-wire; for example: new OneWire(B8);
, addr // address on the one-wire (required for multiples)
, interval // interval in milliseconds of measurements
, preferred // optional, unit - "C" or "F" (default and not F is C)
) {
// set givens
this.name = name;
this.oneWire = oneWire;
this.addr = addr;
this.interval = interval;
this.preferred = (preferred==="F") ? "F" : "C";
this.enabled = (typeof enabled === "undefined") || enabled;
this.t = (this.preferred==="C") ? 0 : 32; // set current to 'frozen'
this.intervalId = null; // later also used as indicator for enabled
this.sensor = null; // laster also used as indicatore for connected
// get going
this.sensor = require("DS18B20").connect(this.oneWire);
this.intervalId = setInterval(function(_this) {
_this.sensor.getTemp(function(t) {
_this.t = t;
});
}, this.interval, this);
};
Temperature.prototype.getTemp = function
( unit // optional, unit "F" or "C", default "C"
) {
var u = ((typeof unit !== "undefined") && (unit === "F")) ? "F" : "C";
var d = (u === "F")
? (this.t * 1.8) + " Fahrenheit"
: this.t + " Celsius";
return d;
};
exports = Temperature;
});
// usage
// setup oneWire
var oneWire = new OneWire(B8);
// setup 1st temperature sensor ts1
var Temperature = require("Temperature");
var ts1 = new Temperature
( "office" // name / location of temperatre sensor 1
, oneWire
, null // addr on one-wire currently not implemented / used
, 5000 // every 5 secs make a read (to keep it not booring)
, "F" // preferred unit is Fahrenheit
);
// for sample's sake, do everhthing deferred for more
// than 5 seconds in order to have value(s) to display
setTimeout(function(){
// get temp in preferred unit
console.log(ts1.getTemp());
},6000);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
3. For the third step, we tidy up the module code with some (extra) documentation.
It is very useful to include in the module code - as comment - some terse documentation about the usage of the module, see example in DS18B20 temperature sensor module. The embedded module documentation/information is at the beginning in block comment.
The usage example is taken from current code. The code to be and to be used as a module -
lines 5..65 - looks like this: