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
@@ -0,0 +1,184 @@
1
+ 'use strict';
2
+
3
+ const RATE_TO_PULSES = { 10: 25, 40: 27 };
4
+
5
+ /**
6
+ * HX710A 24-bit ADC — minimal interface.
7
+ *
8
+ * Reads signed 24-bit ADC values from the single differential input
9
+ * (INN/INP) at the chip's fixed Gain 128, 10 SPS default. No configuration
10
+ * beyond the connection is required. The first post-power-up conversion is
11
+ * discarded during construction.
12
+ */
13
+ class HX710AMinimal {
14
+ /**
15
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
16
+ */
17
+ constructor(connection) {
18
+ this._conn = connection;
19
+ this._conn.readRaw(25);
20
+ }
21
+
22
+ /**
23
+ * Return true if a conversion result is available (DOUT is LOW).
24
+ *
25
+ * Non-blocking.
26
+ *
27
+ * @returns {boolean} True when DOUT is LOW (data ready).
28
+ */
29
+ isReady() {
30
+ return this._conn.isReady();
31
+ }
32
+
33
+ /**
34
+ * Block until data is ready and return a signed 24-bit ADC value.
35
+ *
36
+ * Reads the differential input at Gain 128, 10 SPS.
37
+ *
38
+ * @returns {number} Signed 24-bit ADC value (-8 388 608 to +8 388 607).
39
+ */
40
+ readRaw() {
41
+ return this._conn.readRaw(25);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * HX710A full interface — extends HX710AMinimal with rate selection, tare,
47
+ * calibration, temperature, and power management.
48
+ *
49
+ * The HX710A has no software-selectable gain and no second input channel —
50
+ * unlike the HX711, only the output rate (10 or 40 SPS) is selectable for
51
+ * the differential-input reading.
52
+ */
53
+ class HX710AFull extends HX710AMinimal {
54
+ /**
55
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
56
+ */
57
+ constructor(connection) {
58
+ super(connection);
59
+ this._pulses = 25;
60
+ this._offset = 0;
61
+ this._scale = 1.0;
62
+ }
63
+
64
+ /**
65
+ * Block until data is ready and return a signed 24-bit ADC value.
66
+ *
67
+ * Uses the currently selected output rate.
68
+ *
69
+ * @returns {number} Signed 24-bit ADC value.
70
+ */
71
+ readRaw() {
72
+ return this._conn.readRaw(this._pulses);
73
+ }
74
+
75
+ /**
76
+ * Select the differential-input output rate.
77
+ *
78
+ * Issues one dummy read to apply the new rate before returning.
79
+ *
80
+ * @param {number} rate - 10 or 40 (samples per second).
81
+ * @throws {Error} If rate is not 10 or 40.
82
+ */
83
+ setRate(rate) {
84
+ if (!(rate in RATE_TO_PULSES))
85
+ throw new Error('rate must be 10 or 40');
86
+ this._pulses = RATE_TO_PULSES[rate];
87
+ this._conn.readRaw(this._pulses);
88
+ }
89
+
90
+ /**
91
+ * Return the average of multiple raw differential-input readings.
92
+ * @param {number} times - Number of readings to average (default 10).
93
+ * @returns {number} Average signed 24-bit ADC value.
94
+ */
95
+ readAverage(times = 10) {
96
+ let total = 0;
97
+ for (let i = 0; i < times; i++)
98
+ total += this.readRaw();
99
+ return Math.trunc(total / times);
100
+ }
101
+
102
+ /**
103
+ * Capture the current average reading as the zero offset.
104
+ * @param {number} times - Number of readings to average (default 10).
105
+ */
106
+ tare(times = 10) {
107
+ this._offset = this.readAverage(times);
108
+ }
109
+
110
+ /**
111
+ * Return the stored tare offset.
112
+ * @returns {number} Offset captured by the last tare() call.
113
+ */
114
+ getOffset() {
115
+ return this._offset;
116
+ }
117
+
118
+ /**
119
+ * Set the calibration scale factor.
120
+ *
121
+ * Calibrate: factor = (readAverage() - getOffset()) / known_weight.
122
+ *
123
+ * @param {number} factor - Scale factor (ADC counts per unit weight).
124
+ */
125
+ setScale(factor) {
126
+ this._scale = factor;
127
+ }
128
+
129
+ /**
130
+ * Return the current calibration scale factor.
131
+ * @returns {number} Scale factor set by the last setScale() call.
132
+ */
133
+ getScale() {
134
+ return this._scale;
135
+ }
136
+
137
+ /**
138
+ * Return the calibrated weight in the units defined by the scale factor.
139
+ *
140
+ * Computes (readAverage(times) - offset) / scale.
141
+ *
142
+ * @param {number} times - Number of readings to average (default 1).
143
+ * @returns {number} Calibrated weight value.
144
+ */
145
+ readWeight(times = 1) {
146
+ return (this.readAverage(times) - this._offset) / this._scale;
147
+ }
148
+
149
+ /**
150
+ * Block until data is ready and return the raw on-chip temperature code.
151
+ *
152
+ * Clocks 26 pulses (temperature channel, 40 Hz). This is **not** a
153
+ * calibrated degrees-Celsius value — the datasheet gives only a typical
154
+ * resolution of ~20.4 LSB/°C and states offset and gain vary
155
+ * significantly chip-to-chip. Use for relative drift tracking, or
156
+ * calibrate against a reference thermometer for absolute readings.
157
+ *
158
+ * @returns {number} Signed 24-bit raw ADC code from the temperature sensor.
159
+ */
160
+ readTemperatureRaw() {
161
+ return this._conn.readRaw(26);
162
+ }
163
+
164
+ /**
165
+ * Enter power-down mode (PD_SCK held HIGH for >60 µs).
166
+ */
167
+ powerDown() {
168
+ this._conn.powerDown();
169
+ }
170
+
171
+ /**
172
+ * Exit power-down, reset chip, discard settling conversion.
173
+ *
174
+ * Resets to the differential input at Gain 128, 10 SPS and discards
175
+ * the first post-reset conversion.
176
+ */
177
+ powerUp() {
178
+ this._conn.powerUp();
179
+ this._pulses = 25;
180
+ this._conn.readRaw(25);
181
+ }
182
+ }
183
+
184
+ module.exports = { HX710AMinimal, HX710AFull };
@@ -0,0 +1,186 @@
1
+ 'use strict';
2
+
3
+ const RATE_TO_PULSES = { 10: 25, 40: 27 };
4
+
5
+ /**
6
+ * HX710B 24-bit ADC — minimal interface.
7
+ *
8
+ * Reads signed 24-bit ADC values from the single differential input
9
+ * (INN/INP) at the chip's fixed Gain 128, 10 SPS default. No configuration
10
+ * beyond the connection is required. The first post-power-up conversion is
11
+ * discarded during construction.
12
+ */
13
+ class HX710BMinimal {
14
+ /**
15
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
16
+ */
17
+ constructor(connection) {
18
+ this._conn = connection;
19
+ this._conn.readRaw(25);
20
+ }
21
+
22
+ /**
23
+ * Return true if a conversion result is available (DOUT is LOW).
24
+ *
25
+ * Non-blocking.
26
+ *
27
+ * @returns {boolean} True when DOUT is LOW (data ready).
28
+ */
29
+ isReady() {
30
+ return this._conn.isReady();
31
+ }
32
+
33
+ /**
34
+ * Block until data is ready and return a signed 24-bit ADC value.
35
+ *
36
+ * Reads the differential input at Gain 128, 10 SPS.
37
+ *
38
+ * @returns {number} Signed 24-bit ADC value (-8 388 608 to +8 388 607).
39
+ */
40
+ readRaw() {
41
+ return this._conn.readRaw(25);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * HX710B full interface — extends HX710BMinimal with rate selection, tare,
47
+ * calibration, supply-difference monitoring, and power management.
48
+ *
49
+ * The HX710B has no software-selectable gain and no second input channel —
50
+ * unlike the HX711, only the output rate (10 or 40 SPS) is selectable for
51
+ * the differential-input reading. Pulse count 26 reads the DVDD−AVDD
52
+ * supply-voltage difference for battery-monitoring applications.
53
+ */
54
+ class HX710BFull extends HX710BMinimal {
55
+ /**
56
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
57
+ */
58
+ constructor(connection) {
59
+ super(connection);
60
+ this._pulses = 25;
61
+ this._offset = 0;
62
+ this._scale = 1.0;
63
+ }
64
+
65
+ /**
66
+ * Block until data is ready and return a signed 24-bit ADC value.
67
+ *
68
+ * Uses the currently selected output rate.
69
+ *
70
+ * @returns {number} Signed 24-bit ADC value.
71
+ */
72
+ readRaw() {
73
+ return this._conn.readRaw(this._pulses);
74
+ }
75
+
76
+ /**
77
+ * Select the differential-input output rate.
78
+ *
79
+ * Issues one dummy read to apply the new rate before returning.
80
+ *
81
+ * @param {number} rate - 10 or 40 (samples per second).
82
+ * @throws {Error} If rate is not 10 or 40.
83
+ */
84
+ setRate(rate) {
85
+ if (!(rate in RATE_TO_PULSES))
86
+ throw new Error('rate must be 10 or 40');
87
+ this._pulses = RATE_TO_PULSES[rate];
88
+ this._conn.readRaw(this._pulses);
89
+ }
90
+
91
+ /**
92
+ * Return the average of multiple raw differential-input readings.
93
+ * @param {number} times - Number of readings to average (default 10).
94
+ * @returns {number} Average signed 24-bit ADC value.
95
+ */
96
+ readAverage(times = 10) {
97
+ let total = 0;
98
+ for (let i = 0; i < times; i++)
99
+ total += this.readRaw();
100
+ return Math.trunc(total / times);
101
+ }
102
+
103
+ /**
104
+ * Capture the current average reading as the zero offset.
105
+ * @param {number} times - Number of readings to average (default 10).
106
+ */
107
+ tare(times = 10) {
108
+ this._offset = this.readAverage(times);
109
+ }
110
+
111
+ /**
112
+ * Return the stored tare offset.
113
+ * @returns {number} Offset captured by the last tare() call.
114
+ */
115
+ getOffset() {
116
+ return this._offset;
117
+ }
118
+
119
+ /**
120
+ * Set the calibration scale factor.
121
+ *
122
+ * Calibrate: factor = (readAverage() - getOffset()) / known_weight.
123
+ *
124
+ * @param {number} factor - Scale factor (ADC counts per unit weight).
125
+ */
126
+ setScale(factor) {
127
+ this._scale = factor;
128
+ }
129
+
130
+ /**
131
+ * Return the current calibration scale factor.
132
+ * @returns {number} Scale factor set by the last setScale() call.
133
+ */
134
+ getScale() {
135
+ return this._scale;
136
+ }
137
+
138
+ /**
139
+ * Return the calibrated weight in the units defined by the scale factor.
140
+ *
141
+ * Computes (readAverage(times) - offset) / scale.
142
+ *
143
+ * @param {number} times - Number of readings to average (default 1).
144
+ * @returns {number} Calibrated weight value.
145
+ */
146
+ readWeight(times = 1) {
147
+ return (this.readAverage(times) - this._offset) / this._scale;
148
+ }
149
+
150
+ /**
151
+ * Block until data is ready and return the raw DVDD−AVDD code.
152
+ *
153
+ * Clocks 26 pulses (DVDD−AVDD channel, 40 Hz). This is **not** a
154
+ * calibrated volts value — the datasheet gives no LSB-to-volts scale
155
+ * factor for this reading; it is described only qualitatively as
156
+ * usable for battery-voltage detection. Use for relative drift
157
+ * tracking against a known-good baseline, or calibrate against a
158
+ * reference voltmeter for absolute readings.
159
+ *
160
+ * @returns {number} Signed 24-bit raw ADC code from the supply-difference channel.
161
+ */
162
+ readSupplyDiffRaw() {
163
+ return this._conn.readRaw(26);
164
+ }
165
+
166
+ /**
167
+ * Enter power-down mode (PD_SCK held HIGH for >60 µs).
168
+ */
169
+ powerDown() {
170
+ this._conn.powerDown();
171
+ }
172
+
173
+ /**
174
+ * Exit power-down, reset chip, discard settling conversion.
175
+ *
176
+ * Resets to the differential input at Gain 128, 10 SPS and discards
177
+ * the first post-reset conversion.
178
+ */
179
+ powerUp() {
180
+ this._conn.powerUp();
181
+ this._pulses = 25;
182
+ this._conn.readRaw(25);
183
+ }
184
+ }
185
+
186
+ module.exports = { HX710BMinimal, HX710BFull };
@@ -6,16 +6,16 @@ const GAIN_TO_PULSES = { 128: 25, 32: 26, 64: 27 };
6
6
  * HX711 24-bit ADC — minimal interface.
7
7
  *
8
8
  * Reads signed 24-bit ADC values using Channel A, Gain 128. No configuration
9
- * beyond the transport is required. The first post-power-up conversion is
9
+ * beyond the connection is required. The first post-power-up conversion is
10
10
  * discarded during construction.
11
11
  */
12
12
  class HX711Minimal {
13
13
  /**
14
- * @param {object} transport - Configured HX711 transport (HX711Transport).
14
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
15
15
  */
16
- constructor(transport) {
17
- this._transport = transport;
18
- this._transport.readRaw(25);
16
+ constructor(connection) {
17
+ this._conn = connection;
18
+ this._conn.readRaw(25);
19
19
  }
20
20
 
21
21
  /**
@@ -26,7 +26,7 @@ class HX711Minimal {
26
26
  * @returns {boolean} True when DOUT is LOW (data ready).
27
27
  */
28
28
  isReady() {
29
- return this._transport.isReady();
29
+ return this._conn.isReady();
30
30
  }
31
31
 
32
32
  /**
@@ -37,7 +37,7 @@ class HX711Minimal {
37
37
  * @returns {number} Signed 24-bit ADC value (-8 388 608 to +8 388 607).
38
38
  */
39
39
  readRaw() {
40
- return this._transport.readRaw(25);
40
+ return this._conn.readRaw(25);
41
41
  }
42
42
  }
43
43
 
@@ -49,10 +49,10 @@ class HX711Minimal {
49
49
  */
50
50
  class HX711Full extends HX711Minimal {
51
51
  /**
52
- * @param {object} transport - Configured HX711 transport (HX711Transport).
52
+ * @param {import('../../connection/hx711').HX711Connection} connection - Configured HX711 connection.
53
53
  */
54
- constructor(transport) {
55
- super(transport);
54
+ constructor(connection) {
55
+ super(connection);
56
56
  this._pulses = 25;
57
57
  this._offset = 0;
58
58
  this._scale = 1.0;
@@ -66,7 +66,7 @@ class HX711Full extends HX711Minimal {
66
66
  * @returns {number} Signed 24-bit ADC value.
67
67
  */
68
68
  readRaw() {
69
- return this._transport.readRaw(this._pulses);
69
+ return this._conn.readRaw(this._pulses);
70
70
  }
71
71
 
72
72
  /**
@@ -81,7 +81,7 @@ class HX711Full extends HX711Minimal {
81
81
  if (!(gain in GAIN_TO_PULSES))
82
82
  throw new Error('gain must be 128, 64, or 32');
83
83
  this._pulses = GAIN_TO_PULSES[gain];
84
- this._transport.readRaw(this._pulses);
84
+ this._conn.readRaw(this._pulses);
85
85
  }
86
86
 
87
87
  /**
@@ -147,7 +147,7 @@ class HX711Full extends HX711Minimal {
147
147
  * Enter power-down mode (PD_SCK held HIGH for >60 µs).
148
148
  */
149
149
  powerDown() {
150
- this._transport.powerDown();
150
+ this._conn.powerDown();
151
151
  }
152
152
 
153
153
  /**
@@ -156,9 +156,9 @@ class HX711Full extends HX711Minimal {
156
156
  * Resets to Channel A, Gain 128 and discards the first post-reset conversion.
157
157
  */
158
158
  powerUp() {
159
- this._transport.powerUp();
159
+ this._conn.powerUp();
160
160
  this._pulses = 25;
161
- this._transport.readRaw(25);
161
+ this._conn.readRaw(25);
162
162
  }
163
163
  }
164
164
 
@@ -10,43 +10,45 @@ const GC_WAKE = 0x09;
10
10
  * MCP4725 single-channel 12-bit voltage-output DAC — minimal interface.
11
11
  *
12
12
  * Provides simple voltage output as a fraction of V_DD with no configuration
13
- * beyond the transport. Uses Fast Write (2-byte) for DAC register updates.
13
+ * beyond the connection. Uses Fast Write (2-byte) for DAC register updates.
14
14
  *
15
- * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x61).
15
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x61).
16
16
  */
17
17
  class MCP4725Minimal {
18
18
  /**
19
- * @param {object} transport - Configured I²C transport pointing at the device.
19
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
20
20
  */
21
- constructor(transport) {
22
- this._transport = transport;
21
+ constructor(connection) {
22
+ this._conn = connection;
23
23
  }
24
24
 
25
25
  /**
26
26
  * Set the DAC output as a fraction of V_DD.
27
27
  * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
28
+ * @returns {Promise<void>}
28
29
  */
29
- set_voltage(fraction) {
30
+ async set_voltage(fraction) {
30
31
  if (fraction < 0.0) fraction = 0.0;
31
32
  if (fraction > 1.0) fraction = 1.0;
32
33
  const code = Math.round(fraction * 4095);
33
- this._fast_write(code, 0);
34
+ await this._fast_write(code, 0);
34
35
  }
35
36
 
36
37
  /**
37
38
  * Set the raw 12-bit DAC code directly.
38
39
  * @param {number} code - Raw 12-bit DAC code (0–4095).
40
+ * @returns {Promise<void>}
39
41
  */
40
- set_raw(code) {
42
+ async set_raw(code) {
41
43
  if (code > 4095) code = 4095;
42
- this._fast_write(code, 0);
44
+ await this._fast_write(code, 0);
43
45
  }
44
46
 
45
- _fast_write(code, pd_mode) {
47
+ async _fast_write(code, pd_mode) {
46
48
  const buf = Buffer.alloc(2);
47
49
  buf[0] = ((pd_mode & 0x03) << 4) | ((code >> 8) & 0x0F);
48
50
  buf[1] = code & 0xFF;
49
- this._transport.write(buf);
51
+ await this._conn.write(buf);
50
52
  }
51
53
  }
52
54
 
@@ -56,14 +58,14 @@ class MCP4725Minimal {
56
58
  * Adds write-with-EEPROM persistence, power-down modes, General Call reset/wake,
57
59
  * and full register read-back of both DAC and EEPROM contents.
58
60
  *
59
- * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x61).
61
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x61).
60
62
  */
61
63
  class MCP4725Full extends MCP4725Minimal {
62
64
  /**
63
- * @param {object} transport - Configured I²C transport pointing at the device.
65
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
64
66
  */
65
- constructor(transport) {
66
- super(transport);
67
+ constructor(connection) {
68
+ super(connection);
67
69
  }
68
70
 
69
71
  /** @type {number} Normal operation (power-down mode 0). */
@@ -78,35 +80,38 @@ class MCP4725Full extends MCP4725Minimal {
78
80
  /**
79
81
  * Set the DAC output and persist to EEPROM.
80
82
  * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
83
+ * @returns {Promise<void>}
81
84
  */
82
- set_voltage_eeprom(fraction) {
85
+ async set_voltage_eeprom(fraction) {
83
86
  if (fraction < 0.0) fraction = 0.0;
84
87
  if (fraction > 1.0) fraction = 1.0;
85
88
  const code = Math.round(fraction * 4095);
86
- this._write_dac_eeprom(code, 0);
89
+ await this._write_dac_eeprom(code, 0);
87
90
  }
88
91
 
89
92
  /**
90
93
  * Set the raw 12-bit DAC code and persist to EEPROM.
91
94
  * @param {number} code - Raw 12-bit DAC code (0–4095).
95
+ * @returns {Promise<void>}
92
96
  */
93
- set_raw_eeprom(code) {
97
+ async set_raw_eeprom(code) {
94
98
  if (code > 4095) code = 4095;
95
- this._write_dac_eeprom(code, 0);
99
+ await this._write_dac_eeprom(code, 0);
96
100
  }
97
101
 
98
102
  /**
99
103
  * Read the current DAC register and EEPROM contents.
100
- * @returns {{code: number, voltage_fraction: number, power_down: number, eeprom_code: number, eeprom_power_down: number, eeprom_ready: boolean}} Dictionary with code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready.
104
+ * @returns {Promise<{code: number, voltage_fraction: number, power_down: number, eeprom_code: number, eeprom_power_down: number, eeprom_ready: boolean}>} Dictionary with code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready.
101
105
  */
102
- read() {
103
- const buf = this._transport.writeRead(Buffer.from([0x00]), 5);
106
+ async read() {
107
+ const buf = await this._conn.writeRead(Buffer.from([0x00]), 5);
108
+ const code = (buf[1] << 4) | ((buf[2] >> 4) & 0x0F);
104
109
  return {
105
- code: ((buf[1] & 0x0F) << 8) | buf[2],
106
- voltage_fraction: (((buf[1] & 0x0F) << 8) | buf[2]) / 4095.0,
110
+ code: code,
111
+ voltage_fraction: code / 4095.0,
107
112
  power_down: (buf[0] >> 2) & 0x03,
108
113
  eeprom_code: ((buf[3] & 0x0F) << 8) | buf[4],
109
- eeprom_power_down: (buf[3] >> 6) & 0x03,
114
+ eeprom_power_down: (buf[3] >> 5) & 0x03,
110
115
  eeprom_ready: !!(buf[0] & 0x80),
111
116
  };
112
117
  }
@@ -114,48 +119,51 @@ class MCP4725Full extends MCP4725Minimal {
114
119
  /**
115
120
  * Set the power-down mode and preserve the current DAC code.
116
121
  * @param {number} mode - Power-down mode 0–3 (0 = normal, 1 = 1 kΩ to GND, 2 = 100 kΩ to GND, 3 = 500 kΩ to GND).
122
+ * @returns {Promise<void>}
117
123
  */
118
- set_power_down(mode) {
124
+ async set_power_down(mode) {
119
125
  if (mode > 3) mode = 3;
120
- const code = this._read_dac_code();
121
- this._fast_write(code, mode);
126
+ const code = await this._read_dac_code();
127
+ await this._fast_write(code, mode);
122
128
  }
123
129
 
124
130
  /**
125
131
  * Send General Call Wake-Up (0x00, 0x09) to clear power-down bits.
132
+ * @returns {Promise<void>}
126
133
  */
127
- wake_up() {
128
- this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
134
+ async wake_up() {
135
+ await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
129
136
  }
130
137
 
131
138
  /**
132
139
  * Send General Call Reset (0x00, 0x06) to trigger internal POR.
140
+ * @returns {Promise<void>}
133
141
  */
134
- reset() {
135
- this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
142
+ async reset() {
143
+ await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
136
144
  }
137
145
 
138
146
  /**
139
147
  * Check if the EEPROM write operation is complete.
140
- * @returns {boolean} True when a pending EEPROM write has finished.
148
+ * @returns {Promise<boolean>} True when a pending EEPROM write has finished.
141
149
  */
142
- is_eeprom_ready() {
143
- const buf = this._transport.writeRead(Buffer.from([0x00]), 1);
150
+ async is_eeprom_ready() {
151
+ const buf = await this._conn.writeRead(Buffer.from([0x00]), 1);
144
152
  return !!(buf[0] & 0x80);
145
153
  }
146
154
 
147
- _write_dac_eeprom(code, pd_mode) {
155
+ async _write_dac_eeprom(code, pd_mode) {
148
156
  const buf = Buffer.alloc(3);
149
157
  buf[0] = CMD_WRITE_DAC_EEPROM | ((pd_mode & 0x03) << 1);
150
158
  buf[1] = (code >> 4) & 0xFF;
151
159
  buf[2] = (code & 0x0F) << 4;
152
- this._transport.write(buf);
160
+ await this._conn.write(buf);
153
161
  }
154
162
 
155
- _read_dac_code() {
156
- const buf = this._transport.writeRead(Buffer.from([0x00]), 2);
163
+ async _read_dac_code() {
164
+ const buf = await this._conn.writeRead(Buffer.from([0x00]), 2);
157
165
  return ((buf[0] & 0x0F) << 8) | buf[1];
158
166
  }
159
167
  }
160
168
 
161
- module.exports = { MCP4725Minimal, MCP4725Full };
169
+ module.exports = { MCP4725Minimal, MCP4725Full };