periph 1.1.0 → 1.2.0

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 (231) hide show
  1. package/README.md +54 -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/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +23 -20
  138. package/examples/pressure/bmp280/demo.js +44 -41
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +15 -15
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +57 -44
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +42 -33
  173. package/src/chips/environmental/aht21.js +52 -37
  174. package/src/chips/environmental/bme280.js +85 -66
  175. package/src/chips/environmental/bme680.js +110 -87
  176. package/src/chips/gas/ens160.js +82 -64
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +89 -66
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +158 -123
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +32 -29
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +72 -53
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/{transport → connection}/hx711.js +49 -22
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/gnss/.gitkeep +0 -0
  230. package/examples/humidity/.gitkeep +0 -0
  231. package/src/chips/gnss/.gitkeep +0 -0
@@ -16,47 +16,50 @@ const GC_WAKE = 0x09;
16
16
  *
17
17
  * Provides simple voltage output as a fraction of V_DD for any of the four
18
18
  * channels (A–D) plus a convenience method to update all four channels
19
- * simultaneously. No configuration required beyond the transport. V_REF is
19
+ * simultaneously. No configuration required beyond the connection. V_REF is
20
20
  * fixed at external (V_DD), gain is fixed at ×1, and power-down is off.
21
21
  * EEPROM is never written by this class.
22
22
  *
23
- * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
23
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x67).
24
24
  */
25
25
  class MCP4728Minimal {
26
26
  /**
27
- * @param {object} transport - Configured I²C transport pointing at the device.
27
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
28
28
  */
29
- constructor(transport) {
30
- this._transport = transport;
29
+ constructor(connection) {
30
+ this._conn = connection;
31
31
  }
32
32
 
33
33
  /**
34
34
  * Set one channel's DAC output as a fraction of V_DD.
35
35
  * @param {number} channel - Channel index 0 (A) – 3 (D).
36
36
  * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
37
+ * @returns {Promise<void>}
37
38
  */
38
- set_voltage(channel, fraction) {
39
+ async set_voltage(channel, fraction) {
39
40
  let f = Math.max(0.0, Math.min(1.0, fraction));
40
41
  const code = Math.round(f * 4095);
41
- this.set_raw(channel, code);
42
+ await this.set_raw(channel, code);
42
43
  }
43
44
 
44
45
  /**
45
46
  * Set one channel's raw 12-bit DAC code.
46
47
  * @param {number} channel - Channel index 0 (A) – 3 (D).
47
48
  * @param {number} code - Raw 12-bit DAC code (0–4095).
49
+ * @returns {Promise<void>}
48
50
  */
49
- set_raw(channel, code) {
51
+ async set_raw(channel, code) {
50
52
  const ch = Math.max(0, Math.min(3, channel | 0));
51
53
  const c = Math.max(0, Math.min(4095, code | 0));
52
- this._multi_write(ch, c, 0, 0, 0, 0);
54
+ await this._multi_write(ch, c, 0, 0, 0, 0);
53
55
  }
54
56
 
55
57
  /**
56
58
  * Update all four channels simultaneously using Fast Write.
57
59
  * @param {number[]} fractions - Array of 4 fractions (0.0–1.0), index 0 = A.
60
+ * @returns {Promise<void>}
58
61
  */
59
- set_all(fractions) {
62
+ async set_all(fractions) {
60
63
  if (!Array.isArray(fractions) || fractions.length !== 4) {
61
64
  throw new Error('fractions must have exactly 4 elements');
62
65
  }
@@ -68,16 +71,16 @@ class MCP4728Minimal {
68
71
  buf[i * 2] = (code >> 8) & 0x0F;
69
72
  buf[i * 2 + 1] = code & 0xFF;
70
73
  }
71
- this._transport.write(buf);
74
+ await this._conn.write(buf);
72
75
  }
73
76
 
74
- _multi_write(channel, code, vref, pd, gain, udac) {
77
+ async _multi_write(channel, code, vref, pd, gain, udac) {
75
78
  const buf = Buffer.alloc(3);
76
79
  buf[0] = CMD_MULTI_WRITE_BASE | ((channel & 0x03) << 1) | (udac & 0x01);
77
80
  buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
78
81
  ((gain & 0x01) << 4) | ((code >> 8) & 0x0F);
79
82
  buf[2] = code & 0xFF;
80
- this._transport.write(buf);
83
+ await this._conn.write(buf);
81
84
  }
82
85
  }
83
86
 
@@ -89,7 +92,7 @@ class MCP4728Minimal {
89
92
  * Write), General Call reset/wake-up/software-update, and full 24-byte
90
93
  * read-back of all channel DAC input registers and EEPROM contents.
91
94
  *
92
- * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
95
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x67).
93
96
  */
94
97
  class MCP4728Full extends MCP4728Minimal {
95
98
  /** @type {number} Normal operation (power-down mode 0). */
@@ -112,10 +115,10 @@ class MCP4728Full extends MCP4728Minimal {
112
115
  static GAIN_X2 = 1;
113
116
 
114
117
  /**
115
- * @param {object} transport - Configured I²C transport pointing at the device.
118
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
116
119
  */
117
- constructor(transport) {
118
- super(transport);
120
+ constructor(connection) {
121
+ super(connection);
119
122
  }
120
123
 
121
124
  /**
@@ -124,11 +127,12 @@ class MCP4728Full extends MCP4728Minimal {
124
127
  * @param {number} fraction - Output as a fraction of the configured full-scale.
125
128
  * @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
126
129
  * @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
130
+ * @returns {Promise<void>}
127
131
  */
128
- set_voltage_eeprom(channel, fraction, vref, gain) {
132
+ async set_voltage_eeprom(channel, fraction, vref, gain) {
129
133
  let f = Math.max(0.0, Math.min(1.0, fraction));
130
134
  const code = Math.round(f * 4095);
131
- this._single_write(channel, code, vref, 0, gain, 0);
135
+ await this._single_write(channel, code, vref, 0, gain, 0);
132
136
  }
133
137
 
134
138
  /**
@@ -137,10 +141,11 @@ class MCP4728Full extends MCP4728Minimal {
137
141
  * @param {number} code - Raw 12-bit DAC code (0–4095).
138
142
  * @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
139
143
  * @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
144
+ * @returns {Promise<void>}
140
145
  */
141
- set_raw_eeprom(channel, code, vref, gain) {
146
+ async set_raw_eeprom(channel, code, vref, gain) {
142
147
  const c = Math.max(0, Math.min(4095, code | 0));
143
- this._single_write(channel, c, vref, 0, gain, 0);
148
+ await this._single_write(channel, c, vref, 0, gain, 0);
144
149
  }
145
150
 
146
151
  /**
@@ -148,8 +153,9 @@ class MCP4728Full extends MCP4728Minimal {
148
153
  * @param {number[]} fractions - Array of 4 fractions (0.0–1.0).
149
154
  * @param {number[]} vrefs - Array of 4 V_REF values (0/1).
150
155
  * @param {number[]} gains - Array of 4 gain values (1/2).
156
+ * @returns {Promise<void>}
151
157
  */
152
- set_all_eeprom(fractions, vrefs, gains) {
158
+ async set_all_eeprom(fractions, vrefs, gains) {
153
159
  if (fractions.length !== 4 || vrefs.length !== 4 || gains.length !== 4) {
154
160
  throw new Error('fractions, vrefs, gains must each have 4 elements');
155
161
  }
@@ -166,7 +172,7 @@ class MCP4728Full extends MCP4728Minimal {
166
172
  buf[1 + i * 2] = ((v & 0x01) << 7) | ((g & 0x01) << 4) | ((code >> 8) & 0x0F);
167
173
  buf[1 + i * 2 + 1] = code & 0xFF;
168
174
  }
169
- this._transport.write(buf);
175
+ await this._conn.write(buf);
170
176
  }
171
177
 
172
178
  /**
@@ -175,12 +181,13 @@ class MCP4728Full extends MCP4728Minimal {
175
181
  * @param {number} vref_b
176
182
  * @param {number} vref_c
177
183
  * @param {number} vref_d
184
+ * @returns {Promise<void>}
178
185
  */
179
- set_vref(vref_a, vref_b, vref_c, vref_d) {
186
+ async set_vref(vref_a, vref_b, vref_c, vref_d) {
180
187
  const byte1 = CMD_WRITE_VREF |
181
188
  ((vref_a ? 1 : 0) << 3) | ((vref_b ? 1 : 0) << 2) |
182
189
  ((vref_c ? 1 : 0) << 1) | (vref_d ? 1 : 0);
183
- this._transport.write(Buffer.from([byte1]));
190
+ await this._conn.write(Buffer.from([byte1]));
184
191
  }
185
192
 
186
193
  /**
@@ -189,12 +196,13 @@ class MCP4728Full extends MCP4728Minimal {
189
196
  * @param {number} gain_b
190
197
  * @param {number} gain_c
191
198
  * @param {number} gain_d
199
+ * @returns {Promise<void>}
192
200
  */
193
- set_gain(gain_a, gain_b, gain_c, gain_d) {
201
+ async set_gain(gain_a, gain_b, gain_c, gain_d) {
194
202
  const byte1 = CMD_WRITE_GAIN |
195
203
  ((gain_a === 2 ? 1 : 0) << 3) | ((gain_b === 2 ? 1 : 0) << 2) |
196
204
  ((gain_c === 2 ? 1 : 0) << 1) | (gain_d === 2 ? 1 : 0);
197
- this._transport.write(Buffer.from([byte1]));
205
+ await this._conn.write(Buffer.from([byte1]));
198
206
  }
199
207
 
200
208
  /**
@@ -203,22 +211,23 @@ class MCP4728Full extends MCP4728Minimal {
203
211
  * @param {number} pd_b
204
212
  * @param {number} pd_c
205
213
  * @param {number} pd_d
214
+ * @returns {Promise<void>}
206
215
  */
207
- set_power_down(pd_a, pd_b, pd_c, pd_d) {
216
+ async set_power_down(pd_a, pd_b, pd_c, pd_d) {
208
217
  const byte1 = CMD_WRITE_POWERDOWN |
209
218
  (((pd_a >> 1) & 0x01) << 4) | ((pd_a & 0x01) << 3) |
210
219
  (((pd_b >> 1) & 0x01) << 2) | ((pd_b & 0x01) << 1);
211
220
  const byte2 = (((pd_c >> 1) & 0x01) << 6) | ((pd_c & 0x01) << 5) |
212
221
  (((pd_d >> 1) & 0x01) << 4) | ((pd_d & 0x01) << 3);
213
- this._transport.write(Buffer.from([byte1, byte2]));
222
+ await this._conn.write(Buffer.from([byte1, byte2]));
214
223
  }
215
224
 
216
225
  /**
217
226
  * Read all four channels' DAC input registers and EEPROM contents.
218
- * @returns {{channel: object[], eeprom_ready: boolean}} Per-channel code, vref, gain, power_down, eeprom_*.
227
+ * @returns {Promise<{channel: object[], eeprom_ready: boolean}>} Per-channel code, vref, gain, power_down, eeprom_*.
219
228
  */
220
- read() {
221
- const buf = this._transport.read(24);
229
+ async read() {
230
+ const buf = await this._conn.read(24);
222
231
  const result = { channel: [], eeprom_ready: !!(buf[0] & 0x80) };
223
232
  for (let i = 0; i < 4; i++) {
224
233
  const b = i * 3;
@@ -241,43 +250,47 @@ class MCP4728Full extends MCP4728Minimal {
241
250
 
242
251
  /**
243
252
  * Check if the EEPROM write is complete (RDY/BSY = 1).
244
- * @returns {boolean}
253
+ * @returns {Promise<boolean>}
245
254
  */
246
- is_eeprom_ready() {
247
- const buf = this._transport.read(1);
255
+ async is_eeprom_ready() {
256
+ const buf = await this._conn.read(1);
248
257
  return !!(buf[0] & 0x80);
249
258
  }
250
259
 
251
260
  /**
252
261
  * Send General Call Software Update (0x00, 0x08) to latch all V_OUT.
262
+ * @returns {Promise<void>}
253
263
  */
254
- software_update() {
255
- this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_SOFTWARE_UPD]));
264
+ async software_update() {
265
+ await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_SOFTWARE_UPD]));
256
266
  }
257
267
 
258
268
  /**
259
269
  * Send General Call Wake-Up (0x00, 0x09) to clear all PD bits.
270
+ * @returns {Promise<void>}
260
271
  */
261
- wake_up() {
262
- this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
272
+ async wake_up() {
273
+ await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
263
274
  }
264
275
 
265
276
  /**
266
277
  * Send General Call Reset (0x00, 0x06) to reload EEPROM into all DAC registers.
278
+ * @returns {Promise<void>}
267
279
  */
268
- reset() {
269
- this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
280
+ async reset() {
281
+ await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
270
282
  }
271
283
 
272
- _single_write(channel, code, vref, pd, gain, udac) {
284
+ async _single_write(channel, code, vref, pd, gain, udac) {
273
285
  const ch = Math.max(0, Math.min(3, channel | 0));
274
286
  const c = Math.max(0, Math.min(4095, code | 0));
287
+ const g = (gain === 2) ? 1 : 0;
275
288
  const buf = Buffer.alloc(3);
276
289
  buf[0] = CMD_SINGLE_WRITE | ((ch & 0x03) << 1) | (udac & 0x01);
277
290
  buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
278
- ((gain & 0x01) << 4) | ((c >> 8) & 0x0F);
291
+ (g << 4) | ((c >> 8) & 0x0F);
279
292
  buf[2] = c & 0xFF;
280
- this._transport.write(buf);
293
+ await this._conn.write(buf);
281
294
  }
282
295
  }
283
296
 
@@ -0,0 +1,196 @@
1
+ 'use strict';
2
+
3
+ const NUM_CHANNELS = 4;
4
+ const CONTROL_DEFAULT = 0x00; // AIP=00, AOE=0, AI=0, CHN=0
5
+
6
+ /**
7
+ * PCF8591 8-bit quad ADC + DAC — minimal interface.
8
+ *
9
+ * Provides single-ended reads of the four analog inputs in 4 single-ended
10
+ * mode (AIP=00). No configuration beyond the connection is required. Each
11
+ * read transaction returns 5 bytes: the first is the previous conversion
12
+ * result and must be discarded; the next four are fresh channel samples.
13
+ *
14
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x48–0x4F).
15
+ */
16
+ class PCF8591Minimal {
17
+ /**
18
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
19
+ */
20
+ constructor(connection) {
21
+ this._conn = connection;
22
+ }
23
+
24
+ /**
25
+ * Read a single channel as an unsigned 8-bit value.
26
+ *
27
+ * Uses single-shot conversion: writes the control byte selecting the
28
+ * channel, then reads 2 bytes (discarding the stale first byte).
29
+ *
30
+ * @param {number} channel - Channel number 0–3. Clamped to the valid range.
31
+ * @returns {Promise<number>} Raw 8-bit value (0–255).
32
+ */
33
+ async read_channel(channel) {
34
+ let ch = (channel >= 0 && channel < NUM_CHANNELS) ? channel : 0;
35
+ const ctrl = CONTROL_DEFAULT | (ch & 0x03);
36
+ await this._conn.write(Buffer.from([ctrl]));
37
+ const buf = await this._conn.read(2);
38
+ return buf[1];
39
+ }
40
+
41
+ /**
42
+ * Read all four channels as unsigned 8-bit values.
43
+ *
44
+ * Uses auto-increment (AI=1) to read all four channels in one
45
+ * transaction. Reads 5 bytes and discards the stale first byte.
46
+ *
47
+ * @returns {Promise<number[]>} Four raw 8-bit values [ch0, ch1, ch2, ch3].
48
+ */
49
+ async read_all() {
50
+ const ctrl = CONTROL_DEFAULT | 0x04; // AI=1
51
+ await this._conn.write(Buffer.from([ctrl]));
52
+ const buf = await this._conn.read(NUM_CHANNELS + 1);
53
+ return [buf[1], buf[2], buf[3], buf[4]];
54
+ }
55
+ }
56
+
57
+ /**
58
+ * PCF8591 full interface — extends PCF8591Minimal with differential, voltage, and DAC output.
59
+ *
60
+ * Adds analog input mode selection (single-ended, differential, mixed),
61
+ * auto-increment, DAC enable/disable, raw and voltage-calibrated ADC reads,
62
+ * and signed differential reads.
63
+ *
64
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x48–0x4F).
65
+ */
66
+ class PCF8591Full extends PCF8591Minimal {
67
+ /** @type {number} 4 single-ended inputs (AIN0–AIN3). */
68
+ static MODE_4_SINGLE_ENDED = 0;
69
+ /** @type {number} 3 differential inputs (vs AIN3). */
70
+ static MODE_3_DIFFERENTIAL = 1;
71
+ /** @type {number} AIN0/1 single-ended, AIN2-AIN3 differential. */
72
+ static MODE_MIXED = 2;
73
+ /** @type {number} 2 differential inputs. */
74
+ static MODE_2_DIFFERENTIAL = 3;
75
+
76
+ /**
77
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
78
+ */
79
+ constructor(connection) {
80
+ super(connection);
81
+ this._control = CONTROL_DEFAULT;
82
+ this._input_mode = PCF8591Full.MODE_4_SINGLE_ENDED;
83
+ this._dac_enabled = false;
84
+ this._auto_increment = false;
85
+ this._last_channel = 0;
86
+ }
87
+
88
+ /**
89
+ * Set the analog input mode, auto-increment, and DAC enable.
90
+ * @param {number} input_mode - Analog input programming 0–3 (see MODE_* constants).
91
+ * @param {boolean} auto_increment - If true, AI=1 — channel increments after each conversion.
92
+ * @param {boolean} dac_enabled - If true, AOE=1 — AOUT is active; AOUT returns to
93
+ * high-impedance when false.
94
+ * @returns {Promise<void>}
95
+ */
96
+ async configure(input_mode, auto_increment, dac_enabled) {
97
+ const aip = input_mode & 0x03;
98
+ const ai = auto_increment ? 0x04 : 0x00;
99
+ const aoe = dac_enabled ? 0x40 : 0x00;
100
+ this._control = (aip << 4) | aoe | ai | (this._last_channel & 0x03);
101
+ this._input_mode = aip;
102
+ this._auto_increment = !!auto_increment;
103
+ this._dac_enabled = !!dac_enabled;
104
+ await this._conn.write(Buffer.from([this._control]));
105
+ }
106
+
107
+ /**
108
+ * Read a single channel and convert to voltage.
109
+ * @param {number} channel - Channel number 0–3.
110
+ * @param {number} vref - Reference voltage in volts.
111
+ * @param {number} vagnd - Analog ground voltage in volts.
112
+ * @returns {Promise<number>} Channel voltage in volts.
113
+ */
114
+ async read_channel_voltage(channel, vref, vagnd) {
115
+ const raw = await this.read_channel(channel);
116
+ return vagnd + raw * (vref - vagnd) / 256.0;
117
+ }
118
+
119
+ /**
120
+ * Read all four channels and convert each to voltage.
121
+ * @param {number} vref - Reference voltage in volts.
122
+ * @param {number} vagnd - Analog ground voltage in volts.
123
+ * @returns {Promise<number[]>} Four channel voltages in volts [ch0, ch1, ch2, ch3].
124
+ */
125
+ async read_all_voltage(vref, vagnd) {
126
+ const raws = await this.read_all();
127
+ const vfs = vref - vagnd;
128
+ return raws.map(r => vagnd + r * vfs / 256.0);
129
+ }
130
+
131
+ /**
132
+ * Read a differential channel as a signed value.
133
+ *
134
+ * The chip must be configured in a differential mode (input_mode 1, 2,
135
+ * or 3). The result is interpreted as a signed 8-bit two's complement
136
+ * number.
137
+ *
138
+ * @param {number} channel - Differential channel index (0–2 for 3-diff mode, 0–1
139
+ * for 2-diff and mixed modes).
140
+ * @returns {Promise<number>} Signed 8-bit value (-128 to 127).
141
+ */
142
+ async read_differential(channel) {
143
+ const ch = channel & 0x03;
144
+ this._last_channel = ch;
145
+ const ctrl = this._control | (ch & 0x03);
146
+ await this._conn.write(Buffer.from([ctrl]));
147
+ const buf = await this._conn.read(2);
148
+ const raw = buf[1];
149
+ return raw >= 128 ? raw - 256 : raw;
150
+ }
151
+
152
+ /**
153
+ * Enable the DAC and write a raw 8-bit value.
154
+ *
155
+ * Sets the AOE bit so AOUT becomes active, then writes the DAC value
156
+ * in the byte following the control byte.
157
+ *
158
+ * @param {number} value - Raw 8-bit DAC value (0–255). Output voltage is
159
+ * V_AGND + value × (V_REF − V_AGND) / 256.
160
+ * @returns {Promise<void>}
161
+ */
162
+ async set_dac(value) {
163
+ let v = Math.max(0, Math.min(255, value | 0));
164
+ const ctrl = (this._control | 0x40) & ~0x04; // AOE=1, AI=0
165
+ this._control = ctrl;
166
+ this._dac_enabled = true;
167
+ await this._conn.write(Buffer.from([ctrl, v]));
168
+ }
169
+
170
+ /**
171
+ * Enable the DAC and set the output as a fraction of (VREF−VAGND).
172
+ * @param {number} voltage_fraction - Output level as a fraction of (VREF−VAGND)
173
+ * (0.0 = V_AGND, 1.0 = V_REF). Clamped to [0.0, 1.0].
174
+ * @returns {Promise<void>}
175
+ */
176
+ async set_dac_voltage(voltage_fraction) {
177
+ let f = voltage_fraction;
178
+ if (f < 0.0) f = 0.0;
179
+ if (f > 1.0) f = 1.0;
180
+ const value = Math.round(f * 255);
181
+ await this.set_dac(value);
182
+ }
183
+
184
+ /**
185
+ * Disable the DAC output; AOUT returns to high-impedance.
186
+ * @returns {Promise<void>}
187
+ */
188
+ async disable_dac() {
189
+ const ctrl = this._control & ~0x40; // AOE=0
190
+ this._control = ctrl;
191
+ this._dac_enabled = false;
192
+ await this._conn.write(Buffer.from([ctrl]));
193
+ }
194
+ }
195
+
196
+ module.exports = { PCF8591Minimal, PCF8591Full };