diff --git a/README.cn.md b/README.cn.md deleted file mode 100644 index f1e6a5e..0000000 --- a/README.cn.md +++ /dev/null @@ -1,121 +0,0 @@ -# Max6675 - -[![NPM version](https://img.shields.io/npm/v/max6675-raspi.svg)](https://www.npmjs.com/package/max6675-raspi) [![jaywcjlove/sb](https://jaywcjlove.github.io/sb/lang/english.svg)](README.md) - -在树莓派上使用 Node.js 驱动 Max6675 芯片,读取 K 型热偶的温度值。 - -```sh -npm i max6675-raspi --save -``` - -![Max6675](https://github.com/bubao/Max6675-Raspberry-pi-nodejs/raw/master/imgs/Max6675.png) - -## API - -### `Max6675` - -```js -const Max6675 = require("max6675-raspi"); -const CS = 4; -const SCK = 24; -const SO = [25, 12, 16, 20, 21]; -const UNIT = 1; - -const max6675 = new Max6675(CS, SCK, SO, UNIT); -``` - -可以接收 4 个参数: - -- `CS`: Max6675 模块的`CS`脚对应的树莓派的 GPIO 号。 -- `SCK`: Max6675 模块的`SCK`脚对应的树莓派的 GPIO 号。 -- `SO`: Max6675 模块的`SO`脚对应的树莓派的 GPIO 号,可以接收一个数组,也可以接收一个整数。 -- `UNIT`: 设置结果输出单位,`1`为`°C`,`0`为`°F`,不传参数则默认值为`1`,传其他值则直接返回`Max6675`芯片的二进制数转十进制数值。 - -### `setPin` - -```js -const Max6675 = require("max6675-raspi"); - -const CS = 4; -const SCK = 24; -const SO = [25, 12, 16, 20, 21]; -const UNIT = 1; - -// const max = new Max6675(CS, SCK, SO, UNIT); -const max6675 = new Max6675(); -max6675.setPin(CS, SCK, SO, UNIT); -``` - -如果你在`new Max6675()`的时候没有传参数,就可以调用这个方法设置针脚信息。与`Max6675`一样接收四个参数: - -- `CS`: Max6675 模块的`CS`脚对应的树莓派的 GPIO 号。 -- `SCK`: Max6675 模块的`SCK`脚对应的树莓派的 GPIO 号。 -- `SO`: Max6675 模块的`SO`脚对应的树莓派的 GPIO 号,可以接收一个数组,也可以接收一个整数。 -- `UNIT`: 设置结果输出单位,`1`为`°C`,`2`为`°F`,不传参数则默认值为`1`,传其他值则直接返回`Max6675`芯片的二进制数转十进制数值。 - -### `readTemp` - -在设定了`CS`,`SCK`,`SO`和`UNIT`(默认值为`1`) 后,即能调用这个方法来获取值。 - -```js -const Max6675 = require("max6675-raspi"); - -const CS = 4; -const SCK = 24; -const SO = [25, 12, 16, 20, 21]; -const UNIT = 1; -const max6675 = new Max6675(); -max6675.setPin(CS, SCK, SO, UNIT); -const { temp, unit } = max6675.readTemp(); -console.log(`${new Date()}:${temp.map(item => item + unit)}`); -``` - -`setPin`之后也可以立即调用`readTemp` - -```js -const Max6675 = require("max6675-raspi"); - -const CS = 4; -const SCK = 24; -const SO = [25, 12, 16, 20, 21]; -const UNIT = 1; -const max6675 = new Max6675(); -const { temp, unit } = max6675.setPin(CS, SCK, SO, UNIT).readTemp(); -console.log(`${new Date()}:${temp.map(item => item + unit)}`); -``` - -### `sleep` - -这是个用`Promise`封装的延时器。当你需要循环获取值,但又不想自己写延时器的时候,可以像下面一样使用这个`sleep`方法。 - -```js -const Max6675 = require("max6675-raspi"); - -const CS = 4; -const SCK = 24; -const SO = [25, 12, 16, 20, 21]; -const UNIT = 1; -const max6675 = new Max6675(); -max6675.setPin(CS, SCK, SO, UNIT); - -(async () => { - while (true) { - const { temp, unit } = max6675.readTemp(); - if (temp.length) - console.log(`${new Date()}:${temp.map(item => item + unit)}`); - await max6675.sleep(2000); - } -})(); -``` - -![PIN](https://user-images.githubusercontent.com/13029001/107857184-c58d7100-6e67-11eb-93b1-8a4ebc9c9309.png) - -The PINs value is BCM GPIOs (green). - -## GPIO - -这里特地提 GPIO,`SO`,`CS`,`SCK`的值,是树莓派上的 BCM GPIO(绿色),不是针脚号。 - -引用一张 [`https://github.com/splitbrain/rpibplusleaf`](https://github.com/splitbrain/rpibplusleaf) 的图片。 - -[![rpiblusleaf](https://raw.githubusercontent.com/splitbrain/rpibplusleaf/master/rpiblusleaf.png)](https://github.com/splitbrain/rpibplusleaf) diff --git a/README.md b/README.md index cb502dd..62c4a46 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Max6675 -[![NPM version](https://img.shields.io/npm/v/max6675-raspi.svg)](https://www.npmjs.com/package/max6675-raspi) [![jaywcjlove/sb](https://jaywcjlove.github.io/sb/lang/chinese.svg)](README.cn.md) +[![NPM version](https://img.shields.io/npm/v/max6675-raspi.svg)](https://www.npmjs.com/package/max6675-raspi) Read the temperature with K-type thermocouple. Raspberry pi NodeJS. Get the code from npmjs. ```sh -npm i max6675-raspi --save +npm i github:schme16/Max6675-Raspberry-pi-nodejs --save ``` ![Max6675](https://github.com/bubao/Max6675-Raspberry-pi-nodejs/raw/master/imgs/Max6675.png) diff --git a/index.js b/index.js index 0cfbe3f..92a2dbd 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -const { Gpio } = require("onoff"); +const {Gpio} = require("onoff"); const getValue = Symbol("getValue"); const bin2dec = Symbol("bin2dec"); const isArray = Symbol("isArray"); @@ -11,15 +11,16 @@ module.exports = class Max6675 { * @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". + * @param {number} unit 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) + if (this.cs && this.sck && this.so && this.unit) { this.setPin(this.cs, this.sck, this.so, this.unit); + } process.on("SIGINT", () => this.stop()); } @@ -28,19 +29,39 @@ module.exports = class Max6675 { } stop() { - if (this.cs) { - this.cs.writeSync(0); - this.cs.unexport(); + if (this.CS) { + try { + this.CS.writeSync(0); + this.CS.unexport(); + } + catch (e) { + //console.trace(e) + } } - if (this.sck) { - this.sck.writeSync(0); - this.sck.unexport(); + if (this.SCK) { + try { + this.SCK.writeSync(0); + this.SCK.unexport(); + } + catch (e) { + //console.trace(e) + } + } - if (this.so) - this.so.map(item => { - item.writeSync(0); - item.unexport(); - }); + + if (this.SO) { + + try { + this.SO.map(item => { + item.writeSync(0); + item.unexport(); + }); + } + catch (e) { + //console.trace(e) + } + } + process.exit(); } @@ -62,6 +83,7 @@ module.exports = class Max6675 { } return arr; } + /** * @description set pins * @author bubao @@ -78,18 +100,20 @@ module.exports = class Max6675 { this.so = this[isArray](so) ? so : typeof so === "number" - ? [so] - : this.so; + ? [so] + : this.so; if (this.so.length === 0) { console.error("You must assign a value to so!"); this.stop(); - } else { + } + 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 @@ -128,7 +152,9 @@ module.exports = class Max6675 { * @returns {{temp: string[],unit: string}} */ readTemp() { - if (!(this.CS && this.SCK && this.SO)) return; + if (!(this.CS && this.SCK && this.SO)) { + return; + } this.CS.writeSync(0); this.CS.writeSync(1, 200); this.CS.writeSync(0); @@ -141,10 +167,14 @@ module.exports = class Max6675 { let error = 0; error_tc.forEach(element => { - if (element !== 0) error += 1; + if (element !== 0) { + error += 1; + } }); results.error_tc = error_tc; - if (error !== 0) return { temp: [], unit: "", error_tc }; + if (error !== 0) { + return {temp: [], unit: "", error_tc}; + } return results; } }; diff --git a/package-lock.json b/package-lock.json index d46feb9..e145379 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.13", "license": "MIT", "dependencies": { - "onoff": "^3.2.9" + "onoff": "^6.0.3" }, "devDependencies": {} }, @@ -22,16 +22,16 @@ } }, "node_modules/epoll": { - "version": "2.0.10", - "resolved": "https://registry.npmjs.org/epoll/-/epoll-2.0.10.tgz", - "integrity": "sha512-kx5y1SxivN99HjXDZpE/A73FHJV/dzRQt+qoF88CEza3RcEKGqNfkXPPY/oqVBV5w6G2N6b8xd5s5zprgrUVnQ==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/epoll/-/epoll-4.0.2.tgz", + "integrity": "sha512-4KtBBSBYAfyta2/4TIFT4P7pZdDjU6UPvdzWcF4Fy6Q300V08Wdj/82ovAZBUDra6TTjsKsfy12Qxuwc3ob+Qw==", "hasInstallScript": true, "dependencies": { "bindings": "^1.5.0", - "nan": "^2.14.0" + "nan": "^2.17.0" }, "engines": { - "node": ">=4.0.0" + "node": ">=10.0.0" } }, "node_modules/file-uri-to-path": { @@ -45,20 +45,20 @@ "integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==" }, "node_modules/nan": { - "version": "2.16.0", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.16.0.tgz", - "integrity": "sha512-UdAqHyFngu7TfQKsCBgAA6pWDkT8MAO7d0jyOecVhN5354xbLqdn8mV9Tat9gepAupm0bt2DbeaSC8vS52MuFA==" + "version": "2.19.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", + "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==" }, "node_modules/onoff": { - "version": "3.2.9", - "resolved": "https://registry.npmjs.org/onoff/-/onoff-3.2.9.tgz", - "integrity": "sha512-khCZO81MgC3v74eEuxl4hP5bfAgJ36FtRmshPch7qMkCB6lp1oADXrtVqYlcCOKq+WjQklcKas0BnS6wyp0R4g==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/onoff/-/onoff-6.0.3.tgz", + "integrity": "sha512-xtVlwRDzswYM69bzzIui/qzu7QHsFnjsQiCV1iYVA/HXt5xdc9utc97SYAlXzK8wAhIN7+H7MaVqh2vpfdKk9A==", "dependencies": { - "epoll": "^2.0.7", + "epoll": "^4.0.1", "lodash.debounce": "^4.0.8" }, "engines": { - "node": ">=4.0.0" + "node": ">=10.0.0" } } }, @@ -72,12 +72,12 @@ } }, "epoll": { - "version": "2.0.10", - "resolved": "https://registry.npmjs.org/epoll/-/epoll-2.0.10.tgz", - "integrity": "sha512-kx5y1SxivN99HjXDZpE/A73FHJV/dzRQt+qoF88CEza3RcEKGqNfkXPPY/oqVBV5w6G2N6b8xd5s5zprgrUVnQ==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/epoll/-/epoll-4.0.2.tgz", + "integrity": "sha512-4KtBBSBYAfyta2/4TIFT4P7pZdDjU6UPvdzWcF4Fy6Q300V08Wdj/82ovAZBUDra6TTjsKsfy12Qxuwc3ob+Qw==", "requires": { "bindings": "^1.5.0", - "nan": "^2.14.0" + "nan": "^2.17.0" } }, "file-uri-to-path": { @@ -91,16 +91,16 @@ "integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==" }, "nan": { - "version": "2.16.0", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.16.0.tgz", - "integrity": "sha512-UdAqHyFngu7TfQKsCBgAA6pWDkT8MAO7d0jyOecVhN5354xbLqdn8mV9Tat9gepAupm0bt2DbeaSC8vS52MuFA==" + "version": "2.19.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", + "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==" }, "onoff": { - "version": "3.2.9", - "resolved": "https://registry.npmjs.org/onoff/-/onoff-3.2.9.tgz", - "integrity": "sha512-khCZO81MgC3v74eEuxl4hP5bfAgJ36FtRmshPch7qMkCB6lp1oADXrtVqYlcCOKq+WjQklcKas0BnS6wyp0R4g==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/onoff/-/onoff-6.0.3.tgz", + "integrity": "sha512-xtVlwRDzswYM69bzzIui/qzu7QHsFnjsQiCV1iYVA/HXt5xdc9utc97SYAlXzK8wAhIN7+H7MaVqh2vpfdKk9A==", "requires": { - "epoll": "^2.0.7", + "epoll": "^4.0.1", "lodash.debounce": "^4.0.8" } } diff --git a/package.json b/package.json index be45d88..c7bc06d 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "max6675-raspi", - "version": "0.0.13", + "version": "0.0.20", "main": "index.js", "devDependencies": {}, "dependencies": { - "onoff": "^3.2.9" + "onoff": "^6.0.3" }, "scripts": { "test": "node ./test/test.js" @@ -22,10 +22,10 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/bubao/Max6675-Raspberry-pi-nodejs.git" + "url": "git+https://github.com/schme16/Max6675-Raspberry-pi-nodejs.git" }, "bugs": { - "url": "https://github.com/bubao/Max6675-Raspberry-pi-nodejs/issues" + "url": "https://github.com/schme16/Max6675-Raspberry-pi-nodejs/issues" }, - "homepage": "https://github.com/bubao/Max6675-Raspberry-pi-nodejs#readme" + "homepage": "https://github.com/schme16/Max6675-Raspberry-pi-nodejs#readme" }