periph 1.2.1 → 1.3.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 (123) hide show
  1. package/README.md +11 -0
  2. package/examples/accelerometer/bma150/complete.js +62 -0
  3. package/examples/accelerometer/bma150/demo.js +65 -0
  4. package/examples/accelerometer/bma150/minimal.js +18 -0
  5. package/examples/adc_dac/hy3131/complete.js +66 -0
  6. package/examples/adc_dac/hy3131/demo.js +37 -0
  7. package/examples/adc_dac/hy3131/minimal.js +18 -0
  8. package/examples/discovery/complete.js +25 -0
  9. package/examples/discovery/demo.js +30 -0
  10. package/examples/discovery/minimal.js +12 -0
  11. package/examples/display/il0373/complete.js +71 -0
  12. package/examples/display/il0373/demo.js +49 -0
  13. package/examples/display/il0373/minimal.js +26 -0
  14. package/examples/display/ssd1331/complete.js +83 -0
  15. package/examples/display/ssd1331/demo.js +49 -0
  16. package/examples/display/ssd1331/minimal.js +27 -0
  17. package/examples/display/st7036/complete.js +71 -0
  18. package/examples/display/st7036/demo.js +56 -0
  19. package/examples/display/st7036/minimal.js +21 -0
  20. package/examples/display/uc1611s/complete.js +135 -0
  21. package/examples/display/uc1611s/complete_spi.js +128 -0
  22. package/examples/display/uc1611s/demo.js +90 -0
  23. package/examples/display/uc1611s/demo_spi.js +91 -0
  24. package/examples/display/uc1611s/minimal.js +28 -0
  25. package/examples/display/uc1611s/minimal_spi.js +30 -0
  26. package/examples/display/uc8159/complete.js +64 -0
  27. package/examples/display/uc8159/demo.js +41 -0
  28. package/examples/display/uc8159/minimal.js +28 -0
  29. package/examples/display/uc8276c/complete.js +73 -0
  30. package/examples/display/uc8276c/demo.js +49 -0
  31. package/examples/display/uc8276c/minimal.js +26 -0
  32. package/examples/environmental/bme280/complete.js +1 -1
  33. package/examples/environmental/bme280/demo.js +1 -1
  34. package/examples/environmental/bme280/minimal.js +1 -1
  35. package/examples/gyroscope/l3g4200d/complete.js +1 -1
  36. package/examples/gyroscope/l3g4200d/demo.js +1 -1
  37. package/examples/gyroscope/l3g4200d/minimal.js +1 -1
  38. package/examples/memory/23x1024/complete.js +54 -0
  39. package/examples/memory/23x1024/demo.js +51 -0
  40. package/examples/memory/23x1024/minimal.js +22 -0
  41. package/examples/memory/mx25l12845e/complete.js +66 -0
  42. package/examples/memory/mx25l12845e/demo.js +66 -0
  43. package/examples/memory/mx25l12845e/minimal.js +27 -0
  44. package/examples/memory/sd_spi/complete.js +55 -0
  45. package/examples/memory/sd_spi/demo.js +67 -0
  46. package/examples/memory/sd_spi/minimal.js +32 -0
  47. package/examples/pressure/bmp280/complete.js +1 -1
  48. package/examples/pressure/bmp280/demo.js +1 -1
  49. package/examples/pressure/bmp280/minimal.js +1 -1
  50. package/examples/pressure/bmp581/complete.js +1 -1
  51. package/examples/pressure/bmp581/demo.js +1 -1
  52. package/examples/pressure/bmp581/minimal.js +1 -1
  53. package/examples/pressure/lps22df/complete.js +1 -1
  54. package/examples/pressure/lps22df/demo.js +1 -1
  55. package/examples/pressure/lps22df/minimal.js +1 -1
  56. package/package.json +1 -1
  57. package/src/chips/accelerometer/adxl345.js +10 -31
  58. package/src/chips/accelerometer/adxl362.js +7 -7
  59. package/src/chips/accelerometer/bma150.js +419 -0
  60. package/src/chips/adc_dac/ad7705.js +36 -9
  61. package/src/chips/adc_dac/ad7706.js +39 -9
  62. package/src/chips/adc_dac/hy3131.js +311 -0
  63. package/src/chips/display/il0373.js +607 -0
  64. package/src/chips/display/ssd1331.js +412 -0
  65. package/src/chips/display/st7036.js +401 -0
  66. package/src/chips/display/uc1611s.js +648 -0
  67. package/src/chips/display/uc8159.js +394 -0
  68. package/src/chips/display/uc8276c.js +658 -0
  69. package/src/chips/environmental/bme280.js +31 -46
  70. package/src/chips/environmental/bme680.js +38 -43
  71. package/src/chips/gas/ens160.js +18 -26
  72. package/src/chips/gyroscope/l3g4200d.js +34 -50
  73. package/src/chips/gyroscope/l3gd20h.js +24 -40
  74. package/src/chips/imu/mpu6050.js +29 -41
  75. package/src/chips/imu/mpu9250.js +42 -66
  76. package/src/chips/imu/mpu9255.js +51 -75
  77. package/src/chips/io_expander/mcp23017.js +24 -33
  78. package/src/chips/light/apds-9930.js +52 -60
  79. package/src/chips/light/apds9960.js +69 -77
  80. package/src/chips/magnetometer/as5600.js +13 -27
  81. package/src/chips/magnetometer/hmc5883l.js +32 -29
  82. package/src/chips/memory/_23x1024.js +228 -0
  83. package/src/chips/memory/_24aa02uid.js +8 -11
  84. package/src/chips/memory/mx25l12845e.js +467 -0
  85. package/src/chips/memory/sd_spi.js +486 -0
  86. package/src/chips/motor/drv8830.js +4 -4
  87. package/src/chips/other/mpr121.js +7 -7
  88. package/src/chips/power/ina219.js +5 -8
  89. package/src/chips/power/ina226.js +5 -8
  90. package/src/chips/power/ina3221.js +5 -8
  91. package/src/chips/pressure/bmp085.js +17 -11
  92. package/src/chips/pressure/bmp180.js +7 -7
  93. package/src/chips/pressure/bmp280.js +21 -33
  94. package/src/chips/pressure/bmp384.js +28 -39
  95. package/src/chips/pressure/bmp581.js +59 -70
  96. package/src/chips/pressure/lps22df.js +50 -52
  97. package/src/chips/pressure/lps28dfw.js +15 -13
  98. package/src/chips/pressure/lps33hw.js +7 -5
  99. package/src/chips/rtc/ds3231.js +8 -8
  100. package/src/chips/rtc/pcf8523.js +8 -14
  101. package/src/chips/temperature/mcp9808.js +9 -7
  102. package/src/chips/temperature/tmp117.js +4 -4
  103. package/src/connection/connection.js +6 -4
  104. package/src/connection/dhtxx.js +5 -3
  105. package/src/connection/display.js +278 -0
  106. package/src/connection/display_mock.js +77 -0
  107. package/src/connection/hx711.js +5 -3
  108. package/src/connection/i2c.js +6 -4
  109. package/src/connection/i2c_mock.js +47 -0
  110. package/src/connection/input_pin.js +36 -5
  111. package/src/connection/register.js +14 -0
  112. package/src/connection/register_connection.js +65 -0
  113. package/src/connection/sipo.js +4 -2
  114. package/src/connection/sipo_mock.js +49 -0
  115. package/src/connection/smbus.js +6 -4
  116. package/src/connection/softspi.js +73 -0
  117. package/src/connection/spi.js +110 -23
  118. package/src/connection/spi_mock.js +50 -0
  119. package/src/connection/uart.js +2 -1
  120. package/src/discovery/discovery.js +218 -0
  121. package/src/discovery/registry.js +85 -0
  122. package/src/storage/block_device.js +95 -0
  123. package/src/storage/ram_block_device.js +72 -0
@@ -0,0 +1,228 @@
1
+ 'use strict';
2
+
3
+ const CMD_READ = 0x03;
4
+ const CMD_WRITE = 0x02;
5
+ const CMD_RDMR = 0x05;
6
+ const CMD_WRMR = 0x01;
7
+ const CMD_RSTIO = 0xFF;
8
+ const MODE_MASK = 0xC0;
9
+
10
+ const SIZE = 131072;
11
+ const PAGE_SIZE = 32;
12
+ const MAX_CHUNK_LIMIT = 256;
13
+
14
+ /**
15
+ * 23LC1024 / 23A1024 1-Mbit SPI serial SRAM — minimal interface.
16
+ *
17
+ * Byte read/write access to the 131,072-byte array over plain SPI (Mode 0).
18
+ * The chip is volatile but has unlimited cycles and zero write time: a write
19
+ * is complete when CS is released, with no polling.
20
+ *
21
+ * Default configuration:
22
+ * - Sequential mode (MODE register = 0x40), written and read back by
23
+ * init(); a mismatch (floating/missing MISO, wrong SPI mode, chip stuck
24
+ * in SDI/SQI mode) rejects with an Error.
25
+ * - Full 128 KiB addressable; SDI/SQI bus modes are never used.
26
+ *
27
+ * Every transaction is one instruction byte, a 24-bit big-endian address and
28
+ * data, with CS held low for the whole frame, so the driver takes the bare
29
+ * Connection and uses write()/writeRead() directly (the instruction is not a
30
+ * register address, so RegisterConnection's read-bit logic does not apply).
31
+ *
32
+ * Call `await chip.init()` before use (the constructor cannot await bus I/O).
33
+ */
34
+ class SRAM23X1024Minimal {
35
+ /**
36
+ * @param {import('../../connection/connection').Connection} connection - SPI connection (Mode 0, MSB first, CS managed by the connection).
37
+ */
38
+ constructor(connection) {
39
+ this._conn = connection;
40
+ this._mode = 0x40;
41
+ }
42
+
43
+ /**
44
+ * Select Sequential mode and verify it with a MODE register round-trip.
45
+ * @returns {Promise<void>}
46
+ */
47
+ async init() {
48
+ await this._writeMode(0x40);
49
+ if ((await this._readMode()) !== 0x40) {
50
+ throw new Error('23X1024 MODE register mismatch: check wiring and SPI Mode 0');
51
+ }
52
+ this._mode = 0x40;
53
+ }
54
+
55
+ async _readMode() {
56
+ const r = await this._conn.writeRead(Buffer.from([CMD_RDMR]), 1);
57
+ return r[0];
58
+ }
59
+
60
+ async _writeMode(mode) {
61
+ await this._conn.write(Buffer.from([CMD_WRMR, mode]));
62
+ }
63
+
64
+ static _header(command, address) {
65
+ return Buffer.from([command, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF]);
66
+ }
67
+
68
+ async _readRaw(address, length) {
69
+ const r = await this._conn.writeRead(SRAM23X1024Minimal._header(CMD_READ, address), length);
70
+ return Buffer.from(r);
71
+ }
72
+
73
+ async _writeRaw(address, data) {
74
+ await this._conn.write(Buffer.concat([SRAM23X1024Minimal._header(CMD_WRITE, address), Buffer.from(data)]));
75
+ }
76
+
77
+ /**
78
+ * Read one byte (READ, 0x03).
79
+ * @param {number} address - 0–131071.
80
+ * @returns {Promise<number>} Byte value 0–255.
81
+ */
82
+ async readByte(address) {
83
+ if (!Number.isInteger(address) || address < 0 || address >= SIZE) throw new RangeError('address out of range');
84
+ return (await this._readRaw(address, 1))[0];
85
+ }
86
+
87
+ /**
88
+ * Write one byte (WRITE, 0x02); complete when CS is released.
89
+ * @param {number} address - 0–131071.
90
+ * @param {number} value - Byte value 0–255.
91
+ * @returns {Promise<void>}
92
+ */
93
+ async writeByte(address, value) {
94
+ if (!Number.isInteger(address) || address < 0 || address >= SIZE) throw new RangeError('address out of range');
95
+ await this._writeRaw(address, Buffer.from([value & 0xFF]));
96
+ }
97
+ }
98
+
99
+ /**
100
+ * 23X1024 full interface — extends minimal with block access, operating modes and I/O reset.
101
+ *
102
+ * read()/write()/fill() have linear-address semantics in every mode: the
103
+ * driver splits transfers into transactions of at most maxChunk data bytes
104
+ * and, in Page/Byte mode, at the 32-byte page / single-byte boundaries the
105
+ * hardware would otherwise wrap or truncate at. Ranges that would leave the
106
+ * array reject with a RangeError instead of silently wrapping around.
107
+ */
108
+ class SRAM23X1024Full extends SRAM23X1024Minimal {
109
+ constructor(connection) {
110
+ super(connection);
111
+ this._maxChunk = 256;
112
+ }
113
+
114
+ /** @returns {number} Maximum data bytes per bus transaction (1–256, default 256). */
115
+ get maxChunk() { return this._maxChunk; }
116
+
117
+ /** @param {number} n - Maximum data bytes per bus transaction (1–256). */
118
+ set maxChunk(n) {
119
+ if (!Number.isInteger(n) || n < 1 || n > MAX_CHUNK_LIMIT) throw new RangeError('maxChunk must be 1-256');
120
+ this._maxChunk = n;
121
+ }
122
+
123
+ _chunkLen(address, remaining) {
124
+ let n = Math.min(remaining, this._maxChunk);
125
+ if (this._mode === SRAM23X1024Full.MODE_BYTE) return 1;
126
+ if (this._mode === SRAM23X1024Full.MODE_PAGE) n = Math.min(n, PAGE_SIZE - (address % PAGE_SIZE));
127
+ return n;
128
+ }
129
+
130
+ static _checkRange(address, length) {
131
+ if (!Number.isInteger(address) || !Number.isInteger(length) || address < 0 || length < 0 || address + length > SIZE) {
132
+ throw new RangeError('range leaves the 128 KiB array');
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Block read of `length` bytes starting at `address`.
138
+ * @param {number} address - Start address; address + length must be <= 131072.
139
+ * @param {number} length - Number of bytes.
140
+ * @returns {Promise<Buffer>}
141
+ */
142
+ async read(address, length) {
143
+ SRAM23X1024Full._checkRange(address, length);
144
+ const parts = [];
145
+ while (length > 0) {
146
+ const n = this._chunkLen(address, length);
147
+ parts.push(await this._readRaw(address, n));
148
+ address += n; length -= n;
149
+ }
150
+ return Buffer.concat(parts);
151
+ }
152
+
153
+ /**
154
+ * Block write of `data` starting at `address` (same range rule as read()).
155
+ * @param {number} address - Start address.
156
+ * @param {Buffer|number[]} data - Bytes to write.
157
+ * @returns {Promise<void>}
158
+ */
159
+ async write(address, data) {
160
+ const buf = Buffer.from(data);
161
+ SRAM23X1024Full._checkRange(address, buf.length);
162
+ let pos = 0;
163
+ while (pos < buf.length) {
164
+ const n = this._chunkLen(address, buf.length - pos);
165
+ await this._writeRaw(address, buf.subarray(pos, pos + n));
166
+ address += n; pos += n;
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Write `value` over `length` bytes starting at `address` (chunked).
172
+ * @param {number} address - Start address.
173
+ * @param {number} length - Number of bytes.
174
+ * @param {number} [value=0] - Byte value.
175
+ * @returns {Promise<void>}
176
+ */
177
+ async fill(address, length, value = 0) {
178
+ SRAM23X1024Full._checkRange(address, length);
179
+ const block = Buffer.alloc(PAGE_SIZE, value & 0xFF);
180
+ while (length > 0) {
181
+ const n = Math.min(this._chunkLen(address, length), block.length);
182
+ await this._writeRaw(address, block.subarray(0, n));
183
+ address += n; length -= n;
184
+ }
185
+ }
186
+
187
+ /** @returns {number} Array size in bytes (131072). */
188
+ size() { return SIZE; }
189
+
190
+ /**
191
+ * Select the operating mode (WRMR) and verify it with RDMR.
192
+ * @param {number} mode - SRAM23X1024Full.MODE_BYTE, MODE_PAGE or MODE_SEQUENTIAL; any other value rejects with a RangeError.
193
+ * @returns {Promise<void>}
194
+ */
195
+ async setMode(mode) {
196
+ if (mode !== SRAM23X1024Full.MODE_BYTE && mode !== SRAM23X1024Full.MODE_PAGE && mode !== SRAM23X1024Full.MODE_SEQUENTIAL) {
197
+ throw new RangeError('mode must be MODE_BYTE, MODE_PAGE or MODE_SEQUENTIAL');
198
+ }
199
+ await this._writeMode(mode);
200
+ if ((await this._readMode()) !== mode) throw new Error('23X1024 MODE register read-back mismatch');
201
+ this._mode = mode;
202
+ }
203
+
204
+ /**
205
+ * Read the operating mode (RDMR bits 7:6).
206
+ * @returns {Promise<number>} One of the MODE_* constants (0xC0 if reserved).
207
+ */
208
+ async getMode() {
209
+ return (await this._readMode()) & MODE_MASK;
210
+ }
211
+
212
+ /**
213
+ * Send RSTIO (0xFF) to return the chip to SPI mode after a stray EDIO/EQIO.
214
+ * Resynchronises the cached mode with the chip (re-reads RDMR).
215
+ * @returns {Promise<void>}
216
+ */
217
+ async resetIo() {
218
+ await this._conn.write(Buffer.from([CMD_RSTIO]));
219
+ this._mode = await this.getMode();
220
+ }
221
+ }
222
+
223
+ SRAM23X1024Full.MODE_BYTE = 0x00;
224
+ SRAM23X1024Full.MODE_PAGE = 0x80;
225
+ SRAM23X1024Full.MODE_SEQUENTIAL = 0x40;
226
+ SRAM23X1024Minimal.SIZE = SIZE;
227
+
228
+ module.exports = { SRAM23X1024Minimal, SRAM23X1024Full };
@@ -36,7 +36,7 @@ function _sleep(ms) {
36
36
  */
37
37
  class EEPROM24AA02UIDMinimal {
38
38
  /**
39
- * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
39
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (address 0x50).
40
40
  */
41
41
  constructor(connection) {
42
42
  this._conn = connection;
@@ -47,7 +47,7 @@ class EEPROM24AA02UIDMinimal {
47
47
  * @returns {Promise<Buffer>} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
48
48
  */
49
49
  async readUid() {
50
- return this._conn.writeRead(Buffer.from([ADDR_UID_BASE]), 4);
50
+ return this._conn.readReg(ADDR_UID_BASE, 4);
51
51
  }
52
52
 
53
53
  /**
@@ -56,7 +56,7 @@ class EEPROM24AA02UIDMinimal {
56
56
  * @returns {Promise<number>} Byte value 0-255.
57
57
  */
58
58
  async readByte(address) {
59
- return (await this._conn.writeRead(Buffer.from([address & 0xFF]), 1))[0];
59
+ return (await this._conn.readReg(address & 0xFF, 1))[0];
60
60
  }
61
61
 
62
62
  /**
@@ -71,14 +71,14 @@ class EEPROM24AA02UIDMinimal {
71
71
  * @returns {Promise<void>}
72
72
  */
73
73
  async writeByte(address, value) {
74
- await this._conn.write(Buffer.from([address & 0xFF, value & 0xFF]));
74
+ await this._conn.writeReg(address & 0xFF, value & 0xFF);
75
75
  await this._ackPoll();
76
76
  }
77
77
 
78
78
  async _ackPoll() {
79
79
  for (let i = 0; i < ACK_POLL_MAX; i++) {
80
80
  try {
81
- await this._conn.writeRead(Buffer.from([0x00]), 1);
81
+ await this._conn.readReg(0x00, 1);
82
82
  return;
83
83
  } catch (e) {
84
84
  _sleep(1);
@@ -96,7 +96,7 @@ class EEPROM24AA02UIDMinimal {
96
96
  */
97
97
  class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
98
98
  /**
99
- * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
99
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (address 0x50).
100
100
  */
101
101
  constructor(connection) {
102
102
  super(connection);
@@ -113,7 +113,7 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
113
113
  * @returns {Promise<Buffer>} `length` bytes from the device.
114
114
  */
115
115
  async read(address, length) {
116
- return this._conn.writeRead(Buffer.from([address & 0xFF]), length);
116
+ return this._conn.readReg(address & 0xFF, length);
117
117
  }
118
118
 
119
119
  /**
@@ -130,10 +130,7 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
130
130
  */
131
131
  async writePage(address, data) {
132
132
  if (data.length === 0) return;
133
- const buf = Buffer.alloc(1 + data.length);
134
- buf[0] = address & 0xFF;
135
- for (let i = 0; i < data.length; i++) buf[1 + i] = data[i];
136
- await this._conn.write(buf);
133
+ await this._conn.writeReg(address & 0xFF, Buffer.from(data));
137
134
  await this._ackPoll();
138
135
  }
139
136