// 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 = 0x42 //左边上下0x43 V1=P1=43 this.P2 = 0x40 //右边上下0x41 V2=P2=41 this.P3 = 0x41 //右边左右0x40 V3=P3=40 this.P4 = 0x43 //左边左右0x42 V0=P4=42 this.run() } async run() { // 数模初始值 const data = await this.Orientation() process.send(data); await sleep(50) 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()