|
|
@@ -0,0 +1,38 @@
|
|
|
+// PCF8591 数模转换芯片
|
|
|
+const i2cBus = require("i2c-bus")
|
|
|
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
+class ContrlService {
|
|
|
+ constructor() {
|
|
|
+ this.BUS = i2cBus.openSync(1)
|
|
|
+ // 寄存器地址
|
|
|
+ this.ADR = 0x48
|
|
|
+ // 电位器地址->对应模块in1,in2
|
|
|
+ this.P1 = 0x41
|
|
|
+ this.P2 = 0x42
|
|
|
+ this.P3 = 0x43
|
|
|
+ this.P4 = 0x44
|
|
|
+ this.run()
|
|
|
+ }
|
|
|
+
|
|
|
+ async run() {
|
|
|
+ // 数模初始值
|
|
|
+ const data = await this.Orientation()
|
|
|
+ console.log(data)
|
|
|
+ // process.send(data);
|
|
|
+ await sleep(500)
|
|
|
+ this.run()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取值
|
|
|
+ async Orientation() {
|
|
|
+ const v0 = this.BUS.readByteSync(this.ADR,this.P1)
|
|
|
+ const v1 = this.BUS.readByteSync(this.ADR,this.P2)
|
|
|
+ const v2 = this.BUS.readByteSync(this.ADR,this.P3)
|
|
|
+ const v3 = this.BUS.readByteSync(this.ADR,this.P4)
|
|
|
+ // 接入socket 发送数据
|
|
|
+ const db = `{"type":"conctrl","conctrl":{"v0":${v0},"v1":${v1},"v2":${v2},"v3":${v3}}}`
|
|
|
+ return db
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+new ContrlService()
|