periph 1.0.1 → 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 (241) 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 +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  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 +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  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 +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  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 +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  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 +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  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 +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  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 +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  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 +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  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 +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  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 +289 -0
  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 +499 -0
  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 +182 -0
  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 +188 -206
  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/connection/hx711.js +132 -0
  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/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX710AFull } = require('../../../src/chips/adc_dac/hx710a');
6
+
7
+ // Temperature-monitored load cell demo: tare at startup, then print weight
8
+ // continuously, sampling the on-chip temperature sensor periodically.
9
+ // Replace SCALE_FACTOR with the value calibrated for your load cell and V_DD.
10
+ // Calibration: (1) call tare() with nothing on the scale; (2) place a known
11
+ // 100 g reference weight; (3) SCALE_FACTOR = (readAverage() - getOffset()) / 100.
12
+ const SCALE_FACTOR = 420.0;
13
+
14
+ const dout = Default.input({ chip: 0, line: 5 });
15
+ const pd_sck = Default.output({ chip: 0, line: 6 });
16
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
17
+ const chip = new HX710AFull(connection); // Create HX710A driver — discards first conversion, (connection)
18
+
19
+ // --- Tare the scale before use ---
20
+ // Averaging 10 readings with nothing on the scale suppresses noise in the
21
+ // zero-offset capture, so later weight readings aren't skewed by drift.
22
+ console.log('Taring — keep scale empty...');
23
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
24
+ chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
25
+ console.log('Tare done. Place weight on scale.');
26
+
27
+ let prevWeight = null;
28
+ let iteration = 0;
29
+
30
+ function poll() {
31
+ const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
32
+ const rounded = Math.round(weight * 10) / 10;
33
+ if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
34
+ console.log(`-> ${rounded.toFixed(1)} g`);
35
+ prevWeight = rounded;
36
+ }
37
+
38
+ if (iteration % 10 === 0) {
39
+ // --- Sample the on-chip temperature sensor every ~5 s ---
40
+ // This is an uncalibrated raw ADC code (~20.4 LSB/°C, chip-to-chip
41
+ // offset/gain vary per the datasheet), intended only for the
42
+ // datasheet's stated purpose of relative drift compensation of the
43
+ // weight reading — not as an absolute °C measurement.
44
+ const tempRaw = chip.readTemperatureRaw(); // Read raw on-chip temperature code, () → number
45
+ console.log(`-> ${rounded.toFixed(1)} g (temp raw=${tempRaw})`);
46
+ }
47
+
48
+ iteration++;
49
+ setTimeout(poll, 500);
50
+ }
51
+
52
+ poll();
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX710AMinimal } = require('../../../src/chips/adc_dac/hx710a');
6
+
7
+ const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
8
+ const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
9
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
10
+ const chip = new HX710AMinimal(connection); // Create HX710A driver — discards first conversion, (connection)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
14
+ console.log(raw);
15
+
16
+ connection.close();
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX710BFull } = require('../../../src/chips/adc_dac/hx710b');
6
+
7
+ const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
8
+ const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
9
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
10
+ const chip = new HX710BFull(connection); // Create HX710B driver — discards first conversion, (connection)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ // returns true when DOUT is LOW
14
+ const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
15
+ // blocks until DOUT goes LOW, then clocks out 24 bits
16
+
17
+ chip.setRate(40); // Select differential-input output rate, (rate: 10|40) → undefined
18
+ // takes effect after next read; issues dummy read to apply
19
+ chip.setRate(10); // (restores default 10 SPS)
20
+
21
+ const avg = chip.readAverage(10); // Average multiple raw readings, (times=10) → number
22
+ // blocks for `times` complete conversions
23
+
24
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
25
+ // stores result in internal _offset; call with nothing on the scale
26
+ const offset = chip.getOffset(); // Return stored tare offset, () → number
27
+
28
+ chip.setScale(420.0); // Set calibration scale factor, (factor: number) → undefined
29
+ // factor = (readAverage() - offset) / known_weight_in_target_unit
30
+ const scale = chip.getScale(); // Return current scale factor, () → number
31
+
32
+ const weight = chip.readWeight(5); // Return calibrated weight, (times=1) → number
33
+ // computes (readAverage(times) - offset) / scale
34
+ console.log('weight:', weight);
35
+
36
+ const suppRaw = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
37
+ // uncalibrated ADC code, no absolute LSB-to-volts scale
38
+ console.log('supply diff raw:', suppRaw);
39
+
40
+ chip.powerDown(); // Enter power-down mode, () → undefined
41
+ // holds PD_SCK HIGH for >60 µs
42
+ chip.powerUp(); // Exit power-down, reset chip, discard settling conversion, () → undefined
43
+ // resets to differential input, gain 128, 10 SPS
44
+
45
+ connection.close();
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX710BFull } = require('../../../src/chips/adc_dac/hx710b');
6
+
7
+ // Battery-powered load cell demo: tare at startup, then print weight
8
+ // continuously, watching the DVDD−AVDD supply-difference reading for
9
+ // drift that signals a low battery. Replace SCALE_FACTOR with the value
10
+ // calibrated for your load cell and wiring topology. Calibration: (1)
11
+ // call tare() with nothing on the scale; (2) place a known 100 g reference
12
+ // weight; (3) SCALE_FACTOR = (readAverage() - getOffset()) / 100.
13
+ const SCALE_FACTOR = 420.0;
14
+ const LOW_BATT_DELTA = 50000; // supply-diff drift threshold from baseline
15
+
16
+ const dout = Default.input({ chip: 0, line: 5 });
17
+ const pd_sck = Default.output({ chip: 0, line: 6 });
18
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
19
+ const chip = new HX710BFull(connection); // Create HX710B driver — discards first conversion, (connection)
20
+
21
+ // --- Tare the scale before use ---
22
+ // Averaging 10 readings with nothing on the scale suppresses noise in the
23
+ // zero-offset capture, so later weight readings aren't skewed by drift.
24
+ console.log('Taring — keep scale empty...');
25
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
26
+ chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
27
+ console.log('Tare done. Place weight on scale.');
28
+
29
+ // --- Capture the supply-difference baseline at full charge ---
30
+ // The datasheet gives no absolute LSB-to-volts conversion for this
31
+ // channel — it is only useful for relative drift tracking. Capture one
32
+ // reading at startup as a "known-good battery" baseline, then compare
33
+ // later readings against it to detect discharge.
34
+ const baselineSuppDiff = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
35
+
36
+ let prevWeight = null;
37
+ let iteration = 0;
38
+
39
+ function poll() {
40
+ const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
41
+ const rounded = Math.round(weight * 10) / 10;
42
+ if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
43
+ console.log(`-> ${rounded.toFixed(1)} g`);
44
+ prevWeight = rounded;
45
+ }
46
+
47
+ if (iteration % 20 === 0) {
48
+ // --- Sample the DVDD−AVDD supply-difference channel every ~10 s ---
49
+ // Uncalibrated ADC code, intended only for relative drift tracking
50
+ // against a known-good baseline (the datasheet's stated purpose for
51
+ // this channel in battery-powered weigh-scale applications) — not
52
+ // an absolute voltage reading.
53
+ const suppDiff = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
54
+ const drift = suppDiff - baselineSuppDiff;
55
+ if (Math.abs(drift) > LOW_BATT_DELTA) {
56
+ console.log(`LOW BATTERY (supply_diff=${suppDiff}, drift=${drift})`);
57
+ }
58
+ }
59
+
60
+ iteration++;
61
+ setTimeout(poll, 500);
62
+ }
63
+
64
+ poll();
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX710BMinimal } = require('../../../src/chips/adc_dac/hx710b');
6
+
7
+ const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
8
+ const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
9
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
10
+ const chip = new HX710BMinimal(connection); // Create HX710B driver — discards first conversion, (connection)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
14
+ console.log(raw);
15
+
16
+ connection.close();
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
8
+ const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
9
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
10
+ const chip = new HX711Full(connection); // Create HX711 driver — discards first conversion, (connection)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ // returns true when DOUT is LOW
14
+ const raw = chip.readRaw(); // Read signed 24-bit ADC value at current gain, () → number
15
+ // blocks until DOUT goes LOW, then clocks out 24 bits
16
+
17
+ chip.setGain(64); // Select channel and gain, (gain: 128|64|32) → undefined
18
+ // 128 → Channel A, 64 → Channel A, 32 → Channel B; issues dummy read to apply
19
+ chip.setGain(32);
20
+ chip.setGain(128);
21
+
22
+ const avg = chip.readAverage(10); // Average multiple raw readings, (times=10) → number
23
+ // blocks for `times` complete conversions
24
+
25
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
26
+ // stores result in internal _offset; call with nothing on the scale
27
+ const offset = chip.getOffset(); // Return stored tare offset, () → number
28
+
29
+ chip.setScale(420.0); // Set calibration scale factor, (factor: number) → undefined
30
+ // factor = (readAverage() - offset) / known_weight_in_target_unit
31
+ const scale = chip.getScale(); // Return current scale factor, () → number
32
+
33
+ const weight = chip.readWeight(5); // Return calibrated weight, (times=1) → number
34
+ // computes (readAverage(times) - offset) / scale
35
+ console.log('weight:', weight);
36
+
37
+ chip.powerDown(); // Enter power-down mode, () → undefined
38
+ // holds PD_SCK HIGH for >60 µs
39
+ chip.powerUp(); // Exit power-down, reset chip, discard settling conversion, () → undefined
40
+ // resets to Channel A Gain 128; first post-reset conversion discarded internally
41
+
42
+ connection.close();
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ // Kitchen scale demo: tare at startup, then print weight continuously.
8
+ // Replace SCALE_FACTOR with the value calibrated for your load cell and V_DD.
9
+ // Calibration: (1) call tare() with nothing on the scale; (2) place a known
10
+ // weight W grams; (3) SCALE_FACTOR = (readAverage() - getOffset()) / W.
11
+ const SCALE_FACTOR = 420.0;
12
+
13
+ const dout = Default.input({ chip: 0, line: 5 });
14
+ const pd_sck = Default.output({ chip: 0, line: 6 });
15
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
16
+ const chip = new HX711Full(connection); // Create HX711 driver — discards first conversion, (connection)
17
+
18
+ console.log('Taring — keep scale empty...');
19
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
20
+ chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
21
+ console.log('Tare done. Place weight on scale.');
22
+
23
+ let prevWeight = null;
24
+
25
+ function poll() {
26
+ const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
27
+ const rounded = Math.round(weight * 10) / 10;
28
+ if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
29
+ console.log(`-> ${rounded.toFixed(1)} g`);
30
+ prevWeight = rounded;
31
+ }
32
+ setTimeout(poll, 500);
33
+ }
34
+
35
+ poll();
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const { Default } = require('opengpio');
4
+ const { HX711Connection } = require('../../../src/connection/hx711');
5
+ const { HX711Minimal } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
8
+ const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
9
+ const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
10
+ const chip = new HX711Minimal(connection); // Create HX711 driver — discards first conversion, (connection)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ const raw = chip.readRaw(); // Read signed 24-bit ADC value, () → number
14
+ console.log(raw);
15
+
16
+ connection.close();
@@ -1,34 +1,36 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
4
  const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const dac = new MCP4725Full(transport);
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const dac = new MCP4725Full(connection);
11
11
 
12
- dac.set_voltage(0.75); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
12
+ (async () => {
13
+ await dac.set_voltage(0.75); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
13
14
  // converts fraction to 12-bit code and issues Fast Write
14
- dac.set_raw(3000); // Set raw 12-bit code, (code=0–4095) → None
15
+ await dac.set_raw(3000); // Set raw 12-bit code, (code=0–4095) → None
15
16
  // clamps to [0, 4095] and writes DAC register only
16
- dac.set_voltage_eeprom(0.5); // Set output and persist to EEPROM, (fraction=0.0–1.0) → None
17
+ await dac.set_voltage_eeprom(0.5); // Set output and persist to EEPROM, (fraction=0.0–1.0) → None
17
18
  // writes both DAC register and EEPROM for power-cycle persistence
18
- dac.set_raw_eeprom(2048); // Set raw code and persist to EEPROM, (code=0–4095) → None
19
+ await dac.set_raw_eeprom(2048); // Set raw code and persist to EEPROM, (code=0–4095) → None
19
20
  // writes both DAC register and EEPROM for power-cycle persistence
20
- const state = dac.read(); // Read DAC and EEPROM registers, () → dict
21
+ const state = await dac.read(); // Read DAC and EEPROM registers, () → dict
21
22
  // returns code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready
22
- console.log('code:', state.code);
23
- console.log('voltage_fraction:', state.voltage_fraction);
24
- console.log('eeprom_ready:', state.eeprom_ready);
25
- dac.set_power_down(MCP4725Full.PD_100K_GND); // Set power-down mode with code preserved, (mode=0–3) → None
23
+ console.log('code:', state.code);
24
+ console.log('voltage_fraction:', state.voltage_fraction);
25
+ console.log('eeprom_ready:', state.eeprom_ready);
26
+ await dac.set_power_down(MCP4725Full.PD_100K_GND); // Set power-down mode with code preserved, (mode=0–3) → None
26
27
  // enters power-down; output stage disconnects with 100k to GND
27
- dac.wake_up(); // Send General Call Wake-Up to clear power-down, () → None
28
+ await dac.wake_up(); // Send General Call Wake-Up to clear power-down, () → None
28
29
  // sends 0x00, 0x09 to address 0x00; clears PD bits in DAC register
29
- dac.reset(); // Send General Call Reset and reload EEPROM, () → None
30
+ await dac.reset(); // Send General Call Reset and reload EEPROM, () → None
30
31
  // sends 0x00, 0x06; triggers internal POR and reloads DAC from EEPROM
31
- const ready = dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
32
+ const ready = await dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
32
33
  // returns True when any pending EEPROM write has finished
33
- console.log('eeprom_ready:', ready);
34
- console.log('MCP4725 complete running');
34
+ console.log('eeprom_ready:', ready);
35
+ console.log('MCP4725 complete running');
36
+ })();
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
4
  const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const dac = new MCP4725Full(transport);
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const dac = new MCP4725Full(connection);
11
11
 
12
12
  const STEP = 1.0 / 20.0;
13
13
  const DELAY_MS = 100;
@@ -21,7 +21,7 @@ async function run_triangle_wave() {
21
21
  // Up sweep: 0 to full scale in 21 steps
22
22
  for (let n = 0; n <= 20; n++) {
23
23
  const fraction = n * STEP;
24
- dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
24
+ await dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
25
25
  const code = Math.round(fraction * 4095);
26
26
  const approx_v = code * 3.3 / 4096;
27
27
  console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
@@ -30,7 +30,7 @@ async function run_triangle_wave() {
30
30
  // Down sweep: full scale back to 0 in 20 steps
31
31
  for (let n = 20; n >= 0; n--) {
32
32
  const fraction = n * STEP;
33
- dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
33
+ await dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
34
34
  const code = Math.round(fraction * 4095);
35
35
  const approx_v = code * 3.3 / 4096;
36
36
  console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
@@ -1,14 +1,16 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
4
  const { MCP4725Minimal } = require('../../../src/chips/adc_dac/mcp4725');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const dac = new MCP4725Minimal(transport);
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const dac = new MCP4725Minimal(connection);
11
11
 
12
- dac.set_voltage(0.5); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
13
- dac.set_raw(2048); // Set raw 12-bit code, (code=0–4095) → None
14
- console.log('MCP4725 minimal running');
12
+ (async () => {
13
+ await dac.set_voltage(0.5); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
14
+ await dac.set_raw(2048); // Set raw 12-bit code, (code=0–4095) → None
15
+ console.log('MCP4725 minimal running');
16
+ })();
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
7
+ const dac = new MCP4728Full(connection); // Create MCP4728 driver, (connection)
8
+
9
+ (async () => {
10
+ await dac.set_voltage(0, 0.75); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
11
+ // Multi-Write, V_REF=external, gain=×1, PD=00
12
+ await dac.set_raw(2, 3000); // Set channel C raw code, (channel=0–3, code=0–4095) → None
13
+ // clamps to [0, 4095]; writes channel C only
14
+ await dac.set_all([0.1, 0.2, 0.3, 0.4]); // Update all four channels simultaneously, (fractions[4]) → None
15
+ // single 8-byte Fast Write transaction; EEPROM unaffected
16
+ await dac.set_voltage_eeprom(0, 0.5, 0, 1); // Set channel A and persist to EEPROM, (channel=0–3, fraction, vref=0/1, gain=1/2) → None
17
+ // Single Write updates DAC register and nonvolatile EEPROM
18
+ await dac.set_raw_eeprom(1, 2048, 0, 1); // Set channel B raw code and persist, (channel=0–3, code, vref=0/1, gain=1/2) → None
19
+ // Single Write; takes up to 50 ms for EEPROM write
20
+ await dac.set_all_eeprom([0.0, 0.25, 0.5, 0.75], // Update all four channels + EEPROM, (fractions[4], vrefs[4], gains[4]) → None
21
+ [0, 0, 0, 0], // each channel uses external V_DD
22
+ [1, 1, 1, 1]); // and gain ×1
23
+ // Sequential Write from A to D; persists all four at the end
24
+ await dac.set_vref(0, 0, 0, 0); // Set V_REF for all four channels, (vref_a, vref_b, vref_c, vref_d) → None
25
+ // 0 = external V_DD; volatile register only
26
+ await dac.set_gain(1, 1, 1, 1); // Set gain for all four channels, (gain_a, gain_b, gain_c, gain_d) → None
27
+ // 1 = ×1, 2 = ×2; volatile register only
28
+ await dac.set_power_down(0, 0, 0, 0); // Set power-down for all four channels, (pd_a, pd_b, pd_c, pd_d) → None
29
+ // 0 = normal, 1 = 1 kΩ, 2 = 100 kΩ, 3 = 500 kΩ to GND
30
+ const state = await dac.read(); // Read all four channels' DAC and EEPROM state, () → {channel[], eeprom_ready}
31
+ // 4 channel objects with code, vref, gain, power_down, eeprom_*
32
+ console.log('ch0 code:', state.channel[0].code);
33
+ console.log('eeprom_ready:', state.eeprom_ready);
34
+ await dac.software_update(); // Latch all V_OUT simultaneously, () → None
35
+ // General Call 0x00, 0x08; equivalent to LDAC pin pulse
36
+ await dac.wake_up(); // Clear all PD bits via General Call Wake-Up, () → None
37
+ // sends 0x00, 0x09; clears power-down on all four channels
38
+ await dac.reset(); // Reload EEPROM into all DAC registers, () → None
39
+ // General Call 0x00, 0x06; triggers internal POR
40
+ const ready = await dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
41
+ // True when any pending EEPROM write has finished
42
+ console.log('eeprom ready:', ready);
43
+ })();
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const VDD = 3.3;
7
+
8
+ const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
9
+ const dac = new MCP4728Full(connection); // Create MCP4728 driver, (connection)
10
+
11
+ async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
12
+ (async () => {
13
+ // --- Apply four-point calibration voltages to channels A–D ---
14
+ // A 4-channel DAC is the canonical way to bias a 4-point sensor bridge
15
+ // (load cell, RTD conditioning, strain gauge). Each channel gets a
16
+ // different fraction of full scale to demonstrate independent outputs.
17
+ // Voltages printed below assume a 3.3 V supply.
18
+ const calibration = [0.0, 1.0 / 3, 2.0 / 3, 1.0];
19
+ await dac.set_all(calibration); // Update all four channels simultaneously, (fractions[4]) → None
20
+ for (let ch = 0; ch < 4; ch++) {
21
+ const code = Math.round(calibration[ch] * 4095);
22
+ console.log(`ch=${ch} fraction=${calibration[ch].toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
23
+ }
24
+
25
+ // --- Synchronous staircase from 0 to full scale on all four channels ---
26
+ // Using set_all with the same fraction across channels keeps them in lock-step
27
+ // and demonstrates simultaneous V_OUT update via Fast Write. A 50 ms pause
28
+ // between steps lets the host controller observe each level on the scope.
29
+ const STEP = 1.0 / 16;
30
+ for (let n = 0; n <= 16; n++) {
31
+ const f = n * STEP;
32
+ await dac.set_all([f, f, f, f]); // Update all four channels simultaneously, (fractions[4]) → None
33
+ const code = Math.round(f * 4095);
34
+ console.log(`step=${n.toString().padStart(2)} fraction=${f.toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
35
+ await sleep(50);
36
+ }
37
+
38
+ // --- Reset all channels to 0 V before exit ---
39
+ await dac.set_all([0.0, 0.0, 0.0, 0.0]); // Update all four channels simultaneously, (fractions[4]) → None
40
+ await connection.close();
41
+ })();
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { MCP4728Minimal } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
7
+ const dac = new MCP4728Minimal(connection); // Create MCP4728 driver, (connection)
8
+
9
+ (async () => {
10
+ await dac.set_voltage(0, 0.5); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
11
+ await dac.set_raw(1, 2048); // Set channel B raw 12-bit code, (channel=0–3, code=0–4095) → None
12
+ await dac.set_all([0.0, 0.25, 0.5, 1.0]); // Update all four channels simultaneously, (fractions[4]) → None
13
+ })();
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { PCF8591Full } = require('../../../src/chips/adc_dac/pcf8591');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x48', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const adc = new PCF8591Full(connection);
11
+
12
+ (async () => {
13
+ const ch0_raw = await adc.read_channel(0); // Read single channel, (channel=0–3) → number
14
+ // discards the stale first conversion byte; returns 0–255
15
+ const ch1_raw = await adc.read_channel(1); // Read single channel, (channel=0–3) → number
16
+ // selects channel 1 via the control byte, returns 0–255
17
+ const all_raw = await adc.read_all(); // Read all four channels, () → number[]
18
+ // sets AI=1 and reads 5 bytes; discards stale byte 0
19
+
20
+ const v0 = await adc.read_channel_voltage(0, 3.3, 0.0); // Read channel as voltage, (channel, vref=3.3 V, vagnd=0.0 V) → number V
21
+ // converts raw to voltage using V_AGND + raw × (V_REF−V_AGND) / 256
22
+ const v_all = await adc.read_all_voltage(3.3, 0.0); // Read all channels as voltages, (vref=3.3 V, vagnd=0.0 V) → number[] V
23
+ // returns four voltages using the same conversion
24
+
25
+ await adc.configure(PCF8591Full.MODE_3_DIFFERENTIAL, false, false); // Configure input mode, (input_mode=0–3, auto_increment=bool, dac_enabled=bool) → None
26
+ // sets AIP=01 (3 differential channels vs AIN3) and clears AOE/AI
27
+ const diff = await adc.read_differential(0); // Read differential channel, (channel=0–2) → number
28
+ // returns signed 8-bit two's complement (-128 to 127)
29
+ await adc.configure(PCF8591Full.MODE_4_SINGLE_ENDED, false, true); // Configure input mode, (input_mode=0–3, auto_increment=bool, dac_enabled=bool) → None
30
+ // restores 4 single-ended mode and enables the DAC output
31
+ await adc.set_dac(128); // Enable DAC and set raw value, (value=0–255) → None
32
+ // sets AOE=1 and writes 128 to the DAC register; V_AOUT ≈ V_REF/2
33
+ await adc.set_dac_voltage(0.25); // Set DAC as fraction of (VREF−VAGND), (fraction=0.0–1.0) → None
34
+ // maps fraction to 0–255 and writes the DAC; AOUT follows
35
+ await adc.disable_dac(); // Disable DAC output, () → None
36
+ // clears AOE; AOUT returns to high-impedance
37
+ console.log('ch0_raw=' + ch0_raw);
38
+ console.log('v0=' + v0.toFixed(3) + 'V');
39
+ console.log('diff=' + diff);
40
+ console.log('PCF8591 complete running');
41
+ })();
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { PCF8591Full } = require('../../../src/chips/adc_dac/pcf8591');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x48', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const adc = new PCF8591Full(connection);
11
+
12
+ const VREF = 3.3;
13
+ const VAGND = 0.0;
14
+
15
+ function delay(ms) {
16
+ return new Promise(resolve => setTimeout(resolve, ms));
17
+ }
18
+
19
+ async function run_feedback() {
20
+ // --- Wire a potentiometer across VAGND–VREF with the wiper to AIN0 ---
21
+ // Connect an LED (with series resistor) to AOUT. In a loop, read AIN0, map
22
+ // the 0–255 value to a DAC output value, and write it to AOUT — the LED
23
+ // brightness tracks the potentiometer. This demonstrates the ADC→DAC
24
+ // feedback path inside a single chip.
25
+ await adc.configure(PCF8591Full.MODE_4_SINGLE_ENDED, false, true); // Configure input mode, (input_mode=0–3, auto_increment=bool, dac_enabled=bool) → None
26
+ // single-ended mode with DAC output enabled
27
+ for (let n = 0; n < 20; n++) {
28
+ const raw = await adc.read_channel(0); // Read single channel, (channel=0–3) → number
29
+ const vin = VAGND + raw * (VREF - VAGND) / 256.0;
30
+ await adc.set_dac(raw); // Enable DAC and set raw value, (value=0–255) → None
31
+ const vout = VAGND + raw * (VREF - VAGND) / 256.0;
32
+ console.log(`n=${String(n).padStart(2)} raw=${String(raw).padStart(3)} vin=${vin.toFixed(3)}V vout=${vout.toFixed(3)}V`);
33
+ await delay(200);
34
+ }
35
+ console.log('Demo complete');
36
+ process.exit(0);
37
+ }
38
+
39
+ run_feedback();
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { PCF8591Minimal } = require('../../../src/chips/adc_dac/pcf8591');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x48', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const adc = new PCF8591Minimal(connection);
11
+
12
+ (async () => {
13
+ const ch0 = await adc.read_channel(0); // Read single channel, (channel=0–3) → number
14
+ const allRaw = await adc.read_all(); // Read all four channels, () → number[]
15
+
16
+ console.log('ch0=' + ch0);
17
+ console.log('all=' + JSON.stringify(allRaw));
18
+ console.log('PCF8591 minimal running');
19
+ })();