periph 1.1.0 → 1.2.1

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