-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
150 lines (140 loc) · 3.31 KB
/
index.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { Gpio } = require("onoff");
const getValue = Symbol("getValue");
const bin2dec = Symbol("bin2dec");
const isArray = Symbol("isArray");
const format = Symbol("format");
module.exports = class Max6675 {
/**
*Creates an instance of Max6675.
* @author bubao
* @param {number} cs Chip select,BCM GPIO
* @param {number} sck CMOS clock,BCM GPIO
* @param {number | number[]} so Serial data output,BCM GPIO
* @param {number} untit default 1,1:"°C",2:"°F".
*/
constructor(cs, sck, so, unit = 1) {
this.cs = cs;
this.sck = sck;
this.so = this[isArray](so) ? so : typeof so === "number" ? [so] : [];
this.unit = unit;
if (this.cs && this.sck && this.so && this.unit)
this.setPin(this.cs, this.sck, this.so, this.unit);
process.on("SIGINT", () => this.stop());
}
[isArray](obj) {
return Object.prototype.toString.call(obj) == "[object Array]";
}
stop() {
if (this.cs) {
this.cs.writeSync(0);
this.cs.unexport();
}
if (this.sck) {
this.sck.writeSync(0);
this.sck.unexport();
}
if (this.so)
this.so.map(item => {
item.writeSync(0);
item.unexport();
});
process.exit();
}
[getValue]() {
this.SCK.writeSync(1);
const value = this.SO.map(item => item.readSync());
this.SCK.writeSync(0);
return value;
}
[bin2dec]() {
let arr = [];
let value = [];
for (let i = 11; i > -1; --i) {
arr = this[getValue]().map((item, index) => {
value[index] = (value[index] || 0) + item * Math.pow(2, i);
return value[index];
});
}
return arr;
}
/**
* @description set pins
* @author bubao
* @param {number} cs Chip select,BCM GPIO
* @param {number} sck CMOS clock,BCM GPIO
* @param {number | array} so Serial data output,BCM GPIO
* @param {number} unit default 1 ,1:"°C",2:"°F".
* @returns this
*/
setPin(cs = this.cs, sck = this.sck, so, unit = this.unit) {
this.cs = cs;
this.sck = sck;
this.unit = unit;
this.so = this[isArray](so)
? so
: typeof so === "number"
? [so]
: this.so;
if (this.so.length === 0) {
console.error("You must assign a value to so!");
this.stop();
} else {
this.CS = new Gpio(this.cs + "", "low");
this.SCK = new Gpio(this.sck + "", "low");
this.SO = this.so.map(item => new Gpio(item + "", "in"));
return this;
}
}
/**
* @description delay
* @author bubao
* @param {number} ms
* @returns Promise<any>
*/
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
[format]() {
switch (this.unit) {
case 1:
return {
temp: this[bin2dec]().map(v => (v * 0.25).toFixed(2)),
unit: "°C"
};
case 2:
return {
temp: this[bin2dec]().map(v =>
((v * 0.25 * 9) / 5 + 32).toFixed(2)
),
unit: "°F"
};
default:
return {
temp: this[bin2dec](),
unit: ""
};
}
}
/**
* @description read temp
* @author bubao
* @returns {{temp: string[],unit: string}}
*/
readTemp() {
if (!(this.CS && this.SCK && this.SO)) return;
this.CS.writeSync(0);
this.CS.writeSync(1, 200);
this.CS.writeSync(0);
this[getValue]();
const results = this[format]();
const error_tc = this[getValue]();
this.CS.writeSync(1);
let error = 0;
error_tc.forEach(element => {
if (element !== 0) error += 1;
});
results.error_tc = error_tc;
if (error !== 0) return { temp: [], unit: "", error_tc };
return results;
}
};