periph 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/README.md +54 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +23 -20
  138. package/examples/pressure/bmp280/demo.js +44 -41
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +15 -15
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +57 -44
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +42 -33
  173. package/src/chips/environmental/aht21.js +52 -37
  174. package/src/chips/environmental/bme280.js +85 -66
  175. package/src/chips/environmental/bme680.js +110 -87
  176. package/src/chips/gas/ens160.js +82 -64
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +89 -66
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +158 -123
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +32 -29
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +72 -53
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/{transport → connection}/hx711.js +49 -22
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/gnss/.gitkeep +0 -0
  230. package/examples/humidity/.gitkeep +0 -0
  231. package/src/chips/gnss/.gitkeep +0 -0
@@ -36,27 +36,27 @@ function _sleep(ms) {
36
36
  */
37
37
  class EEPROM24AA02UIDMinimal {
38
38
  /**
39
- * @param {object} transport - Configured I2C transport pointing at the device (address 0x50).
39
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
40
40
  */
41
- constructor(transport) {
42
- this._transport = transport;
41
+ constructor(connection) {
42
+ this._conn = connection;
43
43
  }
44
44
 
45
45
  /**
46
46
  * Read the chip's factory-programmed 32-bit unique serial number.
47
- * @returns {Buffer} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
47
+ * @returns {Promise<Buffer>} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
48
48
  */
49
- readUid() {
50
- return this._transport.writeRead(Buffer.from([ADDR_UID_BASE]), 4);
49
+ async readUid() {
50
+ return this._conn.writeRead(Buffer.from([ADDR_UID_BASE]), 4);
51
51
  }
52
52
 
53
53
  /**
54
54
  * Read a single byte from user EEPROM at 0x00-0x7F.
55
55
  * @param {number} address - Memory address 0-127.
56
- * @returns {number} Byte value 0-255.
56
+ * @returns {Promise<number>} Byte value 0-255.
57
57
  */
58
- readByte(address) {
59
- return this._transport.writeRead(Buffer.from([address & 0xFF]), 1)[0];
58
+ async readByte(address) {
59
+ return (await this._conn.writeRead(Buffer.from([address & 0xFF]), 1))[0];
60
60
  }
61
61
 
62
62
  /**
@@ -68,16 +68,17 @@ class EEPROM24AA02UIDMinimal {
68
68
  *
69
69
  * @param {number} address - Memory address 0-255.
70
70
  * @param {number} value - Byte value 0-255.
71
+ * @returns {Promise<void>}
71
72
  */
72
- writeByte(address, value) {
73
- this._transport.write(Buffer.from([address & 0xFF, value & 0xFF]));
74
- this._ackPoll();
73
+ async writeByte(address, value) {
74
+ await this._conn.write(Buffer.from([address & 0xFF, value & 0xFF]));
75
+ await this._ackPoll();
75
76
  }
76
77
 
77
- _ackPoll() {
78
+ async _ackPoll() {
78
79
  for (let i = 0; i < ACK_POLL_MAX; i++) {
79
80
  try {
80
- this._transport.writeRead(Buffer.from([0x00]), 1);
81
+ await this._conn.writeRead(Buffer.from([0x00]), 1);
81
82
  return;
82
83
  } catch (e) {
83
84
  _sleep(1);
@@ -95,10 +96,10 @@ class EEPROM24AA02UIDMinimal {
95
96
  */
96
97
  class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
97
98
  /**
98
- * @param {object} transport - Configured I2C transport pointing at the device (address 0x50).
99
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x50).
99
100
  */
100
- constructor(transport) {
101
- super(transport);
101
+ constructor(connection) {
102
+ super(connection);
102
103
  }
103
104
 
104
105
  /**
@@ -109,10 +110,10 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
109
110
  *
110
111
  * @param {number} address - Starting address 0-255.
111
112
  * @param {number} length - Number of bytes to read.
112
- * @returns {Buffer} `length` bytes from the device.
113
+ * @returns {Promise<Buffer>} `length` bytes from the device.
113
114
  */
114
- read(address, length) {
115
- return this._transport.writeRead(Buffer.from([address & 0xFF]), length);
115
+ async read(address, length) {
116
+ return this._conn.writeRead(Buffer.from([address & 0xFF]), length);
116
117
  }
117
118
 
118
119
  /**
@@ -125,14 +126,15 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
125
126
  *
126
127
  * @param {number} address - Start address within an 8-byte page (0, 8, 16, …).
127
128
  * @param {Buffer|Uint8Array} data - Bytes to write (1 to 8 bytes).
129
+ * @returns {Promise<void>}
128
130
  */
129
- writePage(address, data) {
131
+ async writePage(address, data) {
130
132
  if (data.length === 0) return;
131
133
  const buf = Buffer.alloc(1 + data.length);
132
134
  buf[0] = address & 0xFF;
133
135
  for (let i = 0; i < data.length; i++) buf[1 + i] = data[i];
134
- this._transport.write(buf);
135
- this._ackPoll();
136
+ await this._conn.write(buf);
137
+ await this._ackPoll();
136
138
  }
137
139
 
138
140
  /**
@@ -143,8 +145,9 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
143
145
  *
144
146
  * @param {number} address - Starting address 0-255.
145
147
  * @param {Buffer|Uint8Array} data - Bytes to write.
148
+ * @returns {Promise<void>}
146
149
  */
147
- write(address, data) {
150
+ async write(address, data) {
148
151
  let offset = 0;
149
152
  let remaining = data.length;
150
153
  let current = address;
@@ -152,7 +155,7 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
152
155
  const pageOffset = current & (PAGE_SIZE - 1);
153
156
  let chunk = PAGE_SIZE - pageOffset;
154
157
  if (chunk > remaining) chunk = remaining;
155
- this.writePage(current, data.slice(offset, offset + chunk));
158
+ await this.writePage(current, data.slice(offset, offset + chunk));
156
159
  offset += chunk;
157
160
  current += chunk;
158
161
  remaining -= chunk;
@@ -161,17 +164,17 @@ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
161
164
 
162
165
  /**
163
166
  * Read the manufacturer code at 0xFA.
164
- * @returns {number} Manufacturer code; expect 0x29 (Microchip).
167
+ * @returns {Promise<number>} Manufacturer code; expect 0x29 (Microchip).
165
168
  */
166
- readManufacturerCode() {
169
+ async readManufacturerCode() {
167
170
  return this.readByte(ADDR_MFR_CODE);
168
171
  }
169
172
 
170
173
  /**
171
174
  * Read the device code at 0xFB.
172
- * @returns {number} Device code; expect 0x41 (I2C 2-Kbit EEPROM).
175
+ * @returns {Promise<number>} Device code; expect 0x41 (I2C 2-Kbit EEPROM).
173
176
  */
174
- readDeviceCode() {
177
+ async readDeviceCode() {
175
178
  return this.readByte(ADDR_DEV_CODE);
176
179
  }
177
180
  }
@@ -0,0 +1,375 @@
1
+ 'use strict';
2
+
3
+ const _REG_ELE0_7_TOUCH = 0x00;
4
+ const _REG_ELE8_PROX_TCH = 0x01;
5
+ const _REG_ELE0_7_OOR = 0x02;
6
+ const _REG_MHDR = 0x2B;
7
+ const _REG_NHDR = 0x2C;
8
+ const _REG_MHDF = 0x2F;
9
+ const _REG_NHDF = 0x30;
10
+ const _REG_E0TTH = 0x41;
11
+ const _REG_E0RTH = 0x42;
12
+ const _REG_EPROXTTH = 0x59;
13
+ const _REG_EPROXRTH = 0x5A;
14
+ const _REG_DEBOUNCE = 0x5B;
15
+ const _REG_CDC_CONFIG = 0x5C;
16
+ const _REG_CDT_CONFIG = 0x5D;
17
+ const _REG_ECR = 0x5E;
18
+ const _REG_AUTOCONFIG0 = 0x7B;
19
+ const _REG_AUTOCONFIG1 = 0x7C;
20
+ const _REG_USL = 0x7D;
21
+ const _REG_LSL = 0x7E;
22
+ const _REG_TL = 0x7F;
23
+ const _REG_SRST = 0x80;
24
+
25
+ const _SOFT_RESET_KEY = 0x63;
26
+ const _TOUCH_DEFAULT = 12;
27
+ const _RELEASE_DEFAULT = 6;
28
+ const _CDC_CONFIG_DEFAULT = 0x10;
29
+ const _CDT_CONFIG_DEFAULT = 0x24;
30
+ const _AUTOCONFIG0_DEFAULT = 0x0B;
31
+ const _ECR_DEFAULT = 0x8C;
32
+ const _USL_3V3 = 0xC9;
33
+ const _TL_3V3 = 0xB4;
34
+ const _LSL_3V3 = 0x82;
35
+
36
+ function _sleep(ms) {
37
+ const end = Date.now() + ms;
38
+ while (Date.now() < end) {}
39
+ }
40
+
41
+ /**
42
+ * MPR121 proximity capacitive touch sensor controller — minimal interface.
43
+ *
44
+ * Provides 12-electrode touch/release detection with no configuration
45
+ * beyond the connection. Performs soft reset, applies default
46
+ * touch/release thresholds, enables the chip's automatic CDC/CDT
47
+ * configuration, and enters Run Mode on all 12 electrodes at
48
+ * construction.
49
+ *
50
+ * Default configuration (written at construction):
51
+ * - Touch threshold = 12 for each of ELE0..ELE11
52
+ * - Release threshold = 6 for each of ELE0..ELE11
53
+ * - MHDR = NHDR = MHDF = NHDF = 1
54
+ * - CDC_CONFIG = 0x10 (16 µA global CDC; FFI = 6 samples)
55
+ * - CDT_CONFIG = 0x24 (CDT = 1 µS, ESI = 16 ms sample interval)
56
+ * - AUTOCONFIG0 = 0x0B
57
+ * - USL = 0xC9, TL = 0xB4, LSL = 0x82 (3.3 V VDD)
58
+ * - ECR = 0x8C (all 12 electrodes enabled)
59
+ *
60
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x5A by default).
61
+ */
62
+ class MPR121Minimal {
63
+ constructor(connection) {
64
+ this._conn = connection;
65
+ this._init();
66
+ }
67
+
68
+ async _init() {
69
+ await this._reset();
70
+ await this._writeReg(_REG_MHDR, 0x01);
71
+ await this._writeReg(_REG_NHDR, 0x01);
72
+ await this._writeReg(_REG_MHDF, 0x01);
73
+ await this._writeReg(_REG_NHDF, 0x01);
74
+ await this._writeReg(_REG_CDC_CONFIG, _CDC_CONFIG_DEFAULT);
75
+ await this._writeReg(_REG_CDT_CONFIG, _CDT_CONFIG_DEFAULT);
76
+ await this._writeReg(_REG_USL, _USL_3V3);
77
+ await this._writeReg(_REG_TL, _TL_3V3);
78
+ await this._writeReg(_REG_LSL, _LSL_3V3);
79
+ await this._writeReg(_REG_AUTOCONFIG0, _AUTOCONFIG0_DEFAULT);
80
+ for (let n = 0; n < 12; n++) {
81
+ await this._writeReg(_REG_E0TTH + 2 * n, _TOUCH_DEFAULT);
82
+ await this._writeReg(_REG_E0RTH + 2 * n, _RELEASE_DEFAULT);
83
+ }
84
+ await this._writeReg(_REG_ECR, _ECR_DEFAULT);
85
+ }
86
+
87
+ async _reset() {
88
+ await this._writeReg(_REG_SRST, _SOFT_RESET_KEY);
89
+ _sleep(1);
90
+ }
91
+
92
+ async _writeReg(reg, value) {
93
+ await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
94
+ }
95
+
96
+ async _readReg(reg) {
97
+ return (await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1))[0];
98
+ }
99
+
100
+ async _readReg16(reg) {
101
+ const buf = await this._conn.writeRead(Buffer.from([reg & 0xFF]), 2);
102
+ return (buf[0]) | ((buf[1] & 0x03) << 8);
103
+ }
104
+
105
+ /**
106
+ * Read the 12-bit electrode touch bitmask.
107
+ *
108
+ * Reads ELE0_7_TOUCH and ELE8_PROX_TOUCH as a coherent two-byte
109
+ * snapshot from register 0x00; ELEPROX is masked out.
110
+ *
111
+ * @returns {Promise<number>} 12-bit bitmask; bit n = 1 if ELEn is touched.
112
+ */
113
+ async touched() {
114
+ const buf = await this._conn.writeRead(Buffer.from([_REG_ELE0_7_TOUCH]), 2);
115
+ return buf[0] | ((buf[1] & 0x0F) << 8);
116
+ }
117
+
118
+ /**
119
+ * Check whether a single electrode is currently touched.
120
+ * @param {number} electrode Electrode index 0-11.
121
+ * @returns {Promise<boolean>} True if ELE_electrode is currently touched.
122
+ */
123
+ async isTouched(electrode) {
124
+ if (electrode < 0 || electrode > 11) throw new Error('electrode must be in 0..11');
125
+ return ((await this.touched()) & (1 << electrode)) !== 0;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * MPR121 full interface — extends MPR121Minimal.
131
+ *
132
+ * Adds explicit Stop/Run control, per-electrode and per-proximity
133
+ * threshold configuration, filtered-data and baseline access, baseline
134
+ * filter and AFE (sampling) configuration, debounce, autoconfig
135
+ * recomputation, OOR status, and over-current flag clear.
136
+ */
137
+ class MPR121Full extends MPR121Minimal {
138
+ static get SOURCE_OOR() { return 0x04; }
139
+ static get SOURCE_ARF() { return 0x02; }
140
+ static get SOURCE_ACF() { return 0x01; }
141
+
142
+ /**
143
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
144
+ */
145
+ constructor(connection) {
146
+ super(connection);
147
+ }
148
+
149
+ /**
150
+ * Software-reset the chip and re-apply Minimal defaults.
151
+ * @returns {Promise<void>}
152
+ */
153
+ async reset() {
154
+ await this._reset();
155
+ await this._writeReg(_REG_MHDR, 0x01);
156
+ await this._writeReg(_REG_NHDR, 0x01);
157
+ await this._writeReg(_REG_MHDF, 0x01);
158
+ await this._writeReg(_REG_NHDF, 0x01);
159
+ await this._writeReg(_REG_CDC_CONFIG, _CDC_CONFIG_DEFAULT);
160
+ await this._writeReg(_REG_CDT_CONFIG, _CDT_CONFIG_DEFAULT);
161
+ await this._writeReg(_REG_USL, _USL_3V3);
162
+ await this._writeReg(_REG_TL, _TL_3V3);
163
+ await this._writeReg(_REG_LSL, _LSL_3V3);
164
+ await this._writeReg(_REG_AUTOCONFIG0, _AUTOCONFIG0_DEFAULT);
165
+ for (let n = 0; n < 12; n++) {
166
+ await this._writeReg(_REG_E0TTH + 2 * n, _TOUCH_DEFAULT);
167
+ await this._writeReg(_REG_E0RTH + 2 * n, _RELEASE_DEFAULT);
168
+ }
169
+ await this._writeReg(_REG_ECR, _ECR_DEFAULT);
170
+ }
171
+
172
+ /**
173
+ * Enter Stop Mode (ECR=0x00).
174
+ * @returns {Promise<void>}
175
+ */
176
+ async stop() {
177
+ await this._writeReg(_REG_ECR, 0x00);
178
+ }
179
+
180
+ /**
181
+ * Enter Run Mode with the given electrode configuration.
182
+ * @param {number} [nElectrodes=12] Number of electrodes 1-12.
183
+ * @param {number} [cl=2] Calibration lock / baseline init 0-3.
184
+ * @param {number} [eleproxEn=0] Proximity enable 0-3.
185
+ * @returns {Promise<void>}
186
+ */
187
+ async start(nElectrodes = 12, cl = 2, eleproxEn = 0) {
188
+ if (nElectrodes < 1 || nElectrodes > 12) throw new Error('n_electrodes must be in 1..12');
189
+ if (cl < 0 || cl > 3) throw new Error('cl must be in 0..3');
190
+ if (eleproxEn < 0 || eleproxEn > 3) throw new Error('eleprox_en must be in 0..3');
191
+ const ecr = ((cl & 0x03) << 6) | ((eleproxEn & 0x03) << 4) | (nElectrodes & 0x0F);
192
+ await this._writeReg(_REG_ECR, ecr);
193
+ }
194
+
195
+ /**
196
+ * Set touch and release thresholds for a single electrode.
197
+ * @param {number} electrode Electrode index 0-11.
198
+ * @param {number} touch Touch threshold 0-255.
199
+ * @param {number} release Release threshold 0-255.
200
+ * @returns {Promise<void>}
201
+ */
202
+ async configureThresholds(electrode, touch, release) {
203
+ if (electrode < 0 || electrode > 11) throw new Error('electrode must be in 0..11');
204
+ await this._writeReg(_REG_E0TTH + 2 * electrode, touch & 0xFF);
205
+ await this._writeReg(_REG_E0RTH + 2 * electrode, release & 0xFF);
206
+ }
207
+
208
+ /**
209
+ * Apply the same touch and release thresholds to all 12 electrodes.
210
+ * @param {number} touch Touch threshold 0-255.
211
+ * @param {number} release Release threshold 0-255.
212
+ * @returns {Promise<void>}
213
+ */
214
+ async configureAllThresholds(touch, release) {
215
+ for (let n = 0; n < 12; n++) {
216
+ await this.configureThresholds(n, touch, release);
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Set ELEPROX touch and release thresholds.
222
+ * @param {number} touch ELEPROX touch threshold 0-255.
223
+ * @param {number} release ELEPROX release threshold 0-255.
224
+ * @returns {Promise<void>}
225
+ */
226
+ async configureProximityThresholds(touch, release) {
227
+ await this._writeReg(_REG_EPROXTTH, touch & 0xFF);
228
+ await this._writeReg(_REG_EPROXRTH, release & 0xFF);
229
+ }
230
+
231
+ /**
232
+ * Read the 10-bit filtered capacitance data for an electrode.
233
+ * @param {number} electrode 0-11 for ELE0-ELE11, 12 for ELEPROX.
234
+ * @returns {Promise<number>} 10-bit value.
235
+ */
236
+ async filtered(electrode) {
237
+ if (electrode < 0 || electrode > 12) throw new Error('electrode must be in 0..12');
238
+ const addr = (electrode === 12) ? 0x1C : (0x04 + 2 * electrode);
239
+ return this._readReg16(addr);
240
+ }
241
+
242
+ /**
243
+ * Read the 10-bit baseline for an electrode.
244
+ * @param {number} electrode 0-11 for ELE0-ELE11, 12 for ELEPROX.
245
+ * @returns {Promise<number>} 10-bit baseline value.
246
+ */
247
+ async baseline(electrode) {
248
+ if (electrode < 0 || electrode > 12) throw new Error('electrode must be in 0..12');
249
+ const addr = (electrode === 12) ? 0x2A : (0x1E + electrode);
250
+ return (await this._readReg(addr)) << 2;
251
+ }
252
+
253
+ /**
254
+ * Write a baseline value (Stop Mode only).
255
+ * @param {number} electrode 0-11 for ELE0-ELE11, 12 for ELEPROX.
256
+ * @param {number} value 10-bit value 0-1023.
257
+ * @returns {Promise<void>}
258
+ */
259
+ async setBaseline(electrode, value) {
260
+ if (electrode < 0 || electrode > 12) throw new Error('electrode must be in 0..12');
261
+ const addr = (electrode === 12) ? 0x2A : (0x1E + electrode);
262
+ await this._writeReg(addr, (value >> 2) & 0xFF);
263
+ }
264
+
265
+ /**
266
+ * Read the 13-bit out-of-range bitmask.
267
+ * @returns {Promise<number>} bits 0-11 = ELE0-ELE11 OOR, bit 12 = ELEPROX OOR.
268
+ */
269
+ async oorStatus() {
270
+ const buf = await this._conn.writeRead(Buffer.from([_REG_ELE0_7_OOR]), 2);
271
+ return buf[0] | ((buf[1] & 0x1F) << 8);
272
+ }
273
+
274
+ /**
275
+ * Set the global baseline filter parameters (Stop Mode).
276
+ * @returns {Promise<void>}
277
+ */
278
+ async configureBaselineFilter(mhdr, nhdr, nclr, fdlr, mhdf, nhdf, nclf, fdlf, nhdt, nclt, fdlt) {
279
+ await this._writeReg(_REG_MHDR, mhdr & 0x3F);
280
+ await this._writeReg(_REG_NHDR, nhdr & 0x3F);
281
+ await this._writeReg(0x2D, nclr & 0xFF);
282
+ await this._writeReg(0x2E, fdlr & 0xFF);
283
+ await this._writeReg(_REG_MHDF, mhdf & 0x3F);
284
+ await this._writeReg(_REG_NHDF, nhdf & 0x3F);
285
+ await this._writeReg(0x31, nclf & 0xFF);
286
+ await this._writeReg(0x32, fdlf & 0xFF);
287
+ await this._writeReg(0x33, nhdt & 0x3F);
288
+ await this._writeReg(0x34, nclt & 0xFF);
289
+ await this._writeReg(0x35, fdlt & 0xFF);
290
+ }
291
+
292
+ /**
293
+ * Set global AFE (sampling) configuration (Stop Mode).
294
+ * @returns {Promise<void>}
295
+ */
296
+ async configureSampling(cdc, cdt, ffi, sfi, esi) {
297
+ const cdcCfg = ((ffi & 0x03) << 6) | (cdc & 0x3F);
298
+ const cdtCfg = ((cdt & 0x07) << 5) | ((sfi & 0x03) << 2) | (esi & 0x07);
299
+ await this._writeReg(_REG_CDC_CONFIG, cdcCfg);
300
+ await this._writeReg(_REG_CDT_CONFIG, cdtCfg);
301
+ }
302
+
303
+ /**
304
+ * Set debounce counts (Stop Mode).
305
+ * @param {number} touch Debounce count for touch 0-7.
306
+ * @param {number} release Debounce count for release 0-7.
307
+ * @returns {Promise<void>}
308
+ */
309
+ async configureDebounce(touch, release) {
310
+ const deb = ((release & 0x07) << 4) | (touch & 0x07);
311
+ await this._writeReg(_REG_DEBOUNCE, deb);
312
+ }
313
+
314
+ /**
315
+ * Compute USL/TL/LSL from VDD and write autoconfig registers (Stop Mode).
316
+ * @param {number} [vddMv=3300] VDD supply in millivolts.
317
+ * @param {number} [retry=0] Auto-retry on autoconfig failure 0-3.
318
+ * @param {boolean} [scts=false] Skip Charge Time Search.
319
+ * @param {boolean} [are=true] Auto-Reconfiguration Enable.
320
+ * @param {boolean} [ace=true] Auto-Configuration Enable.
321
+ * @returns {Promise<void>}
322
+ */
323
+ async configureAutoconfig(vddMv = 3300, retry = 0, scts = false, are = true, ace = true) {
324
+ const usl = Math.floor(((vddMv - 700) * 256) / vddMv);
325
+ const tl = Math.floor(usl * 0.9);
326
+ const lsl = Math.floor(usl * 0.65);
327
+ await this._writeReg(_REG_USL, usl & 0xFF);
328
+ await this._writeReg(_REG_TL, tl & 0xFF);
329
+ await this._writeReg(_REG_LSL, lsl & 0xFF);
330
+ const ffi = (await this._readReg(_REG_CDC_CONFIG) >> 6) & 0x03;
331
+ const autoconfig0 = ((ffi & 0x03) << 6) | ((retry & 0x03) << 4) | (are ? 0x08 : 0) | (ace ? 0x01 : 0);
332
+ await this._writeReg(_REG_AUTOCONFIG0, autoconfig0);
333
+ const autoconfig1 = scts ? 0x80 : 0x00;
334
+ await this._writeReg(_REG_AUTOCONFIG1, autoconfig1);
335
+ }
336
+
337
+ /**
338
+ * Return True if the ELEPROX virtual electrode is touched.
339
+ * @returns {Promise<boolean>}
340
+ */
341
+ async proximityTouched() {
342
+ return ((await this._readReg(_REG_ELE8_PROX_TCH)) & 0x10) !== 0;
343
+ }
344
+
345
+ /**
346
+ * Clear the OVCF bit in register 0x01.
347
+ * @returns {Promise<void>}
348
+ */
349
+ async clearOvercurrent() {
350
+ const raw = await this._readReg(_REG_ELE8_PROX_TCH);
351
+ await this._writeReg(_REG_ELE8_PROX_TCH, raw & 0x7F);
352
+ }
353
+
354
+ /**
355
+ * Enable one of the AUTOCONFIG1-based interrupt sources.
356
+ * @param {number} source One of MPR121Full.SOURCE_OOR, SOURCE_ARF, SOURCE_ACF.
357
+ * @returns {Promise<void>}
358
+ */
359
+ async enableInterrupt(source) {
360
+ const cur = (await this._readReg(_REG_AUTOCONFIG1)) & 0x00;
361
+ await this._writeReg(_REG_AUTOCONFIG1, cur | (source & 0x07));
362
+ }
363
+
364
+ /**
365
+ * Disable one of the AUTOCONFIG1-based interrupt sources.
366
+ * @param {number} source One of MPR121Full.SOURCE_OOR, SOURCE_ARF, SOURCE_ACF.
367
+ * @returns {Promise<void>}
368
+ */
369
+ async disableInterrupt(source) {
370
+ const cur = await this._readReg(_REG_AUTOCONFIG1);
371
+ await this._writeReg(_REG_AUTOCONFIG1, cur & ~(source & 0x07));
372
+ }
373
+ }
374
+
375
+ module.exports = { MPR121Minimal, MPR121Full };