periph 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/README.md +61 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/motor/drv8830/complete.js +50 -0
  117. package/examples/motor/drv8830/demo.js +57 -0
  118. package/examples/motor/drv8830/minimal.js +23 -0
  119. package/examples/other/mpr121/complete.js +50 -0
  120. package/examples/other/mpr121/demo.js +30 -0
  121. package/examples/other/mpr121/minimal.js +17 -0
  122. package/examples/power/ade7953/complete.js +44 -0
  123. package/examples/power/ade7953/demo.js +28 -0
  124. package/examples/power/ade7953/minimal.js +15 -0
  125. package/examples/power/ina219/complete.js +20 -18
  126. package/examples/power/ina219/demo.js +15 -13
  127. package/examples/power/ina219/minimal.js +8 -8
  128. package/examples/power/ina226/complete.js +24 -22
  129. package/examples/power/ina226/demo.js +22 -20
  130. package/examples/power/ina226/minimal.js +5 -5
  131. package/examples/power/ina3221/complete.js +51 -49
  132. package/examples/power/ina3221/demo.js +13 -13
  133. package/examples/power/ina3221/minimal.js +8 -8
  134. package/examples/pressure/bmp085/complete.js +29 -0
  135. package/examples/pressure/bmp085/demo.js +34 -0
  136. package/examples/pressure/bmp085/minimal.js +19 -0
  137. package/examples/pressure/bmp180/complete.js +20 -17
  138. package/examples/pressure/bmp180/demo.js +24 -22
  139. package/examples/pressure/bmp180/minimal.js +13 -11
  140. package/examples/pressure/bmp280/complete.js +23 -20
  141. package/examples/pressure/bmp280/demo.js +44 -41
  142. package/examples/pressure/bmp280/minimal.js +13 -11
  143. package/examples/pressure/bmp384/complete.js +29 -0
  144. package/examples/pressure/bmp384/demo.js +40 -0
  145. package/examples/pressure/bmp384/minimal.js +18 -0
  146. package/examples/pressure/bmp581/complete.js +34 -0
  147. package/examples/pressure/bmp581/demo.js +56 -0
  148. package/examples/pressure/bmp581/minimal.js +19 -0
  149. package/examples/pressure/lps22df/complete.js +50 -0
  150. package/examples/pressure/lps22df/demo.js +41 -0
  151. package/examples/pressure/lps22df/minimal.js +19 -0
  152. package/examples/pressure/lps28dfw/complete.js +37 -0
  153. package/examples/pressure/lps28dfw/demo.js +33 -0
  154. package/examples/pressure/lps28dfw/minimal.js +19 -0
  155. package/examples/pressure/lps33hw/complete.js +43 -0
  156. package/examples/pressure/lps33hw/demo.js +43 -0
  157. package/examples/pressure/lps33hw/minimal.js +19 -0
  158. package/examples/rfid/mfrc522/complete.js +44 -0
  159. package/examples/rfid/mfrc522/demo.js +70 -0
  160. package/examples/rfid/mfrc522/minimal.js +17 -0
  161. package/examples/rtc/ds3231/complete.js +77 -0
  162. package/examples/rtc/ds3231/demo.js +60 -0
  163. package/examples/rtc/ds3231/minimal.js +23 -0
  164. package/examples/rtc/pcf8523/complete.js +66 -0
  165. package/examples/rtc/pcf8523/demo.js +65 -0
  166. package/examples/rtc/pcf8523/minimal.js +20 -0
  167. package/examples/temperature/mcp9808/complete.js +89 -0
  168. package/examples/temperature/mcp9808/demo.js +62 -0
  169. package/examples/temperature/mcp9808/minimal.js +21 -0
  170. package/examples/temperature/tmp117/complete.js +90 -0
  171. package/examples/temperature/tmp117/demo.js +73 -0
  172. package/examples/temperature/tmp117/minimal.js +21 -0
  173. package/examples/tof/vl53l0x/complete.js +118 -0
  174. package/examples/tof/vl53l0x/demo.js +83 -0
  175. package/examples/tof/vl53l0x/minimal.js +25 -0
  176. package/examples/tof/vl53l1x/complete.js +151 -0
  177. package/examples/tof/vl53l1x/demo.js +100 -0
  178. package/examples/tof/vl53l1x/minimal.js +25 -0
  179. package/package.json +5 -1
  180. package/src/chips/accelerometer/adxl345.js +482 -0
  181. package/src/chips/accelerometer/adxl362.js +594 -0
  182. package/src/chips/adc_dac/ad7705.js +345 -0
  183. package/src/chips/adc_dac/ad7706.js +331 -0
  184. package/src/chips/adc_dac/hx710a.js +184 -0
  185. package/src/chips/adc_dac/hx710b.js +186 -0
  186. package/src/chips/adc_dac/hx711.js +15 -15
  187. package/src/chips/adc_dac/mcp4725.js +48 -40
  188. package/src/chips/adc_dac/mcp4728.js +57 -44
  189. package/src/chips/adc_dac/pcf8591.js +196 -0
  190. package/src/chips/comms/mcp2515.js +659 -0
  191. package/src/chips/comms/rda5807m.js +393 -0
  192. package/src/chips/comms/rfm9x.js +590 -0
  193. package/src/chips/display/pcf8576.js +42 -33
  194. package/src/chips/environmental/aht21.js +52 -37
  195. package/src/chips/environmental/bme280.js +85 -66
  196. package/src/chips/environmental/bme680.js +110 -87
  197. package/src/chips/gas/ens160.js +82 -64
  198. package/src/chips/gnss/neo6.js +438 -0
  199. package/src/chips/gyroscope/l3g4200d.js +366 -0
  200. package/src/chips/gyroscope/l3gd20h.js +314 -0
  201. package/src/chips/humidity/dht11.js +135 -0
  202. package/src/chips/imu/mpu6050.js +89 -66
  203. package/src/chips/imu/mpu9250.js +394 -0
  204. package/src/chips/imu/mpu9255.js +447 -0
  205. package/src/chips/io_expander/mcp23017.js +240 -157
  206. package/src/chips/io_expander/pcf8574.js +149 -124
  207. package/src/chips/io_expander/pcf8575.js +164 -135
  208. package/src/chips/io_expander/tpic6b595.js +242 -0
  209. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  210. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  211. package/src/chips/led/apa102.js +236 -0
  212. package/src/chips/led/sk6812rgbw.js +15 -149
  213. package/src/chips/led/ws2812b.js +14 -139
  214. package/src/chips/led/ws2814.js +49 -0
  215. package/src/chips/light/apds-9930.js +342 -0
  216. package/src/chips/light/apds9960.js +158 -123
  217. package/src/chips/magnetometer/as5600.js +102 -82
  218. package/src/chips/magnetometer/hmc5883l.js +222 -0
  219. package/src/chips/memory/_24aa02uid.js +32 -29
  220. package/src/chips/motor/drv8830.js +239 -0
  221. package/src/chips/other/mpr121.js +375 -0
  222. package/src/chips/power/ade7953.js +427 -0
  223. package/src/chips/power/ina219.js +45 -40
  224. package/src/chips/power/ina226.js +50 -45
  225. package/src/chips/power/ina3221.js +95 -76
  226. package/src/chips/pressure/bmp085.js +251 -0
  227. package/src/chips/pressure/bmp180.js +54 -37
  228. package/src/chips/pressure/bmp280.js +72 -53
  229. package/src/chips/pressure/bmp384.js +410 -0
  230. package/src/chips/pressure/bmp581.js +489 -0
  231. package/src/chips/pressure/lps22df.js +341 -0
  232. package/src/chips/pressure/lps28dfw.js +314 -0
  233. package/src/chips/pressure/lps33hw.js +255 -0
  234. package/src/chips/rfid/mfrc522.js +674 -0
  235. package/src/chips/rtc/ds3231.js +504 -0
  236. package/src/chips/rtc/pcf8523.js +539 -0
  237. package/src/chips/temperature/mcp9808.js +488 -0
  238. package/src/chips/temperature/tmp117.js +471 -0
  239. package/src/chips/tof/_vl53_base.js +160 -0
  240. package/src/chips/tof/vl53l0x.js +816 -0
  241. package/src/chips/tof/vl53l1x.js +816 -0
  242. package/src/connection/connection.js +111 -0
  243. package/src/connection/dhtxx.js +138 -0
  244. package/src/connection/dhtxx_mock.js +39 -0
  245. package/src/{transport → connection}/hx711.js +49 -22
  246. package/src/connection/hx711_mock.js +52 -0
  247. package/src/{transport → connection}/i2c.js +13 -9
  248. package/src/connection/i2c_mock.js +91 -0
  249. package/src/connection/input_pin.js +156 -0
  250. package/src/connection/neo6_mock.js +45 -0
  251. package/src/{transport → connection}/neopixel.js +23 -4
  252. package/src/connection/output_pin.js +41 -0
  253. package/src/connection/sipo.js +159 -0
  254. package/src/{transport → connection}/smbus.js +14 -10
  255. package/src/{transport → connection}/spi.js +12 -8
  256. package/src/connection/spi_mock.js +151 -0
  257. package/src/{transport → connection}/uart.js +33 -26
  258. package/examples/gnss/.gitkeep +0 -0
  259. package/examples/humidity/.gitkeep +0 -0
  260. package/examples/motor/.gitkeep +0 -0
  261. package/examples/temperature/.gitkeep +0 -0
  262. package/examples/tof/.gitkeep +0 -0
  263. package/src/chips/gnss/.gitkeep +0 -0
  264. package/src/chips/motor/.gitkeep +0 -0
  265. package/src/chips/temperature/.gitkeep +0 -0
  266. package/src/chips/tof/.gitkeep +0 -0
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Delivers edge notifications from a chip's INT line.
5
+ *
6
+ * Intentionally minimal: it only signals that *an* edge occurred. The chip
7
+ * driver always calls pollInterrupt() to determine the cause. Multiple
8
+ * handlers may be registered on the same InputPin instance — the common
9
+ * pattern for chips with open-drain INT outputs wired to a single GPIO.
10
+ */
11
+ class InputPin {
12
+ /**
13
+ * Register handler to run on each edge matching trigger.
14
+ * @param {function} callback - Zero-argument function invoked on each matching edge.
15
+ * @param {string} [trigger='falling'] - One of 'rising', 'falling', 'change'.
16
+ * @returns {Promise<void>}
17
+ * @abstract
18
+ */
19
+ async onEdge(_callback, _trigger = 'falling') { throw new Error('abstract'); }
20
+
21
+ /**
22
+ * Remove a specific handler. No-op if not registered.
23
+ * @param {function} callback - The exact function previously passed to onEdge().
24
+ * @returns {Promise<void>}
25
+ * @abstract
26
+ */
27
+ async offEdge(_callback) { throw new Error('abstract'); }
28
+ }
29
+
30
+ /**
31
+ * InputPin backed by the `epoll` npm package watching a sysfs GPIO value file.
32
+ *
33
+ * Requires the GPIO to already be exported (e.g. via the `onoff` package or
34
+ * `gpio export N`). Sets the sysfs `edge` attribute to 'both' on first use so
35
+ * a single instance can fan out to handlers registered with different
36
+ * triggers; each handler's own trigger is filtered against the detected
37
+ * transition direction.
38
+ *
39
+ * `epoll` is an optional peer dependency — require() is deferred to onEdge()
40
+ * so consumers who never wire an INT pin never need it installed.
41
+ */
42
+ class EpollInputPin extends InputPin {
43
+ /**
44
+ * @param {string} valuePath - Sysfs GPIO value file (e.g. '/sys/class/gpio/gpio17/value').
45
+ */
46
+ constructor(valuePath) {
47
+ super();
48
+ this._valuePath = valuePath;
49
+ this._handlers = [];
50
+ this._trigger = 'falling';
51
+ this._armed = false;
52
+ this._fd = null;
53
+ this._poll = null;
54
+ this._last = null;
55
+ }
56
+
57
+ async onEdge(callback, trigger = 'falling') {
58
+ this._handlers.push(callback);
59
+ this._trigger = trigger;
60
+ if (this._armed) return;
61
+
62
+ const fs = require('fs');
63
+ const { Epoll } = require('epoll');
64
+
65
+ const edgePath = this._valuePath.replace(/\/value$/, '/edge');
66
+ try { fs.writeFileSync(edgePath, 'both'); } catch (_) { /* already set, or unwritable */ }
67
+
68
+ this._fd = fs.openSync(this._valuePath, 'r');
69
+ this._last = this._readValue();
70
+ this._poll = new Epoll(() => {
71
+ const current = this._readValue();
72
+ const rising = this._last === 0 && current === 1;
73
+ const falling = this._last === 1 && current === 0;
74
+ this._last = current;
75
+ if (this._trigger === 'change'
76
+ || (this._trigger === 'rising' && rising)
77
+ || (this._trigger === 'falling' && falling)) {
78
+ for (const handler of this._handlers) handler();
79
+ }
80
+ });
81
+ this._poll.add(this._fd, Epoll.EPOLLPRI);
82
+ this._armed = true;
83
+ }
84
+
85
+ _readValue() {
86
+ const fs = require('fs');
87
+ const buf = Buffer.alloc(1);
88
+ fs.readSync(this._fd, buf, 0, 1, 0);
89
+ return buf[0] === 0x31 ? 1 : 0; // ASCII '1' vs '0'
90
+ }
91
+
92
+ async offEdge(callback) {
93
+ this._handlers = this._handlers.filter(h => h !== callback);
94
+ if (this._handlers.length === 0 && this._armed) {
95
+ this._poll.remove(this._fd);
96
+ const fs = require('fs');
97
+ fs.closeSync(this._fd);
98
+ this._fd = null;
99
+ this._poll = null;
100
+ this._armed = false;
101
+ }
102
+ }
103
+ }
104
+
105
+ /**
106
+ * InputPin fallback: a 5 ms `setInterval` sampling loop.
107
+ *
108
+ * Used automatically when `epoll` is not installed, or explicitly when no
109
+ * hardware edge-detection is available.
110
+ */
111
+ class PollingInputPin extends InputPin {
112
+ /**
113
+ * @param {function} readValue - Zero-argument function returning the current pin state (0 or 1).
114
+ * @param {number} [pollIntervalMs=5] - Polling period in milliseconds.
115
+ */
116
+ constructor(readValue, pollIntervalMs = 5) {
117
+ super();
118
+ this._readValue = readValue;
119
+ this._pollIntervalMs = pollIntervalMs;
120
+ this._handlers = [];
121
+ this._trigger = 'falling';
122
+ this._last = readValue();
123
+ this._timer = null;
124
+ }
125
+
126
+ async onEdge(callback, trigger = 'falling') {
127
+ this._handlers.push(callback);
128
+ this._trigger = trigger;
129
+ if (!this._timer) {
130
+ this._timer = setInterval(() => this._tick(), this._pollIntervalMs);
131
+ }
132
+ }
133
+
134
+ async offEdge(callback) {
135
+ this._handlers = this._handlers.filter(h => h !== callback);
136
+ if (this._handlers.length === 0 && this._timer) {
137
+ clearInterval(this._timer);
138
+ this._timer = null;
139
+ }
140
+ }
141
+
142
+ _tick() {
143
+ const value = this._readValue();
144
+ if (value === this._last) return;
145
+ const rising = value === 1 && this._last === 0;
146
+ const falling = value === 0 && this._last === 1;
147
+ this._last = value;
148
+ if (this._trigger === 'change'
149
+ || (this._trigger === 'rising' && rising)
150
+ || (this._trigger === 'falling' && falling)) {
151
+ for (const handler of this._handlers) handler();
152
+ }
153
+ }
154
+ }
155
+
156
+ module.exports = { InputPin, EpollInputPin, PollingInputPin };
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * In-memory fake connection for NEO-6 unit tests — no hardware, no bus.
5
+ *
6
+ * NEO-6's driver treats UART, I2C (DDC), and SPI as the same underlying
7
+ * NMEA/UBX byte stream (see specs/gnss/neo-6.md's "Connection abstraction"
8
+ * note): `read(1)` for UART, `writeRead(Buffer.from([0xFF]), 1)` for I2C,
9
+ * `writeRead(Buffer.alloc(0), 1)` for SPI. This mock is transport-shape
10
+ * agnostic: preload the stream with `queueBytes(data)`; `read()` and
11
+ * `writeRead()` both just pop the next byte(s) off the front of one shared
12
+ * FIFO, regardless of the prefix passed to `writeRead` — matching the real
13
+ * module, where all three transports deliver the same bytes and only the
14
+ * framing differs. `write()` calls (e.g. sendUbx) are logged to `writes`
15
+ * for assertions.
16
+ */
17
+ class Neo6ConnectionMock {
18
+ constructor() {
19
+ this._stream = Buffer.alloc(0);
20
+ this.writes = [];
21
+ }
22
+
23
+ /** Append bytes to the end of the shared read stream. */
24
+ queueBytes(data) {
25
+ this._stream = Buffer.concat([this._stream, Buffer.from(data)]);
26
+ }
27
+
28
+ async read(n) {
29
+ const out = this._stream.subarray(0, n);
30
+ this._stream = this._stream.subarray(out.length);
31
+ return Buffer.from(out);
32
+ }
33
+
34
+ async writeRead(data, n) {
35
+ return this.read(n);
36
+ }
37
+
38
+ async write(data) {
39
+ this.writes.push(Buffer.from(data));
40
+ }
41
+
42
+ async close() {}
43
+ }
44
+
45
+ module.exports = { Neo6ConnectionMock };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const spi = require('spi-device');
4
+ const { Connection } = require('./connection');
4
5
 
5
6
  function _encode(data) {
6
7
  const out = Buffer.alloc(data.length * 3 + 16);
@@ -16,22 +17,40 @@ function _encode(data) {
16
17
  return out;
17
18
  }
18
19
 
19
- class NeoPixelTransport {
20
+ /**
21
+ * NeoPixel (WS2812B/SK6812) connection for Node.js — SPI-encoded, write-only.
22
+ *
23
+ * Encodes each data byte into 3 SPI bytes (3 bits per data bit) so an SPI
24
+ * MOSI line at 2.4 MHz produces the WS2812B/SK6812 one-wire timing.
25
+ */
26
+ class NeoPixelConnection extends Connection {
27
+ /**
28
+ * @param {number} busNumber - SPI bus number.
29
+ * @param {number} deviceNumber - Chip-select line on the bus (unused electrically; MOSI only).
30
+ */
20
31
  constructor(busNumber, deviceNumber) {
32
+ super();
21
33
  this._device = spi.openSync(busNumber, deviceNumber, {
22
34
  mode: spi.MODE0,
23
35
  maxSpeedHz: 2_400_000,
24
36
  });
25
37
  }
26
38
 
27
- write(data) {
39
+ /**
40
+ * Encode and send pixel data.
41
+ * @param {Buffer|Uint8Array} data - Raw GRB/GRBW pixel bytes.
42
+ */
43
+ async _write(data) {
28
44
  const encoded = _encode(Buffer.isBuffer(data) ? data : Buffer.from(data));
29
45
  this._device.transferSync([{ sendBuffer: encoded, byteLength: encoded.length }]);
30
46
  }
31
47
 
32
- close() {
48
+ /**
49
+ * Close the SPI device. Must be called when the connection is no longer needed.
50
+ */
51
+ async close() {
33
52
  this._device.closeSync();
34
53
  }
35
54
  }
36
55
 
37
- module.exports = { NeoPixelTransport };
56
+ module.exports = { NeoPixelConnection };
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Drives a chip's EN (hardware enable/power) pin.
5
+ */
6
+ class OutputPin {
7
+ /**
8
+ * Set the pin's output level.
9
+ * @param {boolean} high - true drives the pin high, false drives it low.
10
+ * @returns {Promise<void>}
11
+ * @abstract
12
+ */
13
+ async set(_high) { throw new Error('abstract'); }
14
+ }
15
+
16
+ /**
17
+ * OutputPin wrapping a pre-constructed [`opengpio`](https://www.npmjs.com/package/opengpio)
18
+ * Output instance (libgpiod character-device write).
19
+ *
20
+ * The caller constructs and configures the Output themselves (e.g. via
21
+ * `Default.output({chip, line})`), matching the existing HX711/SiPo/UART
22
+ * constructor convention of taking already-opened opengpio objects rather
23
+ * than raw pin numbers. `opengpio` is an optional dependency — nothing here
24
+ * calls require('opengpio'); consumers who never wire an EN pin never need
25
+ * it installed.
26
+ */
27
+ class GpioOutputPin extends OutputPin {
28
+ /**
29
+ * @param {object} output - opengpio Output instance (boolean `.value`).
30
+ */
31
+ constructor(output) {
32
+ super();
33
+ this._output = output;
34
+ }
35
+
36
+ async set(high) {
37
+ this._output.value = !!high;
38
+ }
39
+ }
40
+
41
+ module.exports = { OutputPin, GpioOutputPin };
@@ -0,0 +1,159 @@
1
+ 'use strict';
2
+
3
+ const spi = require('spi-device');
4
+
5
+ /**
6
+ * SiPo (serial-in/parallel-out shift register) connection for Node.js.
7
+ *
8
+ * Drives cascadable SIPO shift registers (TPIC6B595, SN74HC595, etc.) whose
9
+ * SER IN/SRCK pins are electrically an SPI MOSI/SCK pair. Shifts data over
10
+ * either a hardware spi-device or a bit-banged pair of opengpio Output objects.
11
+ * RCK — and, if configured, SRCLR/G — are always plain opengpio Output objects,
12
+ * independent of which SPI mode is used.
13
+ *
14
+ * Write-only: there is no read() or writeRead().
15
+ *
16
+ * Exactly one of {busNumber, deviceNumber} (hardware) or {serIn, srck}
17
+ * (software) must be given in options.
18
+ *
19
+ * This is a custom protocol with no generic byte read/write, so it does not
20
+ * extend the shared Connection base — it carries its own enabled flag and
21
+ * enPin instead.
22
+ */
23
+ class SiPoConnection {
24
+ /**
25
+ * @param {object} rck - opengpio Output instance (register clock).
26
+ * @param {object} [options]
27
+ * @param {object} [options.srclr] - opengpio Output instance for SRCLR; omit to disable.
28
+ * @param {object} [options.g] - opengpio Output instance for G (output enable); omit to disable.
29
+ * @param {number} [options.busNumber] - Hardware mode: SPI bus number (opens spi-device).
30
+ * @param {number} [options.deviceNumber] - Hardware mode: chip-select line on the bus.
31
+ * @param {number} [options.maxSpeedHz=1000000] - Hardware mode: SPI clock in Hz.
32
+ * @param {object} [options.serIn] - Software mode: opengpio Output instance for SER IN.
33
+ * @param {object} [options.srck] - Software mode: opengpio Output instance for SRCK.
34
+ * @param {import('./output_pin').OutputPin|null} [options.enPin=null] - Optional EN-pin OutputPin.
35
+ */
36
+ constructor(rck, options = {}) {
37
+ const hardware = options.busNumber !== undefined;
38
+ const software = options.serIn !== undefined;
39
+ if (hardware === software) {
40
+ throw new Error(
41
+ 'specify exactly one of {busNumber, deviceNumber} or {serIn, srck}');
42
+ }
43
+
44
+ this._rck = rck;
45
+ this._srclr = options.srclr ?? null;
46
+ this._g = options.g ?? null;
47
+ this._serIn = options.serIn ?? null;
48
+ this._srck = options.srck ?? null;
49
+ this.enPin = options.enPin ?? null;
50
+ this._enabled = true;
51
+
52
+ if (hardware) {
53
+ this._device = spi.openSync(options.busNumber, options.deviceNumber, {
54
+ mode: spi.MODE0,
55
+ maxSpeedHz: options.maxSpeedHz ?? 1_000_000,
56
+ });
57
+ } else {
58
+ this._device = null;
59
+ }
60
+
61
+ this._rck.value = false;
62
+ if (this._srclr !== null) this._srclr.value = true;
63
+ if (this._g !== null) this._g.value = false;
64
+ }
65
+
66
+ /** Resume writes; drives the hardware EN pin high if wired.
67
+ * @returns {Promise<void>}
68
+ */
69
+ async enable() {
70
+ this._enabled = true;
71
+ if (this.enPin) await this.enPin.set(true);
72
+ }
73
+
74
+ /** Gate write()/clear()/setOutputEnable(); drives the hardware EN pin low if wired.
75
+ * @returns {Promise<void>}
76
+ */
77
+ async disable() {
78
+ this._enabled = false;
79
+ if (this.enPin) await this.enPin.set(false);
80
+ }
81
+
82
+ /** @returns {boolean} The current software-gate state. */
83
+ isEnabled() { return this._enabled; }
84
+
85
+ /**
86
+ * Shift data out MSB-first, then latch it into the output register.
87
+ *
88
+ * In hardware mode this transfers data over spi-device; in software mode
89
+ * it bit-bangs SER IN/SRCK. Either way, RCK is then pulsed HIGH then LOW
90
+ * to latch the shifted data into the storage register that drives the
91
+ * outputs.
92
+ *
93
+ * No-op if this connection is disabled.
94
+ *
95
+ * @param {Buffer|Uint8Array} data - Bytes to shift out, one byte per cascaded device.
96
+ */
97
+ write(data) {
98
+ if (!this._enabled) return;
99
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
100
+ if (this._device !== null) {
101
+ this._device.transferSync([{ sendBuffer: buf, byteLength: buf.length }]);
102
+ } else {
103
+ for (const byte of buf) {
104
+ for (let bit = 7; bit >= 0; bit--) {
105
+ this._serIn.value = !!((byte >> bit) & 1);
106
+ this._srck.value = true;
107
+ this._srck.value = false;
108
+ }
109
+ }
110
+ }
111
+ this._rck.value = true;
112
+ this._rck.value = false;
113
+ }
114
+
115
+ /**
116
+ * Pulse SRCLR LOW then HIGH to clear the shift register.
117
+ *
118
+ * The storage register (and therefore the outputs) is unaffected until
119
+ * the next write(). No-op if this connection is disabled.
120
+ *
121
+ * @throws {Error} If srclr was not configured.
122
+ */
123
+ clear() {
124
+ if (!this._enabled) return;
125
+ if (this._srclr === null) throw new Error('SRCLR not configured');
126
+ this._srclr.value = false;
127
+ this._srclr.value = true;
128
+ }
129
+
130
+ /**
131
+ * Drive G LOW (enabled) or HIGH (disabled).
132
+ *
133
+ * No-op if this connection is disabled.
134
+ *
135
+ * @param {boolean} enabled - true drives G LOW, letting the storage
136
+ * register drive the outputs. false drives G HIGH, forcing every
137
+ * output off without disturbing the storage register's contents.
138
+ * @throws {Error} If g was not configured.
139
+ */
140
+ setOutputEnable(enabled) {
141
+ if (!this._enabled) return;
142
+ if (this._g === null) throw new Error('G not configured');
143
+ this._g.value = !enabled;
144
+ }
145
+
146
+ /**
147
+ * Close the SPI device (if opened) and stop all configured GPIO pins.
148
+ */
149
+ close() {
150
+ if (this._device !== null) this._device.closeSync();
151
+ this._rck.stop();
152
+ if (this._srclr !== null) this._srclr.stop();
153
+ if (this._g !== null) this._g.stop();
154
+ if (this._serIn !== null) this._serIn.stop();
155
+ if (this._srck !== null) this._srck.stop();
156
+ }
157
+ }
158
+
159
+ module.exports = { SiPoConnection };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const i2c = require('i2c-bus');
4
+ const { Connection } = require('./connection');
4
5
 
5
6
  /**
6
7
  * Computes CRC-8 with polynomial 0x07 (x⁸ + x² + x + 1), initial value 0x00.
@@ -19,20 +20,23 @@ function crc8(data) {
19
20
  }
20
21
 
21
22
  /**
22
- * SMBus transport for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
23
+ * SMBus connection for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
23
24
  *
24
25
  * Enforces the valid 7-bit SMBus address range (0x08–0x77) and, when pec=true,
25
26
  * appends a CRC-8 byte to writes and verifies it on reads. PEC is computed in
26
27
  * software using raw i2c transfers. Call close() to release the bus when done.
27
28
  */
28
- class SMBusTransport {
29
+ class SMBusConnection extends Connection {
29
30
  /**
30
31
  * @param {number} busNumber - I²C bus number (opens /dev/i2c-{busNumber}).
31
32
  * @param {number} addr - 7-bit device address (0x08–0x77).
32
33
  * @param {boolean} [pec=false] - Enable Packet Error Code (CRC-8) checking.
34
+ * @param {import('./input_pin').InputPin|null} [intPin=null] - Optional INT-line InputPin.
35
+ * @param {import('./output_pin').OutputPin|null} [enPin=null] - Optional EN-pin OutputPin.
33
36
  * @throws {RangeError} If addr is outside the valid SMBus range.
34
37
  */
35
- constructor(busNumber, addr, pec = false) {
38
+ constructor(busNumber, addr, pec = false, intPin = null, enPin = null) {
39
+ super(intPin, enPin);
36
40
  if (addr < 0x08 || addr > 0x77) {
37
41
  throw new RangeError('SMBus address must be in range 0x08-0x77');
38
42
  }
@@ -45,7 +49,7 @@ class SMBusTransport {
45
49
  * Send bytes to the device, appending a PEC byte if enabled.
46
50
  * @param {Buffer|Uint8Array} data - Bytes to write.
47
51
  */
48
- write(data) {
52
+ async _write(data) {
49
53
  let buf = Buffer.from(data);
50
54
  if (this._pec) {
51
55
  const pec = crc8(Buffer.concat([Buffer.from([this._addr << 1]), buf]));
@@ -63,7 +67,7 @@ class SMBusTransport {
63
67
  * @returns {Buffer} The n data bytes (PEC byte stripped if enabled).
64
68
  * @throws {Error} If the received PEC byte does not match.
65
69
  */
66
- read(n) {
70
+ async _read(n) {
67
71
  const count = this._pec ? n + 1 : n;
68
72
  const buf = Buffer.alloc(count);
69
73
  this._bus.i2cReadSync(this._addr, count, buf);
@@ -81,14 +85,14 @@ class SMBusTransport {
81
85
  *
82
86
  * PEC covers the full transaction: write address + write data +
83
87
  * read address + read data. The same single-command-byte limitation as
84
- * I2CTransport applies: only data[0] is sent as the write byte.
88
+ * I2CConnection applies: only data[0] is sent as the write byte.
85
89
  *
86
90
  * @param {Buffer|Uint8Array} data - Register address to send; only data[0] is used.
87
91
  * @param {number} n - Number of data bytes to read back.
88
92
  * @returns {Buffer} The n data bytes (PEC byte stripped if enabled).
89
93
  * @throws {Error} If the received PEC byte does not match.
90
94
  */
91
- writeRead(data, n) {
95
+ async _writeRead(data, n) {
92
96
  const count = this._pec ? n + 1 : n;
93
97
  const buf = Buffer.alloc(count);
94
98
  this._bus.readI2cBlockSync(this._addr, data[0], count, buf);
@@ -107,11 +111,11 @@ class SMBusTransport {
107
111
  }
108
112
 
109
113
  /**
110
- * Close the I²C bus. Must be called when the transport is no longer needed.
114
+ * Close the I²C bus. Must be called when the connection is no longer needed.
111
115
  */
112
- close() {
116
+ async close() {
113
117
  this._bus.closeSync();
114
118
  }
115
119
  }
116
120
 
117
- module.exports = { SMBusTransport };
121
+ module.exports = { SMBusConnection };
@@ -1,22 +1,26 @@
1
1
  'use strict';
2
2
 
3
3
  const spi = require('spi-device');
4
+ const { Connection } = require('./connection');
4
5
 
5
6
  /**
6
- * SPI transport for Node.js (wraps spi-device).
7
+ * SPI connection for Node.js (wraps spi-device).
7
8
  *
8
9
  * Opens the spidev device synchronously at construction. CS is managed by
9
10
  * the kernel spidev driver. Call close() when done.
10
11
  */
11
- class SPITransport {
12
+ class SPIConnection extends Connection {
12
13
  /**
13
14
  * @param {number} busNumber - SPI bus number.
14
15
  * @param {number} deviceNumber - Chip-select line on the bus.
15
16
  * @param {object} [options]
16
17
  * @param {number} [options.mode=spi.MODE0] - SPI mode (0–3).
17
18
  * @param {number} [options.maxSpeedHz=1_000_000] - Clock frequency in Hz.
19
+ * @param {import('./input_pin').InputPin|null} [options.intPin=null] - Optional INT-line InputPin.
20
+ * @param {import('./output_pin').OutputPin|null} [options.enPin=null] - Optional EN-pin OutputPin.
18
21
  */
19
22
  constructor(busNumber, deviceNumber, options = {}) {
23
+ super(options.intPin ?? null, options.enPin ?? null);
20
24
  this._device = spi.openSync(busNumber, deviceNumber, {
21
25
  mode: options.mode ?? spi.MODE0,
22
26
  maxSpeedHz: options.maxSpeedHz ?? 1_000_000,
@@ -27,7 +31,7 @@ class SPITransport {
27
31
  * Send bytes to the device.
28
32
  * @param {Buffer|Uint8Array} data - Bytes to send.
29
33
  */
30
- write(data) {
34
+ async _write(data) {
31
35
  const sendBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
32
36
  this._device.transferSync([{ sendBuffer, byteLength: sendBuffer.length }]);
33
37
  }
@@ -37,7 +41,7 @@ class SPITransport {
37
41
  * @param {number} n - Number of bytes to read.
38
42
  * @returns {Buffer} Data received from the device.
39
43
  */
40
- read(n) {
44
+ async _read(n) {
41
45
  const receiveBuffer = Buffer.alloc(n);
42
46
  this._device.transferSync([{ receiveBuffer, byteLength: n }]);
43
47
  return receiveBuffer;
@@ -53,7 +57,7 @@ class SPITransport {
53
57
  * @param {number} n - Number of response bytes expected.
54
58
  * @returns {Buffer} The n response bytes.
55
59
  */
56
- writeRead(data, n) {
60
+ async _writeRead(data, n) {
57
61
  const prefix = Buffer.isBuffer(data) ? data : Buffer.from(data);
58
62
  const sendBuffer = Buffer.concat([prefix, Buffer.alloc(n)]);
59
63
  const receiveBuffer = Buffer.alloc(sendBuffer.length);
@@ -62,11 +66,11 @@ class SPITransport {
62
66
  }
63
67
 
64
68
  /**
65
- * Close the SPI device. Must be called when the transport is no longer needed.
69
+ * Close the SPI device. Must be called when the connection is no longer needed.
66
70
  */
67
- close() {
71
+ async close() {
68
72
  this._device.closeSync();
69
73
  }
70
74
  }
71
75
 
72
- module.exports = { SPITransport };
76
+ module.exports = { SPIConnection };