1 | const Board = require("./board");
|
2 | const Emitter = require("events");
|
3 | const { toFixed } = require("./fn");
|
4 |
|
5 | const 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 |
|
15 | toPressure: {
|
16 | value(value) {
|
17 |
|
18 |
|
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 |
|
32 | toPressure: {
|
33 | value(value) {
|
34 |
|
35 |
|
36 | const inches = (value / 4) / 3377;
|
37 | const output = inches * 3.39;
|
38 |
|
39 |
|
40 |
|
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 |
|
54 | toPressure: {
|
55 | value(value) {
|
56 |
|
57 |
|
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 |
|
71 | toPressure: {
|
72 | value(value) {
|
73 |
|
74 |
|
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 |
|
88 | toPressure: {
|
89 | value(value) {
|
90 |
|
91 |
|
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 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | return toFixed(value / 1000, 4);
|
116 | }
|
117 | }
|
118 | },
|
119 | };
|
120 |
|
121 | Controllers.BMP085 = Controllers.BMP180;
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | class 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 |
|
191 | if (!!process.env.IS_TEST_MODE) {
|
192 | Barometer.Controllers = Controllers;
|
193 | Barometer.purge = function() {};
|
194 | }
|
195 |
|
196 | module.exports = Barometer;
|