Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 0 additions & 121 deletions README.cn.md

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
70 changes: 50 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Gpio } = require("onoff");
const {Gpio} = require("onoff");
const getValue = Symbol("getValue");
const bin2dec = Symbol("bin2dec");
const isArray = Symbol("isArray");
Expand All @@ -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());
}

Expand All @@ -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();
}

Expand All @@ -62,6 +83,7 @@ module.exports = class Max6675 {
}
return arr;
}

/**
* @description set pins
* @author bubao
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
};
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading