-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a1c448
commit f09413a
Showing
5 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Bus } from '../Bus'; | ||
import Device from './Device'; | ||
|
||
jest.mock('i2c-bus', () => { | ||
const createI2cBusMock = require('../createI2cBusMock').default; // eslint-disable-line global-require | ||
|
||
return { | ||
open: createI2cBusMock({ | ||
devices: { | ||
0xab: Buffer.allocUnsafe(0xff), | ||
}, | ||
}), | ||
}; | ||
}); | ||
|
||
describe('Bus', () => { | ||
describe('open', () => { | ||
it('should open the bus', async () => { | ||
const bus = Bus({ busNumber: 1 }); | ||
|
||
await bus.open(); | ||
|
||
expect(bus.isOpen).toBeTruthy(); | ||
expect(bus.i2cBus).not.toBeNull(); | ||
}); | ||
}); | ||
describe('open', () => { | ||
it('should call promised async function', async () => { | ||
const busNumber = 1; | ||
const bus = Bus({ busNumber }); | ||
const address = 0xab; | ||
const device = Device({ address, bus }); | ||
|
||
await bus.open(); | ||
|
||
const spyOnI2cRead = jest.spyOn(bus, 'i2cRead'); | ||
await device.i2cRead(1, Buffer.from([])); | ||
expect(spyOnI2cRead).toHaveBeenCalledWith(address, 1, Buffer.from([])); | ||
|
||
const spyOnI2cWrite = jest.spyOn(bus, 'i2cWrite'); | ||
await device.i2cWrite(1, Buffer.from([])); | ||
expect(spyOnI2cWrite).toHaveBeenCalledWith(address, 1, Buffer.from([])); | ||
|
||
const spyOnReceiveByte = jest.spyOn(bus, 'receiveByte'); | ||
await device.receiveByte(); | ||
expect(spyOnReceiveByte).toHaveBeenCalledWith(address); | ||
|
||
const spyOnSendByte = jest.spyOn(bus, 'sendByte'); | ||
await device.sendByte(1); | ||
expect(spyOnSendByte).toHaveBeenCalledWith(address, 1); | ||
|
||
const spyOnReadByte = jest.spyOn(bus, 'readByte'); | ||
await device.readByte(1); | ||
expect(spyOnReadByte).toHaveBeenCalledWith(address, 1); | ||
|
||
const spyOnReadWord = jest.spyOn(bus, 'readWord'); | ||
await device.readWord(1); | ||
expect(spyOnReadWord).toHaveBeenCalledWith(address, 1); | ||
|
||
const spyOnReadI2cBlock = jest.spyOn(bus, 'readI2cBlock'); | ||
await device.readI2cBlock(1, 2, Buffer.from([])); | ||
expect(spyOnReadI2cBlock).toHaveBeenCalledWith(address, 1, 2, Buffer.from([])); | ||
|
||
const spyOnWriteByte = jest.spyOn(bus, 'writeByte'); | ||
await device.writeByte(1, 2); | ||
expect(spyOnWriteByte).toHaveBeenCalledWith(address, 1, 2); | ||
|
||
const spyOnWriteWord = jest.spyOn(bus, 'writeWord'); | ||
await device.writeWord(1, 2); | ||
expect(spyOnWriteWord).toHaveBeenCalledWith(address, 1, 2); | ||
|
||
const spyOnWriteI2cBlock = jest.spyOn(bus, 'writeI2cBlock'); | ||
await device.writeI2cBlock(1, 2, Buffer.from([])); | ||
expect(spyOnWriteI2cBlock).toHaveBeenCalledWith(address, 1, 2, Buffer.from([])); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Bus } from '../Bus'; | ||
|
||
export interface Device { | ||
readonly address: number; | ||
readonly bus: Bus; | ||
|
||
i2cRead: (length: number, buffer: Buffer) => Promise<number>; | ||
i2cWrite: (length: number, buffer: Buffer) => Promise<number>; | ||
|
||
receiveByte: () => Promise<number>; | ||
sendByte: (byte: number) => Promise<void>; | ||
|
||
readByte: (command: number) => Promise<number>; | ||
readI2cBlock: (command: number, length: number, buffer: Buffer) => Promise<number>; | ||
readWord: (command: number) => Promise<number>; | ||
|
||
writeByte: (command: number, byte: number) => Promise<void>; | ||
writeWord: (command: number, word: number) => Promise<void>; | ||
writeI2cBlock: (command: number, length: number, buffer: Buffer) => Promise<number>; | ||
} | ||
|
||
const Device = ({ address, bus }: { address: number; bus: Bus }): Device => ({ | ||
get address() { | ||
return address; | ||
}, | ||
get bus() { | ||
return bus; | ||
}, | ||
i2cRead(length: number, buffer: Buffer) { | ||
return bus.i2cRead(address, length, buffer); | ||
}, | ||
i2cWrite(length: number, buffer: Buffer) { | ||
return bus.i2cWrite(address, length, buffer); | ||
}, | ||
|
||
receiveByte() { | ||
return bus.receiveByte(address); | ||
}, | ||
sendByte(byte: number) { | ||
return bus.sendByte(address, byte); | ||
}, | ||
|
||
readByte(command: number) { | ||
return bus.readByte(address, command); | ||
}, | ||
readWord(command: number) { | ||
return bus.readWord(address, command); | ||
}, | ||
readI2cBlock(command: number, length: number, buffer: Buffer) { | ||
return bus.readI2cBlock(address, command, length, buffer); | ||
}, | ||
|
||
writeByte(command: number, byte: number) { | ||
return bus.writeByte(address, command, byte); | ||
}, | ||
writeWord(command: number, word: number) { | ||
return bus.writeWord(address, command, word); | ||
}, | ||
writeI2cBlock(command: number, length: number, buffer: Buffer) { | ||
return bus.writeI2cBlock(address, command, length, buffer); | ||
}, | ||
}); | ||
|
||
export default Device; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default class DeviceError extends Error { | ||
public busNumber: number; | ||
public deviceAddress: number; | ||
|
||
constructor(msg: string, busNumber: number, deviceAddress: number) { | ||
super(msg); | ||
|
||
Error.captureStackTrace(this, DeviceError); | ||
|
||
this.busNumber = busNumber; | ||
this.deviceAddress = deviceAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Device from './Device'; | ||
import DeviceError from './DeviceError'; | ||
|
||
export { Device, DeviceError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { Bus, BusError } from './Bus'; | ||
import { Device, DeviceError } from './Device'; | ||
|
||
import createI2cBusMock from './createI2cBusMock'; | ||
|
||
export { Bus, BusError, createI2cBusMock }; | ||
export { Bus, BusError, Device, DeviceError, createI2cBusMock }; |