periph 1.0.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 (101) hide show
  1. package/examples/accelerometer/.gitkeep +0 -0
  2. package/examples/adc_dac/.gitkeep +0 -0
  3. package/examples/adc_dac/mcp4725/complete.js +34 -0
  4. package/examples/adc_dac/mcp4725/demo.js +43 -0
  5. package/examples/adc_dac/mcp4725/minimal.js +14 -0
  6. package/examples/color/.gitkeep +0 -0
  7. package/examples/comms/.gitkeep +0 -0
  8. package/examples/display/.gitkeep +0 -0
  9. package/examples/environmental/.gitkeep +0 -0
  10. package/examples/gas/.gitkeep +0 -0
  11. package/examples/gnss/.gitkeep +0 -0
  12. package/examples/gpio/.gitkeep +0 -0
  13. package/examples/gyroscope/.gitkeep +0 -0
  14. package/examples/humidity/.gitkeep +0 -0
  15. package/examples/imu/.gitkeep +0 -0
  16. package/examples/io_expander/mcp23017/complete.js +80 -0
  17. package/examples/io_expander/mcp23017/demo.js +59 -0
  18. package/examples/io_expander/mcp23017/minimal.js +16 -0
  19. package/examples/io_expander/pcf8574/complete.js +68 -0
  20. package/examples/io_expander/pcf8574/demo.js +41 -0
  21. package/examples/io_expander/pcf8574/minimal.js +16 -0
  22. package/examples/io_expander/pcf8575/complete.js +31 -0
  23. package/examples/io_expander/pcf8575/demo.js +33 -0
  24. package/examples/io_expander/pcf8575/minimal.js +20 -0
  25. package/examples/led/.gitkeep +0 -0
  26. package/examples/led/sk6812rgbw/complete.js +69 -0
  27. package/examples/led/sk6812rgbw/demo.js +85 -0
  28. package/examples/led/sk6812rgbw/minimal.js +25 -0
  29. package/examples/led/ws2812b/complete.js +64 -0
  30. package/examples/led/ws2812b/demo.js +101 -0
  31. package/examples/led/ws2812b/minimal.js +22 -0
  32. package/examples/light/.gitkeep +0 -0
  33. package/examples/magnetometer/.gitkeep +0 -0
  34. package/examples/magnetometer/as5600/complete.js +43 -0
  35. package/examples/magnetometer/as5600/demo.js +49 -0
  36. package/examples/magnetometer/as5600/minimal.js +16 -0
  37. package/examples/memory/.gitkeep +0 -0
  38. package/examples/motor/.gitkeep +0 -0
  39. package/examples/other/.gitkeep +0 -0
  40. package/examples/power/ina219/complete.js +30 -0
  41. package/examples/power/ina219/demo.js +26 -0
  42. package/examples/power/ina219/minimal.js +17 -0
  43. package/examples/power/ina226/complete.js +33 -0
  44. package/examples/power/ina226/demo.js +30 -0
  45. package/examples/power/ina226/minimal.js +11 -0
  46. package/examples/power/ina3221/complete.js +65 -0
  47. package/examples/power/ina3221/demo.js +56 -0
  48. package/examples/power/ina3221/minimal.js +16 -0
  49. package/examples/pressure/.gitkeep +0 -0
  50. package/examples/pressure/bmp180/complete.js +26 -0
  51. package/examples/pressure/bmp180/demo.js +32 -0
  52. package/examples/pressure/bmp180/minimal.js +17 -0
  53. package/examples/pressure/bmp280/complete.js +28 -0
  54. package/examples/pressure/bmp280/demo.js +34 -0
  55. package/examples/pressure/bmp280/minimal.js +17 -0
  56. package/examples/rfid/.gitkeep +0 -0
  57. package/examples/rtc/.gitkeep +0 -0
  58. package/examples/temperature/.gitkeep +0 -0
  59. package/examples/tof/.gitkeep +0 -0
  60. package/package.json +20 -0
  61. package/src/chips/accelerometer/.gitkeep +0 -0
  62. package/src/chips/adc_dac/.gitkeep +0 -0
  63. package/src/chips/adc_dac/mcp4725.js +161 -0
  64. package/src/chips/color/.gitkeep +0 -0
  65. package/src/chips/comms/.gitkeep +0 -0
  66. package/src/chips/display/.gitkeep +0 -0
  67. package/src/chips/environmental/.gitkeep +0 -0
  68. package/src/chips/gas/.gitkeep +0 -0
  69. package/src/chips/gnss/.gitkeep +0 -0
  70. package/src/chips/gpio/.gitkeep +0 -0
  71. package/src/chips/gyroscope/.gitkeep +0 -0
  72. package/src/chips/humidity/.gitkeep +0 -0
  73. package/src/chips/imu/.gitkeep +0 -0
  74. package/src/chips/io_expander/mcp23017.js +396 -0
  75. package/src/chips/io_expander/pcf8574.js +298 -0
  76. package/src/chips/io_expander/pcf8575.js +295 -0
  77. package/src/chips/led/.gitkeep +0 -0
  78. package/src/chips/led/_color.js +25 -0
  79. package/src/chips/led/sk6812rgbw.js +183 -0
  80. package/src/chips/led/ws2812b.js +171 -0
  81. package/src/chips/light/.gitkeep +0 -0
  82. package/src/chips/magnetometer/as5600.js +293 -0
  83. package/src/chips/memory/.gitkeep +0 -0
  84. package/src/chips/motor/.gitkeep +0 -0
  85. package/src/chips/other/.gitkeep +0 -0
  86. package/src/chips/power/ina219.js +154 -0
  87. package/src/chips/power/ina226.js +208 -0
  88. package/src/chips/power/ina3221.js +341 -0
  89. package/src/chips/pressure/.gitkeep +0 -0
  90. package/src/chips/pressure/bmp180.js +234 -0
  91. package/src/chips/pressure/bmp280.js +328 -0
  92. package/src/chips/rfid/.gitkeep +0 -0
  93. package/src/chips/rtc/.gitkeep +0 -0
  94. package/src/chips/temperature/.gitkeep +0 -0
  95. package/src/chips/tof/.gitkeep +0 -0
  96. package/src/index.js +2 -0
  97. package/src/transport/i2c.js +67 -0
  98. package/src/transport/neopixel.js +37 -0
  99. package/src/transport/smbus.js +117 -0
  100. package/src/transport/spi.js +72 -0
  101. package/src/transport/uart.js +145 -0
@@ -0,0 +1,328 @@
1
+ 'use strict';
2
+
3
+ const _REG_ID = 0xD0;
4
+ const _REG_RESET = 0xE0;
5
+ const _REG_STATUS = 0xF3;
6
+ const _REG_CTRL_MEAS = 0xF4;
7
+ const _REG_CONFIG = 0xF5;
8
+ const _REG_CAL_START = 0x88;
9
+ const _REG_DATA = 0xF7;
10
+
11
+ const _CHIP_ID = 0x58;
12
+ const _RESET_CMD = 0xB6;
13
+ const _CTRL_MEAS_DEFAULT = 0x25;
14
+
15
+ function _delay(ms) {
16
+ const start = Date.now();
17
+ while (Date.now() - start < ms) { /* spin */ }
18
+ }
19
+
20
+ /**
21
+ * BMP280 piezo-resistive pressure + temperature sensor — minimal interface.
22
+ *
23
+ * Provides calibrated temperature (°C) and pressure (hPa) with no configuration
24
+ * beyond the transport. Default configuration: forced mode, oversampling ×1 for
25
+ * both channels, IIR filter off.
26
+ *
27
+ * @param {object} transport - Configured I²C transport.
28
+ * @param {number} [addr=0x76] - 7-bit I²C address (default 0x76, alternate 0x77).
29
+ */
30
+ class BMP280Minimal {
31
+ constructor(transport, addr = 0x76) {
32
+ this._transport = transport;
33
+ this._addr = addr;
34
+ this._t_fine = null;
35
+ this._ctrlMeasCache = _CTRL_MEAS_DEFAULT;
36
+ this._loadCalibration();
37
+ this._writeCtrlMeas(_CTRL_MEAS_DEFAULT);
38
+ this._writeConfig(0x00);
39
+ }
40
+
41
+ _writeReg(reg, value) {
42
+ this._transport.write(Buffer.from([reg, value]));
43
+ }
44
+
45
+ _readReg(reg, len) {
46
+ return this._transport.writeRead(Buffer.from([reg]), len);
47
+ }
48
+
49
+ _writeCtrlMeas(value) {
50
+ this._writeReg(_REG_CTRL_MEAS, value);
51
+ }
52
+
53
+ _writeConfig(value) {
54
+ this._writeReg(_REG_CONFIG, value);
55
+ }
56
+
57
+ _loadCalibration() {
58
+ const raw = this._readReg(_REG_CAL_START, 24);
59
+ this._dig_T1 = raw.readUInt16LE(0);
60
+ this._dig_T2 = raw.readInt16LE(2);
61
+ this._dig_T3 = raw.readInt16LE(4);
62
+ this._dig_P1 = raw.readUInt16LE(6);
63
+ this._dig_P2 = raw.readInt16LE(8);
64
+ this._dig_P3 = raw.readInt16LE(10);
65
+ this._dig_P4 = raw.readInt16LE(12);
66
+ this._dig_P5 = raw.readInt16LE(14);
67
+ this._dig_P6 = raw.readInt16LE(16);
68
+ this._dig_P7 = raw.readInt16LE(18);
69
+ this._dig_P8 = raw.readInt16LE(20);
70
+ this._dig_P9 = raw.readInt16LE(22);
71
+ }
72
+
73
+ _triggerReadBurst() {
74
+ const raw = this._readReg(_REG_DATA, 6);
75
+ const adc_P = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
76
+ const adc_T = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
77
+ return [adc_T, adc_P];
78
+ }
79
+
80
+ _compensateTemp(adc_T) {
81
+ const T1 = BigInt(this._dig_T1);
82
+ const T2 = BigInt(this._dig_T2);
83
+ const T3 = BigInt(this._dig_T3);
84
+ const t = BigInt(adc_T);
85
+ const var1 = (((t >> 3n) - (T1 << 1n)) * T2) >> 11n;
86
+ const var2 = (((((t >> 4n) - T1) * ((t >> 4n) - T1)) >> 12n) * T3) >> 14n;
87
+ this._t_fine = Number(var1 + var2);
88
+ return Number(((var1 + var2) * 5n + 128n) >> 8n) / 100.0;
89
+ }
90
+
91
+ _compensatePressure(adc_P) {
92
+ const tf = BigInt(this._t_fine !== null ? this._t_fine : 0);
93
+ const P1 = BigInt(this._dig_P1), P2 = BigInt(this._dig_P2), P3 = BigInt(this._dig_P3);
94
+ const P4 = BigInt(this._dig_P4), P5 = BigInt(this._dig_P5), P6 = BigInt(this._dig_P6);
95
+ const P7 = BigInt(this._dig_P7), P8 = BigInt(this._dig_P8), P9 = BigInt(this._dig_P9);
96
+ let var1 = tf - 128000n;
97
+ let var2 = var1 * var1 * P6;
98
+ var2 = var2 + ((var1 * P5) << 17n);
99
+ var2 = var2 + (P4 << 35n);
100
+ var1 = ((var1 * var1 * P3) >> 8n) + ((var1 * P2) << 12n);
101
+ var1 = (((1n << 47n) + var1) * P1) >> 33n;
102
+ if (var1 === 0n) return 0.0;
103
+ let p = BigInt(1048576) - BigInt(adc_P);
104
+ p = (((p << 31n) - var2) * 3125n) / var1;
105
+ var1 = (P9 * (p >> 13n) * (p >> 13n)) >> 25n;
106
+ var2 = (P8 * p) >> 19n;
107
+ p = ((p + var1 + var2) >> 8n) + (P7 << 4n);
108
+ return Number(p) / 25600.0;
109
+ }
110
+
111
+ _triggerMeasurement() {
112
+ this._writeCtrlMeas(this._ctrlMeasCache);
113
+ _delay(7);
114
+ }
115
+
116
+ /**
117
+ * Read calibrated temperature.
118
+ *
119
+ * Triggers a forced-mode conversion and returns temperature in °C.
120
+ * Caches t_fine for use in subsequent pressure() calls.
121
+ *
122
+ * @returns {number} Temperature in degrees Celsius.
123
+ */
124
+ temperature() {
125
+ this._triggerMeasurement();
126
+ const [adc_T] = this._triggerReadBurst();
127
+ return this._compensateTemp(adc_T);
128
+ }
129
+
130
+ /**
131
+ * Read calibrated pressure.
132
+ *
133
+ * Triggers a forced-mode conversion and returns pressure in hPa.
134
+ * Re-reads the temperature ADC alongside pressure to refresh t_fine.
135
+ *
136
+ * @returns {number} Pressure in hPa.
137
+ */
138
+ pressure() {
139
+ this._triggerMeasurement();
140
+ const [adc_T, adc_P] = this._triggerReadBurst();
141
+ this._compensateTemp(adc_T);
142
+ return this._compensatePressure(adc_P);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * BMP280 full interface — extends BMP280Minimal with configuration and altitude helpers.
148
+ *
149
+ * Adds power-mode control, oversampling settings, IIR filter, standby time,
150
+ * status read, altitude / sea-level helpers, chip_id, and reset.
151
+ *
152
+ * @param {object} transport - Configured I²C transport.
153
+ * @param {number} [addr=0x76] - 7-bit I²C address.
154
+ * @param {number} [osrs_t=1] - Temperature oversampling 0–5 (default ×1).
155
+ * @param {number} [osrs_p=1] - Pressure oversampling 0–5 (default ×1).
156
+ * @param {number} [mode=1] - Power mode (default forced).
157
+ * @param {number} [filter=0] - IIR filter coefficient (default off).
158
+ * @param {number} [t_sb=0] - Standby time in normal mode 0–7 (default 0.5 ms).
159
+ */
160
+ class BMP280Full extends BMP280Minimal {
161
+ static OSRS_SKIP = 0;
162
+ static OSRS_X1 = 1;
163
+ static OSRS_X2 = 2;
164
+ static OSRS_X4 = 3;
165
+ static OSRS_X8 = 4;
166
+ static OSRS_X16 = 5;
167
+
168
+ static MODE_SLEEP = 0;
169
+ static MODE_FORCED = 1;
170
+ static MODE_NORMAL = 3;
171
+
172
+ static FILTER_OFF = 0;
173
+ static FILTER_2 = 1;
174
+ static FILTER_4 = 2;
175
+ static FILTER_8 = 3;
176
+ static FILTER_16 = 4;
177
+
178
+ static T_SB_0_5_MS = 0;
179
+ static T_SB_62_5_MS = 1;
180
+ static T_SB_125_MS = 2;
181
+ static T_SB_250_MS = 3;
182
+ static T_SB_500_MS = 4;
183
+ static T_SB_1000_MS = 5;
184
+ static T_SB_2000_MS = 6;
185
+ static T_SB_4000_MS = 7;
186
+
187
+ static STATUS_MEASURING = 0x08;
188
+ static STATUS_IM_UPDATE = 0x01;
189
+
190
+ constructor(transport, addr = 0x76, osrs_t = 1, osrs_p = 1, mode = 1, filter = 0, t_sb = 0) {
191
+ super(transport, addr);
192
+ this._osrs_t = osrs_t;
193
+ this._osrs_p = osrs_p;
194
+ this._mode = mode;
195
+ this._filter = filter;
196
+ this._t_sb = t_sb;
197
+ this._ctrlMeasCache = this._ctrlMeasValue();
198
+ this._writeCtrlMeas(this._ctrlMeasCache);
199
+ this._writeConfig(this._configValue());
200
+ }
201
+
202
+ _ctrlMeasValue() {
203
+ return (this._osrs_t << 5) | (this._osrs_p << 2) | this._mode;
204
+ }
205
+
206
+ _configValue() {
207
+ return (this._t_sb << 5) | (this._filter << 2);
208
+ }
209
+
210
+ /**
211
+ * Update chip configuration.
212
+ *
213
+ * @param {number} [osrs_t] - Temperature oversampling 0–5.
214
+ * @param {number} [osrs_p] - Pressure oversampling 0–5.
215
+ * @param {number} [mode] - Power mode (0=sleep, 1=forced, 3=normal).
216
+ * @param {number} [filter] - IIR filter coefficient (0=off, 1, 2, 3, 4=×16).
217
+ * @param {number} [t_sb] - Standby time in normal mode (0–7).
218
+ */
219
+ configure(osrs_t, osrs_p, mode, filter, t_sb) {
220
+ if (osrs_t !== undefined) this._osrs_t = osrs_t;
221
+ if (osrs_p !== undefined) this._osrs_p = osrs_p;
222
+ if (mode !== undefined) this._mode = mode;
223
+ if (filter !== undefined) this._filter = filter;
224
+ if (t_sb !== undefined) this._t_sb = t_sb;
225
+ this._ctrlMeasCache = this._ctrlMeasValue();
226
+ this._writeCtrlMeas(this._ctrlMeasCache);
227
+ this._writeConfig(this._configValue());
228
+ }
229
+
230
+ /**
231
+ * Update oversampling settings.
232
+ *
233
+ * @param {number} osrs_t - Temperature oversampling 0–5.
234
+ * @param {number} osrs_p - Pressure oversampling 0–5.
235
+ */
236
+ setOversampling(osrs_t, osrs_p) {
237
+ this._osrs_t = osrs_t;
238
+ this._osrs_p = osrs_p;
239
+ this._ctrlMeasCache = this._ctrlMeasValue();
240
+ this._writeCtrlMeas(this._ctrlMeasCache);
241
+ }
242
+
243
+ /**
244
+ * Update power mode.
245
+ *
246
+ * @param {number} mode - 0=sleep, 1=forced, 3=normal.
247
+ */
248
+ setMode(mode) {
249
+ this._mode = mode;
250
+ this._ctrlMeasCache = this._ctrlMeasValue();
251
+ this._writeCtrlMeas(this._ctrlMeasCache);
252
+ }
253
+
254
+ /**
255
+ * Update IIR filter coefficient.
256
+ *
257
+ * @param {number} coeff - 0=off, 1=×2, 2=×4, 3=×8, 4=×16.
258
+ */
259
+ setFilter(coeff) {
260
+ this._filter = coeff;
261
+ this._writeConfig(this._configValue());
262
+ }
263
+
264
+ /**
265
+ * Update standby time (only relevant in normal mode).
266
+ *
267
+ * @param {number} t_sb - 0=0.5ms, 1=62.5ms, 2=125ms, 3=250ms, 4=500ms, 5=1s, 6=2s, 7=4s.
268
+ */
269
+ setStandby(t_sb) {
270
+ this._t_sb = t_sb;
271
+ this._writeConfig(this._configValue());
272
+ }
273
+
274
+ /**
275
+ * Read status register.
276
+ *
277
+ * @returns {number} Status byte; check STATUS_MEASURING and STATUS_IM_UPDATE bits.
278
+ */
279
+ status() {
280
+ return this._readReg(_REG_STATUS, 1)[0];
281
+ }
282
+
283
+ /**
284
+ * Compute altitude above sea level from current pressure.
285
+ *
286
+ * @param {number} [seaLevelHpa=1013.25] - Reference sea-level pressure in hPa.
287
+ * @returns {number} Altitude in metres.
288
+ */
289
+ altitude(seaLevelHpa = 1013.25) {
290
+ const p = this.pressure();
291
+ return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
292
+ }
293
+
294
+ /**
295
+ * Compute sea-level pressure for a known altitude.
296
+ *
297
+ * @param {number} altitudeM - Altitude in metres.
298
+ * @returns {number} Sea-level pressure in hPa.
299
+ */
300
+ seaLevelPressure(altitudeM) {
301
+ const p = this.pressure();
302
+ return p / Math.pow(1 - altitudeM / 44330, 5.255);
303
+ }
304
+
305
+ /**
306
+ * Read chip ID register.
307
+ *
308
+ * @returns {number} Chip ID; expect 0x58 for BMP280.
309
+ */
310
+ chipId() {
311
+ return this._readReg(_REG_ID, 1)[0];
312
+ }
313
+
314
+ /**
315
+ * Perform soft reset and re-read calibration coefficients.
316
+ *
317
+ * Re-applies the current ctrl_meas and config settings.
318
+ */
319
+ reset() {
320
+ this._writeReg(_REG_RESET, _RESET_CMD);
321
+ _delay(2);
322
+ this._loadCalibration();
323
+ this._writeCtrlMeas(this._ctrlMeasValue());
324
+ this._writeConfig(this._configValue());
325
+ }
326
+ }
327
+
328
+ module.exports = { BMP280Minimal, BMP280Full };
File without changes
File without changes
File without changes
File without changes
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // Import chip drivers directly by path:
2
+ // const INA226 = require('periph/src/chips/power/ina226')
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ const i2c = require('i2c-bus');
4
+
5
+ /**
6
+ * I²C transport for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
7
+ *
8
+ * One instance represents one device on the bus; the bus is opened
9
+ * synchronously at construction. Call close() to release the bus when done.
10
+ */
11
+ class I2CTransport {
12
+ /**
13
+ * @param {number} busNumber - I²C bus number (opens /dev/i2c-{busNumber}).
14
+ * @param {number} addr - 7-bit device address.
15
+ */
16
+ constructor(busNumber, addr) {
17
+ this._bus = i2c.openSync(busNumber);
18
+ this._addr = addr;
19
+ }
20
+
21
+ /**
22
+ * Send bytes to the device.
23
+ * @param {Buffer|Uint8Array} data - Bytes to write.
24
+ */
25
+ write(data) {
26
+ this._bus.i2cWriteSync(this._addr, data.length, data);
27
+ }
28
+
29
+ /**
30
+ * Read bytes from the device.
31
+ * @param {number} n - Number of bytes to read.
32
+ * @returns {Buffer} Data received from the device.
33
+ */
34
+ read(n) {
35
+ const buf = Buffer.alloc(n);
36
+ this._bus.i2cReadSync(this._addr, n, buf);
37
+ return buf;
38
+ }
39
+
40
+ /**
41
+ * Write a single register address byte then read data back (repeated start).
42
+ *
43
+ * Uses readI2cBlockSync, which accepts exactly one command byte. Only
44
+ * data[0] is sent; any additional bytes in data are ignored. This is a
45
+ * library constraint: i2c-bus v5 has no synchronous multi-message transfer
46
+ * API (I2C_RDWR with multiple messages), so multi-byte writes before the
47
+ * repeated start are not supported in synchronous mode.
48
+ *
49
+ * @param {Buffer|Uint8Array} data - Register address to send; only data[0] is used.
50
+ * @param {number} n - Number of bytes to read back.
51
+ * @returns {Buffer} Data received from the device.
52
+ */
53
+ writeRead(data, n) {
54
+ const buf = Buffer.alloc(n);
55
+ this._bus.readI2cBlockSync(this._addr, data[0], n, buf);
56
+ return buf;
57
+ }
58
+
59
+ /**
60
+ * Close the I²C bus. Must be called when the transport is no longer needed.
61
+ */
62
+ close() {
63
+ this._bus.closeSync();
64
+ }
65
+ }
66
+
67
+ module.exports = { I2CTransport };
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ const spi = require('spi-device');
4
+
5
+ function _encode(data) {
6
+ const out = Buffer.alloc(data.length * 3 + 16);
7
+ for (let i = 0; i < data.length; i++) {
8
+ let bits = 0;
9
+ for (let bit = 7; bit >= 0; bit--) {
10
+ bits = (bits << 3) | ((data[i] >> bit) & 1 ? 0b110 : 0b100);
11
+ }
12
+ out[i * 3] = (bits >> 16) & 0xFF;
13
+ out[i * 3 + 1] = (bits >> 8) & 0xFF;
14
+ out[i * 3 + 2] = bits & 0xFF;
15
+ }
16
+ return out;
17
+ }
18
+
19
+ class NeoPixelTransport {
20
+ constructor(busNumber, deviceNumber) {
21
+ this._device = spi.openSync(busNumber, deviceNumber, {
22
+ mode: spi.MODE0,
23
+ maxSpeedHz: 2_400_000,
24
+ });
25
+ }
26
+
27
+ write(data) {
28
+ const encoded = _encode(Buffer.isBuffer(data) ? data : Buffer.from(data));
29
+ this._device.transferSync([{ sendBuffer: encoded, byteLength: encoded.length }]);
30
+ }
31
+
32
+ close() {
33
+ this._device.closeSync();
34
+ }
35
+ }
36
+
37
+ module.exports = { NeoPixelTransport };
@@ -0,0 +1,117 @@
1
+ 'use strict';
2
+
3
+ const i2c = require('i2c-bus');
4
+
5
+ /**
6
+ * Computes CRC-8 with polynomial 0x07 (x⁸ + x² + x + 1), initial value 0x00.
7
+ * @param {Buffer|Uint8Array} data
8
+ * @returns {number}
9
+ */
10
+ function crc8(data) {
11
+ let crc = 0;
12
+ for (const byte of data) {
13
+ crc ^= byte;
14
+ for (let i = 0; i < 8; i++) {
15
+ crc = (crc & 0x80) ? ((crc << 1) ^ 0x07) & 0xFF : (crc << 1) & 0xFF;
16
+ }
17
+ }
18
+ return crc;
19
+ }
20
+
21
+ /**
22
+ * SMBus transport for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
23
+ *
24
+ * Enforces the valid 7-bit SMBus address range (0x08–0x77) and, when pec=true,
25
+ * appends a CRC-8 byte to writes and verifies it on reads. PEC is computed in
26
+ * software using raw i2c transfers. Call close() to release the bus when done.
27
+ */
28
+ class SMBusTransport {
29
+ /**
30
+ * @param {number} busNumber - I²C bus number (opens /dev/i2c-{busNumber}).
31
+ * @param {number} addr - 7-bit device address (0x08–0x77).
32
+ * @param {boolean} [pec=false] - Enable Packet Error Code (CRC-8) checking.
33
+ * @throws {RangeError} If addr is outside the valid SMBus range.
34
+ */
35
+ constructor(busNumber, addr, pec = false) {
36
+ if (addr < 0x08 || addr > 0x77) {
37
+ throw new RangeError('SMBus address must be in range 0x08-0x77');
38
+ }
39
+ this._bus = i2c.openSync(busNumber);
40
+ this._addr = addr;
41
+ this._pec = pec;
42
+ }
43
+
44
+ /**
45
+ * Send bytes to the device, appending a PEC byte if enabled.
46
+ * @param {Buffer|Uint8Array} data - Bytes to write.
47
+ */
48
+ write(data) {
49
+ let buf = Buffer.from(data);
50
+ if (this._pec) {
51
+ const pec = crc8(Buffer.concat([Buffer.from([this._addr << 1]), buf]));
52
+ buf = Buffer.concat([buf, Buffer.from([pec])]);
53
+ }
54
+ this._bus.i2cWriteSync(this._addr, buf.length, buf);
55
+ }
56
+
57
+ /**
58
+ * Read bytes from the device, verifying the PEC byte if enabled.
59
+ *
60
+ * Reads n+1 bytes when PEC is enabled; the trailing byte is the CRC.
61
+ *
62
+ * @param {number} n - Number of data bytes to read.
63
+ * @returns {Buffer} The n data bytes (PEC byte stripped if enabled).
64
+ * @throws {Error} If the received PEC byte does not match.
65
+ */
66
+ read(n) {
67
+ const count = this._pec ? n + 1 : n;
68
+ const buf = Buffer.alloc(count);
69
+ this._bus.i2cReadSync(this._addr, count, buf);
70
+ if (this._pec) {
71
+ const data = buf.slice(0, n);
72
+ const expected = crc8(Buffer.concat([Buffer.from([(this._addr << 1) | 1]), data]));
73
+ if (expected !== buf[n]) throw new Error('SMBus PEC error');
74
+ return data;
75
+ }
76
+ return buf;
77
+ }
78
+
79
+ /**
80
+ * Write a register address byte then read data back (repeated start), with optional PEC.
81
+ *
82
+ * PEC covers the full transaction: write address + write data +
83
+ * read address + read data. The same single-command-byte limitation as
84
+ * I2CTransport applies: only data[0] is sent as the write byte.
85
+ *
86
+ * @param {Buffer|Uint8Array} data - Register address to send; only data[0] is used.
87
+ * @param {number} n - Number of data bytes to read back.
88
+ * @returns {Buffer} The n data bytes (PEC byte stripped if enabled).
89
+ * @throws {Error} If the received PEC byte does not match.
90
+ */
91
+ writeRead(data, n) {
92
+ const count = this._pec ? n + 1 : n;
93
+ const buf = Buffer.alloc(count);
94
+ this._bus.readI2cBlockSync(this._addr, data[0], count, buf);
95
+ if (this._pec) {
96
+ const payload = buf.slice(0, n);
97
+ const expected = crc8(Buffer.concat([
98
+ Buffer.from([this._addr << 1]),
99
+ Buffer.from([data[0]]),
100
+ Buffer.from([(this._addr << 1) | 1]),
101
+ payload,
102
+ ]));
103
+ if (expected !== buf[n]) throw new Error('SMBus PEC error');
104
+ return payload;
105
+ }
106
+ return buf;
107
+ }
108
+
109
+ /**
110
+ * Close the I²C bus. Must be called when the transport is no longer needed.
111
+ */
112
+ close() {
113
+ this._bus.closeSync();
114
+ }
115
+ }
116
+
117
+ module.exports = { SMBusTransport };
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ const spi = require('spi-device');
4
+
5
+ /**
6
+ * SPI transport for Node.js (wraps spi-device).
7
+ *
8
+ * Opens the spidev device synchronously at construction. CS is managed by
9
+ * the kernel spidev driver. Call close() when done.
10
+ */
11
+ class SPITransport {
12
+ /**
13
+ * @param {number} busNumber - SPI bus number.
14
+ * @param {number} deviceNumber - Chip-select line on the bus.
15
+ * @param {object} [options]
16
+ * @param {number} [options.mode=spi.MODE0] - SPI mode (0–3).
17
+ * @param {number} [options.maxSpeedHz=1_000_000] - Clock frequency in Hz.
18
+ */
19
+ constructor(busNumber, deviceNumber, options = {}) {
20
+ this._device = spi.openSync(busNumber, deviceNumber, {
21
+ mode: options.mode ?? spi.MODE0,
22
+ maxSpeedHz: options.maxSpeedHz ?? 1_000_000,
23
+ });
24
+ }
25
+
26
+ /**
27
+ * Send bytes to the device.
28
+ * @param {Buffer|Uint8Array} data - Bytes to send.
29
+ */
30
+ write(data) {
31
+ const sendBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
32
+ this._device.transferSync([{ sendBuffer, byteLength: sendBuffer.length }]);
33
+ }
34
+
35
+ /**
36
+ * Read bytes from the device.
37
+ * @param {number} n - Number of bytes to read.
38
+ * @returns {Buffer} Data received from the device.
39
+ */
40
+ read(n) {
41
+ const receiveBuffer = Buffer.alloc(n);
42
+ this._device.transferSync([{ receiveBuffer, byteLength: n }]);
43
+ return receiveBuffer;
44
+ }
45
+
46
+ /**
47
+ * Full-duplex write+read in a single SPI transfer (CS held for the entire transfer).
48
+ *
49
+ * Sends len(data)+n bytes total and discards the first len(data) received
50
+ * bytes (chip response during the command phase).
51
+ *
52
+ * @param {Buffer|Uint8Array} data - Command bytes to send.
53
+ * @param {number} n - Number of response bytes expected.
54
+ * @returns {Buffer} The n response bytes.
55
+ */
56
+ writeRead(data, n) {
57
+ const prefix = Buffer.isBuffer(data) ? data : Buffer.from(data);
58
+ const sendBuffer = Buffer.concat([prefix, Buffer.alloc(n)]);
59
+ const receiveBuffer = Buffer.alloc(sendBuffer.length);
60
+ this._device.transferSync([{ sendBuffer, receiveBuffer, byteLength: sendBuffer.length }]);
61
+ return receiveBuffer.subarray(prefix.length);
62
+ }
63
+
64
+ /**
65
+ * Close the SPI device. Must be called when the transport is no longer needed.
66
+ */
67
+ close() {
68
+ this._device.closeSync();
69
+ }
70
+ }
71
+
72
+ module.exports = { SPITransport };