-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitThermistor.js
More file actions
90 lines (76 loc) · 2.29 KB
/
BitThermistor.js
File metadata and controls
90 lines (76 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
+(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
var Module = scope.Module;
var BoardEvent = scope.BoardEvent;
var proto;
var ThermistorEvent = {
MESSAGE: 'message'
};
function Thermistor(board, analogPinNumber) {
Module.call(this);
this._board = board;
this._pinNumber = Number(analogPinNumber);
this._messageHandler = onMessage.bind(this);
}
function onMessage(event) {
var pin = event.pin;
if (this._pinNumber !== pin.analogNumber) {
return false;
}
this.emit(ThermistorEvent.MESSAGE, pin.value);
}
Thermistor.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Thermistor
},
state: {
get: function () {
return this._state;
},
set: function (val) {
this._state = val;
}
}
});
proto.measure = function (callback) {
var _this = this;
this._board.enableAnalogPin(this._pinNumber);
if (typeof callback !== 'function') {
callback = function () {};
}
this._callback = function (val) {
callback(_this.parserVal(val));
};
this._state = 'on';
this._board.on(BoardEvent.ANALOG_DATA, this._messageHandler);
this.addListener(ThermistorEvent.MESSAGE, this._callback);
};
proto.off = function () {
this._state = 'off';
this._board.disableAnalogPin(this._pinNumber);
this._board.removeListener(BoardEvent.ANALOG_DATA, this._messageHandler);
this.removeListener(ThermistorEvent.MESSAGE, this._callback || function () { });
this._callback = null;
};
/**
* https://github.com/BPI-STEAM/BPI-BIT-Arduino-IDE/tree/master/example/Temperature
*/
proto.parserVal = function (val) {
var voltagePower = 3.3;
var Rs = 5.1; // Sampling Resistance is 5.1K ohm
var B = 3950;
var T = 273.15 + 25; // Normal Temperature Parameters
var R1 = 10; // Normal Temperature Resistance (K ohm)
var voltageValue = val * voltagePower;
var Rt = ((voltagePower - voltageValue) * Rs) / voltageValue;
var newVal = ((T * B) / (B + T * Math.log10(Rt / R1))) - 273.15;
return Math.round(newVal * 100) / 100;
};
scope.module.Thermistor = Thermistor;
}));