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
@@ -27,82 +27,96 @@ const _STATUS_MH = 0x20;
27
27
  * AS5600 12-bit programmable contactless rotary position sensor — minimal interface.
28
28
  *
29
29
  * Reads the absolute angle in degrees with no configuration required beyond the
30
- * transport. Verifies magnet presence at construction; throws if no magnet is detected.
30
+ * connection. Verifies magnet presence at construction; throws if no magnet is detected.
31
+ *
32
+ * Constructor caveat: JS constructors cannot be async, but the original
33
+ * synchronous constructor validated magnet presence and threw synchronously
34
+ * on mismatch — that guarantee cannot be preserved exactly. The check is
35
+ * fired off unawaited (matching the fire-and-forget convention used
36
+ * throughout this port); "magnet not detected" now surfaces as an
37
+ * *unhandled promise rejection* shortly after construction rather than a
38
+ * synchronous throw from `new AS5600Minimal(...)`. Callers that need to
39
+ * detect this deterministically should call `await sensor.isMagnetDetected()`
40
+ * right after construction.
31
41
  */
32
42
  class AS5600Minimal {
33
43
  /**
34
- * @param {object} transport - Configured I²C transport pointing at the device (fixed address 0x36).
44
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x36).
35
45
  */
36
- constructor(transport) {
37
- this._transport = transport;
38
- const status = this._readReg8(_REG_STATUS);
46
+ constructor(connection) {
47
+ this._conn = connection;
48
+ this._init();
49
+ }
50
+
51
+ async _init() {
52
+ const status = await this._readReg8(_REG_STATUS);
39
53
  if (!(status & _STATUS_MD)) {
40
54
  throw new Error('AS5600: magnet not detected (MD=0)');
41
55
  }
42
56
  }
43
57
 
44
- _readReg8(reg) {
45
- return this._transport.writeRead(Buffer.from([reg]), 1)[0];
58
+ async _readReg8(reg) {
59
+ return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
46
60
  }
47
61
 
48
- _readReg16(reg) {
49
- const raw = this._transport.writeRead(Buffer.from([reg]), 2);
62
+ async _readReg16(reg) {
63
+ const raw = await this._conn.writeRead(Buffer.from([reg]), 2);
50
64
  return (raw[0] << 8) | raw[1];
51
65
  }
52
66
 
53
- _writeReg8(reg, value) {
67
+ async _writeReg8(reg, value) {
54
68
  const buf = Buffer.alloc(2);
55
69
  buf[0] = reg;
56
70
  buf[1] = value;
57
- this._transport.write(buf);
71
+ await this._conn.write(buf);
58
72
  }
59
73
 
60
- _writeReg16(reg, value) {
74
+ async _writeReg16(reg, value) {
61
75
  const buf = Buffer.alloc(3);
62
76
  buf[0] = reg;
63
77
  buf.writeUInt16BE(value, 1);
64
- this._transport.write(buf);
78
+ await this._conn.write(buf);
65
79
  }
66
80
 
67
81
  /**
68
82
  * Read the scaled absolute angle.
69
- * @returns {number} Angle in degrees, 0.0–360.0 (exclusive).
83
+ * @returns {Promise<number>} Angle in degrees, 0.0–360.0 (exclusive).
70
84
  */
71
- angle() {
72
- return this.angleRaw() * 360.0 / 4096;
85
+ async angle() {
86
+ return (await this.angleRaw()) * 360.0 / 4096;
73
87
  }
74
88
 
75
89
  /**
76
90
  * Read the scaled 12-bit angle count.
77
- * @returns {number} Scaled angle count, 0–4095 (respects ZPOS/MPOS if programmed).
91
+ * @returns {Promise<number>} Scaled angle count, 0–4095 (respects ZPOS/MPOS if programmed).
78
92
  */
79
- angleRaw() {
80
- const raw = this._readReg16(_REG_ANGLE_H);
81
- return (raw >> 4) & 0x0FFF;
93
+ async angleRaw() {
94
+ const raw = await this._readReg16(_REG_ANGLE_H);
95
+ return raw & 0x0FFF;
82
96
  }
83
97
 
84
98
  /**
85
99
  * Check if a magnet is detected.
86
- * @returns {boolean} True if STATUS.MD=1 (magnetic field >= 8 mT).
100
+ * @returns {Promise<boolean>} True if STATUS.MD=1 (magnetic field >= 8 mT).
87
101
  */
88
- isMagnetDetected() {
89
- return !!(this._readReg8(_REG_STATUS) & _STATUS_MD);
102
+ async isMagnetDetected() {
103
+ return !!((await this._readReg8(_REG_STATUS)) & _STATUS_MD);
90
104
  }
91
105
 
92
106
  /**
93
107
  * Check if the magnet is too strong.
94
- * @returns {boolean} True if STATUS.MH=1 (AGC minimum gain overflow, Bz > 90 mT).
108
+ * @returns {Promise<boolean>} True if STATUS.MH=1 (AGC minimum gain overflow, Bz > 90 mT).
95
109
  */
96
- isMagnetTooStrong() {
97
- return !!(this._readReg8(_REG_STATUS) & _STATUS_MH);
110
+ async isMagnetTooStrong() {
111
+ return !!((await this._readReg8(_REG_STATUS)) & _STATUS_MH);
98
112
  }
99
113
 
100
114
  /**
101
115
  * Check if the magnet is too weak.
102
- * @returns {boolean} True if STATUS.ML=1 (AGC maximum gain overflow, Bz < 30 mT).
116
+ * @returns {Promise<boolean>} True if STATUS.ML=1 (AGC maximum gain overflow, Bz < 30 mT).
103
117
  */
104
- isMagnetTooWeak() {
105
- return !!(this._readReg8(_REG_STATUS) & _STATUS_ML);
118
+ async isMagnetTooWeak() {
119
+ return !!((await this._readReg8(_REG_STATUS)) & _STATUS_ML);
106
120
  }
107
121
  }
108
122
 
@@ -123,51 +137,51 @@ class AS5600Full extends AS5600Minimal {
123
137
  static OUTS_PWM = 2;
124
138
 
125
139
  /**
126
- * @param {object} transport - Configured I²C transport pointing at the device (fixed address 0x36).
140
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x36).
127
141
  */
128
- constructor(transport) {
129
- super(transport);
142
+ constructor(connection) {
143
+ super(connection);
130
144
  }
131
145
 
132
146
  /**
133
147
  * Read the unscaled raw 12-bit angle count.
134
- * @returns {number} Raw angle count, 0–4095 (unaffected by ZPOS/MPOS).
148
+ * @returns {Promise<number>} Raw angle count, 0–4095 (unaffected by ZPOS/MPOS).
135
149
  */
136
- rawAngle() {
137
- const raw = this._readReg16(_REG_RAW_ANGLE_H);
138
- return (raw >> 4) & 0x0FFF;
150
+ async rawAngle() {
151
+ const raw = await this._readReg16(_REG_RAW_ANGLE_H);
152
+ return raw & 0x0FFF;
139
153
  }
140
154
 
141
155
  /**
142
156
  * Read the unscaled raw angle in degrees.
143
- * @returns {number} Raw angle in degrees, 0.0–360.0.
157
+ * @returns {Promise<number>} Raw angle in degrees, 0.0–360.0.
144
158
  */
145
- rawAngleDegrees() {
146
- return this.rawAngle() * 360.0 / 4096;
159
+ async rawAngleDegrees() {
160
+ return (await this.rawAngle()) * 360.0 / 4096;
147
161
  }
148
162
 
149
163
  /**
150
164
  * Read the automatic gain control value.
151
- * @returns {number} AGC value (0–255 in 5 V mode; 0–127 in 3.3 V mode).
165
+ * @returns {Promise<number>} AGC value (0–255 in 5 V mode; 0–127 in 3.3 V mode).
152
166
  */
153
- agc() {
167
+ async agc() {
154
168
  return this._readReg8(_REG_AGC);
155
169
  }
156
170
 
157
171
  /**
158
172
  * Read the CORDIC magnitude value.
159
- * @returns {number} 12-bit CORDIC magnitude value.
173
+ * @returns {Promise<number>} 12-bit CORDIC magnitude value.
160
174
  */
161
- magnitude() {
162
- const raw = this._readReg16(_REG_MAGNITUDE_H);
163
- return (raw >> 4) & 0x0FFF;
175
+ async magnitude() {
176
+ const raw = await this._readReg16(_REG_MAGNITUDE_H);
177
+ return raw & 0x0FFF;
164
178
  }
165
179
 
166
180
  /**
167
181
  * Read the raw STATUS register byte.
168
- * @returns {number} Raw STATUS register (bits MH, ML, MD in positions 5, 4, 3).
182
+ * @returns {Promise<number>} Raw STATUS register (bits MH, ML, MD in positions 5, 4, 3).
169
183
  */
170
- statusByte() {
184
+ async statusByte() {
171
185
  return this._readReg8(_REG_STATUS);
172
186
  }
173
187
 
@@ -184,75 +198,79 @@ class AS5600Full extends AS5600Minimal {
184
198
  * @param {number} [sf=0] - Slow filter 0–3 (0=16x, 1=8x, 2=4x, 3=2x).
185
199
  * @param {number} [fth=0] - Fast filter threshold 0–7.
186
200
  * @param {boolean} [wd=false] - Watchdog enable.
201
+ * @returns {Promise<void>}
187
202
  */
188
- configure(pm = 0, hyst = 0, outs = 0, pwmf = 0, sf = 0, fth = 0, wd = false) {
189
- let confH = this._readReg8(_REG_CONF_H);
190
- let confL = this._readReg8(_REG_CONF_L);
203
+ async configure(pm = 0, hyst = 0, outs = 0, pwmf = 0, sf = 0, fth = 0, wd = false) {
204
+ let confH = await this._readReg8(_REG_CONF_H);
205
+ let confL = await this._readReg8(_REG_CONF_L);
191
206
  confH = (confH & 0xC0) | ((wd ? 1 : 0) << 5) | ((fth & 0x07) << 2) | (sf & 0x03);
192
207
  confL = ((pwmf & 0x03) << 6) | ((outs & 0x03) << 4) | ((hyst & 0x03) << 2) | (pm & 0x03);
193
- this._writeReg16(_REG_CONF_H, (confH << 8) | confL);
208
+ await this._writeReg16(_REG_CONF_H, (confH << 8) | confL);
194
209
  }
195
210
 
196
211
  /**
197
212
  * Write the zero position (start angle) to volatile RAM.
198
213
  * @param {number} pos - Zero position 0–4095. Lost on power cycle unless burned.
214
+ * @returns {Promise<void>}
199
215
  */
200
- setZeroPosition(pos) {
201
- this._writeReg8(_REG_ZPOS_H, (pos >> 8) & 0x0F);
202
- this._writeReg8(_REG_ZPOS_L, pos & 0xFF);
216
+ async setZeroPosition(pos) {
217
+ await this._writeReg8(_REG_ZPOS_H, (pos >> 8) & 0x0F);
218
+ await this._writeReg8(_REG_ZPOS_L, pos & 0xFF);
203
219
  }
204
220
 
205
221
  /**
206
222
  * Write the maximum position (stop angle) to volatile RAM.
207
223
  * @param {number} pos - Maximum position 0–4095. Lost on power cycle unless burned.
224
+ * @returns {Promise<void>}
208
225
  */
209
- setMaxPosition(pos) {
210
- this._writeReg8(_REG_MPOS_H, (pos >> 8) & 0x0F);
211
- this._writeReg8(_REG_MPOS_L, pos & 0xFF);
226
+ async setMaxPosition(pos) {
227
+ await this._writeReg8(_REG_MPOS_H, (pos >> 8) & 0x0F);
228
+ await this._writeReg8(_REG_MPOS_L, pos & 0xFF);
212
229
  }
213
230
 
214
231
  /**
215
232
  * Write the maximum angle span to volatile RAM.
216
233
  * @param {number} span - Angle span 0–4095 (must correspond to >= 18 degrees).
234
+ * @returns {Promise<void>}
217
235
  */
218
- setMaxAngle(span) {
219
- this._writeReg8(_REG_MANG_H, (span >> 8) & 0x0F);
220
- this._writeReg8(_REG_MANG_L, span & 0xFF);
236
+ async setMaxAngle(span) {
237
+ await this._writeReg8(_REG_MANG_H, (span >> 8) & 0x0F);
238
+ await this._writeReg8(_REG_MANG_L, span & 0xFF);
221
239
  }
222
240
 
223
241
  /**
224
242
  * Read the zero position (start angle).
225
- * @returns {number} ZPOS value 0–4095.
243
+ * @returns {Promise<number>} ZPOS value 0–4095.
226
244
  */
227
- zeroPosition() {
228
- const raw = this._readReg16(_REG_ZPOS_H);
229
- return (raw >> 4) & 0x0FFF;
245
+ async zeroPosition() {
246
+ const raw = await this._readReg16(_REG_ZPOS_H);
247
+ return raw & 0x0FFF;
230
248
  }
231
249
 
232
250
  /**
233
251
  * Read the maximum position (stop angle).
234
- * @returns {number} MPOS value 0–4095.
252
+ * @returns {Promise<number>} MPOS value 0–4095.
235
253
  */
236
- maxPosition() {
237
- const raw = this._readReg16(_REG_MPOS_H);
238
- return (raw >> 4) & 0x0FFF;
254
+ async maxPosition() {
255
+ const raw = await this._readReg16(_REG_MPOS_H);
256
+ return raw & 0x0FFF;
239
257
  }
240
258
 
241
259
  /**
242
260
  * Read the maximum angle span.
243
- * @returns {number} MANG value 0–4095.
261
+ * @returns {Promise<number>} MANG value 0–4095.
244
262
  */
245
- maxAngle() {
246
- const raw = this._readReg16(_REG_MANG_H);
247
- return (raw >> 4) & 0x0FFF;
263
+ async maxAngle() {
264
+ const raw = await this._readReg16(_REG_MANG_H);
265
+ return raw & 0x0FFF;
248
266
  }
249
267
 
250
268
  /**
251
269
  * Read the number of permanent ZPOS/MPOS burns already performed.
252
- * @returns {number} ZMCO value 0–3. Remaining permanent writes = 3 - ZMCO.
270
+ * @returns {Promise<number>} ZMCO value 0–3. Remaining permanent writes = 3 - ZMCO.
253
271
  */
254
- burnCount() {
255
- return this._readReg8(_REG_ZMCO) & 0x03;
272
+ async burnCount() {
273
+ return (await this._readReg8(_REG_ZMCO)) & 0x03;
256
274
  }
257
275
 
258
276
  /**
@@ -261,17 +279,18 @@ class AS5600Full extends AS5600Minimal {
261
279
  * Requires MD=1 (magnet present) and ZMCO < 3.
262
280
  *
263
281
  * @throws {Error} If magnet not detected or ZMCO >= 3.
282
+ * @returns {Promise<void>}
264
283
  */
265
- burnAngle() {
266
- const status = this._readReg8(_REG_STATUS);
284
+ async burnAngle() {
285
+ const status = await this._readReg8(_REG_STATUS);
267
286
  if (!(status & _STATUS_MD)) {
268
287
  throw new Error('AS5600: cannot burn angle — magnet not detected');
269
288
  }
270
- const zmco = this._readReg8(_REG_ZMCO) & 0x03;
289
+ const zmco = (await this._readReg8(_REG_ZMCO)) & 0x03;
271
290
  if (zmco >= 3) {
272
291
  throw new Error('AS5600: cannot burn angle — ZMCO limit reached (3)');
273
292
  }
274
- this._writeReg8(_REG_BURN, 0x80);
293
+ await this._writeReg8(_REG_BURN, 0x80);
275
294
  }
276
295
 
277
296
  /**
@@ -280,13 +299,14 @@ class AS5600Full extends AS5600Minimal {
280
299
  * Requires ZMCO=0 (ZPOS/MPOS never burned). Can only be executed once.
281
300
  *
282
301
  * @throws {Error} If ZMCO != 0.
302
+ * @returns {Promise<void>}
283
303
  */
284
- burnSetting() {
285
- const zmco = this._readReg8(_REG_ZMCO) & 0x03;
304
+ async burnSetting() {
305
+ const zmco = (await this._readReg8(_REG_ZMCO)) & 0x03;
286
306
  if (zmco !== 0) {
287
307
  throw new Error('AS5600: cannot burn setting — ZMCO must be 0');
288
308
  }
289
- this._writeReg8(_REG_BURN, 0x40);
309
+ await this._writeReg8(_REG_BURN, 0x40);
290
310
  }
291
311
  }
292
312
 
@@ -0,0 +1,222 @@
1
+ 'use strict';
2
+
3
+ const _REG_CONFIG_A = 0x00;
4
+ const _REG_CONFIG_B = 0x01;
5
+ const _REG_MODE = 0x02;
6
+ const _REG_DATA_X_MSB = 0x03;
7
+ const _REG_STATUS = 0x09;
8
+ const _REG_ID_A = 0x0A;
9
+ const _REG_ID_B = 0x0B;
10
+ const _REG_ID_C = 0x0C;
11
+
12
+ const _GAIN_LSB_PER_GAUSS = {
13
+ 0: 1370, // GN=0: ±0.88 Ga
14
+ 1: 1090, // GN=1: ±1.3 Ga (default)
15
+ 2: 820, // GN=2: ±1.9 Ga
16
+ 3: 660, // GN=3: ±2.5 Ga
17
+ 4: 440, // GN=4: ±4.0 Ga
18
+ 5: 390, // GN=5: ±4.7 Ga
19
+ 6: 330, // GN=6: ±5.6 Ga
20
+ 7: 230, // GN=7: ±8.1 Ga
21
+ };
22
+
23
+ /**
24
+ * HMC5883L 3-axis magnetometer — minimal interface.
25
+ *
26
+ * Reads magnetic field on all three axes in continuous mode with sensible
27
+ * defaults baked in. No configuration required beyond the connection.
28
+ *
29
+ * Default behaviour (baked into Minimal):
30
+ * - Averaging: 8 samples (MA=11)
31
+ * - ODR: 15 Hz (DO=100)
32
+ * - Gain: ±1.3 Ga (GN=001), 1090 LSb/Gauss
33
+ * - Mode: continuous measurement
34
+ */
35
+ class HMC5883LMinimal {
36
+ /**
37
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x1E).
38
+ */
39
+ constructor(connection) {
40
+ this._conn = connection;
41
+ this._gain = 1;
42
+ this._gainLsbPerGauss = _GAIN_LSB_PER_GAUSS[this._gain];
43
+ this._initMinimal();
44
+ }
45
+
46
+ async _initMinimal() {
47
+ await this._writeReg8(_REG_CONFIG_A, 0x70);
48
+ await this._writeReg8(_REG_CONFIG_B, 0x20);
49
+ await this._writeReg8(_REG_MODE, 0x00);
50
+ // 6 ms delay for first measurement - caller should wait if needed
51
+ }
52
+
53
+ async _readReg8(reg) {
54
+ return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
55
+ }
56
+
57
+ async _readReg16(reg) {
58
+ const raw = await this._conn.writeRead(Buffer.from([reg]), 2);
59
+ return raw.readInt16BE(0);
60
+ }
61
+
62
+ async _writeReg8(reg, value) {
63
+ const buf = Buffer.alloc(2);
64
+ buf[0] = reg;
65
+ buf[1] = value;
66
+ await this._conn.write(buf);
67
+ }
68
+
69
+ async _readDataBurst() {
70
+ const raw = await this._conn.writeRead(Buffer.from([_REG_DATA_X_MSB]), 6);
71
+ const rawX = raw.readInt16BE(0);
72
+ const rawZ = raw.readInt16BE(2);
73
+ const rawY = raw.readInt16BE(4);
74
+ return { rawX, rawY, rawZ };
75
+ }
76
+
77
+ _rawToTesla(raw) {
78
+ if (raw === -4096) {
79
+ return null;
80
+ }
81
+ return (raw / this._gainLsbPerGauss) * 1e-4;
82
+ }
83
+
84
+ /**
85
+ * Read magnetic field on all three axes.
86
+ * @returns {Promise<{x: number|null, y: number|null, z: number|null}>} X, Y, Z field strength in Tesla.
87
+ * Returns null for any axis that overflows (raw == -4096).
88
+ */
89
+ async magneticField() {
90
+ const { rawX, rawY, rawZ } = await this._readDataBurst();
91
+ return {
92
+ x: this._rawToTesla(rawX),
93
+ y: this._rawToTesla(rawY),
94
+ z: this._rawToTesla(rawZ),
95
+ };
96
+ }
97
+ }
98
+
99
+ /**
100
+ * HMC5883L full interface — extends HMC5883LMinimal with complete chip functionality.
101
+ *
102
+ * Adds configuration, single-shot mode, self-test, identification, and status access.
103
+ */
104
+ class HMC5883LFull extends HMC5883LMinimal {
105
+ /**
106
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x1E).
107
+ */
108
+ constructor(connection) {
109
+ super(connection);
110
+ }
111
+
112
+ /**
113
+ * Write Configuration Registers A and B.
114
+ * @param {number} [odr=15] - Data output rate in Hz (continuous mode). Valid: 0.75, 1.5, 3, 7.5, 15, 30, 75.
115
+ * @param {number} [averaging=8] - Samples averaged per output. Valid: 1, 2, 4, 8.
116
+ * @param {number} [gain=1] - Gain index 0–7.
117
+ * @returns {Promise<void>}
118
+ */
119
+ async configure(odr = 15, averaging = 8, gain = 1) {
120
+ const maMap = { 1: 0b00, 2: 0b01, 4: 0b10, 8: 0b11 };
121
+ const doMap = { 0.75: 0b000, 1.5: 0b001, 3: 0b010, 7.5: 0b011,
122
+ 15: 0b100, 30: 0b101, 75: 0b110 };
123
+
124
+ if (!maMap[averaging]) throw new Error('averaging must be 1, 2, 4, or 8');
125
+ if (!doMap[odr]) throw new Error('odr must be 0.75, 1.5, 3, 7.5, 15, 30, or 75');
126
+ if (gain < 0 || gain > 7) throw new Error('gain must be 0–7');
127
+
128
+ const ma = maMap[averaging];
129
+ const doBits = doMap[odr];
130
+ const configA = (ma << 5) | (doBits << 2);
131
+ await this._writeReg8(_REG_CONFIG_A, configA);
132
+
133
+ const configB = (gain << 5);
134
+ await this._writeReg8(_REG_CONFIG_B, configB);
135
+
136
+ this._gain = gain;
137
+ this._gainLsbPerGauss = _GAIN_LSB_PER_GAUSS[gain];
138
+ }
139
+
140
+ /**
141
+ * Update the gain setting (GN bits in Config B).
142
+ * @param {number} gain - Gain index 0–7.
143
+ * @returns {Promise<void>}
144
+ */
145
+ async setGain(gain) {
146
+ if (gain < 0 || gain > 7) throw new Error('gain must be 0–7');
147
+ await this._writeReg8(_REG_CONFIG_B, gain << 5);
148
+ this._gain = gain;
149
+ this._gainLsbPerGauss = _GAIN_LSB_PER_GAUSS[gain];
150
+ }
151
+
152
+ /**
153
+ * Set the operating mode.
154
+ * @param {string} mode - 'continuous', 'single', or 'idle'.
155
+ * @returns {Promise<void>}
156
+ */
157
+ async setMode(mode) {
158
+ const modeMap = { continuous: 0b00, single: 0b01, idle: 0b10 };
159
+ if (!modeMap[mode]) throw new Error("mode must be 'continuous', 'single', or 'idle'");
160
+ await this._writeReg8(_REG_MODE, modeMap[mode]);
161
+ }
162
+
163
+ /**
164
+ * Check if new measurement data is ready.
165
+ * @returns {Promise<boolean>} True if RDY bit is set in Status Register.
166
+ */
167
+ async dataReady() {
168
+ const status = await this._readReg8(_REG_STATUS);
169
+ return !!(status & 0x01);
170
+ }
171
+
172
+ /**
173
+ * Read the raw Status Register.
174
+ * @returns {Promise<number>} Raw STATUS register byte (RDY in bit 0, LOCK in bit 1).
175
+ */
176
+ async status() {
177
+ return this._readReg8(_REG_STATUS);
178
+ }
179
+
180
+ /**
181
+ * Take a single measurement in single-shot mode.
182
+ * @returns {Promise<{x: number|null, y: number|null, z: number|null}>} X, Y, Z field strength in Tesla.
183
+ * Returns null for any axis that overflows (raw == -4096).
184
+ */
185
+ async singleMeasurement() {
186
+ await this._writeReg8(_REG_MODE, 0x01);
187
+ // Caller should wait 6 ms before reading
188
+ return this.magneticField();
189
+ }
190
+
191
+ /**
192
+ * Read the identification registers.
193
+ * @returns {Promise<[number, number, number]>} (id_a, id_b, id_c) — expected (0x48, 0x34, 0x33) = ASCII 'H43'.
194
+ */
195
+ async identify() {
196
+ const idA = await this._readReg8(_REG_ID_A);
197
+ const idB = await this._readReg8(_REG_ID_B);
198
+ const idC = await this._readReg8(_REG_ID_C);
199
+ return [idA, idB, idC];
200
+ }
201
+
202
+ /**
203
+ * Run self-test with positive or negative bias.
204
+ * @param {boolean} [positive=true] - True for positive bias (MS=01), false for negative bias (MS=10).
205
+ * @returns {Promise<{x: number|null, y: number|null, z: number|null}>} X, Y, Z field deflection in Tesla.
206
+ * Returns null for any axis that overflows.
207
+ */
208
+ async selfTest(positive = true) {
209
+ const configA = await this._readReg8(_REG_CONFIG_A);
210
+ const ms = positive ? 0b01 : 0b10;
211
+ await this._writeReg8(_REG_CONFIG_A, (configA & 0xFC) | ms);
212
+
213
+ await this._writeReg8(_REG_MODE, 0x01);
214
+ // Caller should wait 6 ms
215
+ const result = await this.magneticField();
216
+
217
+ await this._writeReg8(_REG_CONFIG_A, (configA & 0xFC) | 0b00);
218
+ return result;
219
+ }
220
+ }
221
+
222
+ module.exports = { HMC5883LMinimal, HMC5883LFull };