diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 08fdb58..7ec6626 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,34 +1,14 @@ name: CI -on: [ push, pull_request] +on: [ push, pull_request ] jobs: Build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4.0.2 - - run: npm install - - run: npm run build --if-present - Lnt: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - - run: npm install - - run: npm run lint --if-present -- --quiet - Test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 - run: npm install - run: npm run build --if-present + - run: npm run lint --if-present -- --quiet - run: npm run test --if-present -- --quiet - Cover: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - - run: npm install - - run: npm run build --if-present - - run: npm run coverage --if-present -- --quiet \ No newline at end of file + - run: npm run coverage --if-present -- --quiet diff --git a/README.md b/README.md index d20ba4b..5178bf5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # MCP2221 -Full featured library MCP2221 (A) via USB HID for the Web and Node +Full featured MCP2221 library with WebHID and node support over StreamAPI 🥳 [![npm Version](http://img.shields.io/npm/v/@johntalton/mcp2221.svg)](https://www.npmjs.com/package/@johntalton/mcp2221) ![GitHub package.json version](https://img.shields.io/github/package-json/v/johntalton/mcp2221) @@ -14,16 +14,18 @@ Standard [Adafruit](https://www.adafruit.com/product/4471) link. - [Stream API](#stream-api) - [WebHID](#web-hid) - [Node-HID](#node-hid) -- [I²C](#ic) (raw) +- [I²C](#direct-api) (raw) - [`I2CBus`](#i2cbus-abstraction-recommended) +![i2c scan](./example/mcp2221-scan.png) # Features + Support full range of command and functionality, including: - Password Protected - Access Password setting - - new password Flash writes + - New password Flash writes - Alter Security settings - no guard against humans @@ -53,12 +55,13 @@ Support full range of command and functionality, including: # Example -The following example give the outline of the usage pattern for creating the binding layer between the underlying HID implementations and the chip library itself. + +The following example gives the outline of the usage pattern for creating the binding layer between the underlying HID implementations and this chip library. ```javascript -import { MCP2221A } from '@johntalton/mcp2221' +import { MCP2221 } from '@johntalton/mcp2221' -const hidDevice = {} /* likely navigator.hid.getDevices() ... etc */ +const hidDevice = { /* likely navigator.hid.getDevices() ... etc */ } const source = new HIDStreamSource(hidDevice) const chip = MCP2221.from(source) @@ -67,24 +70,21 @@ const { adc } = await chip.common.status() const { ch0, ch1, ch2 } = adc ``` - # Stream API -Individual HID implementations are abstrated over the [Stream API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) that supports BYOB (bring you own buffer) and Byte specific stream. - -As such, a `HIDStreamSource` is used to normalize the WebHID (`EventTarget`) API into a stream (as well as other concrete implementations). +Individual HID implementations are abstracted over the [Stream API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) that supports BYOB (bring you own buffer) and Byte specific stream. -A simplified stream reader is used which ignore (assumes zero) the report Id, which is correct in this case. +As such, the interface `HIDStreamSource` is used to normalize the sources into a stream. Two examples of Stream Sources are given bellow: WebHID and Node-HID. ## Web HID -the WebHID api provides a simplified HID implementation within the browser. +The [WebHID](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API) interface provides a robust browser based HID implementation ([browser support](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API#browser_compatibility)) -The example code above can replace the abstract `HIDStreamSource` with the concrete [`WebHIDStreamSource`]() +The example code above can be updated to use the the concrete `WebHIDStreamSource`. -The HID API has several way of acquiring a `HIDDevice`, most common is to make a request for existing connected devices via [`navigator.hid.getDevices()`](https://developer.mozilla.org/en-US/docs/Web/API/HID/getDevices). +The API has several way of acquiring a `HIDDevice`, most common is to make a request for existing connected devices via [`navigator.hid.getDevices()`](https://developer.mozilla.org/en-US/docs/Web/API/HID/getDevices). NOTE: an un-packaged version of WebHIDStraemSource can be found [here](https://github.com/johntalton/webapp-device-playground/blob/main/public/util/hid-stream.js). (future package publication may be forthcoming) @@ -116,21 +116,55 @@ await chip.sram.set({ gp: { interrupt: { clear: true } } }) # I²C +## `I2CBus` abstraction (recommended) + +Basic usage of such API can be useful in some instances. However, due to complexities and error checking logic, it is recommended to use the [`I2CBus`](https://github.com/johntalton/i2c-bus-mcp2221) abstraction layer + + +## Direct API + The MCP2221 exposes several low-level I²C constructs. +These can be used to build custom bus interactions + +Note: the chip's I²C state machine can hang / crash is care is not taken to check status and appropriately cancel transaction. `I2CBus` usage is encouraged, and may also be a good reference. + +The following example is simplification from the the above `I2CBus` implementation [code](https://github.com/johntalton/i2c-bus-mcp2221/blob/main/src/utils/read.ts). ```javascript -// chip from above examples +const chip // chip from base examples + +// request a read of length 3 bytes from address 0x70 (7-bit address) +const REQUESTED_I2C_BYTE_LENGTH = 3 -const { status } = await chip.i2c.readData() -if(status !== 'success') { throw new Error('not ok') } -const { validData, buffer } = await chip.i2c.readGetData({ - address: 0x79, - length: 1 +// start the request +const { status } = await chip.i2c.readData({ + address: 0x70, + length: REQUESTED_I2C_BYTE_LENGTH }) +if(status !== 'success') { throw new Error('☠️') } +// check i2c state and other transfer values for readiness +// +// it is almost sertian that a call to `status` is needed, the commands "success" status value is not sufficiant for checking the state of the bus +// const { status, i2cStateName, ... } = await device.common.status({ opaque }) +// if (__i2cStatusIsNotOk__) { throw new Error('😢') } -``` +// if that all went well then attempt to get the buffer +// here we allow the chip to allocate the buffer +// BYOB can be used here also for performance / efficiency +const { validData, buffer, readBackBytes } = await chip.i2c.readGetData() +if(!validData) { throw new Error('🧨') } +if(readBackBytes === REQUESTED_I2C_BYTE_LENGTH) { throw new Error('👎') } -## `I2CBus` abstraction (recommended) -Basic usage of such API can be useful in some instances. However, due to the complexities and error checking, it is recommended to sue the [`I2CBus`](https://github.com/johntalton/i2c-bus-mcp2221) abstraction layer +// process the data +// check if the returned buffer is a view and coheres it into a Uint8Array +const u8 = ArrayBuffer.isView(buffer) ? + new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength) : + new Uint8Array(buffer) + +// deconstruct TypedArray +const [ one, two, three ] = u8 + + +``` diff --git a/examples/chip.mjs b/examples/chip.mjs deleted file mode 100644 index 914d02e..0000000 --- a/examples/chip.mjs +++ /dev/null @@ -1,14 +0,0 @@ -import pkg from '@johntalton/mcp2221' -const { MCP2221 } = pkg - -const usb = { - read: async length => Uint8Array.from([]), - write: async buffer => 64 -} - -const chip = await MCP2221.openPromisified(usb) - -const res = await chip.common.status({ opaque: 'synthetic' }) - - -console.log(res) diff --git a/examples/libs/detect.mjs b/examples/libs/detect.mjs deleted file mode 100644 index 299d689..0000000 --- a/examples/libs/detect.mjs +++ /dev/null @@ -1,9 +0,0 @@ -import usbDetect from 'usb-detection' - -usbDetect.startMonitoring() - -usbDetect.on('change', device => { - console.log('change', device) -}) - -// usbDetect.startMonitoring() \ No newline at end of file diff --git a/examples/libs/usb.mjs b/examples/libs/usb.mjs deleted file mode 100644 index bc61bbd..0000000 --- a/examples/libs/usb.mjs +++ /dev/null @@ -1,37 +0,0 @@ -import usb from 'usb' - -function dump(device) { - device.interfaces.forEach(i => { - console.log('Interface', i.id) - i.endpoints.forEach(e => { - const type = e.transferType === usb.LIBUSB_TRANSFER_TYPE_BULK ? 'Bulk' : - e.transferType === usb.LIBUSB_TRANSFER_TYPE_CONTROL ? 'Control' : - e.transferType === usb.LIBUSB_TRANSFER_TYPE_INTERRUPT ? 'Interrupt' : - e.transferType === usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS ? 'Isochronous' : - e.transferType - console.log('\t', 'Endpoint:', e.address, 'direction', e.direction, 'type', type) - }) - }) -} - -usb.on('attach', device => console.log(device)) -usb.on('detach', device => console.log(device)) - -const intF = 1 - -usb.getDeviceList() - //.filter(d => d.idVendor === 0x04d8 && d.idProduct === 0x00dd) - .forEach(d => { - d.open() - d.interface(intF).claim() - dump(d) - - d.interface(intF).endpoint(2).transfer([ 0x10, 0x00, 0x10 ], (err) => { - console.log('here', err) - d.interface(intF).endpoint(130).transfer(1, (err, data) => { - console.log('there', err, data) - }) - }) - - //d.interface(1).release(err => {}) - }) \ No newline at end of file diff --git a/examples/libs/webusb.js b/examples/libs/webusb.js deleted file mode 100644 index 46a7bb6..0000000 --- a/examples/libs/webusb.js +++ /dev/null @@ -1,130 +0,0 @@ -import webusb from 'webusb' -const { USB } = webusb - -import { MCP2221 } from '@johntalton/mcp2221' - -function dumpConfig(device) { - console.log(device.manufacturerName) - console.log(device.productName) - console.log(device.serialNumber) - console.log() - device.configurations.forEach(c => { - const defaultConfig = c === device.configuration ? '*' : '' - console.log(defaultConfig, 'Config: ', c.configurationValue, c.configurationName) - c.interfaces.forEach(i => { - const defaultInt = i === c.interface ? '*' : '' - console.log('\t', defaultInt, i.claimed ? '(' : '', 'Interface', i.claimed ? '): ' : ': ', i.interfaceNumber) - i.alternates.forEach(a => { - const defaultAlt = a === i.alternate ? '*' : '' - const aClass = a.interfaceClass === 10 ? '"CDC-Data"' : - a.interfaceClass === 2 ? '"Com / CDC-Ctrl"' : - a.interfaceClass === 3 ? '"HID"' : - a.interfaceClass - console.log('\t\t', defaultAlt, 'Alternate: ', a.interfaceName, 'class', aClass, 'protocol', a.interfaceProtocol) - a.endpoints.forEach(e => { - const defaultEp = e === a.endpoint ? '*' : '' - console.log('\t\t\t', defaultEp, 'Endpoint: ', e.endpointNumber, e.direction, e.type) - }) - }) - }) - }) -} - -async function setupMcp2221(usb, device) { - - await device.open() - - const cfg = { - configuration: 1, - interface: 1, - endpoint: 2 - } - - if (device.configuration === null) { - console.log('unselected configuration, selecting') - await device.selectConfiguration(cfg.configuration); - } - - console.log('claim interface ', cfg.interface) - await device.claimInterface(cfg.interface); - - dumpConfig(device) - - const buffer = Uint8Array.from([ 0x10, 0, 0x20 ]) - - const report_number = 1 - const foo = await device.controlTransferOut({ - requestType: 'class', - recipient: 'endpoint', - request: 0x09, - value: (2 << 8) | report_number, - index: 2 - }) - console.log('controlTransferOut', foo) - - - const out = await device.transferOut(2, buffer) - console.log('transferOut', out) - - const result = await device.transferIn(2, 64) - console.log('transferIn', result) - - const mcp = await MCP2221.openPromisified(usb) - return mcp -} - -async function teardownMcp2221(device) { - -} - -async function requestMcp2221(usb) { - try { - return await usb.requestDevice({ - filters: [ { vendorId: 0x04d8, productId: 0x00dd } ] - }) - } - catch(e) { - return; - } -} - -async function onConnect(e) { - console.log('USB CONNECTION', e.device.productName) - const { usbDevice: device } = e - - if(usbDevice.vendorId !== 1240 || usbDevice.productId !== 221) { - const device = await requestMcp2221(usb) - if(device === undefined) { console.log('request on connect failed'); return } - const mcp = await setupMcp2221(device) - } -} - -async function onDisconnect(e) { - console.log('USB DISCONNECT', e.device.productName) - const { device } = e - - if(device.vendorId !== 1240 || device.productId !== 221) { - await teardownMcp2221(device) - } -} - -async function main() { - const usb = new USB({ devicesFound: async d => { - //console.log('deviceFound', d); - return d[0] - } - }) - - const device = await requestMcp2221(usb) - if(device === undefined) { console.log('no device found'); return; } - const mcp = await setupMcp2221(usb, device) - - // start watching - console.log('Watching Connect and Disconnect') - usb.onconnect = onConnect - usb.ondisconnect = onDisconnect - - setTimeout(() => usb.removeAllListeners(), 10 * 1000) -} - -main() \ No newline at end of file diff --git a/examples/mcp2221-descriptors.png b/examples/mcp2221-descriptors.png new file mode 100644 index 0000000..a12031b Binary files /dev/null and b/examples/mcp2221-descriptors.png differ diff --git a/examples/mcp2221-gpio-config.png b/examples/mcp2221-gpio-config.png new file mode 100644 index 0000000..6af7bdd Binary files /dev/null and b/examples/mcp2221-gpio-config.png differ diff --git a/examples/mcp2221-no-password-error.png b/examples/mcp2221-no-password-error.png new file mode 100644 index 0000000..029dabd Binary files /dev/null and b/examples/mcp2221-no-password-error.png differ diff --git a/examples/mcp2221-scan.png b/examples/mcp2221-scan.png new file mode 100644 index 0000000..5a88023 Binary files /dev/null and b/examples/mcp2221-scan.png differ diff --git a/examples/mcp2221-status-after-speed-set.png b/examples/mcp2221-status-after-speed-set.png new file mode 100644 index 0000000..d16a1f2 Binary files /dev/null and b/examples/mcp2221-status-after-speed-set.png differ diff --git a/examples/uart.js b/examples/uart.js deleted file mode 100644 index 2750925..0000000 --- a/examples/uart.js +++ /dev/null @@ -1,60 +0,0 @@ -// sreen -U /dev/tty.usbmodem14401 115200 - -const stream = require('stream/promises') - -//const SerialPort = require('serialport') -const SerialPort = require('@serialport/stream') -const ByteLength = require('@serialport/parser-byte-length') -const Bindings = require('@serialport/bindings') - -SerialPort.Binding = Bindings - -async function openDevices() { - const portList = await SerialPort.list() - return portList - .filter(p => p.vendorId === '04d8' && p.productId === '00dd') - .map(item => ({ - ...item, - port: new SerialPort(item.path, { - baudRate: 115200, - dataBits: 8, - stopBits: 1, - parity: 'none', - autoOpen: false }) - })) - // .map(item => ({ - // ...item, - // parser: item.port.pipe(new ByteLength({ length: 2 })) - // })) -} - -async function main() { - const devices = await openDevices() - - devices.forEach(d => { - - //d.parser.on('data', chunk => console.log('chunk', chunk)) - - d.port.on('open', () => { - console.log('----- OPEN', d.path) - - d.port.write(Buffer.from([ - - ]), 'ascii', async err => { - if(err) { console.log('WRITE ERROR', err); return } - console.log('WRITE SUCCESS') - - }) - - setTimeout(() => { - d.port.close() - }, 5 * 1000) - }) - d.port.on('error', () => console.log('----- ERROR')) - d.port.on('close', () => console.log('----- CLOSE')) - - d.port.open() - }) -} - -main() \ No newline at end of file diff --git a/package.json b/package.json index 7ebabb9..254b547 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@johntalton/mcp2221", - "version": "4.0.0", + "version": "4.0.1", "exports": { ".": "./lib/index.js", "./bindings": "./lib/index.bindings.js", @@ -19,8 +19,7 @@ "build:watch": "tsc -p . -w" }, "license": "MIT", - "dependencies": { - "@johntalton/bitsmush": "^1.0.1", + "devDependencies": { "typescript": "^5.5.2" } } diff --git a/test/chip.chip.spec.js b/test/chip.chip.spec.js deleted file mode 100644 index c0ecf5f..0000000 --- a/test/chip.chip.spec.js +++ /dev/null @@ -1,18 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -import { MCP2221A } from '@johntalton/mcp2221' - -describe('MCP2221A', () => { - describe('constructor', () => { - it('succeeds (new)', () => { - const binding = undefined - new MCP2221A(binding) - }) - - it('succeeds (from)', () => { - const binding = undefined - MCP2221A.from(binding) - }) - }) -}) diff --git a/test/chip.common.spec.js b/test/chip.common.spec.js deleted file mode 100644 index 0f57be6..0000000 --- a/test/chip.common.spec.js +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -// import { StatusParametersResponse } from '../src' -import { MCP2221Common } from '@johntalton/mcp2221/bindings' - -const RESPONSE_COMMON_STATUS = Uint8Array.from([ 0x10, 0x00, 0x10 ] - .concat(Array.from({ length: 43 }) - .concat([ - 'A'.codePointAt(0), - '6'.codePointAt(0), - '1'.codePointAt(0), - '2'.codePointAt(0) - ]) - .concat(Array.from({ length: 6 })))) - .buffer - - - -describe('MCP2221Common', () => { - describe('constructor', () => { - it('succeeds', () => { - const binding = undefined - const common = new MCP2221Common(binding) - expect(common).to.not.be.undefined - expect(common).to.be.instanceOf(MCP2221Common) - }) - }) - - describe('status', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_COMMON_STATUS, write: async buffer => 64 } - const common = new MCP2221Common(binding) - const res = await common.status({ opaque: '', command: 0x10 }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - describe('reset', () => { - it('succeeds', async () => { - const binding = { read: async length => new ArrayBuffer(length), write: async buffer => 64 } - const common = new MCP2221Common(binding) - const res = await common.reset({ opaque: '', magic: [171, 205, 239] }) - expect(res).to.be.undefined - }) - }) -}) diff --git a/test/chip.flash.spec.js b/test/chip.flash.spec.js deleted file mode 100644 index a2d3f98..0000000 --- a/test/chip.flash.spec.js +++ /dev/null @@ -1,119 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -// import { StatusParametersResponse } from '../src' -import { MCP2221Flash } from '@johntalton/mcp2221/bindings' - -const RESPONSE_FLASH_WRITE = Uint8Array.from( - [] - .concat(Array.from({ length: 64 })) -).buffer - -const RESPONSE_FLASH_SET_PASSWORD = Uint8Array.from( - [ 0xB2, 0x00 ] - .concat(Array.from({ length: 62 })) -).buffer - -const RESPONSE_FLASH_SET_PASSWORD_NOT_ALLOWED = Uint8Array.from( - [ 0xB2, 0x03 ] - .concat(Array.from({ length: 62 })) -).buffer - -const RESPONSE_FLASH_READ_CHIP_SETTINGS = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_GP_SETTINGS = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_USB_MANUFACTURER = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_USB_PRODUCT = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_USB_SERIAL_NUMBER = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_FACTORY_SERIAL_NUMBER = Uint8Array.from([ 0xB0, 0x00 ].concat(Array.from({ length: 62 }))).buffer -const RESPONSE_FLASH_READ_UNSUPPORTED = Uint8Array.from([ 0xB0, 0x01 ].concat(Array.from({ length: 62 }))).buffer - - -describe('MCP2221Flash', () => { - describe('constructor', () => { - it('succeeds', () => { - const binding = undefined - const common = new MCP2221Flash(binding) - expect(common).to.not.be.undefined - expect(common).to.be.instanceOf(MCP2221Flash) - }) - }) - - xdescribe('read', () => { - it('should read chip settings', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_CHIP_SETTINGS, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read gp settings', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_GP_SETTINGS, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read usb manufacturer', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_USB_MANUFACTURER, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read usb product', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_USB_PRODUCT, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read usb sn', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_USB_SERIAL_NUMBER, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read factory sn', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_FACTORY_SERIAL_NUMBER, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - - it('should read unsupported', async () => { - const binding = { read: async length => RESPONSE_FLASH_READ_UNSUPPORTED, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.read({ }) - expect(res).to.not.be.undefined - }) - }) - - xdescribe('write', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_FLASH_WRITE, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.write({ }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - xdescribe('sendPassword', () => { - it('should send password successfully', async () => { - const binding = { read: async length => RESPONSE_FLASH_SET_PASSWORD, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.sendPassword({ }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - - it('should send password not allowed', async () => { - const binding = { read: async length => RESPONSE_FLASH_SET_PASSWORD_NOT_ALLOWED, write: async buffer => 64 } - const flash = new MCP2221Flash(binding) - const res = await flash.sendPassword({ }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) -}) diff --git a/test/chip.gpio.spec.js b/test/chip.gpio.spec.js deleted file mode 100644 index 1a22802..0000000 --- a/test/chip.gpio.spec.js +++ /dev/null @@ -1,40 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -// import { StatusParametersResponse } from '../src' -import { MCP2221Gpio } from '@johntalton/mcp2221/bindings' - -const RESPONSE_GPIO_GET = Uint8Array.from([ 0x00 ]) - .buffer - - - -describe('MCP2221Gpio', () => { - describe('constructor', () => { - it('succeeds', () => { - const binding = undefined - const common = new MCP2221Gpio(binding) - expect(common).to.not.be.undefined - expect(common).to.be.instanceOf(MCP2221Gpio) - }) - }) - - xdescribe('get', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_GPIO_GET, write: async buffer => 64 } - const gpio = new MCP2221Gpio(binding) - const res = await gpio.get({ }) - expect(res).to.be.undefined - }) - }) - - xdescribe('set', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_GPIO_SET, write: async buffer => 64 } - const gpio = new MCP2221Gpio(binding) - const res = await common.set({ }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) -}) diff --git a/test/chip.i2c.spec.js b/test/chip.i2c.spec.js deleted file mode 100644 index b11dcd1..0000000 --- a/test/chip.i2c.spec.js +++ /dev/null @@ -1,128 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -// import { StatusParametersResponse } from '../src' -import { MCP2221I2C } from '@johntalton/mcp2221/bindings' - -const RESPONSE_I2C_ = Uint8Array.from([ 0 ].concat(Array.from({ length: 64 }))).buffer - -const RESPONSE_I2C_WRITE_DATA = Uint8Array.from( - [ 0x90, 0x00 ] - .concat(Array.from({ length: 62 })) -).buffer - -const RESPONSE_I2C_WRITE_DATA_BUSY = Uint8Array.from( - [ 0x90, 0x01 ] - .concat(Array.from({ length: 62 })) -).buffer - -const RESPONSE_I2C_WRITE_NO_STOP = Uint8Array.from( - [ 0x94, 0x01 ] - .concat(Array.from({ length: 62 })) -).buffer - - - - - -describe('MCP2221I2C', () => { - describe('constructor', () => { - it('succeeds', () => { - const binding = undefined - const common = new MCP2221I2C(binding) - expect(common).to.not.be.undefined - expect(common).to.be.instanceOf(MCP2221I2C) - }) - }) - - describe('writeData', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_WRITE_DATA, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.writeData({ - address: 0x00, - buffer: new ArrayBuffer(13) - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - - it('succeeds data busy', async () => { - const binding = { read: async length => RESPONSE_I2C_WRITE_DATA_BUSY, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.writeData({ - address: 0x00, - buffer: new ArrayBuffer(13) - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - xdescribe('writeRepeatedSTART', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.writeRepeatedSTART({ - address: 0x00, - buffer: new ArrayBuffer(13) - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - describe('writeNoSTOP', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_WRITE_NO_STOP, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.writeNoSTOP({ - address: 0x00, - buffer: new ArrayBuffer(13) - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - xdescribe('readData', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.readData({ - address: 0x00, - length: 13 - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - - xdescribe('readRepeatedSTART', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.readRepeatedSTART({ - address: 0x00, - length: 13 - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - xdescribe('readGetData', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_I2C_, write: async buffer => 64 } - const i2c = new MCP2221I2C(binding) - const res = await i2c.readGetData({ - address: 0x00 - }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - -}) diff --git a/test/chip.sram.spec.js b/test/chip.sram.spec.js deleted file mode 100644 index a148f38..0000000 --- a/test/chip.sram.spec.js +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -// import { StatusParametersResponse } from '../src' -import { MCP2221SRAM } from '@johntalton/mcp2221/bindings' - -const RESPONSE_SRAM_GET = Uint8Array.from([ 0x10, 0x00, 0x10 ] - .concat(Array.from({ length: 43 }) - .concat([ - 'A'.codePointAt(0), - '6'.codePointAt(0), - '1'.codePointAt(0), - '2'.codePointAt(0) - ]) - .concat(Array.from({ length: 6 })))) - .buffer - - - -describe('MCP2221SRAM', () => { - describe('constructor', () => { - it('succeeds', () => { - const binding = undefined - const sram = new MCP2221SRAM(binding) - expect(sram).to.not.be.undefined - expect(sram).to.be.instanceOf(MCP2221SRAM) - }) - }) - - xdescribe('get', () => { - it('succeeds', async () => { - const binding = { read: async length => RESPONSE_SRAM_GET, write: async buffer => 64 } - const sram = new MCP2221SRAM(binding) - const res = await sram.get({ }) - expect(res).to.not.be.undefined - expect(res).to.be.exist - }) - }) - - xdescribe('set', () => { - it('succeeds', async () => { - const binding = { read: async length => new ArrayBuffer(length), write: async buffer => 64 } - const sram = new MCP2221SRAM(binding) - const res = await sram.set({ }) - expect(res).to.be.undefined - }) - }) -}) diff --git a/test/converter.spec.js b/test/converter.spec.js deleted file mode 100644 index 3dc9f1d..0000000 --- a/test/converter.spec.js +++ /dev/null @@ -1,299 +0,0 @@ - -import { describe, it } from 'mocha' -import { expect } from 'chai' - -import { - StatusParametersRequestCoder, StatusParametersResponseCoder, - ResetChipRequestCoder, ResetChipResponseCoder, - // ReadFlashDataRequestCoder, ReadFlashDataResponseCoder, - // WriteFlashDataRequestCoder, WriteFlashDataResponseCoder, - SendFlashAccessPasswordRequestCoder, SendFlashAccessPasswordResponseCoder, - GetGPIOValuesRequestCoder, GetGPIOValuesResponseCoder, - SetGPIOOutputValuesRequestCoder, SetGPIOOutputValuesResponseCoder, - I2CReadDataRequestCoder, I2CReadDataResponseCoder, - I2CReadDataRepeatedSTARTRequestCoder, I2CReadDataRepeatedSTARTResponseCoder, - I2CReadGetDataRequestCoder, I2CReadGetDataResponseCoder, - I2CWriteDataNoSTOPRequestCoder, I2CWriteDataNoSTOPResponseCoder, - I2CWriteDataRequestCoder, I2CWriteDataResponseCoder, - I2CWriteDataRepeatedSTARTRequestCoder, I2CWriteDataRepeatedSTARTResponseCoder, - GetSRAMSettingsRequestCoder, GetSRAMSettingsResponseCoder, - SetSRAMSettingsRequestCoder, SetSRAMSettingsResponseCoder -} from '@johntalton/mcp2221/coders' -// import { Coder } from '../src/converter/converter' -// import { Message } from '../src/messages/message' - -/* -type Test = { - coder: Coder - buffer: ArrayBuffer - message: T -}*/ - -function padZeros(count) { - return new Array(count).fill(0) -} - -const USB_MAJ_MIN = [ - 'A'.charCodeAt(0), - '6'.charCodeAt(0), - '1'.charCodeAt(0), - '1'.charCodeAt(0) -] - -describe('Converter', () => { - describe('encode & decode', () => { - const matrix /* :Array> */ = [ - // -// { coder: StatusParametersRequestCoder, buffer: Uint8Array.from([ 16, 0, 0, 0, 0 ]), message: { opaque: '' } }, - // { coder: StatusParametersRequestCoder, buffer: Uint8Array.from([ 16, 0, 0x10, 0, 0 ]), message: { opaque: '', cancelI2c: true } }, - { coder: StatusParametersResponseCoder, buffer: Uint8Array.from([ 16, 0, ...padZeros(44), ...USB_MAJ_MIN, ...padZeros(20) ]), message: - { - opaque: '__kinda_mostly_close__', - status: 'success', statusCode: 0, - command: 0x10, - interruptEdgeDetectorState: false, - - i2cState: 0, - i2cStateName: 'IDLE', - - i2cCancelled: false, - i2cClock: undefined, - - setSpeedByte: 0, - setSpeedRequested: false, - setSpeedSuccessfull: false, - speedDividerByte: 0, - - adc: { - ch0: 0, - ch1: 0, - ch2: 0 - }, - i2c: { - ACKed: true, - SCL: 0, - SDA: 0, - address: 0, - communicationSpeedDivider: 0, - dataBufferCounter: 0, - pendingValue: 0, - requestedTransferLength: 0, - timeoutMs: 0, - transferredBytes: 0, - }, - - revision: { firmware: { major: '1', minor: '1' }, hardware: { major: 'A', minor: '6'} } - } - }, - // - // { coder: ResetChipRequestCoder, buffer: Uint8Array.from([ 0x70, 0xAB, 0xCD, 0xEF ]), message: { opaque: '' } }, - // { coder: ResetChipResponseCoder, buffer: Uint8Array.from([ ]), message: undefined }, - // - // { coder: ReadFlashDataRequestCoder, buffer: Uint8Array.from([ 0xB0, 0x00 ]), message: { opaque: '' } }, - // { coder: ReadFlashDataRequestCoder, buffer: Uint8Array.from([ 0xB0, 0x00 ]), message: { opaque: '' } }, - // { coder: ReadFlashDataResponseCoder, buffer: Uint8Array.from([ 0xB0 ]), message: { opaque: '__invalid__' } }, - // - // { coder: WriteFlashDataRequestCoder, buffer: Uint8Array.from([ 0xB1 ]), message: { opaque: '' } }, - // { coder: WriteFlashDataResponseCoder, buffer: Uint8Array.from([ 0xB1 ]), message: { opaque: '__invalid__' } }, - // - // { coder: SendFlashAccessPasswordRequestCoder, buffer: Uint8Array.from([ 0xB2 ]), message: { opaque: '' } }, - // { coder: SendFlashAccessPasswordResponseCoder, buffer: Uint8Array.from([ 0xB2 ]), message: { opaque: '__invalid__' } }, - // - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesRequestCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '' } }, - // { coder: GetGPIOValuesResponseCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '__invalid__' } }, - // { coder: GetGPIOValuesResponseCoder, buffer: Uint8Array.from([ 0x51 ]), message: { opaque: '__invalid__' } }, - // - // { coder: SetGPIOOutputValuesRequestCoder, buffer: Uint8Array.from([ 0x50 ]), message: { opaque: '' } }, - // { coder: SetGPIOOutputValuesResponseCoder, buffer: Uint8Array.from([ 0x50, 0 ]), message: { opaque: '__invalid__' } }, - // - // { coder: I2CWriteDataRequestCoder, buffer: Uint8Array.from([ 0x90 ]), message: { opaque: '' } }, - { coder: I2CWriteDataResponseCoder, buffer: Uint8Array.from([ 0x90, 0, 0 ]), message: { - command: 0x90, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - } }, - // - // { coder: I2CWriteDataRepeatedSTARTRequestCoder, buffer: Uint8Array.from([ 0x92 ]), message: { opaque: '' } }, - { coder: I2CWriteDataRepeatedSTARTResponseCoder, buffer: Uint8Array.from([ 0x92, 0, 0 ]), message: { - command: 0x92, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - } }, - // - // { coder: I2CWriteDataNoSTOPRequestCoder, buffer: Uint8Array.from([ 0x94 ]), message: { opaque: '' } }, - { coder: I2CWriteDataNoSTOPResponseCoder, buffer: Uint8Array.from([ 0x94, 0, 0 ]), message: { - command: 0x94, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - } }, - // - // { coder: I2CReadDataRequestCoder, buffer: Uint8Array.from([ 0x91 ]), message: { opaque: '' } }, - { coder: I2CReadDataResponseCoder, buffer: Uint8Array.from([ 0x91, 0, 0 ]), message: { - command: 0x91, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - } }, - // -// { coder: I2CReadDataRepeatedSTARTRequestCoder, buffer: Uint8Array.from([ 0x93 ]), message: { opaque: '' } }, - { coder: I2CReadDataRepeatedSTARTResponseCoder, - buffer: Uint8Array.from([ 0x93, 0, 0 ]), - message: { - command: 0x93, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - } - }, - // -// { coder: I2CReadGetDataRequestCoder, buffer: Uint8Array.from([ 0x40 ]), message: { opaque: '' } }, - { coder: I2CReadGetDataResponseCoder, - buffer: Uint8Array.from([ 0x40, 0, 0, 0 ]), - message: { - command: 0x40, - statusCode: 0, - status: 'success', - opaque: '__transparent_from_decoder__', - - i2cState: 0, - i2cStateName: 'IDLE', - readBackBytes: 0, - - validData: true, - buffer: Uint8Array.from([]).buffer - } - }, - // - // { coder: GetSRAMSettingsRequestCoder, buffer: Uint8Array.from([ 0x61 ]), message: { opaque: '' } }, - { coder: GetSRAMSettingsResponseCoder, - buffer: Uint8Array.from([ 0x61, 0, 18, 4, 0, 0b111, ...padZeros(20) ]), - message: { - command: 0x61, - opaque: '__here_we_go__', - status: 'success', - statusCode: 0, - password: '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000', - - chip: { - SSPND: 'off', - USBCFG: 'off', - enabledCDCSerialEnumeration: false, - i2cLED: 'off', - security: 'unsecured', - uartLED: { - rx: 'off', - tx: 'off' - } - }, - - gp: { - adc: { - referenceOptions: 'Vdd', - referenceVoltage: 'Off' - }, - clock: { - divider: '375 kHz', - dutyCycle: '0%' - }, - dac: { - initialValue: 0, - referenceOptions: 'Vdd', - referenceVoltage: 'Off' - }, - interrupt: { - edge: 'off' - } - }, - - gpio0: { - designation: 'Gpio', - direction: 'out', - outputValue: 0 - }, - gpio1: { - designation: 'Gpio', - direction: 'out', - outputValue: 0 - }, - gpio2: { - designation: 'Gpio', - direction: 'out', - outputValue: 0 - }, - gpio3: { - designation: 'Gpio', - direction: 'out', - outputValue: 0 - }, - - usb: { - mARequested: 0, - powerAttribute: 0, - productId: 0, - vendorId: 0 - } - } - }, - // - // { coder: SetSRAMSettingsRequestCoder, buffer: Uint8Array.from([ 0x60, 1 ]), message: { opaque: '' } }, - { coder: SetSRAMSettingsResponseCoder, buffer: Uint8Array.from([ 0x60, 0 ]), message: - { - command: 0x60, - statusCode: 0, - opaque: '__response_from_set__', - status: 'success' - } - } - ] - - matrix.forEach((test, i) => { - const { coder, buffer, message } = test - - it('encode #' + i, async function() { - try { - const result = coder.encode(message) - expect(new Uint8Array(result)).to.deep.equal(buffer) - } - catch(e) { - if(e.message === 'unused') { this.skip() } - throw e - } - }) - - it('decode #' + i, async function() { - try { - const msg = coder.decode(buffer) - expect(msg).to.deep.equal(message) - } - catch(e) { - if(e.message === 'unused') { this.skip() } - throw e - } - }) - }) - - }) -}) \ No newline at end of file diff --git a/test/interface.spec.ts.skip b/test/interface.spec.ts.skip deleted file mode 100644 index 0f939f0..0000000 --- a/test/interface.spec.ts.skip +++ /dev/null @@ -1,378 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -import { - Common, Flash, Gpio, I2C, SRAM -} from '@johntalton/mcp2221' - - -describe('Interface', () => { - describe('Common', () => { - describe('status', () => { - it('conforms', async () => { - const common: Common = { - status: req => Promise.resolve({ - opaque: '', - command: 0x10, - status: 'success', - statusCode: 0x00, - - i2c: { - address: 0x00, - requestedTransferLength: 0, - - transferredBytes: 0, - dataBufferCounter: 0, - communicationSpeedDivider: 0, - timeoutMs: 0, - - SCL: 0, - SDA: 0, - - pendingValue: 0 - }, - adc: { - ch0: 0, - ch1: 0, - ch2: 0 - }, - interruptEdgeDetectorState: 0, - - revision: { - hardware: { major: 'A', minor: '6' }, - firmware: { major: '1', minor: '1' } - } - }), - reset: () => Promise.resolve() - } - - const res = await common.status({ opaque: '', command: 0x10 }) - }) - }) - - describe('reset', () => { - it('conforms', async () => { - const common: Common = { - status: undefined, - reset: req => Promise.resolve() - } - - const res = await common.reset({ opaque: '' }) - }) - }) - }) - - describe('Flash', () => { - describe('read', () => { - it('conforms', async () => { - const flash: Flash = { - read: async req => ({ - opaque: '', - command: 0xB0, - subCommand: 0x02, - - status: 'success', - statusCode: 0x00, - - descriptor: 'this is it' - }), - write: undefined, - sendPassword: undefined - } - - const res = await flash.read({ opaque: '', command: 0xb0, subCommand: 0x02 }) - }) - }) - - describe('write', () => { - it('conforms', async () => { - const flash: Flash = { - read: undefined, - write: req => Promise.resolve({ - opaque: '', - command: 0xb1, - status: 'success', - statusCode: 0x00 - }), - sendPassword: undefined - } - - const res = await flash.write({ opaque: '', command: 0xb1, subCommand: 0x00 }) - }) - }) - - describe('sendPassword', () => { - it('conforms', async () => { - const flash: Flash = { - read: undefined, - write: undefined, - sendPassword: req => Promise.resolve({ - opaque: '', - command: 0xb2, - status: 'success', - statusCode: 0x00 - }) - } - - const res = await flash.sendPassword({ opaque: '', command: 0xb2, password: 'Secret!' }) - }) - }) - }) - - describe('Gpio', () => { - describe('set', () => { - it('conforms', async () => { - const gpio: Gpio = { - set: req => Promise.resolve({ - opaque: '', - command: 0x50, - status: 'success', - statusCode: 0x00 - }), - get: undefined - } - - const res = await gpio.set({ opaque: '', command: 0x50 }) - }) - }) - - describe('get', () => { - it('conforms', async () => { - const gpio: Gpio = { - set: undefined, - get: req => Promise.resolve({ - opaque: '', - command: 0x51, - status: 'success', - statusCode: 0x00 - }) - } - - const res = await gpio.get({ opaque: '', command: 0x51 }) - }) - }) - }) - - describe('I²C', () => { - describe('writeData', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: req => Promise.resolve({ - opaque: '', - command: 0x90, - address: 0xFF, - status: 'success', - statusCode: 0x00 - }), - writeRepeatedSTART: undefined, - writeNoSTOP: undefined, - readData: undefined, - readRepeatedSTART: undefined, - readGetData: undefined - } - - const res = await i2c.writeData({ opaque: '', command: 0x90, address: 0xFF, buffer: new Uint8Array() }) - }) - }) - - describe('writeRepeatedSTART', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: undefined, - writeRepeatedSTART: req => Promise.resolve({ - opaque: '', - command: 0x92, - address: 0xFF, - status: 'success', - statusCode: 0x00 - }), - writeNoSTOP: undefined, - readData: undefined, - readRepeatedSTART: undefined, - readGetData: undefined - } - - const res = await i2c.writeRepeatedSTART({ opaque: '', command: 0x92, address: 0xFF, buffer: new Uint8Array() }) - }) - }) - - describe('writeNoSTOP', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: undefined, - writeRepeatedSTART: undefined, - writeNoSTOP: req => Promise.resolve({ - opaque: '', - command: 0x94, - address: 0xFF, - status: 'success', - statusCode: 0x00 - }), - readData: undefined, - readRepeatedSTART: undefined, - readGetData: undefined - } - - const res = await i2c.writeNoSTOP({ opaque: '', command: 0x94, address: 0xFF, buffer: new Uint8Array() }) - }) - }) - - describe('readData', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: undefined, - writeRepeatedSTART: undefined, - writeNoSTOP: undefined, - readData: req => Promise.resolve({ - opaque: '', - command: 0x91, - address: 0xFF, - status: 'success', - statusCode: 0x00, - - buffer: new Uint8Array(req.length) - }), - readRepeatedSTART: undefined, - readGetData: undefined - } - - const res = await i2c.readData({ opaque: '', command: 0x91, address: 0xFF, length: 0 }) - }) - }) - - describe('readRepeatedSTART', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: undefined, - writeRepeatedSTART: undefined, - writeNoSTOP: undefined, - readData: undefined, - readRepeatedSTART: req => Promise.resolve({ - opaque: '', - command: 0x93, - address: 0xFF, - status: 'success', - statusCode: 0x00, - - buffer: new Uint8Array(req.length) - }), - readGetData: undefined - } - - const res = await i2c.readRepeatedSTART({ opaque: '', command: 0x93, address: 0xFF, length: 0 }) - }) - }) - - describe('readGetData', () => { - it('conforms', async () => { - const i2c: I2C = { - writeData: undefined, - writeRepeatedSTART: undefined, - writeNoSTOP: undefined, - readData: undefined, - readRepeatedSTART: undefined, - readGetData: req => Promise.resolve({ - opaque: '', - command: 0x40, - address: 0xFF, - status: 'success', - statusCode: 0x00 - }) - } - - const res = await i2c.readGetData({ opaque: '', command: 0x40, address: 0xFF }) - }) - }) - }) - - describe('SRAM', () => { - describe('set', () => { - it('conforms', async () => { - const sram: SRAM = { - set: req => Promise.resolve({ - opaque: '', - command: 0x60, - status: 'success', - statusCode: 0x00 - }), - get: undefined - } - - const res = await sram.set({ opaque: '', command: 0x60, gp: {} }) - }) - }) - - describe('get', () => { - it('conforms', async () => { - const sram: SRAM = { - set: undefined, - get: req => Promise.resolve({ - opaque: '', - command: 0x61, - status: 'success', - statusCode: 0x00, - - usb: { - vendorId: 0, - productId: 0, - powerAttribute: 0, - mARequested: 0 - }, - - chip: { - enabledCDCSerialEnumeration: false, - - uartLED: { tx: false, rx: false }, - i2cLED: false, - SSPND: false, - USBCFG: false, - - security: 'unsecured' - }, - - password: 'passWrd', - - gp: { - clockDivider: 0, - dac: { - referenceVoltage: 'Off', - referenceOptions: 'Vdd', - initialValue: 1 - }, - adc: { - referenceVoltage: 'Off', - referenceOptions: 'Vdd' - }, - interrupt: { - edge: 'both' - }, - }, - - gpio0: { - outputValue: 1, - direction: 'in', - designation: 'Gpio' - }, - gpio1: { - outputValue: 1, - direction: 'in', - designation: 'Gpio' - }, - gpio2: { - outputValue: 1, - direction: 'in', - designation: 'Gpio' - }, - gpio3: { - outputValue: 1, - direction: 'in', - designation: 'Gpio' - } - }) - } - - const res = await sram.get({ opaque: '', command: 0x61 }) - }) - }) - }) -}) \ No newline at end of file diff --git a/test/message.spec.ts.skip b/test/message.spec.ts.skip deleted file mode 100644 index 5ede561..0000000 --- a/test/message.spec.ts.skip +++ /dev/null @@ -1,169 +0,0 @@ -import { describe, it } from 'mocha' -import { expect } from 'chai' - -import { - StatusParametersRequest, ResetChipRequest, - GetGPIOValuesRequest, SetGPIOOutputValuesRequest, - I2CWriteDataRequest, I2CWriteDataRepeatedSTARTRequest, I2CWriteDataNoSTOPRequest, - I2CReadDataRequest, I2CReadDataRepeatedSTARTRequest, I2CReadGetDataRequest, - GetSRAMSettingsRequest, SetSRAMSettingsRequest, - ReadFlashDataRequest, WriteFlashDataRequest, SendFlashAccessPasswordRequest -} from '@johntalton/mcp2221' - -describe('Message', () => { - describe('Request', () => { - describe('Common', () => { - it('type StatusParametersRequest', () => { - const foo: StatusParametersRequest = { - opaque: '', - command: 0x10 - } - expect(foo).to.exist - }) - - it('type ResetChipRequest', () => { - const foo: ResetChipRequest = { - ...{ magic: [ 0xAB, 0xCD, 0xEF ] }, - opaque: '', - command: 0x70 - } - expect(foo).to.exist - }) - }) - - describe('Gpio', () => { - it('type SetGPIOOutputValuesRequest', () => { - const foo: SetGPIOOutputValuesRequest = { - opaque: '', - command: 0x50 - } - expect(foo).to.exist - }) - - it('type GetGPIOValuesRequest', () => { - const foo: GetGPIOValuesRequest = { - opaque: '', - command: 0x51 - } - expect(foo).to.exist - }) - }) - - describe('I²C', () => { - it('type I2CWriteDataRequest', () => { - const foo: I2CWriteDataRequest = { - opaque: '', - command: 0x90, - - address: 0x00, - buffer: Uint8Array.from([ 3, 5, 7 ]) - } - expect(foo).to.exist - }) - - it('type I2CWriteDataRepeatedSTARTRequest', () => { - const foo: I2CWriteDataRepeatedSTARTRequest= { - opaque: '', - command: 0x92, - - address: 0x00, - buffer: Uint8Array.from([ 3, 5, 7 ]) - } - expect(foo).to.exist - }) - - it('type I2CWriteDataNoSTOPRequest', () => { - const foo: I2CWriteDataNoSTOPRequest = { - opaque: '', - command: 0x94, - - address: 0x00, - buffer: Uint8Array.from([ 3, 5, 7 ]) - } - expect(foo).to.exist - }) - - it('type I2CReadDataRequest', () => { - const foo: I2CReadDataRequest= { - opaque: '', - command: 0x91, - - address: 0x00, - length: 0 - } - expect(foo).to.exist - }) - - it('type I2CReadDataRepeatedSTARTRequest', () => { - const foo: I2CReadDataRepeatedSTARTRequest = { - opaque: '', - command: 0x93, - - address: 0x00, - length: 0 - } - expect(foo).to.exist - }) - - it('type I2CReadGetDataRequest', () => { - const foo: I2CReadGetDataRequest = { - opaque: '', - command: 0x40, - - address: 0x00 - } - expect(foo).to.exist - }) - }) - - describe('SRAM', () => { - it('type SetSRAMSettingsRequest', () => { - const foo: SetSRAMSettingsRequest = { - opaque: '', - command: 0x60, - - gp: { - } - } - expect(foo).to.exist - }) - - it('type GetSRAMSettingsRequest', () => { - const foo: GetSRAMSettingsRequest = { - opaque: '', - command: 0x61 - } - expect(foo).to.exist - }) - }) - - describe('Flash', () => { - it('type ReadFlashDataRequest', () => { - const foo: ReadFlashDataRequest = { - opaque: '', - command: 0xB0, - subCommand: 0xFF - } - expect(foo).to.exist - }) - - it('type WriteFlashDataRequest', () => { - const foo: WriteFlashDataRequest = { - opaque: '', - command: 0xB1, - subCommand: 0xFF - } - expect(foo).to.exist - }) - - it('type SendFlashAccessPasswordRequest', () => { - const foo: SendFlashAccessPasswordRequest = { - opaque: '', - command: 0xB2, - password: 'P@$$\/\/0rD' - } - expect(foo).to.exist - }) - }) - }) -}) diff --git a/tsconfig.json b/tsconfig.json index 5d4cc76..edc7c87 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "target": "ESNext", "module": "ESNext", - "lib": ["ESNext", "DOM"], + "lib": [ "ESNext", "DOM" ], "esModuleInterop": true, "isolatedModules": false,