contrl.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // PCF8591 数模转换芯片
  2. const i2cBus = require("i2c-bus")
  3. const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  4. class ContrlService {
  5. constructor() {
  6. this.BUS = i2cBus.openSync(1)
  7. // 寄存器地址
  8. this.ADR = 0x48
  9. // 电位器地址->对应模块in1,in2
  10. this.P1 = 0x41
  11. this.P2 = 0x42
  12. this.P3 = 0x43
  13. this.P4 = 0x44
  14. }
  15. async run() {
  16. // 数模初始值
  17. const data = this.Orientation()
  18. process.send(data);
  19. await sleep(0.03)
  20. this.run()
  21. }
  22. // 获取值
  23. async Orientation() {
  24. this.BUS.write_byte(this.ADR, this.P1)
  25. const v0 = this.BUS.read_byte(this.ADR)
  26. await sleep(0.02)
  27. this.BUS.write_byte(this.ADR, this.P2)
  28. const v1 = this.BUS.read_byte(this.ADR)
  29. await sleep(0.02)
  30. this.BUS.write_byte(this.ADR, this.P3)
  31. const v2 = this.BUS.read_byte(this.ADR)
  32. await sleep(0.02)
  33. this.BUS.write_byte(this.ADR, this.P4)
  34. const v3 = this.BUS.read_byte(this.ADR)
  35. // 接入socket 发送数据
  36. db = `{"type":"conctrl","conctrl":{"v0":${v0},"v1":${v1},"v2":${v2},"v3":${v3}}}`
  37. return db
  38. }
  39. }
  40. new ContrlService()