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
@@ -0,0 +1,438 @@
1
+ 'use strict';
2
+
3
+ const SENTENCE_START = 0x24; // '$'
4
+ const CR = 0x0D;
5
+ const LF = 0x0A;
6
+ const MAX_SENTENCE = 96;
7
+
8
+ const UBX_SYNC1 = 0xB5;
9
+ const UBX_SYNC2 = 0x62;
10
+ const CLASS_ACK = 0x05;
11
+ const ID_ACK_NAK = 0x00;
12
+
13
+ /**
14
+ * Validate the *XX checksum of a $...*XX\r\n NMEA sentence.
15
+ * @param {Buffer} sentence - Full sentence including '$' and trailing CRLF.
16
+ * @returns {boolean} True if the checksum matches.
17
+ */
18
+ function nmeaChecksumOk(sentence) {
19
+ const star = sentence.indexOf(0x2A); // '*'
20
+ if (star < 1 || star + 4 > sentence.length) return false;
21
+ let checksum = 0;
22
+ for (let i = 1; i < star; i++) checksum ^= sentence[i];
23
+ const expected = parseInt(sentence.toString('ascii', star + 1, star + 3), 16);
24
+ if (Number.isNaN(expected)) return false;
25
+ return checksum === expected;
26
+ }
27
+
28
+ /**
29
+ * Convert NMEA ddmm.mmmm / dddmm.mmmm to signed decimal degrees.
30
+ * @param {string} raw - Raw NMEA coordinate field.
31
+ * @param {string} hemisphere - 'N', 'S', 'E', or 'W'.
32
+ * @returns {number} Decimal degrees, negative for S/W.
33
+ */
34
+ function nmeaToDegrees(raw, hemisphere) {
35
+ const value = parseFloat(raw);
36
+ const deg = Math.trunc(value / 100);
37
+ const minutes = value - deg * 100;
38
+ let decimal = deg + minutes / 60.0;
39
+ if (hemisphere === 'S' || hemisphere === 'W') decimal = -decimal;
40
+ return decimal;
41
+ }
42
+
43
+ /**
44
+ * 8-bit Fletcher checksum over class, id, length, and payload bytes.
45
+ * @param {Buffer} data - Bytes to checksum (class, id, length LE, payload).
46
+ * @returns {[number, number]} [ckA, ckB].
47
+ */
48
+ function ubxChecksum(data) {
49
+ let ckA = 0, ckB = 0;
50
+ for (let i = 0; i < data.length; i++) {
51
+ ckA = (ckA + data[i]) & 0xFF;
52
+ ckB = (ckB + ckA) & 0xFF;
53
+ }
54
+ return [ckA, ckB];
55
+ }
56
+
57
+ /**
58
+ * u-blox NEO-6 GNSS receiver: NMEA position, altitude, and fix status.
59
+ *
60
+ * Reads bytes from the connection and assembles complete NMEA sentences
61
+ * terminated by CR/LF. Works out of the box with the module's factory
62
+ * defaults (NMEA output at 9600 baud, 1 Hz, all standard sentences
63
+ * enabled) -- no chip-side configuration is sent.
64
+ *
65
+ * The driver is connection-agnostic; pass busType to match the connection
66
+ * given at construction:
67
+ *
68
+ * - 'uart' (default): a UART connection. read() rejects on timeout; a
69
+ * rejection is treated as "no new byte this call", not an error.
70
+ * - 'i2c': an I2C (DDC) connection. Each byte is fetched with a
71
+ * random-read to register 0xFF, per the DDC protocol.
72
+ * - 'spi': an SPI connection. Each byte is fetched with a full-duplex
73
+ * transfer; writeRead() is called with an empty command so the
74
+ * module's real output byte is never discarded mid-transfer.
75
+ *
76
+ * A stray idle-filler byte (0xFF on I2C/SPI when the module has nothing
77
+ * queued) can never start a sentence (NMEA sentences start with '$'); if
78
+ * one lands mid-sentence during a buffer underrun, the resulting sentence
79
+ * simply fails its checksum and is discarded, same as any other corrupted
80
+ * sentence.
81
+ */
82
+ class NEO6Minimal {
83
+ /**
84
+ * @param {import('../../connection/connection').Connection} connection - UART, I2C, or SPI connection.
85
+ * @param {string} [busType='uart'] - 'uart', 'i2c', or 'spi'.
86
+ */
87
+ constructor(connection, busType = 'uart') {
88
+ this._conn = connection;
89
+ this._busType = busType;
90
+ this._buf = [];
91
+ this._inSentence = false;
92
+ this._lat = null;
93
+ this._lon = null;
94
+ this._alt = null;
95
+ this._fix = 0;
96
+ this._satellites = 0;
97
+ }
98
+
99
+ /**
100
+ * Fetch one byte if available; returns null if none is ready yet.
101
+ * @returns {Promise<number|null>} The byte (0-255), or null.
102
+ */
103
+ async _readByte() {
104
+ let b;
105
+ if (this._busType === 'uart') {
106
+ try {
107
+ b = await this._conn.read(1);
108
+ } catch (_) {
109
+ return null;
110
+ }
111
+ } else if (this._busType === 'i2c') {
112
+ // DDC random-read: set the register pointer to 0xFF, then read
113
+ // one stream byte. The pointer saturates at 0xFF once set, so
114
+ // re-sending it on every byte is redundant but harmless.
115
+ b = await this._conn.writeRead(Buffer.from([0xFF]), 1);
116
+ } else {
117
+ // SPI has no register-address concept, so the write phase must
118
+ // stay empty: writeRead(prefix, n) clocks prefix's response
119
+ // bytes and discards them, and any non-empty prefix here would
120
+ // throw away a real byte of the module's output stream. An
121
+ // empty prefix makes the whole call one true 1:1 full-duplex
122
+ // transfer, so no incoming byte is ever discarded.
123
+ b = await this._conn.writeRead(Buffer.alloc(0), 1);
124
+ }
125
+ return (b && b.length) ? b[0] : null;
126
+ }
127
+
128
+ /**
129
+ * Read available bytes and parse at most one complete NMEA sentence.
130
+ * @returns {Promise<boolean>} True if a GGA sentence with a valid fix
131
+ * (fix status > 0) was parsed during this call.
132
+ */
133
+ async update() {
134
+ const byte = await this._readByte();
135
+ if (byte === null) return false;
136
+ if (byte === SENTENCE_START) {
137
+ this._buf = [byte];
138
+ this._inSentence = true;
139
+ return false;
140
+ }
141
+ if (!this._inSentence) return false;
142
+ this._buf.push(byte);
143
+ if (this._buf.length > MAX_SENTENCE) {
144
+ this._buf = [];
145
+ this._inSentence = false;
146
+ return false;
147
+ }
148
+ if (byte === LF && this._buf.length >= 2 && this._buf[this._buf.length - 2] === CR) {
149
+ const sentence = Buffer.from(this._buf);
150
+ this._buf = [];
151
+ this._inSentence = false;
152
+ return this._onSentence(sentence);
153
+ }
154
+ return false;
155
+ }
156
+
157
+ _onSentence(sentence) {
158
+ if (!nmeaChecksumOk(sentence)) return false;
159
+ const star = sentence.indexOf(0x2A);
160
+ if (star < 0) return false;
161
+ const body = sentence.toString('ascii', 1, star);
162
+ const fields = body.split(',');
163
+ if (fields[0].length < 5) return false;
164
+ const sentenceId = fields[0].substring(2, 5);
165
+ let result = false;
166
+ if (sentenceId === 'GGA') result = this._parseGga(fields);
167
+ this._handleExtra(sentenceId, fields);
168
+ return result;
169
+ }
170
+
171
+ _parseGga(fields) {
172
+ if (fields.length < 15) return false;
173
+ const fix = fields[6] ? parseInt(fields[6], 10) : 0;
174
+ this._fix = Number.isNaN(fix) ? 0 : fix;
175
+ const sats = fields[7] ? parseInt(fields[7], 10) : 0;
176
+ this._satellites = Number.isNaN(sats) ? 0 : sats;
177
+ if (this._fix > 0 && fields[2] && fields[3] && fields[4] && fields[5]) {
178
+ this._lat = nmeaToDegrees(fields[2], fields[3]);
179
+ this._lon = nmeaToDegrees(fields[4], fields[5]);
180
+ this._alt = fields[9] ? parseFloat(fields[9]) : null;
181
+ }
182
+ return this._fix > 0;
183
+ }
184
+
185
+ /**
186
+ * Hook for Full to parse additional sentence types. No-op here.
187
+ * @param {string} sentenceId - Three-letter NMEA sentence ID (e.g. 'RMC').
188
+ * @param {string[]} fields - Comma-split fields, including the talker+ID at index 0.
189
+ */
190
+ _handleExtra(sentenceId, fields) {
191
+ // no-op in Minimal
192
+ }
193
+
194
+ /**
195
+ * Latitude of the last valid fix.
196
+ * @returns {number|null} Decimal degrees, positive north; null until the first valid GGA fix.
197
+ */
198
+ latitude() { return this._lat; }
199
+
200
+ /**
201
+ * Longitude of the last valid fix.
202
+ * @returns {number|null} Decimal degrees, positive east; null until the first valid GGA fix.
203
+ */
204
+ longitude() { return this._lon; }
205
+
206
+ /**
207
+ * Height above mean sea level of the last valid fix.
208
+ * @returns {number|null} Meters; null until the first valid GGA fix.
209
+ */
210
+ altitude() { return this._alt; }
211
+
212
+ /**
213
+ * GGA fix quality of the last parsed GGA sentence.
214
+ * @returns {number} 0 = no fix, 1 = GPS, 2 = DGPS.
215
+ */
216
+ fix() { return this._fix; }
217
+
218
+ /**
219
+ * Number of satellites used in the last GGA fix.
220
+ * @returns {number} Satellite count (GGA field 7).
221
+ */
222
+ satellites() { return this._satellites; }
223
+ }
224
+
225
+ /**
226
+ * NEO-6 with UBX binary messaging, rate/platform configuration, and richer
227
+ * NMEA fields (speed, course, UTC time/date, HDOP).
228
+ *
229
+ * Extends NEO6Minimal; all Minimal methods are inherited unchanged.
230
+ */
231
+ class NEO6Full extends NEO6Minimal {
232
+ /**
233
+ * @param {import("./../../connection/connection").Connection} connection - UART, I2C, or SPI connection.
234
+ * @param {string} [busType='uart'] - 'uart', 'i2c', or 'spi'.
235
+ */
236
+ constructor(connection, busType = 'uart') {
237
+ super(connection, busType);
238
+ this._speed = null;
239
+ this._course = null;
240
+ this._utcTime = null;
241
+ this._utcDate = null;
242
+ this._hdop = null;
243
+ }
244
+
245
+ _handleExtra(sentenceId, fields) {
246
+ if (sentenceId === 'GGA') {
247
+ if (fields.length > 1 && fields[1]) this._utcTime = fields[1];
248
+ if (fields.length > 8 && fields[8]) {
249
+ const hdop = parseFloat(fields[8]);
250
+ if (!Number.isNaN(hdop)) this._hdop = hdop;
251
+ }
252
+ } else if (sentenceId === 'RMC') {
253
+ this._parseRmc(fields);
254
+ } else if (sentenceId === 'VTG') {
255
+ this._parseVtg(fields);
256
+ }
257
+ }
258
+
259
+ _parseRmc(fields) {
260
+ if (fields.length < 10) return;
261
+ if (fields[1]) this._utcTime = fields[1];
262
+ if (fields[7]) {
263
+ const knots = parseFloat(fields[7]);
264
+ if (!Number.isNaN(knots)) this._speed = knots * 0.514444;
265
+ }
266
+ if (fields[8]) {
267
+ const course = parseFloat(fields[8]);
268
+ if (!Number.isNaN(course)) this._course = course;
269
+ }
270
+ if (fields[9]) this._utcDate = fields[9];
271
+ }
272
+
273
+ _parseVtg(fields) {
274
+ if (fields.length > 1 && fields[1]) {
275
+ const course = parseFloat(fields[1]);
276
+ if (!Number.isNaN(course)) this._course = course;
277
+ }
278
+ if (fields.length > 7 && fields[7]) {
279
+ const kmh = parseFloat(fields[7]);
280
+ if (!Number.isNaN(kmh)) this._speed = kmh / 3.6;
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Speed over ground.
286
+ * @returns {number|null} Meters per second, converted from RMC/VTG; null until the first speed field is parsed.
287
+ */
288
+ speed() { return this._speed; }
289
+
290
+ /**
291
+ * Course over ground.
292
+ * @returns {number|null} Degrees, 0-360, from RMC/VTG; null until the first course field is parsed.
293
+ */
294
+ course() { return this._course; }
295
+
296
+ /**
297
+ * UTC time of the last GGA or RMC sentence.
298
+ * @returns {string|null} 'hhmmss.ss'; null until the first sentence with a time field is parsed.
299
+ */
300
+ utcTime() { return this._utcTime; }
301
+
302
+ /**
303
+ * UTC date of the last RMC sentence.
304
+ * @returns {string|null} 'ddmmyy'; null until the first RMC sentence is parsed.
305
+ */
306
+ utcDate() { return this._utcDate; }
307
+
308
+ /**
309
+ * Horizontal dilution of precision from the last GGA sentence.
310
+ * @returns {number|null} Unitless HDOP; null until the first GGA sentence with a populated HDOP field is parsed.
311
+ */
312
+ hdop() { return this._hdop; }
313
+
314
+ /**
315
+ * Frame and write a UBX message (adds sync bytes, length, checksum).
316
+ * @param {number} msgClass - UBX message class (e.g. 0x06 for CFG).
317
+ * @param {number} msgId - UBX message ID within the class.
318
+ * @param {Buffer} [payload] - Message payload bytes; default empty (a poll request).
319
+ * @returns {Promise<void>}
320
+ */
321
+ async sendUbx(msgClass, msgId, payload = Buffer.alloc(0)) {
322
+ const length = payload.length;
323
+ const body = Buffer.concat([Buffer.from([msgClass, msgId, length & 0xFF, (length >> 8) & 0xFF]), payload]);
324
+ const [ckA, ckB] = ubxChecksum(body);
325
+ const frame = Buffer.concat([Buffer.from([UBX_SYNC1, UBX_SYNC2]), body, Buffer.from([ckA, ckB])]);
326
+ await this._conn.write(frame);
327
+ }
328
+
329
+ /**
330
+ * Send a poll request and return the response payload.
331
+ * @param {number} msgClass - UBX message class to poll.
332
+ * @param {number} msgId - UBX message ID to poll.
333
+ * @returns {Promise<Buffer>} The response message's payload.
334
+ * @throws {Error} If the module answers with ACK-NAK, or no matching
335
+ * response arrives before the internal idle budget is spent.
336
+ */
337
+ async pollUbx(msgClass, msgId) {
338
+ await this.sendUbx(msgClass, msgId);
339
+ return this._readUbxResponse(msgClass, msgId);
340
+ }
341
+
342
+ async _readUbxResponse(wantClass, wantId, maxFrames = 400, maxIdle = 4000) {
343
+ let idle = 0;
344
+ let frames = 0;
345
+ while (frames < maxFrames) {
346
+ const byte = await this._readByte();
347
+ if (byte === null) {
348
+ idle++;
349
+ if (idle > maxIdle) throw new Error('UBX response timeout');
350
+ continue;
351
+ }
352
+ idle = 0;
353
+ if (byte !== UBX_SYNC1) continue;
354
+ if ((await this._readByte()) !== UBX_SYNC2) continue;
355
+ const header = [];
356
+ let headerOk = true;
357
+ for (let i = 0; i < 4; i++) {
358
+ const b = await this._readByte();
359
+ if (b === null) { headerOk = false; break; }
360
+ header.push(b);
361
+ }
362
+ if (!headerOk) continue;
363
+ const [cls, mid, lenLo, lenHi] = header;
364
+ const length = lenLo | (lenHi << 8);
365
+ const payload = [];
366
+ for (let i = 0; i < length; i++) {
367
+ const b = await this._readByte();
368
+ if (b === null) break;
369
+ payload.push(b);
370
+ }
371
+ if (payload.length !== length) { frames++; continue; }
372
+ const ckA = await this._readByte();
373
+ const ckB = await this._readByte();
374
+ const [expA, expB] = ubxChecksum(Buffer.from([cls, mid, lenLo, lenHi, ...payload]));
375
+ frames++;
376
+ if (ckA !== expA || ckB !== expB) continue;
377
+ if (cls === CLASS_ACK && mid === ID_ACK_NAK) {
378
+ throw new Error(`UBX NAK for class 0x${wantClass.toString(16)} id 0x${wantId.toString(16)}`);
379
+ }
380
+ if (cls === wantClass && mid === wantId) return Buffer.from(payload);
381
+ }
382
+ throw new Error('UBX response timeout');
383
+ }
384
+
385
+ /**
386
+ * Set the navigation update rate via CFG-RATE.
387
+ * @param {number} hz - Update rate in Hz (1-5 Hz for standard NEO-6 models).
388
+ * @returns {Promise<void>}
389
+ */
390
+ async setRate(hz) {
391
+ const measRateMs = Math.trunc(1000 / hz);
392
+ const payload = Buffer.alloc(6);
393
+ payload.writeUInt16LE(measRateMs, 0);
394
+ payload.writeUInt16LE(1, 2);
395
+ payload.writeUInt16LE(0, 4);
396
+ await this.sendUbx(0x06, 0x08, payload);
397
+ }
398
+
399
+ /**
400
+ * Set the dynamic platform model via CFG-NAV5.
401
+ * @param {number} model - Platform model code -- 0=portable, 2=stationary,
402
+ * 3=pedestrian, 4=automotive, 5=sea, 6=airborne<1g, 7=airborne<2g, 8=airborne<4g.
403
+ * @returns {Promise<void>}
404
+ */
405
+ async setPlatform(model) {
406
+ const payload = Buffer.alloc(36);
407
+ payload.writeUInt16LE(0x0001, 0); // mask: apply dynModel only
408
+ payload[2] = model & 0xFF;
409
+ await this.sendUbx(0x06, 0x24, payload);
410
+ }
411
+
412
+ /**
413
+ * Force a cold start via CFG-RST (clears almanac, ephemeris, and last known position).
414
+ * @returns {Promise<void>}
415
+ */
416
+ async coldStart() {
417
+ const payload = Buffer.alloc(4);
418
+ payload.writeUInt16LE(0xFFFF, 0);
419
+ payload[2] = 0x02;
420
+ payload[3] = 0x00;
421
+ await this.sendUbx(0x06, 0x04, payload);
422
+ }
423
+
424
+ /**
425
+ * Persist the current configuration via CFG-CFG (saves to battery-backed RAM and flash, where available).
426
+ * @returns {Promise<void>}
427
+ */
428
+ async saveConfig() {
429
+ const payload = Buffer.alloc(13);
430
+ payload.writeUInt32LE(0x00000000, 0);
431
+ payload.writeUInt32LE(0xFFFFFFFF, 4);
432
+ payload.writeUInt32LE(0x00000000, 8);
433
+ payload[12] = 0x07;
434
+ await this.sendUbx(0x06, 0x09, payload);
435
+ }
436
+ }
437
+
438
+ module.exports = { NEO6Minimal, NEO6Full };