UNPKG

4.56 kBJavaScriptView Raw
1const Board = require("./board");
2const Emitter = require("events");
3const { toFixed } = require("./fn");
4
5const Controllers = {
6 MPL115A2: {
7 initialize: {
8 value(options, callback) {
9 const { Drivers } = require("./sip");
10 Drivers.get(this.board, "MPL115A2", options)
11 .on("data", ({pressure}) => callback(pressure));
12 }
13 },
14 // kPa (Kilopascals)
15 toPressure: {
16 value(value) {
17 // Pressure output in kPa explained at P. 6, Eqn. 2
18 // Typical resolution 0.15kPa from paragraph 2.2 page 3
19 return toFixed(((65 / 1023) * value) + 50, 2);
20 }
21 }
22 },
23 MPL3115A2: {
24 initialize: {
25 value(options, callback) {
26 const { Drivers } = require("./sip");
27 Drivers.get(this.board, "MPL3115A2", options)
28 .on("data", ({pressure}) => callback(pressure));
29 }
30 },
31 // kPa (Kilopascals)
32 toPressure: {
33 value(value) {
34 // formulas extracted from code example:
35 // https://github.com/adafruit/Adafruit_MPL3115A2_Library
36 const inches = (value / 4) / 3377;
37 const output = inches * 3.39;
38
39 // Page 8, Table 5
40 // Typical resolution 1.5Pa = 0.0015kPa
41 return toFixed(output, 4);
42 }
43 }
44 },
45 BMP180: {
46 initialize: {
47 value(options, callback) {
48 const { Drivers } = require("./sip");
49 Drivers.get(this.board, "BMP180", options)
50 .on("data", ({pressure}) => callback(pressure));
51 }
52 },
53 // kPa (Kilopascals)
54 toPressure: {
55 value(value) {
56 // Page 6, Table 1
57 // Typical resolution 0.01hPa = 0.001kPa
58 return toFixed(value / 1000, 3);
59 }
60 }
61 },
62 BMP280: {
63 initialize: {
64 value(options, callback) {
65 const { Drivers } = require("./sip");
66 Drivers.get(this.board, "BMP280", options)
67 .on("data", ({pressure}) => callback(pressure));
68 }
69 },
70 // kPa (Kilopascals)
71 toPressure: {
72 value(value) {
73 // Page 8, Table 2
74 // Resolution in ultra high resolution mode 0.0016hPa = 0.00016kPa
75 return toFixed(value / 1000, 5);
76 }
77 }
78 },
79 BME280: {
80 initialize: {
81 value(options, callback) {
82 const { Drivers } = require("./sip");
83 Drivers.get(this.board, "BME280", options)
84 .on("data", ({pressure}) => callback(pressure));
85 }
86 },
87 // kPa (Kilopascals)
88 toPressure: {
89 value(value) {
90 // Page 10, Table 3
91 // Typical resolution 0.18Pa = 0.00018kPa
92 return toFixed(value / 1000, 5);
93 }
94 }
95 },
96 MS5611: {
97 initialize: {
98 value(options, callback) {
99 const { Drivers } = require("./sip");
100 Drivers.get(this.board, "MS5611", options)
101 .on("data", ({pressure}) => callback(pressure));
102 }
103 },
104 toPressure: {
105 value(value) {
106 // Page 2, Table ?
107 // Resolution Over sampling ratio
108 // 0.065mbar 256
109 // 0.042mbar 512
110 // 0.027mbar 1024
111 // 0.018mbar 2048
112 // 0.012mbar 4096
113 //
114 // 0.012mbar = 1,2Pa = 0.0012kPa
115 return toFixed(value / 1000, 4);
116 }
117 }
118 },
119};
120
121Controllers.BMP085 = Controllers.BMP180;
122
123/**
124 * Barometer
125 * @constructor
126 *
127 * five.Barometer(options);
128 *
129 * five.Barometer({
130 * controller: "CONTROLLER"
131 * address: 0x00
132 * });
133 *
134 *
135 * @param {Object} options [description]
136 *
137 */
138
139class Barometer extends Emitter {
140 constructor(options) {
141 super();
142
143 Board.Component.call(
144 this, options = Board.Options(options)
145 );
146
147 Board.Controller.call(this, Controllers, options);
148
149 const freq = options.freq || 25;
150 let last = null;
151 let raw = null;
152
153 if (!this.toPressure) {
154 this.toPressure = options.toPressure || (x => x);
155 }
156
157 if (typeof this.initialize === "function") {
158 this.initialize(options, data => {
159 raw = data;
160 });
161 }
162
163 Object.defineProperties(this, {
164 pressure: {
165 get() {
166 return this.toPressure(raw);
167 }
168 }
169 });
170
171 setInterval(() => {
172 if (raw === null) {
173 return;
174 }
175
176 const data = {
177 pressure: this.pressure
178 };
179
180 this.emit("data", data);
181
182 if (this.pressure !== last) {
183 last = this.pressure;
184 this.emit("change", data);
185 }
186 }, freq);
187 }
188}
189
190/* istanbul ignore else */
191if (!!process.env.IS_TEST_MODE) {
192 Barometer.Controllers = Controllers;
193 Barometer.purge = function() {};
194}
195
196module.exports = Barometer;