Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions/setup-node…
Browse files Browse the repository at this point in the history
…-4.0.2
  • Loading branch information
johntalton authored Mar 27, 2024
2 parents 6f42e84 + e040368 commit b641307
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 119 deletions.
12 changes: 8 additions & 4 deletions src/busutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export class BusUtil {
* @returns A Promise the resolves to the read Buffer.
*
**/
static async readI2cBlocks(atbus: I2CAddressedTransactionBus, blocks: BlockList, sourceBufferOrNull: UtilBufferSource | undefined = undefined): Promise<ArrayBuffer> {
static async readI2cBlocks(
atBus: I2CAddressedTransactionBus,
blocks: BlockList, sourceBufferOrNull: UtilBufferSource | undefined = undefined
): Promise<ArrayBuffer> {

BusUtil.assertNormalBlock(blocks)

const totalLength = BusUtil.sourceDataLength(blocks)
Expand All @@ -60,13 +64,13 @@ export class BusUtil {
new Uint8Array(sourceBuffer.buffer, sourceBuffer.byteOffset, sourceBuffer.byteLength) :
new Uint8Array(sourceBuffer)

return atbus.transaction(async abus => {
return atBus.transaction(async aBus => {
let cursor = 0

Check warning on line 68 in src/busutil.ts

View workflow job for this annotation

GitHub Actions / Test (16, macos-latest)

Number constants declarations must use 'const'

Check warning on line 68 in src/busutil.ts

View workflow job for this annotation

GitHub Actions / Test (16, ubuntu-latest)

Number constants declarations must use 'const'

Check warning on line 68 in src/busutil.ts

View workflow job for this annotation

GitHub Actions / Test (20, macos-latest)

Number constants declarations must use 'const'

Check warning on line 68 in src/busutil.ts

View workflow job for this annotation

GitHub Actions / Test (20, ubuntu-latest)

Number constants declarations must use 'const'
for (const block of blocks) {
const [reg, len] = block
try {
const abuffer = await abus.readI2cBlock(reg, len)
buffer.set(new Uint8Array(abuffer), cursor)
const aBuffer = await aBus.readI2cBlock(reg, len)
buffer.set(new Uint8Array(aBuffer), cursor)
cursor += len
} catch (e) { console.warn({ e }); throw e }
}
Expand Down
8 changes: 7 additions & 1 deletion src/i2c-addressedtransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ export class AddressedTransactionBusProxy implements _I2CAddressedBus {
get name(): string {
throw new Error('Method not implemented.')
}

close(): void {
throw new Error('Method not implemented.')
}

async readI2cBlock(cmd: number, length: number, _readBufferSource?: I2CBufferSource | undefined): Promise<ArrayBuffer> {
async readI2cBlock(
cmd: number,
length: number,
_readBufferSource?: I2CBufferSource | undefined
): Promise<ArrayBuffer> {

const { bytesRead, buffer } = await this.#bus.readI2cBlock(this.#address, cmd, length)
return buffer.slice(0, bytesRead)
}
Expand Down
228 changes: 114 additions & 114 deletions test/busutil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,132 +4,132 @@ import { describe, it } from 'mocha'
import { expect } from 'chai'

// eslint-disable-next-line sort-imports
import { BusUtil, EOS_SCRIPT, I2CAddressedBus, I2CScriptBus } from './aod.js'
import { BusUtil, EOS_SCRIPT, I2CTransactionBus, I2CAddressedTransactionBus, I2CScriptBus } from './aod.js'

const READ_SINGLE_SCRIPT = [
{ method: 'readI2cBlock', parameters: [0x37], result: { bytesRead: 2, buffer: new Uint8Array([3, 5]) } },
...EOS_SCRIPT
{ method: 'readI2cBlock', parameters: [0x37], result: { bytesRead: 2, buffer: new Uint8Array([3, 5]) } },
...EOS_SCRIPT
]

const READ_MULTI_SCRIPT = [
{ method: 'readI2cBlock', parameters: [0x37], result: { bytesRead: 2, buffer: new Uint8Array([3, 5]) } },
{ method: 'readI2cBlock', parameters: [0x42], result: { bytesRead: 4, buffer: new Uint8Array([7, 9, 11, 13]) } },
...EOS_SCRIPT
{ method: 'readI2cBlock', parameters: [0x37], result: { bytesRead: 2, buffer: new Uint8Array([3, 5]) } },
{ method: 'readI2cBlock', parameters: [0x42], result: { bytesRead: 4, buffer: new Uint8Array([7, 9, 11, 13]) } },
...EOS_SCRIPT
]

const WRITE_SINGLE_SCRIPT = [
{ method: 'writeI2cBlock', parameters: [0, 1, 2], result: { bytesWritten: 2, buffer: new Uint8Array([]) } },
...EOS_SCRIPT
{ method: 'writeI2cBlock', parameters: [0, 1, 2], result: { bytesWritten: 2, buffer: new Uint8Array([]) } },
...EOS_SCRIPT
]

const WRITE_MULTI_SCRIPT = [
{ method: 'writeI2cBlock', parameters: [0, 1, 2], result: { bytesWritten: 2, buffer: new ArrayBuffer(2) } },
{ method: 'writeI2cBlock', parameters: [0, 4, 4], result: { bytesWritten: 4, buffer: new ArrayBuffer(4) } },
...EOS_SCRIPT
{ method: 'writeI2cBlock', parameters: [0, 1, 2], result: { bytesWritten: 2, buffer: new ArrayBuffer(2) } },
{ method: 'writeI2cBlock', parameters: [0, 4, 4], result: { bytesWritten: 4, buffer: new ArrayBuffer(4) } },
...EOS_SCRIPT
]

describe('BusUtil', () => {
describe('#readI2cBlocks', () => {
it('should return empty on empty block read', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(EOS_SCRIPT), 0x00)
const result = await BusUtil.readI2cBlocks(ab, [])
expect(result).to.deep.equal(new ArrayBuffer(0))
})

it('should read a simple block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(READ_SINGLE_SCRIPT), 0x00)
const result = await BusUtil.readI2cBlocks(ab, [[0, 2]])
expect(new Uint8Array(result)).to.deep.equal(new Uint8Array([3, 5]))
})

it('should read a multi block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(READ_MULTI_SCRIPT), 0x00)
const result = await BusUtil.readI2cBlocks(ab, [[0x37, 2], [0x42, 4]])
expect(new Uint8Array(result)).to.deep.equal(new Uint8Array([3, 5, 7, 9, 11, 13]))
})

it('should error when bus layer errors', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(READ_MULTI_SCRIPT), 0x00)
await expect(async () => { await BusUtil.readBlock(ab, [[0x37, 2], [0x42, 4], [0x77, 0]]) })
})
})

describe('#writeI2cBlocks', () => {
it('should write empty on empty block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(EOS_SCRIPT), 0x00)
await BusUtil.writeI2cBlocks(ab, [], new Uint8Array([]))
})

it('should write simple byte single block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(WRITE_SINGLE_SCRIPT), 0x00)
await BusUtil.writeI2cBlocks(ab, [[0x01, 2]], new Uint8Array([0, 3, 5]))
})

it('should write simple block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(WRITE_SINGLE_SCRIPT), 0x00)
await BusUtil.writeI2cBlocks(ab, [[0x01, 2]], new Uint8Array([0, 3, 5]))
})

it('should write multi block', async () => {
const ab = new I2CAddressedBus(await I2CScriptBus.openPromisified(WRITE_MULTI_SCRIPT), 0x00)
await BusUtil.writeI2cBlocks(ab, [[0x01, 2], [0x4, 4]], new Uint8Array([0, 3, 5, 0, 7, 9, 11, 13]))
})
})

/*
describe('#expandBlock', () => {
it('should pass most basic 1:1 test', () => {
expect(BusUtil.expandBlock([0], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([3]))
})
it('should pass most basic 1:1 test (offset)', () => {
expect(BusUtil.expandBlock([3], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 0xFE, 3]))
})
it('should pass basic example', () => {
const fill = -1
const firstPad = Buffer.alloc(10, fill)
const pre20Pad = Buffer.alloc(8, fill)
const pre30Pad = Buffer.alloc(9, fill)
const expected = Buffer.concat([firstPad, Buffer.from([3, 5]), pre20Pad, Buffer.from([7]), pre30Pad, Buffer.from([9, 11, 13])], 33)
expect(BusUtil.expandBlock([[10, 2], [20, 1], [30, 3]], Buffer.from([3, 5, 7, 9, 11, 13]), fill)).to.deep.equal(expected)
})
it('should pass basic example (collapsed)', () => {
const fill = -1
const firstPad = Buffer.alloc(10, fill)
const pre30Pad = Buffer.alloc(17, fill)
const expected = Buffer.concat([firstPad, Buffer.from([3, 5, 7]), pre30Pad, Buffer.from([9, 11, 13])], 33)
expect(BusUtil.expandBlock([[10, 2], [12, 1], [30, 3]], Buffer.from([3, 5, 7, 9, 11, 13]), fill)).to.deep.equal(expected)
})
it('should fill in the middle', () => {
expect(BusUtil.expandBlock([0, 2], Buffer.from([3, 5]), 0xFE, false)).to.deep.equal(Buffer.from([3, 0xFE, 5]))
})
it('should fill in front', () => {
expect(BusUtil.expandBlock([2], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 3]))
})
it('should fill in both', () => {
expect(BusUtil.expandBlock([1, 4], Buffer.from([3, 5]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 3, 0xFE, 0xFE, 5]))
})
it('should handle multi-byte', () => {
expect(BusUtil.expandBlock([[0, 4], 4], Buffer.from([3, 5, 7, 9, 11]), 0xFE, false)).to.deep.equal(Buffer.from([3, 5, 7, 9, 11]))
})
it('should handle multi-byte padded', () => {
expect(BusUtil.expandBlock([[2, 4], 8], Buffer.from([3, 5, 7, 9, 11]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 3, 5, 7, 9, 0xFE, 0xFE, 11]))
})
it('should error if input buffer length does not match BlockDefinition', () => {
expect(() => BusUtil.expandBlock([1, 2, 3], Buffer.alloc(5, 0), 0xFE, false)).to.throw(Error)
})
it('should match example used in hand coded test', () => {
expect(BusUtil.expandBlock([[0x01, 2], [0x4, 4]], Buffer.from([3, 5, 7, 9, 11, 13]))).to.deep.equal(Buffer.from([0, 3, 5, 0, 7, 9, 11, 13]))
})
})
*/
describe('#readI2cBlocks', () => {
it('should return empty on empty block read', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(EOS_SCRIPT), 0x00))
const result = await BusUtil.readI2cBlocks(ab, [])
expect(result).to.deep.equal(new ArrayBuffer(0))
})

it('should read a simple block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(READ_SINGLE_SCRIPT), 0x00))
const result = await BusUtil.readI2cBlocks(ab, [[0, 2]])
expect(new Uint8Array(result)).to.deep.equal(new Uint8Array([3, 5]))
})

it('should read a multi block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(READ_MULTI_SCRIPT), 0x00))
const result = await BusUtil.readI2cBlocks(ab, [[0x37, 2], [0x42, 4]])
expect(new Uint8Array(result)).to.deep.equal(new Uint8Array([3, 5, 7, 9, 11, 13]))
})

it('should error when bus layer errors', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(READ_MULTI_SCRIPT), 0x00))
await expect(async () => { await BusUtil.readBlock(ab, [[0x37, 2], [0x42, 4], [0x77, 0]]) })
})
})

describe('#writeI2cBlocks', () => {
it('should write empty on empty block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(EOS_SCRIPT), 0x00))
await BusUtil.writeI2cBlocks(ab, [], new Uint8Array([]))
})

it('should write simple byte single block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(WRITE_SINGLE_SCRIPT), 0x00))
await BusUtil.writeI2cBlocks(ab, [[0x01, 2]], new Uint8Array([0, 3, 5]))
})

it('should write simple block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(WRITE_SINGLE_SCRIPT), 0x00))
await BusUtil.writeI2cBlocks(ab, [[0x01, 2]], new Uint8Array([0, 3, 5]))
})

it('should write multi block', async () => {
const ab = new I2CAddressedTransactionBus(I2CTransactionBus.from(await I2CScriptBus.openPromisified(WRITE_MULTI_SCRIPT), 0x00))
await BusUtil.writeI2cBlocks(ab, [[0x01, 2], [0x4, 4]], new Uint8Array([0, 3, 5, 0, 7, 9, 11, 13]))
})
})

/*
describe('#expandBlock', () => {
it('should pass most basic 1:1 test', () => {
expect(BusUtil.expandBlock([0], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([3]))
})
it('should pass most basic 1:1 test (offset)', () => {
expect(BusUtil.expandBlock([3], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 0xFE, 3]))
})
it('should pass basic example', () => {
const fill = -1
const firstPad = Buffer.alloc(10, fill)
const pre20Pad = Buffer.alloc(8, fill)
const pre30Pad = Buffer.alloc(9, fill)
const expected = Buffer.concat([firstPad, Buffer.from([3, 5]), pre20Pad, Buffer.from([7]), pre30Pad, Buffer.from([9, 11, 13])], 33)
expect(BusUtil.expandBlock([[10, 2], [20, 1], [30, 3]], Buffer.from([3, 5, 7, 9, 11, 13]), fill)).to.deep.equal(expected)
})
it('should pass basic example (collapsed)', () => {
const fill = -1
const firstPad = Buffer.alloc(10, fill)
const pre30Pad = Buffer.alloc(17, fill)
const expected = Buffer.concat([firstPad, Buffer.from([3, 5, 7]), pre30Pad, Buffer.from([9, 11, 13])], 33)
expect(BusUtil.expandBlock([[10, 2], [12, 1], [30, 3]], Buffer.from([3, 5, 7, 9, 11, 13]), fill)).to.deep.equal(expected)
})
it('should fill in the middle', () => {
expect(BusUtil.expandBlock([0, 2], Buffer.from([3, 5]), 0xFE, false)).to.deep.equal(Buffer.from([3, 0xFE, 5]))
})
it('should fill in front', () => {
expect(BusUtil.expandBlock([2], Buffer.from([3]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 3]))
})
it('should fill in both', () => {
expect(BusUtil.expandBlock([1, 4], Buffer.from([3, 5]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 3, 0xFE, 0xFE, 5]))
})
it('should handle multi-byte', () => {
expect(BusUtil.expandBlock([[0, 4], 4], Buffer.from([3, 5, 7, 9, 11]), 0xFE, false)).to.deep.equal(Buffer.from([3, 5, 7, 9, 11]))
})
it('should handle multi-byte padded', () => {
expect(BusUtil.expandBlock([[2, 4], 8], Buffer.from([3, 5, 7, 9, 11]), 0xFE, false)).to.deep.equal(Buffer.from([0xFE, 0xFE, 3, 5, 7, 9, 0xFE, 0xFE, 11]))
})
it('should error if input buffer length does not match BlockDefinition', () => {
expect(() => BusUtil.expandBlock([1, 2, 3], Buffer.alloc(5, 0), 0xFE, false)).to.throw(Error)
})
it('should match example used in hand coded test', () => {
expect(BusUtil.expandBlock([[0x01, 2], [0x4, 4]], Buffer.from([3, 5, 7, 9, 11, 13]))).to.deep.equal(Buffer.from([0, 3, 5, 0, 7, 9, 11, 13]))
})
})
*/
})

0 comments on commit b641307

Please sign in to comment.