periph 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/README.md +54 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  116. package/examples/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +289 -0
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +499 -0
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +182 -0
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +188 -206
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/connection/hx711.js +132 -0
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,499 @@
1
+ 'use strict';
2
+
3
+ const _REG_ENABLE = 0x80;
4
+ const _REG_ATIME = 0x81;
5
+ const _REG_WTIME = 0x83;
6
+ const _REG_AILTL = 0x84;
7
+ const _REG_AILTH = 0x85;
8
+ const _REG_AIHTL = 0x86;
9
+ const _REG_AIHTH = 0x87;
10
+ const _REG_PILT = 0x89;
11
+ const _REG_PIHT = 0x8B;
12
+ const _REG_PERS = 0x8C;
13
+ const _REG_CONFIG1 = 0x8D;
14
+ const _REG_PPULSE = 0x8E;
15
+ const _REG_CONTROL = 0x8F;
16
+ const _REG_CONFIG2 = 0x90;
17
+ const _REG_ID = 0x92;
18
+ const _REG_STATUS = 0x93;
19
+ const _REG_CDATAL = 0x94;
20
+ const _REG_RDATAL = 0x96;
21
+ const _REG_GDATAL = 0x98;
22
+ const _REG_BDATAL = 0x9A;
23
+ const _REG_PDATA = 0x9C;
24
+ const _REG_POFFSET_UR = 0x9D;
25
+ const _REG_POFFSET_DL = 0x9E;
26
+ const _REG_CONFIG3 = 0x9F;
27
+ const _REG_GPENTH = 0xA0;
28
+ const _REG_GEXTH = 0xA1;
29
+ const _REG_GCONF1 = 0xA2;
30
+ const _REG_GCONF2 = 0xA3;
31
+ const _REG_GOFFSET_U = 0xA4;
32
+ const _REG_GOFFSET_D = 0xA5;
33
+ const _REG_GPULSE = 0xA6;
34
+ const _REG_GOFFSET_L = 0xA7;
35
+ const _REG_GOFFSET_R = 0xA9;
36
+ const _REG_GCONF3 = 0xAA;
37
+ const _REG_GCONF4 = 0xAB;
38
+ const _REG_GFLVL = 0xAE;
39
+ const _REG_GSTATUS = 0xAF;
40
+ const _REG_PICLEAR = 0xE5;
41
+ const _REG_CICLEAR = 0xE6;
42
+ const _REG_AICLEAR = 0xE7;
43
+ const _REG_GFIFO_U = 0xFC;
44
+
45
+ const _ATIME_DEFAULT = 0xB6;
46
+ const _CONTROL_DEFAULT = 0x01;
47
+ const _CONFIG2_DEFAULT = 0x01;
48
+
49
+ function sleep(ms) {
50
+ const end = Date.now() + ms;
51
+ while (Date.now() < end) {}
52
+ }
53
+
54
+ /**
55
+ * APDS-9960 digital proximity, ambient light, RGB and gesture sensor — minimal interface.
56
+ *
57
+ * Provides ambient light and color (RGBC) readings with no configuration
58
+ * beyond the connection. The ALS/Color engine is enabled at construction
59
+ * with sensible defaults.
60
+ *
61
+ * Default configuration (written at construction):
62
+ * - ATIME = 0xB6 (72 cycles, ~200 ms integration, max count 65535)
63
+ * - AGAIN = 1 (4x ALS gain)
64
+ * - CONFIG2 = 0x01 (LED_BOOST=100%, reserved bit 0 set)
65
+ * - PON + AEN enabled; no wait, proximity, gesture, or interrupts
66
+ *
67
+ * Constructor caveat: JS constructors cannot be async, but the original
68
+ * synchronous constructor validated the ID register and threw synchronously
69
+ * on mismatch — that guarantee cannot be preserved exactly. The whole init
70
+ * sequence is fired off unawaited (matching the fire-and-forget convention
71
+ * used throughout this port); an ID mismatch now surfaces as an *unhandled
72
+ * promise rejection* shortly after construction rather than a synchronous
73
+ * throw from `new APDS9960Minimal(...)`. Callers that need to detect
74
+ * "wrong or absent device" deterministically should call an async method
75
+ * (e.g. `await sensor.color()`) right after construction and handle its
76
+ * rejection.
77
+ */
78
+ class APDS9960Minimal {
79
+ /**
80
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
81
+ */
82
+ constructor(connection) {
83
+ this._conn = connection;
84
+ this._init();
85
+ }
86
+
87
+ async _init() {
88
+ sleep(6);
89
+ const id = await this._readReg(_REG_ID);
90
+ if (id !== 0xAB) throw new Error('APDS-9960 not found (ID=0x' + id.toString(16) + ', expected 0xAB)');
91
+ await this._writeReg(_REG_ENABLE, 0x00);
92
+ await this._writeReg(_REG_ATIME, _ATIME_DEFAULT);
93
+ await this._writeReg(_REG_CONTROL, _CONTROL_DEFAULT);
94
+ await this._writeReg(_REG_CONFIG2, _CONFIG2_DEFAULT);
95
+ await this._writeReg(_REG_ENABLE, 0x03);
96
+ sleep(210);
97
+ }
98
+
99
+ async _writeReg(reg, value) {
100
+ await this._conn.write(Buffer.from([reg, value]));
101
+ }
102
+
103
+ async _readReg(reg) {
104
+ return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
105
+ }
106
+
107
+ async _readReg16LE(reg) {
108
+ const buf = await this._conn.writeRead(Buffer.from([reg]), 2);
109
+ return buf[0] | (buf[1] << 8);
110
+ }
111
+
112
+ /**
113
+ * Read the clear (unfiltered) channel.
114
+ * @returns {Promise<number>} Raw clear channel count, 0-65535.
115
+ */
116
+ async colorClear() { return this._readReg16LE(_REG_CDATAL); }
117
+
118
+ /**
119
+ * Read the red channel.
120
+ *
121
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
122
+ * @returns {Promise<number>} Raw red channel count, 0-65535.
123
+ */
124
+ async colorRed() {
125
+ const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
126
+ return raw[2] | (raw[3] << 8);
127
+ }
128
+
129
+ /**
130
+ * Read the green channel.
131
+ *
132
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
133
+ * @returns {Promise<number>} Raw green channel count, 0-65535.
134
+ */
135
+ async colorGreen() {
136
+ const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
137
+ return raw[4] | (raw[5] << 8);
138
+ }
139
+
140
+ /**
141
+ * Read the blue channel.
142
+ *
143
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
144
+ * @returns {Promise<number>} Raw blue channel count, 0-65535.
145
+ */
146
+ async colorBlue() {
147
+ const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
148
+ return raw[6] | (raw[7] << 8);
149
+ }
150
+
151
+ /**
152
+ * Read all four RGBC channels in one burst.
153
+ *
154
+ * Reading CDATAL at 0x94 atomically latches all eight bytes 0x94-0x9B.
155
+ *
156
+ * @returns {Promise<{ clear: number, red: number, green: number, blue: number }>}
157
+ */
158
+ async color() {
159
+ const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
160
+ return {
161
+ clear: raw[0] | (raw[1] << 8),
162
+ red: raw[2] | (raw[3] << 8),
163
+ green: raw[4] | (raw[5] << 8),
164
+ blue: raw[6] | (raw[7] << 8)
165
+ };
166
+ }
167
+ }
168
+
169
+ /**
170
+ * APDS-9960 full interface — extends APDS9960Minimal with proximity, gesture, and configuration.
171
+ *
172
+ * Adds proximity detection, gesture engine, wait engine, threshold and
173
+ * interrupt configuration, status queries, and device identification.
174
+ */
175
+ class APDS9960Full extends APDS9960Minimal {
176
+ /**
177
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
178
+ */
179
+ constructor(connection) {
180
+ super(connection);
181
+ }
182
+
183
+ /**
184
+ * Enable or disable the proximity engine.
185
+ * @param {boolean} enabled - true to enable PEN, false to disable.
186
+ * @returns {Promise<void>}
187
+ */
188
+ async enableProximity(enabled) {
189
+ let val = await this._readReg(_REG_ENABLE);
190
+ if (enabled) val |= 0x04; else val &= ~0x04;
191
+ await this._writeReg(_REG_ENABLE, val);
192
+ }
193
+
194
+ /**
195
+ * Read the proximity count.
196
+ * @returns {Promise<number>} Proximity count 0-255; higher means closer.
197
+ */
198
+ async proximity() { return this._readReg(_REG_PDATA); }
199
+
200
+ /**
201
+ * Enable or disable the wait engine.
202
+ * @param {boolean} enabled - true to enable WEN, false to disable.
203
+ * @returns {Promise<void>}
204
+ */
205
+ async enableWait(enabled) {
206
+ let val = await this._readReg(_REG_ENABLE);
207
+ if (enabled) val |= 0x08; else val &= ~0x08;
208
+ await this._writeReg(_REG_ENABLE, val);
209
+ }
210
+
211
+ /**
212
+ * Configure the wait time between ALS/proximity cycles.
213
+ * @param {number} wtime - WTIME register value 0-255.
214
+ * @param {boolean} [wlong=false] - true to enable WLONG 12x multiplier.
215
+ * @returns {Promise<void>}
216
+ */
217
+ async configureWait(wtime, wlong = false) {
218
+ await this._writeReg(_REG_WTIME, wtime & 0xFF);
219
+ let c1 = await this._readReg(_REG_CONFIG1);
220
+ if (wlong) c1 |= 0x02; else c1 &= ~0x02;
221
+ c1 = (c1 & 0x03) | 0x60;
222
+ await this._writeReg(_REG_CONFIG1, c1);
223
+ }
224
+
225
+ /**
226
+ * Configure ALS integration time and gain.
227
+ * @param {number} atime - ATIME register value 0-255.
228
+ * @param {number} again - ALS gain 0-3 (0=1x, 1=4x, 2=16x, 3=64x).
229
+ * @returns {Promise<void>}
230
+ */
231
+ async configureAls(atime, again) {
232
+ await this._writeReg(_REG_ATIME, atime & 0xFF);
233
+ let ctrl = await this._readReg(_REG_CONTROL);
234
+ ctrl = (ctrl & 0xFC) | (again & 0x03);
235
+ await this._writeReg(_REG_CONTROL, ctrl);
236
+ }
237
+
238
+ /**
239
+ * Configure proximity LED drive, gain, pulse count and length.
240
+ * @param {number} ldrive - LED drive strength 0-3.
241
+ * @param {number} pgain - Proximity gain 0-3.
242
+ * @param {number} ppulse - Pulse count minus 1, 0-63.
243
+ * @param {number} pplen - Pulse length 0-3.
244
+ * @returns {Promise<void>}
245
+ */
246
+ async configureProximityLed(ldrive, pgain, ppulse, pplen) {
247
+ let ctrl = await this._readReg(_REG_CONTROL);
248
+ ctrl = ((ldrive & 0x03) << 6) | ((pgain & 0x03) << 2) | (ctrl & 0x03);
249
+ await this._writeReg(_REG_CONTROL, ctrl);
250
+ await this._writeReg(_REG_PPULSE, ((pplen & 0x03) << 6) | (ppulse & 0x3F));
251
+ }
252
+
253
+ /**
254
+ * Set additional LED current boost.
255
+ * @param {number} boost - LED_BOOST 0-3 (0=100%, 1=150%, 2=200%, 3=300%).
256
+ * @returns {Promise<void>}
257
+ */
258
+ async setLedBoost(boost) {
259
+ let c2 = await this._readReg(_REG_CONFIG2);
260
+ c2 = (c2 & 0xCF) | ((boost & 0x03) << 4) | 0x01;
261
+ await this._writeReg(_REG_CONFIG2, c2);
262
+ }
263
+
264
+ /**
265
+ * Set ALS interrupt thresholds.
266
+ * @param {number} low - Low threshold 0-65535.
267
+ * @param {number} high - High threshold 0-65535.
268
+ * @returns {Promise<void>}
269
+ */
270
+ async alsThreshold(low, high) {
271
+ await this._writeReg(_REG_AILTL, low & 0xFF);
272
+ await this._writeReg(_REG_AILTH, (low >> 8) & 0xFF);
273
+ await this._writeReg(_REG_AIHTL, high & 0xFF);
274
+ await this._writeReg(_REG_AIHTH, (high >> 8) & 0xFF);
275
+ }
276
+
277
+ /**
278
+ * Set proximity interrupt thresholds.
279
+ * @param {number} low - Low threshold 0-255.
280
+ * @param {number} high - High threshold 0-255.
281
+ * @returns {Promise<void>}
282
+ */
283
+ async proximityThreshold(low, high) {
284
+ await this._writeReg(_REG_PILT, low & 0xFF);
285
+ await this._writeReg(_REG_PIHT, high & 0xFF);
286
+ }
287
+
288
+ /**
289
+ * Set interrupt persistence filters.
290
+ * @param {number} ppers - Proximity persistence 0-15.
291
+ * @param {number} apers - ALS persistence 0-15.
292
+ * @returns {Promise<void>}
293
+ */
294
+ async setPersistence(ppers, apers) {
295
+ await this._writeReg(_REG_PERS, ((ppers & 0x0F) << 4) | (apers & 0x0F));
296
+ }
297
+
298
+ /**
299
+ * Enable or disable ALS interrupt.
300
+ * @param {boolean} enabled - true to enable AIEN, false to disable.
301
+ * @returns {Promise<void>}
302
+ */
303
+ async enableAlsInterrupt(enabled) {
304
+ let val = await this._readReg(_REG_ENABLE);
305
+ if (enabled) val |= 0x10; else val &= ~0x10;
306
+ await this._writeReg(_REG_ENABLE, val);
307
+ }
308
+
309
+ /**
310
+ * Enable or disable proximity interrupt.
311
+ * @param {boolean} enabled - true to enable PIEN, false to disable.
312
+ * @returns {Promise<void>}
313
+ */
314
+ async enableProximityInterrupt(enabled) {
315
+ let val = await this._readReg(_REG_ENABLE);
316
+ if (enabled) val |= 0x20; else val &= ~0x20;
317
+ await this._writeReg(_REG_ENABLE, val);
318
+ }
319
+
320
+ /**
321
+ * Clear the proximity interrupt via address-only write to PICLEAR.
322
+ * @returns {Promise<void>}
323
+ */
324
+ async clearProximityInterrupt() {
325
+ await this._conn.write(Buffer.from([_REG_PICLEAR]));
326
+ }
327
+
328
+ /**
329
+ * Clear the ALS/color interrupt via address-only write to CICLEAR.
330
+ * @returns {Promise<void>}
331
+ */
332
+ async clearAlsInterrupt() {
333
+ await this._conn.write(Buffer.from([_REG_CICLEAR]));
334
+ }
335
+
336
+ /**
337
+ * Clear all non-gesture interrupts via address-only write to AICLEAR.
338
+ * @returns {Promise<void>}
339
+ */
340
+ async clearAllInterrupts() {
341
+ await this._conn.write(Buffer.from([_REG_AICLEAR]));
342
+ }
343
+
344
+ /**
345
+ * Set proximity offset for UP/RIGHT and DOWN/LEFT photodiodes.
346
+ * @param {number} ur - UP/RIGHT offset -127 to +127 (sign-magnitude).
347
+ * @param {number} dl - DOWN/LEFT offset -127 to +127 (sign-magnitude).
348
+ * @returns {Promise<void>}
349
+ */
350
+ async setProximityOffset(ur, dl) {
351
+ const encode = (v) => v < 0 ? (0x80 | ((-v) & 0x7F)) : (v & 0x7F);
352
+ await this._writeReg(_REG_POFFSET_UR, encode(ur));
353
+ await this._writeReg(_REG_POFFSET_DL, encode(dl));
354
+ }
355
+
356
+ /**
357
+ * Mask individual photodiodes in proximity detection.
358
+ * @param {boolean} u - true to mask UP.
359
+ * @param {boolean} d - true to mask DOWN.
360
+ * @param {boolean} l - true to mask LEFT.
361
+ * @param {boolean} r - true to mask RIGHT.
362
+ * @returns {Promise<void>}
363
+ */
364
+ async setProximityMask(u, d, l, r) {
365
+ let c3 = (await this._readReg(_REG_CONFIG3)) & 0xF0;
366
+ if (u) c3 |= 0x08;
367
+ if (d) c3 |= 0x04;
368
+ if (l) c3 |= 0x02;
369
+ if (r) c3 |= 0x01;
370
+ await this._writeReg(_REG_CONFIG3, c3);
371
+ }
372
+
373
+ /**
374
+ * Enable or disable the gesture engine.
375
+ * @param {boolean} enabled - true to enable GEN and set GMODE, false to disable.
376
+ * @returns {Promise<void>}
377
+ */
378
+ async enableGesture(enabled) {
379
+ let val = await this._readReg(_REG_ENABLE);
380
+ if (enabled) {
381
+ val |= 0x40;
382
+ await this._writeReg(_REG_ENABLE, val);
383
+ let g4 = await this._readReg(_REG_GCONF4);
384
+ g4 |= 0x01;
385
+ await this._writeReg(_REG_GCONF4, g4);
386
+ } else {
387
+ val &= ~0x40;
388
+ await this._writeReg(_REG_ENABLE, val);
389
+ let g4 = await this._readReg(_REG_GCONF4);
390
+ g4 &= ~0x01;
391
+ await this._writeReg(_REG_GCONF4, g4);
392
+ }
393
+ }
394
+
395
+ /**
396
+ * Configure gesture engine parameters.
397
+ * @param {number} ggain - Gesture gain 0-3.
398
+ * @param {number} gldrive - Gesture LED drive 0-3.
399
+ * @param {number} gpulse - Gesture pulse count minus 1, 0-63.
400
+ * @param {number} gplen - Gesture pulse length 0-3.
401
+ * @param {number} gwtime - Gesture wait time 0-7.
402
+ * @param {number} gpenth - Gesture proximity entry threshold 0-255.
403
+ * @param {number} gexth - Gesture exit threshold 0-255.
404
+ * @returns {Promise<void>}
405
+ */
406
+ async configureGesture(ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) {
407
+ await this._writeReg(_REG_GPENTH, gpenth & 0xFF);
408
+ await this._writeReg(_REG_GEXTH, gexth & 0xFF);
409
+ const g2 = ((ggain & 0x03) << 5) | ((gldrive & 0x03) << 3) | (gwtime & 0x07);
410
+ await this._writeReg(_REG_GCONF2, g2);
411
+ await this._writeReg(_REG_GPULSE, ((gplen & 0x03) << 6) | (gpulse & 0x3F));
412
+ }
413
+
414
+ /**
415
+ * Check if gesture data is available in the FIFO.
416
+ * @returns {Promise<boolean>} true if GSTATUS.GVALID is set.
417
+ */
418
+ async gestureAvailable() { return !!((await this._readReg(_REG_GSTATUS)) & 0x01); }
419
+
420
+ /**
421
+ * Read all gesture datasets from the FIFO.
422
+ * @returns {Promise<Array<{ u: number, d: number, l: number, r: number }>>}
423
+ */
424
+ async readGestureFifo() {
425
+ const level = await this._readReg(_REG_GFLVL);
426
+ if (level === 0) return [];
427
+ const result = [];
428
+ for (let i = 0; i < level; i++) {
429
+ const raw = await this._conn.writeRead(Buffer.from([_REG_GFIFO_U]), 4);
430
+ result.push({ u: raw[0], d: raw[1], l: raw[2], r: raw[3] });
431
+ }
432
+ return result;
433
+ }
434
+
435
+ /**
436
+ * Read the number of datasets in the gesture FIFO.
437
+ * @returns {Promise<number>} Number of 4-byte datasets currently in FIFO.
438
+ */
439
+ async gestureFifoLevel() { return this._readReg(_REG_GFLVL); }
440
+
441
+ /**
442
+ * Clear the gesture FIFO by setting GFIFO_CLR in GCONF4.
443
+ * @returns {Promise<void>}
444
+ */
445
+ async clearGestureFifo() {
446
+ let g4 = await this._readReg(_REG_GCONF4);
447
+ g4 |= 0x04;
448
+ await this._writeReg(_REG_GCONF4, g4);
449
+ }
450
+
451
+ /**
452
+ * Enable or disable gesture interrupt.
453
+ * @param {boolean} enabled - true to enable GIEN, false to disable.
454
+ * @returns {Promise<void>}
455
+ */
456
+ async enableGestureInterrupt(enabled) {
457
+ let g4 = await this._readReg(_REG_GCONF4);
458
+ if (enabled) g4 |= 0x02; else g4 &= ~0x02;
459
+ await this._writeReg(_REG_GCONF4, g4);
460
+ }
461
+
462
+ /**
463
+ * Read the raw STATUS register.
464
+ * @returns {Promise<number>} Raw STATUS byte.
465
+ */
466
+ async status() { return this._readReg(_REG_STATUS); }
467
+
468
+ /**
469
+ * Check if ALS/color data is valid.
470
+ * @returns {Promise<boolean>} true if STATUS.AVALID is set.
471
+ */
472
+ async isAlsValid() { return !!((await this._readReg(_REG_STATUS)) & 0x01); }
473
+
474
+ /**
475
+ * Check if proximity data is valid.
476
+ * @returns {Promise<boolean>} true if STATUS.PVALID is set.
477
+ */
478
+ async isProximityValid() { return !!((await this._readReg(_REG_STATUS)) & 0x02); }
479
+
480
+ /**
481
+ * Check if the clear photodiode is saturated.
482
+ * @returns {Promise<boolean>} true if STATUS.CPSAT is set.
483
+ */
484
+ async isAlsSaturated() { return !!((await this._readReg(_REG_STATUS)) & 0x80); }
485
+
486
+ /**
487
+ * Check if analog saturation occurred during proximity.
488
+ * @returns {Promise<boolean>} true if STATUS.PGSAT is set.
489
+ */
490
+ async isProximitySaturated() { return !!((await this._readReg(_REG_STATUS)) & 0x40); }
491
+
492
+ /**
493
+ * Read the device ID register.
494
+ * @returns {Promise<number>} ID register value (expect 0xAB).
495
+ */
496
+ async chipId() { return this._readReg(_REG_ID); }
497
+ }
498
+
499
+ module.exports = { APDS9960Minimal, APDS9960Full };