periph 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +61 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/motor/drv8830/complete.js +50 -0
  117. package/examples/motor/drv8830/demo.js +57 -0
  118. package/examples/motor/drv8830/minimal.js +23 -0
  119. package/examples/other/mpr121/complete.js +50 -0
  120. package/examples/other/mpr121/demo.js +30 -0
  121. package/examples/other/mpr121/minimal.js +17 -0
  122. package/examples/power/ade7953/complete.js +44 -0
  123. package/examples/power/ade7953/demo.js +28 -0
  124. package/examples/power/ade7953/minimal.js +15 -0
  125. package/examples/power/ina219/complete.js +20 -18
  126. package/examples/power/ina219/demo.js +15 -13
  127. package/examples/power/ina219/minimal.js +8 -8
  128. package/examples/power/ina226/complete.js +24 -22
  129. package/examples/power/ina226/demo.js +22 -20
  130. package/examples/power/ina226/minimal.js +5 -5
  131. package/examples/power/ina3221/complete.js +51 -49
  132. package/examples/power/ina3221/demo.js +13 -13
  133. package/examples/power/ina3221/minimal.js +8 -8
  134. package/examples/pressure/bmp085/complete.js +29 -0
  135. package/examples/pressure/bmp085/demo.js +34 -0
  136. package/examples/pressure/bmp085/minimal.js +19 -0
  137. package/examples/pressure/bmp180/complete.js +20 -17
  138. package/examples/pressure/bmp180/demo.js +24 -22
  139. package/examples/pressure/bmp180/minimal.js +13 -11
  140. package/examples/pressure/bmp280/complete.js +23 -20
  141. package/examples/pressure/bmp280/demo.js +44 -41
  142. package/examples/pressure/bmp280/minimal.js +13 -11
  143. package/examples/pressure/bmp384/complete.js +29 -0
  144. package/examples/pressure/bmp384/demo.js +40 -0
  145. package/examples/pressure/bmp384/minimal.js +18 -0
  146. package/examples/pressure/bmp581/complete.js +34 -0
  147. package/examples/pressure/bmp581/demo.js +56 -0
  148. package/examples/pressure/bmp581/minimal.js +19 -0
  149. package/examples/pressure/lps22df/complete.js +50 -0
  150. package/examples/pressure/lps22df/demo.js +41 -0
  151. package/examples/pressure/lps22df/minimal.js +19 -0
  152. package/examples/pressure/lps28dfw/complete.js +37 -0
  153. package/examples/pressure/lps28dfw/demo.js +33 -0
  154. package/examples/pressure/lps28dfw/minimal.js +19 -0
  155. package/examples/pressure/lps33hw/complete.js +43 -0
  156. package/examples/pressure/lps33hw/demo.js +43 -0
  157. package/examples/pressure/lps33hw/minimal.js +19 -0
  158. package/examples/rfid/mfrc522/complete.js +44 -0
  159. package/examples/rfid/mfrc522/demo.js +70 -0
  160. package/examples/rfid/mfrc522/minimal.js +17 -0
  161. package/examples/rtc/ds3231/complete.js +77 -0
  162. package/examples/rtc/ds3231/demo.js +60 -0
  163. package/examples/rtc/ds3231/minimal.js +23 -0
  164. package/examples/rtc/pcf8523/complete.js +66 -0
  165. package/examples/rtc/pcf8523/demo.js +65 -0
  166. package/examples/rtc/pcf8523/minimal.js +20 -0
  167. package/examples/temperature/mcp9808/complete.js +89 -0
  168. package/examples/temperature/mcp9808/demo.js +62 -0
  169. package/examples/temperature/mcp9808/minimal.js +21 -0
  170. package/examples/temperature/tmp117/complete.js +90 -0
  171. package/examples/temperature/tmp117/demo.js +73 -0
  172. package/examples/temperature/tmp117/minimal.js +21 -0
  173. package/examples/tof/vl53l0x/complete.js +118 -0
  174. package/examples/tof/vl53l0x/demo.js +83 -0
  175. package/examples/tof/vl53l0x/minimal.js +25 -0
  176. package/examples/tof/vl53l1x/complete.js +151 -0
  177. package/examples/tof/vl53l1x/demo.js +100 -0
  178. package/examples/tof/vl53l1x/minimal.js +25 -0
  179. package/package.json +5 -1
  180. package/src/chips/accelerometer/adxl345.js +482 -0
  181. package/src/chips/accelerometer/adxl362.js +594 -0
  182. package/src/chips/adc_dac/ad7705.js +345 -0
  183. package/src/chips/adc_dac/ad7706.js +331 -0
  184. package/src/chips/adc_dac/hx710a.js +184 -0
  185. package/src/chips/adc_dac/hx710b.js +186 -0
  186. package/src/chips/adc_dac/hx711.js +15 -15
  187. package/src/chips/adc_dac/mcp4725.js +48 -40
  188. package/src/chips/adc_dac/mcp4728.js +57 -44
  189. package/src/chips/adc_dac/pcf8591.js +196 -0
  190. package/src/chips/comms/mcp2515.js +659 -0
  191. package/src/chips/comms/rda5807m.js +393 -0
  192. package/src/chips/comms/rfm9x.js +590 -0
  193. package/src/chips/display/pcf8576.js +42 -33
  194. package/src/chips/environmental/aht21.js +52 -37
  195. package/src/chips/environmental/bme280.js +85 -66
  196. package/src/chips/environmental/bme680.js +110 -87
  197. package/src/chips/gas/ens160.js +82 -64
  198. package/src/chips/gnss/neo6.js +438 -0
  199. package/src/chips/gyroscope/l3g4200d.js +366 -0
  200. package/src/chips/gyroscope/l3gd20h.js +314 -0
  201. package/src/chips/humidity/dht11.js +135 -0
  202. package/src/chips/imu/mpu6050.js +89 -66
  203. package/src/chips/imu/mpu9250.js +394 -0
  204. package/src/chips/imu/mpu9255.js +447 -0
  205. package/src/chips/io_expander/mcp23017.js +240 -157
  206. package/src/chips/io_expander/pcf8574.js +149 -124
  207. package/src/chips/io_expander/pcf8575.js +164 -135
  208. package/src/chips/io_expander/tpic6b595.js +242 -0
  209. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  210. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  211. package/src/chips/led/apa102.js +236 -0
  212. package/src/chips/led/sk6812rgbw.js +15 -149
  213. package/src/chips/led/ws2812b.js +14 -139
  214. package/src/chips/led/ws2814.js +49 -0
  215. package/src/chips/light/apds-9930.js +342 -0
  216. package/src/chips/light/apds9960.js +158 -123
  217. package/src/chips/magnetometer/as5600.js +102 -82
  218. package/src/chips/magnetometer/hmc5883l.js +222 -0
  219. package/src/chips/memory/_24aa02uid.js +32 -29
  220. package/src/chips/motor/drv8830.js +239 -0
  221. package/src/chips/other/mpr121.js +375 -0
  222. package/src/chips/power/ade7953.js +427 -0
  223. package/src/chips/power/ina219.js +45 -40
  224. package/src/chips/power/ina226.js +50 -45
  225. package/src/chips/power/ina3221.js +95 -76
  226. package/src/chips/pressure/bmp085.js +251 -0
  227. package/src/chips/pressure/bmp180.js +54 -37
  228. package/src/chips/pressure/bmp280.js +72 -53
  229. package/src/chips/pressure/bmp384.js +410 -0
  230. package/src/chips/pressure/bmp581.js +489 -0
  231. package/src/chips/pressure/lps22df.js +341 -0
  232. package/src/chips/pressure/lps28dfw.js +314 -0
  233. package/src/chips/pressure/lps33hw.js +255 -0
  234. package/src/chips/rfid/mfrc522.js +674 -0
  235. package/src/chips/rtc/ds3231.js +504 -0
  236. package/src/chips/rtc/pcf8523.js +539 -0
  237. package/src/chips/temperature/mcp9808.js +488 -0
  238. package/src/chips/temperature/tmp117.js +471 -0
  239. package/src/chips/tof/_vl53_base.js +160 -0
  240. package/src/chips/tof/vl53l0x.js +816 -0
  241. package/src/chips/tof/vl53l1x.js +816 -0
  242. package/src/connection/connection.js +111 -0
  243. package/src/connection/dhtxx.js +138 -0
  244. package/src/connection/dhtxx_mock.js +39 -0
  245. package/src/{transport → connection}/hx711.js +49 -22
  246. package/src/connection/hx711_mock.js +52 -0
  247. package/src/{transport → connection}/i2c.js +13 -9
  248. package/src/connection/i2c_mock.js +91 -0
  249. package/src/connection/input_pin.js +156 -0
  250. package/src/connection/neo6_mock.js +45 -0
  251. package/src/{transport → connection}/neopixel.js +23 -4
  252. package/src/connection/output_pin.js +41 -0
  253. package/src/connection/sipo.js +159 -0
  254. package/src/{transport → connection}/smbus.js +14 -10
  255. package/src/{transport → connection}/spi.js +12 -8
  256. package/src/connection/spi_mock.js +151 -0
  257. package/src/{transport → connection}/uart.js +33 -26
  258. package/examples/gnss/.gitkeep +0 -0
  259. package/examples/humidity/.gitkeep +0 -0
  260. package/examples/motor/.gitkeep +0 -0
  261. package/examples/temperature/.gitkeep +0 -0
  262. package/examples/tof/.gitkeep +0 -0
  263. package/src/chips/gnss/.gitkeep +0 -0
  264. package/src/chips/motor/.gitkeep +0 -0
  265. package/src/chips/temperature/.gitkeep +0 -0
  266. package/src/chips/tof/.gitkeep +0 -0
@@ -17,7 +17,7 @@ const _CONFIG_DEFAULT = 0x4127;
17
17
  * INA226 36V, 16-bit current/voltage/power monitor — minimal interface.
18
18
  *
19
19
  * Provides bus voltage, shunt voltage, current, and power readings with no
20
- * configuration beyond the transport and shunt resistor. Writes the
20
+ * configuration beyond the connection and shunt resistor. Writes the
21
21
  * Calibration Register automatically at construction.
22
22
  *
23
23
  * Default configuration (written at construction):
@@ -28,56 +28,56 @@ const _CONFIG_DEFAULT = 0x4127;
28
28
  */
29
29
  class INA226Minimal {
30
30
  /**
31
- * @param {object} transport - Configured I²C or SMBus transport (writeRead, write).
31
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C or SMBus connection (writeRead, write).
32
32
  * @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
33
33
  * @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
34
34
  */
35
- constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
36
- this._transport = transport;
35
+ constructor(connection, rShunt = 0.1, maxCurrent = 2.0) {
36
+ this._conn = connection;
37
37
  this._currentLsb = maxCurrent / 32768;
38
38
  this._cal = Math.floor(0.00512 / (this._currentLsb * rShunt));
39
39
  this._writeReg(_REG_CONFIG, _CONFIG_DEFAULT);
40
40
  this._writeReg(_REG_CAL, this._cal);
41
41
  }
42
42
 
43
- _writeReg(reg, value) {
43
+ async _writeReg(reg, value) {
44
44
  const buf = Buffer.alloc(3);
45
45
  buf[0] = reg;
46
46
  buf.writeUInt16BE(value, 1);
47
- this._transport.write(buf);
47
+ await this._conn.write(buf);
48
48
  }
49
49
 
50
- _readReg(reg) {
51
- return this._transport.writeRead(Buffer.from([reg]), 2).readUInt16BE(0);
50
+ async _readReg(reg) {
51
+ return (await this._conn.writeRead(Buffer.from([reg]), 2)).readUInt16BE(0);
52
52
  }
53
53
 
54
- _readRegSigned(reg) {
55
- return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
54
+ async _readRegSigned(reg) {
55
+ return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
56
56
  }
57
57
 
58
58
  /**
59
59
  * Read bus voltage.
60
- * @returns {number} Bus voltage in volts (raw × 1.25 mV LSB).
60
+ * @returns {Promise<number>} Bus voltage in volts (raw × 1.25 mV LSB).
61
61
  */
62
- voltage() { return this._readReg(_REG_BUS) * 1.25e-3; }
62
+ async voltage() { return (await this._readReg(_REG_BUS)) * 1.25e-3; }
63
63
 
64
64
  /**
65
65
  * Read differential shunt voltage.
66
- * @returns {number} Shunt voltage in volts, signed (raw × 2.5 µV LSB).
66
+ * @returns {Promise<number>} Shunt voltage in volts, signed (raw × 2.5 µV LSB).
67
67
  */
68
- shuntVoltage() { return this._readRegSigned(_REG_SHUNT) * 2.5e-6; }
68
+ async shuntVoltage() { return (await this._readRegSigned(_REG_SHUNT)) * 2.5e-6; }
69
69
 
70
70
  /**
71
71
  * Read calculated current through the shunt.
72
- * @returns {number} Current in amperes, signed.
72
+ * @returns {Promise<number>} Current in amperes, signed.
73
73
  */
74
- current() { return this._readRegSigned(_REG_CURRENT) * this._currentLsb; }
74
+ async current() { return (await this._readRegSigned(_REG_CURRENT)) * this._currentLsb; }
75
75
 
76
76
  /**
77
77
  * Read calculated power.
78
- * @returns {number} Power in watts (raw × 25 × current LSB).
78
+ * @returns {Promise<number>} Power in watts (raw × 25 × current LSB).
79
79
  */
80
- power() { return this._readReg(_REG_POWER) * 25 * this._currentLsb; }
80
+ async power() { return (await this._readReg(_REG_POWER)) * 25 * this._currentLsb; }
81
81
  }
82
82
 
83
83
  /**
@@ -104,12 +104,12 @@ class INA226Full extends INA226Minimal {
104
104
  static AFF = 0x0010;
105
105
 
106
106
  /**
107
- * @param {object} transport - Configured I²C or SMBus transport.
107
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C or SMBus connection.
108
108
  * @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
109
109
  * @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
110
110
  */
111
- constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
112
- super(transport, rShunt, maxCurrent);
111
+ constructor(connection, rShunt = 0.1, maxCurrent = 2.0) {
112
+ super(connection, rShunt, maxCurrent);
113
113
  this._mode = 0x07;
114
114
  }
115
115
 
@@ -119,11 +119,12 @@ class INA226Full extends INA226Minimal {
119
119
  * @param {number} [vbusCt=4] - Bus voltage conversion time selector 0–7 (default 4 = 1.1 ms).
120
120
  * @param {number} [vshCt=4] - Shunt voltage conversion time selector 0–7 (default 4 = 1.1 ms).
121
121
  * @param {number} [mode=7] - Operating mode 0–7 (7 = shunt+bus continuous).
122
+ * @returns {Promise<void>}
122
123
  */
123
- configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
124
+ async configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
124
125
  const config = ((avg & 0x07) << 9) | ((vbusCt & 0x07) << 6) | ((vshCt & 0x07) << 3) | (mode & 0x07);
125
126
  this._mode = mode & 0x07;
126
- this._writeReg(_REG_CONFIG, config);
127
+ await this._writeReg(_REG_CONFIG, config);
127
128
  }
128
129
 
129
130
  /**
@@ -131,15 +132,15 @@ class INA226Full extends INA226Minimal {
131
132
  *
132
133
  * Note: Reading Mask/Enable clears CVRF. Read it last if also checking other flags.
133
134
  *
134
- * @returns {boolean} True if a conversion completed since the last Mask/Enable read.
135
+ * @returns {Promise<boolean>} True if a conversion completed since the last Mask/Enable read.
135
136
  */
136
- conversionReady() { return !!(this._readReg(_REG_MASK) & 0x0008); }
137
+ async conversionReady() { return !!((await this._readReg(_REG_MASK)) & 0x0008); }
137
138
 
138
139
  /**
139
140
  * Read the Math Overflow Flag (OVF) from the Mask/Enable Register.
140
- * @returns {boolean} True if an arithmetic overflow occurred in the power calculation.
141
+ * @returns {Promise<boolean>} True if an arithmetic overflow occurred in the power calculation.
141
142
  */
142
- overflow() { return !!(this._readReg(_REG_MASK) & 0x0004); }
143
+ async overflow() { return !!((await this._readReg(_REG_MASK)) & 0x0004); }
143
144
 
144
145
  /**
145
146
  * Configure the alert pin function and threshold.
@@ -150,59 +151,63 @@ class INA226Full extends INA226Minimal {
150
151
  * @param {number} [limit=0] - Threshold in natural units (V for voltage, W for power).
151
152
  * @param {number} [polarity=0] - 0 = active-low (default), 1 = active-high.
152
153
  * @param {number} [latch=0] - 0 = transparent (default), 1 = latch until Mask/Enable is read.
154
+ * @returns {Promise<void>}
153
155
  */
154
- setAlert(fn, limit = 0, polarity = 0, latch = 0) {
156
+ async setAlert(fn, limit = 0, polarity = 0, latch = 0) {
155
157
  let raw = 0;
156
158
  if (fn === INA226Full.SOL || fn === INA226Full.SUL) raw = Math.floor(limit / 2.5e-6);
157
159
  else if (fn === INA226Full.BOL || fn === INA226Full.BUL) raw = Math.floor(limit / 1.25e-3);
158
160
  else if (fn === INA226Full.POL) raw = Math.floor(limit / (25 * this._currentLsb));
159
161
  const mask = fn | ((polarity & 1) << 1) | (latch & 1);
160
- this._writeReg(_REG_MASK, mask);
161
- this._writeReg(_REG_ALERT, raw & 0xFFFF);
162
+ await this._writeReg(_REG_MASK, mask);
163
+ await this._writeReg(_REG_ALERT, raw & 0xFFFF);
162
164
  }
163
165
 
164
166
  /**
165
167
  * Read the Mask/Enable Register.
166
- * @returns {number} Raw 16-bit value containing alert and status flags.
168
+ * @returns {Promise<number>} Raw 16-bit value containing alert and status flags.
167
169
  */
168
- alertFlags() { return this._readReg(_REG_MASK); }
170
+ async alertFlags() { return this._readReg(_REG_MASK); }
169
171
 
170
172
  /**
171
173
  * Reset all registers to power-on defaults, then re-write the Calibration Register.
174
+ * @returns {Promise<void>}
172
175
  */
173
- reset() {
174
- this._writeReg(_REG_CONFIG, 0x8000);
175
- this._writeReg(_REG_CAL, this._cal);
176
+ async reset() {
177
+ await this._writeReg(_REG_CONFIG, 0x8000);
178
+ await this._writeReg(_REG_CAL, this._cal);
176
179
  }
177
180
 
178
181
  /**
179
182
  * Enter power-down mode (MODE = 000) and save the current mode for wake().
183
+ * @returns {Promise<void>}
180
184
  */
181
- shutdown() {
182
- const config = this._readReg(_REG_CONFIG);
185
+ async shutdown() {
186
+ const config = await this._readReg(_REG_CONFIG);
183
187
  this._mode = config & 0x07;
184
- this._writeReg(_REG_CONFIG, config & 0xFFF8);
188
+ await this._writeReg(_REG_CONFIG, config & 0xFFF8);
185
189
  }
186
190
 
187
191
  /**
188
192
  * Restore the operating mode saved by shutdown().
193
+ * @returns {Promise<void>}
189
194
  */
190
- wake() {
191
- const config = this._readReg(_REG_CONFIG);
192
- this._writeReg(_REG_CONFIG, (config & 0xFFF8) | this._mode);
195
+ async wake() {
196
+ const config = await this._readReg(_REG_CONFIG);
197
+ await this._writeReg(_REG_CONFIG, (config & 0xFFF8) | this._mode);
193
198
  }
194
199
 
195
200
  /**
196
201
  * Read the Manufacturer ID register.
197
- * @returns {number} Manufacturer ID; expect 0x5449 (Texas Instruments).
202
+ * @returns {Promise<number>} Manufacturer ID; expect 0x5449 (Texas Instruments).
198
203
  */
199
- manufacturerId() { return this._readReg(_REG_MFR_ID); }
204
+ async manufacturerId() { return this._readReg(_REG_MFR_ID); }
200
205
 
201
206
  /**
202
207
  * Read the Die ID register.
203
- * @returns {number} Die revision ID; expect 0x2260.
208
+ * @returns {Promise<number>} Die revision ID; expect 0x2260.
204
209
  */
205
- dieId() { return this._readReg(_REG_DIE_ID); }
210
+ async dieId() { return this._readReg(_REG_DIE_ID); }
206
211
  }
207
212
 
208
213
  module.exports = { INA226Minimal, INA226Full };
@@ -30,18 +30,18 @@ const _WARN_REGS = [_REG_CH1_WARN, _REG_CH2_WARN, _REG_CH3_WARN];
30
30
  * INA3221 three-channel 26V current/voltage/power monitor — minimal interface.
31
31
  *
32
32
  * Reads bus voltage, shunt voltage, current, and power for each of the three
33
- * channels with no configuration beyond the transport and shunt resistors.
33
+ * channels with no configuration beyond the connection and shunt resistors.
34
34
  * The chip's power-on default (all three channels on, continuous shunt+bus)
35
35
  * is used without modification.
36
36
  *
37
- * @param {object} transport - Configured I2C or SMBus transport (writeRead, write).
37
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C or SMBus connection (writeRead, write).
38
38
  * @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms. Pass a
39
39
  * single number to apply the same value to all three channels, or an
40
40
  * array of 3 numbers for per-channel values.
41
41
  */
42
42
  class INA3221Minimal {
43
- constructor(transport, rShunt = 0.1) {
44
- this._transport = transport;
43
+ constructor(connection, rShunt = 0.1) {
44
+ this._conn = connection;
45
45
  if (Array.isArray(rShunt)) {
46
46
  this._rShunt = rShunt.map(v => Number(v));
47
47
  } else {
@@ -49,19 +49,19 @@ class INA3221Minimal {
49
49
  }
50
50
  }
51
51
 
52
- _writeReg(reg, value) {
52
+ async _writeReg(reg, value) {
53
53
  const buf = Buffer.alloc(3);
54
54
  buf[0] = reg;
55
55
  buf.writeUInt16BE(value, 1);
56
- this._transport.write(buf);
56
+ await this._conn.write(buf);
57
57
  }
58
58
 
59
- _readReg(reg) {
60
- return this._transport.writeRead(Buffer.from([reg]), 2).readUInt16BE(0);
59
+ async _readReg(reg) {
60
+ return (await this._conn.writeRead(Buffer.from([reg]), 2)).readUInt16BE(0);
61
61
  }
62
62
 
63
- _readRegSigned(reg) {
64
- return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
63
+ async _readRegSigned(reg) {
64
+ return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
65
65
  }
66
66
 
67
67
  _channelValid(channel) {
@@ -73,43 +73,43 @@ class INA3221Minimal {
73
73
  /**
74
74
  * Read bus voltage for a channel.
75
75
  * @param {number} channel - Channel number 1, 2, or 3.
76
- * @returns {number} Bus voltage in volts.
76
+ * @returns {Promise<number>} Bus voltage in volts.
77
77
  */
78
- voltage(channel) {
78
+ async voltage(channel) {
79
79
  const ch = this._channelValid(channel);
80
- const raw = this._readReg(_BUS_REGS[ch - 1]);
80
+ const raw = await this._readReg(_BUS_REGS[ch - 1]);
81
81
  return (raw >> 3) * 8e-3;
82
82
  }
83
83
 
84
84
  /**
85
85
  * Read differential shunt voltage for a channel.
86
86
  * @param {number} channel - Channel number 1, 2, or 3.
87
- * @returns {number} Shunt voltage in volts, signed.
87
+ * @returns {Promise<number>} Shunt voltage in volts, signed.
88
88
  */
89
- shuntVoltage(channel) {
89
+ async shuntVoltage(channel) {
90
90
  const ch = this._channelValid(channel);
91
- const raw = this._readRegSigned(_SHUNT_REGS[ch - 1]);
91
+ const raw = await this._readRegSigned(_SHUNT_REGS[ch - 1]);
92
92
  return raw * 5e-6;
93
93
  }
94
94
 
95
95
  /**
96
96
  * Read calculated current through the shunt for a channel.
97
97
  * @param {number} channel - Channel number 1, 2, or 3.
98
- * @returns {number} Current in amperes.
98
+ * @returns {Promise<number>} Current in amperes.
99
99
  */
100
- current(channel) {
100
+ async current(channel) {
101
101
  const ch = this._channelValid(channel);
102
- return this.shuntVoltage(ch) / this._rShunt[ch - 1];
102
+ return (await this.shuntVoltage(ch)) / this._rShunt[ch - 1];
103
103
  }
104
104
 
105
105
  /**
106
106
  * Read calculated power for a channel.
107
107
  * @param {number} channel - Channel number 1, 2, or 3.
108
- * @returns {number} Power in watts.
108
+ * @returns {Promise<number>} Power in watts.
109
109
  */
110
- power(channel) {
110
+ async power(channel) {
111
111
  const ch = this._channelValid(channel);
112
- return this.voltage(ch) * this.current(ch);
112
+ return (await this.voltage(ch)) * (await this.current(ch));
113
113
  }
114
114
  }
115
115
 
@@ -137,7 +137,7 @@ class INA3221Minimal {
137
137
  * - INA3221Full.MODE_BUS_CONT = 6
138
138
  * - INA3221Full.MODE_SHUNT_BUS_CONT = 7
139
139
  *
140
- * @param {object} transport - Configured I2C or SMBus transport.
140
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C or SMBus connection.
141
141
  * @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms.
142
142
  */
143
143
  class INA3221Full extends INA3221Minimal {
@@ -160,8 +160,8 @@ class INA3221Full extends INA3221Minimal {
160
160
  static MODE_BUS_CONT = 6;
161
161
  static MODE_SHUNT_BUS_CONT = 7;
162
162
 
163
- constructor(transport, rShunt = 0.1) {
164
- super(transport, rShunt);
163
+ constructor(connection, rShunt = 0.1) {
164
+ super(connection, rShunt);
165
165
  this._mode = 0x07;
166
166
  }
167
167
 
@@ -171,45 +171,47 @@ class INA3221Full extends INA3221Minimal {
171
171
  * @param {number} [vbusCt=4] - Bus voltage conversion time selector 0–7 (default 4=1.1 ms).
172
172
  * @param {number} [vshCt=4] - Shunt voltage conversion time selector 0–7 (default 4=1.1 ms).
173
173
  * @param {number} [mode=7] - Operating mode (default 7=shunt+bus continuous).
174
+ * @returns {Promise<void>}
174
175
  */
175
- configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
176
- const cfg = this._readReg(_REG_CONFIG);
176
+ async configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
177
+ const cfg = await this._readReg(_REG_CONFIG);
177
178
  const config = ((avg & 0x07) << 9) | ((vbusCt & 0x07) << 6) | ((vshCt & 0x07) << 3) | (mode & 0x07);
178
179
  this._mode = mode & 0x07;
179
- this._writeReg(_REG_CONFIG, config | (cfg & 0x7000));
180
+ await this._writeReg(_REG_CONFIG, config | (cfg & 0x7000));
180
181
  }
181
182
 
182
183
  /**
183
184
  * Enable or disable a channel.
184
185
  * @param {number} channel - Channel number 1, 2, or 3.
185
186
  * @param {boolean} enabled - true to enable, false to disable.
187
+ * @returns {Promise<void>}
186
188
  */
187
- enableChannel(channel, enabled) {
189
+ async enableChannel(channel, enabled) {
188
190
  const ch = this._channelValid(channel);
189
- const cfg = this._readReg(_REG_CONFIG);
191
+ const cfg = await this._readReg(_REG_CONFIG);
190
192
  const bit = 14 - (ch - 1);
191
- if (enabled) this._writeReg(_REG_CONFIG, cfg | (1 << bit));
192
- else this._writeReg(_REG_CONFIG, cfg & ~(1 << bit));
193
+ if (enabled) await this._writeReg(_REG_CONFIG, cfg | (1 << bit));
194
+ else await this._writeReg(_REG_CONFIG, cfg & ~(1 << bit));
193
195
  }
194
196
 
195
197
  /**
196
198
  * Read whether a channel is enabled.
197
199
  * @param {number} channel - Channel number 1, 2, or 3.
198
- * @returns {boolean} true if the channel is enabled.
200
+ * @returns {Promise<boolean>} true if the channel is enabled.
199
201
  */
200
- channelEnabled(channel) {
202
+ async channelEnabled(channel) {
201
203
  const ch = this._channelValid(channel);
202
- const cfg = this._readReg(_REG_CONFIG);
204
+ const cfg = await this._readReg(_REG_CONFIG);
203
205
  const bit = 14 - (ch - 1);
204
206
  return (cfg & (1 << bit)) !== 0;
205
207
  }
206
208
 
207
209
  /**
208
210
  * Read the Conversion Ready Flag (CVRF).
209
- * @returns {boolean} true if a conversion completed.
211
+ * @returns {Promise<boolean>} true if a conversion completed.
210
212
  */
211
- conversionReady() {
212
- return (this._readReg(_REG_MASK_EN) & INA3221Full.CVRF) !== 0;
213
+ async conversionReady() {
214
+ return ((await this._readReg(_REG_MASK_EN)) & INA3221Full.CVRF) !== 0;
213
215
  }
214
216
 
215
217
  /**
@@ -217,13 +219,14 @@ class INA3221Full extends INA3221Minimal {
217
219
  * @param {number} channel - Channel number 1, 2, or 3.
218
220
  * @param {number} limitV - Voltage limit in volts.
219
221
  * @param {boolean} [latch=false] - If true, use latched mode.
222
+ * @returns {Promise<void>}
220
223
  */
221
- setCriticalAlert(channel, limitV, latch = false) {
224
+ async setCriticalAlert(channel, limitV, latch = false) {
222
225
  const ch = this._channelValid(channel);
223
226
  const raw = (Math.floor(limitV / 40e-6) << 3) & 0xFFF8;
224
- this._writeReg(_CRIT_REGS[ch - 1], raw);
225
- const cfg = this._readReg(_REG_MASK_EN);
226
- this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0400) : (cfg & ~0x0400));
227
+ await this._writeReg(_CRIT_REGS[ch - 1], raw);
228
+ const cfg = await this._readReg(_REG_MASK_EN);
229
+ await this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0400) : (cfg & ~0x0400));
227
230
  }
228
231
 
229
232
  /**
@@ -231,13 +234,14 @@ class INA3221Full extends INA3221Minimal {
231
234
  * @param {number} channel - Channel number 1, 2, or 3.
232
235
  * @param {number} limitV - Voltage limit in volts.
233
236
  * @param {boolean} [latch=false] - If true, use latched mode.
237
+ * @returns {Promise<void>}
234
238
  */
235
- setWarningAlert(channel, limitV, latch = false) {
239
+ async setWarningAlert(channel, limitV, latch = false) {
236
240
  const ch = this._channelValid(channel);
237
241
  const raw = (Math.floor(limitV / 40e-6) << 3) & 0xFFF8;
238
- this._writeReg(_WARN_REGS[ch - 1], raw);
239
- const cfg = this._readReg(_REG_MASK_EN);
240
- this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0800) : (cfg & ~0x0800));
242
+ await this._writeReg(_WARN_REGS[ch - 1], raw);
243
+ const cfg = await this._readReg(_REG_MASK_EN);
244
+ await this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0800) : (cfg & ~0x0800));
241
245
  }
242
246
 
243
247
  /**
@@ -246,9 +250,9 @@ class INA3221Full extends INA3221Minimal {
246
250
  * Reading this register clears the latched alert flags (CF1/CF2/CF3,
247
251
  * WF1/WF2/WF3, SF) when latch mode is enabled.
248
252
  *
249
- * @returns {number} Raw 16-bit Mask/Enable register value.
253
+ * @returns {Promise<number>} Raw 16-bit Mask/Enable register value.
250
254
  */
251
- alertFlags() {
255
+ async alertFlags() {
252
256
  return this._readReg(_REG_MASK_EN);
253
257
  }
254
258
 
@@ -256,86 +260,101 @@ class INA3221Full extends INA3221Minimal {
256
260
  * Configure the shunt-voltage summation function.
257
261
  * @param {number[]} channels - Array of channel numbers to sum (e.g. [1, 2, 3]).
258
262
  * @param {number} limitV - Shunt-voltage sum limit in volts.
263
+ * @returns {Promise<void>}
259
264
  */
260
- setSummationChannels(channels, limitV) {
261
- let cfg = this._readReg(_REG_MASK_EN) & ~0xE000;
265
+ async setSummationChannels(channels, limitV) {
266
+ // SCC1/SCC2/SCC3 occupy bits 14/13/12 - clear all three (0x7000), not
267
+ // just bits 15:13 (0xE000), or a previously-set SCC3 would survive a
268
+ // call that no longer includes channel 3.
269
+ let cfg = (await this._readReg(_REG_MASK_EN)) & ~0x7000;
262
270
  for (const ch of channels) {
263
271
  this._channelValid(ch);
264
- cfg |= 1 << (15 - (Number(ch) - 1));
272
+ // SCC1/SCC2/SCC3 are bits 14/13/12 (same positions as CH1en/
273
+ // CH2en/CH3en in enableChannel) - 14-(ch-1), not 15-(ch-1), or
274
+ // channel 1 would incorrectly toggle the reserved bit 15
275
+ // instead of SCC1.
276
+ cfg |= 1 << (14 - (Number(ch) - 1));
265
277
  }
266
- this._writeReg(_REG_MASK_EN, cfg);
278
+ await this._writeReg(_REG_MASK_EN, cfg);
267
279
  const raw = (Math.floor(limitV / 40e-6) << 1) & 0xFFFE;
268
- this._writeReg(_REG_SUM_LIMIT, raw);
280
+ await this._writeReg(_REG_SUM_LIMIT, raw);
269
281
  }
270
282
 
271
283
  /**
272
284
  * Read the shunt-voltage sum.
273
- * @returns {number} Sum of selected channels' shunt voltages in volts.
285
+ * @returns {Promise<number>} Sum of selected channels' shunt voltages in volts.
274
286
  */
275
- summationValue() {
276
- const raw = this._readRegSigned(_REG_SUM);
277
- return raw * 5e-6;
287
+ async summationValue() {
288
+ // The Sum register is 14-bit signed, left-aligned by 1 bit (bit 0
289
+ // reserved) -- not by 3 bits like the per-channel shunt registers --
290
+ // so the raw-as-i16 shortcut scale is 40e-6 / 2 = 20e-6, not 5e-6.
291
+ const raw = await this._readRegSigned(_REG_SUM);
292
+ return raw * 20e-6;
278
293
  }
279
294
 
280
295
  /**
281
296
  * Set the Power-Valid upper and lower voltage limits.
282
297
  * @param {number} upperV - Upper bus voltage limit in volts.
283
298
  * @param {number} lowerV - Lower bus voltage limit in volts.
299
+ * @returns {Promise<void>}
284
300
  */
285
- setPowerValidLimits(upperV, lowerV) {
301
+ async setPowerValidLimits(upperV, lowerV) {
286
302
  const rawUpper = (Math.floor(upperV / 8e-3) << 3) & 0xFFF8;
287
303
  const rawLower = (Math.floor(lowerV / 8e-3) << 3) & 0xFFF8;
288
- this._writeReg(_REG_PV_UPPER, rawUpper);
289
- this._writeReg(_REG_PV_LOWER, rawLower);
304
+ await this._writeReg(_REG_PV_UPPER, rawUpper);
305
+ await this._writeReg(_REG_PV_LOWER, rawLower);
290
306
  }
291
307
 
292
308
  /**
293
309
  * Read the Power-Valid flag (PVF).
294
- * @returns {boolean} true if all enabled bus voltages are within the PV limits.
310
+ * @returns {Promise<boolean>} true if all enabled bus voltages are within the PV limits.
295
311
  */
296
- powerValid() {
297
- return (this._readReg(_REG_MASK_EN) & INA3221Full.PVF) !== 0;
312
+ async powerValid() {
313
+ return ((await this._readReg(_REG_MASK_EN)) & INA3221Full.PVF) !== 0;
298
314
  }
299
315
 
300
316
  /**
301
317
  * Enter power-down mode and save the current mode for wake().
318
+ * @returns {Promise<void>}
302
319
  */
303
- shutdown() {
304
- const cfg = this._readReg(_REG_CONFIG);
320
+ async shutdown() {
321
+ const cfg = await this._readReg(_REG_CONFIG);
305
322
  this._mode = cfg & 0x07;
306
- this._writeReg(_REG_CONFIG, cfg & 0xFFF8);
323
+ await this._writeReg(_REG_CONFIG, cfg & 0xFFF8);
307
324
  }
308
325
 
309
326
  /**
310
327
  * Restore the operating mode saved by shutdown().
328
+ * @returns {Promise<void>}
311
329
  */
312
- wake() {
313
- const cfg = this._readReg(_REG_CONFIG);
314
- this._writeReg(_REG_CONFIG, (cfg & 0xFFF8) | this._mode);
330
+ async wake() {
331
+ const cfg = await this._readReg(_REG_CONFIG);
332
+ await this._writeReg(_REG_CONFIG, (cfg & 0xFFF8) | this._mode);
315
333
  }
316
334
 
317
335
  /**
318
336
  * Reset all registers to power-on defaults.
337
+ * @returns {Promise<void>}
319
338
  */
320
- reset() {
321
- this._writeReg(_REG_CONFIG, 0x8000);
339
+ async reset() {
340
+ await this._writeReg(_REG_CONFIG, 0x8000);
322
341
  }
323
342
 
324
343
  /**
325
344
  * Read the Manufacturer ID register.
326
- * @returns {number} Manufacturer ID; expect 0x5449 (Texas Instruments).
345
+ * @returns {Promise<number>} Manufacturer ID; expect 0x5449 (Texas Instruments).
327
346
  */
328
- manufacturerId() {
347
+ async manufacturerId() {
329
348
  return this._readReg(_REG_MFR_ID);
330
349
  }
331
350
 
332
351
  /**
333
352
  * Read the Die ID register.
334
- * @returns {number} Die revision ID; expect 0x3220.
353
+ * @returns {Promise<number>} Die revision ID; expect 0x3220.
335
354
  */
336
- dieId() {
355
+ async dieId() {
337
356
  return this._readReg(_REG_DIE_ID);
338
357
  }
339
358
  }
340
359
 
341
- module.exports = { INA3221Minimal, INA3221Full };
360
+ module.exports = { INA3221Minimal, INA3221Full };