-
Notifications
You must be signed in to change notification settings - Fork 1
/
extendingDevice.js
47 lines (38 loc) · 1002 Bytes
/
extendingDevice.js
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
// @flow
/* eslint-disable no-console */
import { Bus, Device } from '../src';
import type { ByteType, WordType } from '../src/types';
class WeatherSensor extends Device {
constructor(bus: Bus) {
super(bus, 0x72);
}
writeConfig(config: ByteType) {
return this.writeByte(0x24, config);
}
readTemperature() {
return this.readWord(0x50);
}
readPressure() {
return this.readWord(0x52);
}
}
const main = async () => {
const bus = new Bus();
const weatherSensor = new WeatherSensor(bus);
await bus.open();
await weatherSensor.writeConfig(0b1 | 0b100);
return Promise.all([
weatherSensor.readTemperature(),
weatherSensor.readPressure(),
])
.then(([temperature, pressure]: Array<WordType>) => {
console.log(`The temperature is ${temperature}°C`);
console.log(`The pressure is ${pressure}Pa`);
});
};
main()
.then(() => process.exit(0))
.catch((error: Error) => {
console.error(error.message);
process.exit(1);
});