Skip to content

Commit 6ae4c7c

Browse files
committed
types cleanup and scan return array
1 parent b8f3cc9 commit 6ae4c7c

File tree

7 files changed

+70
-37
lines changed

7 files changed

+70
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@johntalton/excamera-i2cdriver",
3-
"version": "3.1.3",
3+
"version": "3.1.4",
44
"license": "MIT",
55
"type": "module",
66
"exports": {

src/defs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const PULLUP_1_5_K = 0b101
6262
export const PULLUP_2_2_K_ALT = 0b110
6363
export const PULLUP_1_1_K = 0b111
6464

65+
/** @type {Record<number, string>} */
6566
export const PULLUP_LOOKUP = {
6667
[PULLUP_0_0_K]: 'Zero',
6768
[PULLUP_2_2_K]: '2.2K',

src/i2c-driver-i2c.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import { INITIAL_CRC } from './crc-16-ccitt.js'
22
import { ExcameraLabsI2CDriver } from './i2c-driver.js'
33

44
/** @import { I2CAddress, I2CBufferSource, I2CReadResult } from '@johntalton/and-other-delights' */
5-
/** @import { SerialPort } from './serial.js' */
65
/** @import { Start } from './parse-buffers.js' */
76

7+
/**
8+
* @typedef {Object} ExcameraLabsI2CDriverI2COptions
9+
* @property {ReadableWritablePair} port
10+
*/
11+
812
export class ExcameraLabsI2CDriverI2C {
913
#port
1014
#crc = INITIAL_CRC
1115

16+
/**
17+
* @param {ExcameraLabsI2CDriverI2COptions} options
18+
*/
1219
static from(options) { return new ExcameraLabsI2CDriverI2C(options) }
1320

1421
/**
15-
* @param {SerialPort} port
22+
* @param {ExcameraLabsI2CDriverI2COptions} options
1623
*/
1724
constructor({ port }) { this.#port = port }
1825

src/i2c-driver.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import {
2929
COMMAND_TRANSMIT_STATUS_INFO
3030
} from './defs.js'
3131

32-
/** @import { SerialPort, ReadResult, TargetReadBuffer, SendBuffer, CommandOptions } from './serial.js' */
32+
/** @import { ReadResult, TargetReadBuffer, SendBuffer, CommandOptions } from './serial.js' */
3333
/** @import { TransmitStatusInfo, Start, StartWithDevice, BusStatus } from './parse-buffers.js' */
3434

35-
3635
export class ExcameraLabsI2CDriver {
3736
/**
38-
* @param {SerialPort} port
37+
* @param {ReadableWritablePair} port
3938
* @param {CommandOptions} [options]
4039
* @returns {Promise<TransmitStatusInfo>}
4140
*/
@@ -45,7 +44,7 @@ export class ExcameraLabsI2CDriver {
4544
}
4645

4746
/**
48-
* @param {SerialPort} port
47+
* @param {ReadableWritablePair} port
4948
* @param {CommandOptions} [options]
5049
* @returns {Promise<any>}
5150
*/
@@ -55,7 +54,7 @@ export class ExcameraLabsI2CDriver {
5554
}
5655

5756
/**
58-
* @param {SerialPort} port
57+
* @param {ReadableWritablePair} port
5958
* @param {number} b
6059
* @param {CommandOptions} [options]
6160
* @returns {Promise<number>}
@@ -66,7 +65,7 @@ export class ExcameraLabsI2CDriver {
6665
}
6766

6867
/**
69-
* @param {SerialPort} port
68+
* @param {ReadableWritablePair} port
7069
* @param {number} speed
7170
* @param {CommandOptions} [options]
7271
* @returns {Promise<void>}
@@ -78,7 +77,7 @@ export class ExcameraLabsI2CDriver {
7877
}
7978

8079
/**
81-
* @param {SerialPort} port
80+
* @param {ReadableWritablePair} port
8281
* @param {number} dev
8382
* @param {boolean} readMode
8483
* @param {CommandOptions} [options]
@@ -92,7 +91,7 @@ export class ExcameraLabsI2CDriver {
9291
}
9392

9493
/**
95-
* @param {SerialPort} port
94+
* @param {ReadableWritablePair} port
9695
* @param {number} count
9796
* @param {TargetReadBuffer} readBuffer
9897
* @param {CommandOptions} [options]
@@ -109,7 +108,7 @@ export class ExcameraLabsI2CDriver {
109108
}
110109

111110
/**
112-
* @param {SerialPort} port
111+
* @param {ReadableWritablePair} port
113112
* @param {number} count
114113
* @param {SendBuffer} bufferSource
115114
* @param {CommandOptions} [options]
@@ -127,7 +126,7 @@ export class ExcameraLabsI2CDriver {
127126
}
128127

129128
/**
130-
* @param {SerialPort} port
129+
* @param {ReadableWritablePair} port
131130
* @param {number} count
132131
* @param {TargetReadBuffer} readBuffer
133132
* @param {CommandOptions} [options]
@@ -138,7 +137,7 @@ export class ExcameraLabsI2CDriver {
138137
}
139138

140139
/**
141-
* @param {SerialPort} port
140+
* @param {ReadableWritablePair} port
142141
* @param {CommandOptions} [options]
143142
* @returns {Promise<void>}
144143
*/
@@ -147,7 +146,7 @@ export class ExcameraLabsI2CDriver {
147146
}
148147

149148
/**
150-
* @param {SerialPort} port
149+
* @param {ReadableWritablePair} port
151150
* @param {CommandOptions} [options]
152151
* @returns {Promise<BusStatus>}
153152
*/
@@ -157,7 +156,7 @@ export class ExcameraLabsI2CDriver {
157156
}
158157

159158
/**
160-
* @param {SerialPort} port
159+
* @param {ReadableWritablePair} port
161160
* @param {number} dev
162161
* @param {number} addr
163162
* @param {number} count
@@ -171,7 +170,7 @@ export class ExcameraLabsI2CDriver {
171170
}
172171

173172
/**
174-
* @param {SerialPort} port
173+
* @param {ReadableWritablePair} port
175174
* @param {CommandOptions} [options]
176175
* @returns {Promise<StartWithDevice[]>}
177176
*/
@@ -181,7 +180,7 @@ export class ExcameraLabsI2CDriver {
181180
}
182181

183182
/**
184-
* @param {SerialPort} port
183+
* @param {ReadableWritablePair} port
185184
* @param {CommandOptions} [options]
186185
* @returns {Promise<void>}
187186
*/
@@ -190,7 +189,7 @@ export class ExcameraLabsI2CDriver {
190189
}
191190

192191
/**
193-
* @param {SerialPort} port
192+
* @param {ReadableWritablePair} port
194193
* @param {CommandOptions} [options]
195194
* @returns {Promise<void>}
196195
*/
@@ -199,7 +198,7 @@ export class ExcameraLabsI2CDriver {
199198
}
200199

201200
/**
202-
* @param {SerialPort} port
201+
* @param {ReadableWritablePair} port
203202
* @param {CommandOptions} [options]
204203
* @returns {Promise<void>}
205204
*/
@@ -208,7 +207,7 @@ export class ExcameraLabsI2CDriver {
208207
}
209208

210209
/**
211-
* @param {SerialPort} port
210+
* @param {ReadableWritablePair} port
212211
* @param {CommandOptions} [options]
213212
* @returns {Promise<void>}
214213
*/
@@ -217,7 +216,7 @@ export class ExcameraLabsI2CDriver {
217216
}
218217

219218
/**
220-
* @param {SerialPort} port
219+
* @param {ReadableWritablePair} port
221220
* @param {number[]} commands
222221
* @param {CommandOptions} [options]
223222
* @returns {Promise<ReadResult>}
@@ -236,7 +235,7 @@ export class ExcameraLabsI2CDriver {
236235
}
237236

238237
/**
239-
* @param {SerialPort} port
238+
* @param {ReadableWritablePair} port
240239
* @param {CommandOptions} [options]
241240
* @returns {Promise<void>}
242241
*/
@@ -245,7 +244,7 @@ export class ExcameraLabsI2CDriver {
245244
}
246245

247246
/**
248-
* @param {SerialPort} port
247+
* @param {ReadableWritablePair} port
249248
* @param {CommandOptions} [options]
250249
* @returns {Promise<void>}
251250
*/
@@ -254,7 +253,9 @@ export class ExcameraLabsI2CDriver {
254253
}
255254

256255
/**
257-
* @param {SerialPort} port
256+
* @param {ReadableWritablePair} port
257+
* @param {number} scl
258+
* @param {number} sda
258259
* @param {CommandOptions} [options]
259260
* @returns {Promise<void>}
260261
*/
@@ -264,7 +265,7 @@ export class ExcameraLabsI2CDriver {
264265
}
265266

266267
/**
267-
* @param {SerialPort} port
268+
* @param {ReadableWritablePair} port
268269
* @param {CommandOptions} [options]
269270
* @returns {Promise<void>}
270271
*/

src/parse-buffers.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from './defs.js'
66
import { range } from './range.js'
77

8-
/** @import { ReadResult } from './serial.js' */
8+
/** @import { ReadResult, TargetReadBuffer } from './serial.js' */
99

1010
/**
1111
* @typedef {Object} Start
@@ -27,14 +27,25 @@ import { range } from './range.js'
2727
* @property {number} sda
2828
*/
2929

30+
/**
31+
* @param {TargetReadBuffer} buffer
32+
* @param {number} length
33+
*/
3034
function assertAtLeastByteLength(buffer, length) {
3135
if(buffer.byteLength < length) { throw new Error('invalid byte length - short') }
3236
}
3337

38+
/**
39+
* @param {TargetReadBuffer} buffer
40+
*/
3441
function assertNonZeroByteLength(buffer) {
3542
if(buffer.byteLength <= 0) { throw new Error('zero (or less 🤯) length buffer') }
3643
}
3744

45+
/**
46+
* @param {number} bytesRead
47+
* @param {number} target
48+
*/
3849
function assertBytesRead(bytesRead, target) {
3950
if(bytesRead !== target) { throw new Error('invalid byte length') }
4051
}
@@ -185,7 +196,7 @@ export class ResponseBufferParser {
185196
const SCAN_END_ADDRESS = 0x77
186197
const count = SCAN_END_ADDRESS - SCAN_START_ADDRESS
187198

188-
return range(0, count).map(index => {
199+
return [ ...range(0, count) ].map(index => {
189200
const result = ResponseBufferParser.parseStart({ bytesRead: 1, buffer: u8.subarray(index, index + 1) })
190201
return {
191202
...result,

src/range.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @param {number} start
3+
* @param {number} end
4+
* @param {number} [step=1]
5+
* @returns {Generator<number>}
6+
*/
17
export function* range(start, end, step = 1) {
28
yield start
39
if (start >= end) return

src/serial.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
* @property {TargetReadBuffer} buffer
1616
*/
1717

18-
19-
/** @typedef {Object} SerialPort */
20-
2118
export class CoreExcameraLabsI2CDriver {
2219
static defaultTimeoutMs = 500
2320

2421
/**
25-
* @param {SerialPort} port
22+
* @param {ReadableWritablePair} port
2623
* @param {TargetReadBuffer} readBuffer
2724
* @param {number} recvLength
2825
* @param {CommandOptions} [options]
@@ -104,6 +101,12 @@ export class CoreExcameraLabsI2CDriver {
104101
throw new Error('exit')
105102
}
106103

104+
/**
105+
* @param {WritableStreamDefaultWriter} defaultWriter
106+
* @param {number} command
107+
* @param {SendBuffer|undefined} sendBuffer
108+
* @param {CommandOptions} [options]
109+
*/
107110
static async #sendCommand(defaultWriter, command, sendBuffer, options) {
108111
const { signal } = options ?? {}
109112

@@ -142,7 +145,7 @@ export class CoreExcameraLabsI2CDriver {
142145
}
143146

144147
/**
145-
* @param {SerialPort} port
148+
* @param {ReadableWritablePair} port
146149
* @param {number} command
147150
* @param {TargetReadBuffer} readBuffer
148151
* @param {number} recvLength
@@ -187,7 +190,7 @@ export class CoreExcameraLabsI2CDriver {
187190
}
188191

189192
/**
190-
* @param {SerialPort} port
193+
* @param {ReadableWritablePair} port
191194
* @param {number} command
192195
* @param {SendBuffer|undefined} sendBuffer
193196
* @param {CommandOptions} [options]
@@ -214,7 +217,7 @@ export class CoreExcameraLabsI2CDriver {
214217
}
215218

216219
/**
217-
* @param {SerialPort} port
220+
* @param {ReadableWritablePair} port
218221
* @param {number} command
219222
* @param {CommandOptions} [options]
220223
*/
@@ -223,7 +226,7 @@ export class CoreExcameraLabsI2CDriver {
223226
}
224227

225228
/**
226-
* @param {SerialPort} port
229+
* @param {ReadableWritablePair} port
227230
* @param {string} textCommand
228231
* @param {SendBuffer|undefined} sendBuffer
229232
* @param {number|undefined} recvLength
@@ -232,8 +235,12 @@ export class CoreExcameraLabsI2CDriver {
232235
static async sendRecvTextCommand(port, textCommand, sendBuffer, recvLength, options) {
233236
const encoder = new TextEncoder()
234237
const encoded = encoder.encode(textCommand)
235-
236238
const [ command ] = encoded
239+
240+
if(recvLength === undefined || recvLength <= 0) {
241+
return CoreExcameraLabsI2CDriver.sendCommandNoReply(port, command, sendBuffer, options)
242+
}
243+
237244
const readBuffer = new ArrayBuffer(recvLength)
238245
return CoreExcameraLabsI2CDriver.sendRecvCommand(port, command, sendBuffer, recvLength, readBuffer, options)
239246
}

0 commit comments

Comments
 (0)