Skip to content

Commit

Permalink
loosen return result
Browse files Browse the repository at this point in the history
  • Loading branch information
johntalton committed Jun 27, 2024
1 parent 46d8c16 commit d2110de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@johntalton/and-other-delights",
"version": "8.2.0",
"version": "8.3.0",
"license": "MIT",
"main": "./lib/index.js",
"exports": {
Expand All @@ -19,6 +19,6 @@
"build:watch": "npm run build -- -w"
},
"devDependencies": {
"typescript": "^5.3.3"
"typescript": "^5.5.2"
}
}
25 changes: 14 additions & 11 deletions src/i2c-addressed.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
/* eslint-disable import/exports-last */
import { I2CAddress, I2CBufferSource, I2CBus, I2CWriteResult } from './i2c.js'

const BASE_16 = 16

declare global {
interface ArrayBuffer {
detached: boolean
}
interface SharedArrayBuffer {
detached: boolean
}
}

export interface _I2CAddressedBus {
close(): void

readI2cBlock(cmd: number, length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer>
readI2cBlock(cmd: number, length: number, readBufferSource?: I2CBufferSource): Promise<I2CBufferSource>
writeI2cBlock(cmd: number, bufferSource: I2CBufferSource): Promise<I2CWriteResult>

sendByte(value: number): Promise<void>

i2cRead(length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer>
i2cRead(length: number, readBufferSource?: I2CBufferSource): Promise<I2CBufferSource>
i2cWrite(bufferSource: I2CBufferSource): Promise<I2CWriteResult>
}

Expand Down Expand Up @@ -58,42 +66,37 @@ export class I2CAddressedBus implements _I2CAddressedBus {
}

salvageReadBuffer(buffer: I2CBufferSource) {
if(buffer.detached) { throw new Error(' what') }
if(ArrayBuffer.isView(buffer) ? buffer.buffer.detached : buffer.detached) { throw new Error('salvage attempt on detached buffer') }
this.#commonReadBuffer = ArrayBuffer.isView(buffer) ? buffer.buffer : buffer
}


async readI2cBlock(cmd: number, length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer> {
async readI2cBlock(cmd: number, length: number, readBufferSource?: I2CBufferSource): Promise<I2CBufferSource> {
const readBuffer = readBufferSource ?? this.defaultReadBuffer(length)
const { bytesRead, buffer } = await this.#bus.readI2cBlock(this.#address, cmd, length, readBuffer)
if(bytesRead !== length) { throw new Error('invalid length read') }
if(readBufferSource === undefined) { this.salvageReadBuffer(buffer) }

return buffer
}

async writeI2cBlock(cmd: number, bufferSource: I2CBufferSource): Promise<I2CWriteResult> {
assertBufferSource(bufferSource)

return this.#bus.writeI2cBlock(this.#address, cmd, bufferSource.byteLength, bufferSource)
}

async sendByte(value: number): Promise<void> {
return this.#bus.sendByte(this.#address, value)
}

async i2cRead(length: number, readBufferSource?: I2CBufferSource): Promise<ArrayBuffer> {
async i2cRead(length: number, readBufferSource?: I2CBufferSource): Promise<I2CBufferSource> {
const readBuffer = readBufferSource ?? this.defaultReadBuffer(length)
const { bytesRead, buffer } = await this.#bus.i2cRead(this.#address, length, readBuffer)
if(bytesRead !== length) { throw new Error('invalid length read') }
if(readBufferSource === undefined) { this.salvageReadBuffer(buffer) }

return buffer
}

async i2cWrite(bufferSource: I2CBufferSource): Promise<I2CWriteResult> {
assertBufferSource(bufferSource)

return this.#bus.i2cWrite(this.#address, bufferSource.byteLength, bufferSource)
}
}
16 changes: 10 additions & 6 deletions src/i2c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ export type I2CBufferSource = ArrayBufferLike | ArrayBufferView
//
export type I2CReadResult = {
bytesRead: number
buffer: ArrayBuffer
buffer: I2CBufferSource
}

//
export type I2CWriteResult = {
bytesWritten: number
buffer: ArrayBuffer
buffer: I2CBufferSource
}

//
Expand All @@ -23,13 +23,17 @@ export interface Bus {
export interface I2CBus extends Bus {
sendByte(address: I2CAddress, byteValue: number): Promise<void>

readI2cBlock(address: I2CAddress, cmd: number, length: number, bufferSource?: I2CBufferSource): Promise<I2CReadResult>
readI2cBlock(address: I2CAddress, cmd: number, length: number, targetBuffer?: I2CBufferSource): Promise<I2CReadResult>
writeI2cBlock(
address: I2CAddress,
cmd: number,
length: number,
bufferSource: I2CBufferSource): Promise<I2CWriteResult>
buffer: I2CBufferSource): Promise<I2CWriteResult>

i2cRead(address: I2CAddress, length: number, bufferSource?: I2CBufferSource): Promise<I2CReadResult>
i2cWrite(address: I2CAddress, length: number, bufferSource: I2CBufferSource): Promise<I2CWriteResult>
i2cRead(address: I2CAddress, length: number, targetBuffer?: I2CBufferSource): Promise<I2CReadResult>
i2cWrite(address: I2CAddress, length: number, buffer: I2CBufferSource): Promise<I2CWriteResult>
}

export interface I2CScannableBus extends I2CBus {
scan(): Promise<Array<I2CAddress>>
}

0 comments on commit d2110de

Please sign in to comment.