Skip to content

Commit

Permalink
perf(typescript): Improve type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroHerr committed Dec 15, 2018
1 parent f09413a commit 33ec1f5
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 75 deletions.
31 changes: 3 additions & 28 deletions src/Bus/Bus.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import { promisifyAll } from 'bluebird';
import { I2cBusFuncs, open as openI2cBus } from 'i2c-bus';
import { open as openI2cBus } from 'i2c-bus';

import { I2cBusPromised } from '../types';

import BusError from './BusError';
import BusInterface from './BusInterface';

interface Bus {
readonly busNumber: number;
readonly i2cBus: I2cBusPromised | null;
readonly isOpen: boolean;

open: () => Promise<Bus>;
close: () => Promise<Bus>;

i2cFuncs: () => Promise<I2cBusFuncs>;
scan: () => Promise<number[]>;

i2cRead: (address: number, length: number, buffer: Buffer) => Promise<number>;
i2cWrite: (address: number, length: number, buffer: Buffer) => Promise<number>;

receiveByte: (address: number) => Promise<number>;
sendByte: (address: number, byte: number) => Promise<void>;

readByte: (address: number, command: number) => Promise<number>;
readI2cBlock: (address: number, command: number, length: number, buffer: Buffer) => Promise<number>;
readWord: (address: number, command: number) => Promise<number>;

writeByte: (address: number, command: number, byte: number) => Promise<void>;
writeWord: (address: number, command: number, word: number) => Promise<void>;
writeI2cBlock: (address: number, command: number, length: number, buffer: Buffer) => Promise<number>;
}

const Bus = ({ busNumber = 1, openBus = openI2cBus } = {}): Bus => {
const Bus = ({ busNumber = 1, openBus = openI2cBus } = {}): BusInterface => {
let i2cBus: I2cBusPromised | null = null;
let isOpen: boolean = false;

Expand Down
6 changes: 4 additions & 2 deletions src/Bus/BusError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export default class BusError extends Error {
public busNumber: number;

constructor(msg: string, busNumber: number) {
super(msg);
constructor(message: string, busNumber: number) {
super(message);

this.name = this.constructor.name;

Error.captureStackTrace(this, BusError);

Expand Down
29 changes: 29 additions & 0 deletions src/Bus/BusInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { I2cBusFuncs } from 'i2c-bus';

import { I2cBusPromised } from '../types';

export default interface BusInterface {
readonly busNumber: number;
readonly i2cBus: I2cBusPromised | null;
readonly isOpen: boolean;

open(): Promise<BusInterface>;
close(): Promise<BusInterface>;

i2cFuncs(): Promise<I2cBusFuncs>;
scan(): Promise<number[]>;

i2cRead(address: number, length: number, buffer: Buffer): Promise<number>;
i2cWrite(address: number, length: number, buffer: Buffer): Promise<number>;

receiveByte(address: number): Promise<number>;
sendByte(address: number, byte: number): Promise<void>;

readByte(address: number, command: number): Promise<number>;
readI2cBlock(address: number, command: number, length: number, buffer: Buffer): Promise<number>;
readWord(address: number, command: number): Promise<number>;

writeByte(address: number, command: number, byte: number): Promise<void>;
writeWord(address: number, command: number, word: number): Promise<void>;
writeI2cBlock(address: number, command: number, length: number, buffer: Buffer): Promise<number>;
}
3 changes: 2 additions & 1 deletion src/Bus/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Bus from './Bus';
import BusError from './BusError';
import BusInterface from './BusInterface';

export { Bus, BusError };
export { Bus, BusError, BusInterface };
23 changes: 3 additions & 20 deletions src/Device/Device.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import { Bus } from '../Bus';
import BusInterface from '../Bus/BusInterface';

export interface Device {
readonly address: number;
readonly bus: Bus;
import DeviceInterface from './DeviceInterface';

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 => ({
const Device = ({ address, bus }: { address: number; bus: BusInterface }): DeviceInterface => ({
get address() {
return address;
},
Expand Down
6 changes: 4 additions & 2 deletions src/Device/DeviceError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export default class DeviceError extends Error {
public busNumber: number;
public deviceAddress: number;

constructor(msg: string, busNumber: number, deviceAddress: number) {
super(msg);
constructor(message: string, busNumber: number, deviceAddress: number) {
super(message);

this.name = this.constructor.name;

Error.captureStackTrace(this, DeviceError);

Expand Down
20 changes: 20 additions & 0 deletions src/Device/DeviceInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import BusInterface from '../Bus/BusInterface';

export default interface DeviceInterface {
readonly address: number;
readonly bus: BusInterface;

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>;
}
3 changes: 2 additions & 1 deletion src/Device/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Device from './Device';
import DeviceError from './DeviceError';
import DeviceInterface from './DeviceInterface';

export { Device, DeviceError };
export { Device, DeviceError, DeviceInterface };
9 changes: 4 additions & 5 deletions src/createI2cBusMock/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { BufferCallback, CompletionCallback, I2cBus, I2cBusFuncs, ResultCallback } from 'i2c-bus';

interface Devices {
[address: number]: Buffer;
}

const defaultI2cBusFuncs = {
i2c: false,
tenBitAddr: false,
Expand All @@ -28,7 +24,10 @@ const defaultI2cBusFuncs = {
export default ({
devices = {},
i2cBusFuncs = defaultI2cBusFuncs,
}: { devices?: Devices; i2cBusFuncs?: I2cBusFuncs } = {}) => (_: number, openCallback: CompletionCallback): I2cBus => {
}: {
devices?: { [address: number]: Buffer };
i2cBusFuncs?: I2cBusFuncs;
} = {}) => (_: number, openCallback: CompletionCallback): I2cBus => {
openCallback(null);

return {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bus, BusError } from './Bus';
import { Device, DeviceError } from './Device';
import { Bus, BusError, BusInterface } from './Bus';
import { Device, DeviceError, DeviceInterface } from './Device';

import createI2cBusMock from './createI2cBusMock';

export { Bus, BusError, Device, DeviceError, createI2cBusMock };
export { Bus, BusError, BusInterface, Device, DeviceError, DeviceInterface, createI2cBusMock };
26 changes: 13 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { I2cBus, I2cBusFuncs } from 'i2c-bus';

/* tslint:disable-next-line:interface-name */
export interface I2cBusPromised extends I2cBus {
closeAsync: () => Promise<void>;
closeAsync(): Promise<void>;

i2cFuncsAsync: () => Promise<I2cBusFuncs>;
scanAsync: () => Promise<number[]>;
i2cFuncsAsync(): Promise<I2cBusFuncs>;
scanAsync(): Promise<number[]>;

i2cReadAsync: (address: number, length: number, buffer: Buffer) => Promise<number>;
i2cWriteAsync: (address: number, length: number, buffer: Buffer) => Promise<number>;
i2cReadAsync(address: number, length: number, buffer: Buffer): Promise<number>;
i2cWriteAsync(address: number, length: number, buffer: Buffer): Promise<number>;

receiveByteAsync: (address: number) => Promise<number>;
sendByteAsync: (address: number, byte: number) => Promise<void>;
receiveByteAsync(address: number): Promise<number>;
sendByteAsync(address: number, byte: number): Promise<void>;

readByteAsync: (address: number, command: number) => Promise<number>;
readWordAsync: (address: number, command: number) => Promise<number>;
readI2cBlockAsync: (address: number, command: number, length: number, buffer: Buffer) => Promise<number>;
readByteAsync(address: number, command: number): Promise<number>;
readWordAsync(address: number, command: number): Promise<number>;
readI2cBlockAsync(address: number, command: number, length: number, buffer: Buffer): Promise<number>;

writeByteAsync: (address: number, command: number, byte: number) => Promise<void>;
writeWordAsync: (address: number, command: number, word: number) => Promise<void>;
writeI2cBlockAsync: (address: number, command: number, length: number, buffer: Buffer) => Promise<number>;
writeByteAsync(address: number, command: number, byte: number): Promise<void>;
writeWordAsync(address: number, command: number, word: number): Promise<void>;
writeI2cBlockAsync(address: number, command: number, length: number, buffer: Buffer): Promise<number>;
}

0 comments on commit 33ec1f5

Please sign in to comment.