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
@@ -35,11 +35,11 @@ const _SEVEN_SEG = [
35
35
  * Default: 1:4 multiplex drive mode, 1/3 bias, display enabled, and a
36
36
  * 7-segment digit lookup table for the default multiplex mode.
37
37
  *
38
- * @param {object} transport - Configured I2C transport pointing at the device.
38
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
39
39
  */
40
40
  class PCF8576Minimal {
41
- constructor(transport) {
42
- this._transport = transport;
41
+ constructor(connection) {
42
+ this._conn = connection;
43
43
  this._backplanes = 4;
44
44
  this._clear();
45
45
  }
@@ -48,31 +48,32 @@ class PCF8576Minimal {
48
48
  return _CMD_MODE_SET | (enable ? _DISPLAY_ON : _DISPLAY_OFF) | bias | mode;
49
49
  }
50
50
 
51
- _sendCommands(cmds) {
51
+ async _sendCommands(cmds) {
52
52
  const out = [];
53
53
  for (let i = 0; i < cmds.length - 1; i++) {
54
54
  out.push(0x80 | (cmds[i] & 0x7F));
55
55
  }
56
56
  out.push(cmds[cmds.length - 1] & 0x7F);
57
- this._transport.write(Buffer.from(out));
57
+ await this._conn.write(Buffer.from(out));
58
58
  }
59
59
 
60
- _sendCommandsWithData(cmd, data) {
60
+ async _sendCommandsWithData(cmd, data) {
61
61
  const out = [cmd & 0x7F, ...data];
62
- this._transport.write(Buffer.from(out));
62
+ await this._conn.write(Buffer.from(out));
63
63
  }
64
64
 
65
- _clear() {
66
- this._sendCommands([this._cmdMode(true, _BIAS_1_3, _MODE_1_4)]);
65
+ async _clear() {
66
+ await this._sendCommands([this._cmdMode(true, _BIAS_1_3, _MODE_1_4)]);
67
67
  const zeros = new Array(20).fill(0);
68
- this._sendCommandsWithData(_CMD_LOAD_PTR, zeros);
68
+ await this._sendCommandsWithData(_CMD_LOAD_PTR, zeros);
69
69
  }
70
70
 
71
71
  /**
72
72
  * Zero all 40 columns of display RAM; all segments off.
73
+ * @returns {Promise<void>}
73
74
  */
74
- clear() {
75
- this._clear();
75
+ async clear() {
76
+ await this._clear();
76
77
  }
77
78
 
78
79
  /**
@@ -80,13 +81,14 @@ class PCF8576Minimal {
80
81
  *
81
82
  * @param {number} address - RAM column address, 0-39.
82
83
  * @param {Buffer|Uint8Array|number[]} data - Bytes to write to display RAM.
84
+ * @returns {Promise<void>}
83
85
  */
84
- writeRaw(address, data) {
86
+ async writeRaw(address, data) {
85
87
  if (address < 0 || address > 39) {
86
88
  throw new RangeError('address must be in 0..39');
87
89
  }
88
90
  if (!data || data.length === 0) return;
89
- this._sendCommandsWithData(_CMD_LOAD_PTR | (address & 0x3F), Array.from(data));
91
+ await this._sendCommandsWithData(_CMD_LOAD_PTR | (address & 0x3F), Array.from(data));
90
92
  }
91
93
 
92
94
  /**
@@ -95,12 +97,13 @@ class PCF8576Minimal {
95
97
  * @param {number} position - Digit index, 0-19. Maps to RAM address position * 2.
96
98
  * @param {number} segments - 7-segment byte (a/c/b/DP/f/e/g/d packed, MSB-first).
97
99
  * Add 0x10 to set the decimal point.
100
+ * @returns {Promise<void>}
98
101
  */
99
- setDigit7seg(position, segments) {
102
+ async setDigit7seg(position, segments) {
100
103
  if (position < 0 || position > 19) {
101
104
  throw new RangeError('position must be in 0..19');
102
105
  }
103
- this.writeRaw(position * 2, [segments & 0xFF]);
106
+ await this.writeRaw(position * 2, [segments & 0xFF]);
104
107
  }
105
108
  }
106
109
 
@@ -112,7 +115,7 @@ class PCF8576Minimal {
112
115
  * static and 1:2 multiplex use, and change the device subaddress counter
113
116
  * for cascaded displays.
114
117
  *
115
- * @param {object} transport - Configured I2C transport pointing at the device.
118
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
116
119
  */
117
120
  class PCF8576Full extends PCF8576Minimal {
118
121
  static BLINK_OFF = 0;
@@ -131,8 +134,8 @@ class PCF8576Full extends PCF8576Minimal {
131
134
  static BANK_0 = 0;
132
135
  static BANK_1 = 1;
133
136
 
134
- constructor(transport) {
135
- super(transport);
137
+ constructor(connection) {
138
+ super(connection);
136
139
  this._enabled = true;
137
140
  this._bias = PCF8576Full.BIAS_1_3;
138
141
  }
@@ -147,25 +150,27 @@ class PCF8576Full extends PCF8576Minimal {
147
150
  }
148
151
  }
149
152
 
150
- _applyMode() {
153
+ async _applyMode() {
151
154
  const biasBits = (this._bias === PCF8576Full.BIAS_1_2) ? _BIAS_1_2 : _BIAS_1_3;
152
- this._sendCommands([this._cmdMode(this._enabled, biasBits, this._modeCode(this._backplanes))]);
155
+ await this._sendCommands([this._cmdMode(this._enabled, biasBits, this._modeCode(this._backplanes))]);
153
156
  }
154
157
 
155
158
  /**
156
159
  * Turn the display on (E = 1). RAM contents are preserved.
160
+ * @returns {Promise<void>}
157
161
  */
158
- enable() {
162
+ async enable() {
159
163
  this._enabled = true;
160
- this._applyMode();
164
+ await this._applyMode();
161
165
  }
162
166
 
163
167
  /**
164
168
  * Blank the display output (E = 0). RAM contents are preserved.
169
+ * @returns {Promise<void>}
165
170
  */
166
- disable() {
171
+ async disable() {
167
172
  this._enabled = false;
168
- this._applyMode();
173
+ await this._applyMode();
169
174
  }
170
175
 
171
176
  /**
@@ -173,11 +178,12 @@ class PCF8576Full extends PCF8576Minimal {
173
178
  *
174
179
  * @param {number} backplanes - Number of backplanes: 1 (static), 2 (1:2), 3 (1:3), 4 (1:4).
175
180
  * @param {number} [bias=0] - 0 = 1/3 bias, 1 = 1/2 bias.
181
+ * @returns {Promise<void>}
176
182
  */
177
- setMode(backplanes, bias = 0) {
183
+ async setMode(backplanes, bias = 0) {
178
184
  this._backplanes = backplanes;
179
185
  this._bias = bias;
180
- this._applyMode();
186
+ await this._applyMode();
181
187
  }
182
188
 
183
189
  /**
@@ -185,13 +191,14 @@ class PCF8576Full extends PCF8576Minimal {
185
191
  *
186
192
  * @param {number} frequency - 0 = off, 1 = ~2 Hz, 2 = ~1 Hz, 3 = ~0.5 Hz.
187
193
  * @param {boolean} [alternateBank=false] - Alternate-RAM-bank blinking (static/1:2 only).
194
+ * @returns {Promise<void>}
188
195
  */
189
- setBlink(frequency, alternateBank = false) {
196
+ async setBlink(frequency, alternateBank = false) {
190
197
  if (frequency < 0 || frequency > 3) {
191
198
  throw new RangeError('frequency must be in 0..3');
192
199
  }
193
200
  const ab = alternateBank ? 0x04 : 0x00;
194
- this._sendCommands([_CMD_BLINK_SELECT | ab | (frequency & 0x03)]);
201
+ await this._sendCommands([_CMD_BLINK_SELECT | ab | (frequency & 0x03)]);
195
202
  }
196
203
 
197
204
  /**
@@ -199,21 +206,23 @@ class PCF8576Full extends PCF8576Minimal {
199
206
  *
200
207
  * @param {number} inputBank - 0 (rows 0-1) or 1 (rows 2-3).
201
208
  * @param {number} outputBank - 0 (rows 0-1) or 1 (rows 2-3).
209
+ * @returns {Promise<void>}
202
210
  */
203
- setBank(inputBank, outputBank) {
204
- this._sendCommands([_CMD_BANK_SELECT | ((inputBank & 1) << 1) | (outputBank & 1)]);
211
+ async setBank(inputBank, outputBank) {
212
+ await this._sendCommands([_CMD_BANK_SELECT | ((inputBank & 1) << 1) | (outputBank & 1)]);
205
213
  }
206
214
 
207
215
  /**
208
216
  * Change the subaddress counter for cascaded displays.
209
217
  *
210
218
  * @param {number} subaddress - 0-7; must match the A0/A1/A2 pin state.
219
+ * @returns {Promise<void>}
211
220
  */
212
- deviceSelect(subaddress) {
221
+ async deviceSelect(subaddress) {
213
222
  if (subaddress < 0 || subaddress > 7) {
214
223
  throw new RangeError('subaddress must be in 0..7');
215
224
  }
216
- this._sendCommands([_CMD_DEVICE_SELECT | (subaddress & 0x07)]);
225
+ await this._sendCommands([_CMD_DEVICE_SELECT | (subaddress & 0x07)]);
217
226
  }
218
227
  }
219
228
 
@@ -18,39 +18,53 @@ function _sleep(ms) {
18
18
  * AHT21 temperature and humidity sensor — minimal interface.
19
19
  *
20
20
  * Provides temperature and humidity readings with no configuration beyond
21
- * the transport. Handles power-on initialization, calibration check, and
21
+ * the connection. Handles power-on initialization, calibration check, and
22
22
  * measurement triggering automatically.
23
23
  *
24
24
  * Default configuration (baked in at construction):
25
25
  * - Measurement triggered on every read() call (no continuous mode)
26
26
  * - 80 ms fixed wait after trigger (no busy-polling)
27
27
  * - No CRC verification (reduces complexity; CRC check is Full-only)
28
+ *
29
+ * Constructor caveat: the calibration-check sequence below is conditional on
30
+ * read results (a genuine async dependency chain), but JS constructors
31
+ * cannot be async. It is fired off unawaited, matching the fire-and-forget
32
+ * convention used for every other constructor-time connection call in this
33
+ * port — in practice it settles before any subsequent await in caller code
34
+ * gets scheduled, since the underlying I2C connection is synchronous under
35
+ * the hood, but this is not a guaranteed blocking wait the way the
36
+ * pre-async version was. Call `await sensor.read()` once before relying on
37
+ * calibration state if this matters for your use case.
28
38
  */
29
39
  class AHT21Minimal {
30
40
  /**
31
- * @param {object} transport - Configured I²C transport pointing at the device (address 0x38).
41
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x38).
32
42
  */
33
- constructor(transport) {
34
- this._transport = transport;
43
+ constructor(connection) {
44
+ this._conn = connection;
45
+ this._init();
46
+ }
47
+
48
+ async _init() {
35
49
  _sleep(100);
36
- let status = this._readStatus();
50
+ let status = await this._readStatus();
37
51
  if ((status & 0x18) !== 0x18) {
38
- this._transport.write(_CMD_SOFT_RESET);
52
+ await this._conn.write(_CMD_SOFT_RESET);
39
53
  _sleep(20);
40
- status = this._readStatus();
54
+ status = await this._readStatus();
41
55
  if ((status & 0x18) !== 0x18) {
42
- this._transport.write(_CMD_CAL_INIT_1);
56
+ await this._conn.write(_CMD_CAL_INIT_1);
43
57
  _sleep(10);
44
- this._transport.write(_CMD_CAL_INIT_2);
58
+ await this._conn.write(_CMD_CAL_INIT_2);
45
59
  _sleep(10);
46
- this._transport.write(_CMD_CAL_INIT_3);
60
+ await this._conn.write(_CMD_CAL_INIT_3);
47
61
  _sleep(10);
48
62
  }
49
63
  }
50
64
  }
51
65
 
52
- _readStatus() {
53
- return this._transport.read(1)[0];
66
+ async _readStatus() {
67
+ return (await this._conn.read(1))[0];
54
68
  }
55
69
 
56
70
  /**
@@ -59,14 +73,14 @@ class AHT21Minimal {
59
73
  * Sends the trigger command, waits 80 ms, reads 6 bytes, and decodes
60
74
  * the raw 20-bit values into physical units.
61
75
  *
62
- * @returns {{ temperature_c: number, humidity_pct: number }}
76
+ * @returns {Promise<{ temperature_c: number, humidity_pct: number }>}
63
77
  * temperature_c: Temperature in degrees Celsius (-50 to 150 °C).
64
78
  * humidity_pct: Relative humidity in percent (0 to 100 %RH).
65
79
  */
66
- read() {
67
- this._transport.write(_CMD_TRIGGER);
80
+ async read() {
81
+ await this._conn.write(_CMD_TRIGGER);
68
82
  _sleep(80);
69
- const data = this._transport.read(6);
83
+ const data = await this._conn.read(6);
70
84
  const rawRh = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
71
85
  const rawT = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
72
86
  const humidityPct = (rawRh / 1048576.0) * 100.0;
@@ -83,26 +97,26 @@ class AHT21Minimal {
83
97
  */
84
98
  class AHT21Full extends AHT21Minimal {
85
99
  /**
86
- * @param {object} transport - Configured I²C transport pointing at the device (address 0x38).
100
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x38).
87
101
  */
88
- constructor(transport) {
89
- super(transport);
102
+ constructor(connection) {
103
+ super(connection);
90
104
  }
91
105
 
92
106
  /**
93
107
  * Trigger a measurement and return temperature only.
94
- * @returns {number} Temperature in degrees Celsius (-50 to 150 °C).
108
+ * @returns {Promise<number>} Temperature in degrees Celsius (-50 to 150 °C).
95
109
  */
96
- readTemperature() {
97
- return this.read().temperature_c;
110
+ async readTemperature() {
111
+ return (await this.read()).temperature_c;
98
112
  }
99
113
 
100
114
  /**
101
115
  * Trigger a measurement and return humidity only.
102
- * @returns {number} Relative humidity in percent (0 to 100 %RH).
116
+ * @returns {Promise<number>} Relative humidity in percent (0 to 100 %RH).
103
117
  */
104
- readHumidity() {
105
- return this.read().humidity_pct;
118
+ async readHumidity() {
119
+ return (await this.read()).humidity_pct;
106
120
  }
107
121
 
108
122
  /**
@@ -111,15 +125,15 @@ class AHT21Full extends AHT21Minimal {
111
125
  * Uses polynomial x^8 + x^5 + x^4 + 1 (0x31) with initial value 0xFF
112
126
  * to verify the CRC byte against bytes 0–5 of the response.
113
127
  *
114
- * @returns {{ temperature_c: number, humidity_pct: number, crc_ok: boolean }}
128
+ * @returns {Promise<{ temperature_c: number, humidity_pct: number, crc_ok: boolean }>}
115
129
  * temperature_c: Temperature in degrees Celsius.
116
130
  * humidity_pct: Relative humidity in percent.
117
131
  * crc_ok: True if CRC-8 verification passed.
118
132
  */
119
- readWithCrc() {
120
- this._transport.write(_CMD_TRIGGER);
133
+ async readWithCrc() {
134
+ await this._conn.write(_CMD_TRIGGER);
121
135
  _sleep(80);
122
- const data = this._transport.read(7);
136
+ const data = await this._conn.read(7);
123
137
  const rawRh = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4);
124
138
  const rawT = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
125
139
  const humidityPct = (rawRh / 1048576.0) * 100.0;
@@ -130,26 +144,27 @@ class AHT21Full extends AHT21Minimal {
130
144
 
131
145
  /**
132
146
  * Send the soft reset command and wait 20 ms for recovery.
147
+ * @returns {Promise<void>}
133
148
  */
134
- softReset() {
135
- this._transport.write(_CMD_SOFT_RESET);
149
+ async softReset() {
150
+ await this._conn.write(_CMD_SOFT_RESET);
136
151
  _sleep(20);
137
152
  }
138
153
 
139
154
  /**
140
155
  * Check if the calibration bit is set in the status byte.
141
- * @returns {boolean} True if the sensor reports calibration enabled.
156
+ * @returns {Promise<boolean>} True if the sensor reports calibration enabled.
142
157
  */
143
- isCalibrated() {
144
- return !!(this._readStatus() & _STATUS_CAL);
158
+ async isCalibrated() {
159
+ return !!((await this._readStatus()) & _STATUS_CAL);
145
160
  }
146
161
 
147
162
  /**
148
163
  * Check if the busy bit is set in the status byte.
149
- * @returns {boolean} True if a measurement is in progress.
164
+ * @returns {Promise<boolean>} True if a measurement is in progress.
150
165
  */
151
- isBusy() {
152
- return !!(this._readStatus() & _STATUS_BUSY);
166
+ async isBusy() {
167
+ return !!((await this._readStatus()) & _STATUS_BUSY);
153
168
  }
154
169
 
155
170
  _crc8(data) {