diff --git a/types/ds18b20/ds18b20-tests.ts b/types/ds18b20/ds18b20-tests.ts new file mode 100644 index 0000000000..a504f8b753 --- /dev/null +++ b/types/ds18b20/ds18b20-tests.ts @@ -0,0 +1,34 @@ +// Tests taken from documentation samples. + +import * as ds18b20 from 'ds18b20'; + +ds18b20.sensors((err, ids) => { + if (err) { + console.log('Can not get sensor IDs', err); + return; + } + + console.log('Sensor IDs', ids); + ids.forEach((id) => { + ds18b20.temperature(id, (err, result) => { + if (err) { + console.log('Can not get temperature from sensor', err); + } else { + console.log(`Sensor ${id} :`, result); + } + }); + }); +}); + +ds18b20.sensors((err, ids) => { + if (err) { + console.log('Can not get sensor IDs', err); + return; + } + + console.log('Sensor IDs', ids); + ids.forEach((id) => { + console.log(`Sensor ${id} (decimal) :`, ds18b20.temperatureSync(id)); + console.log(`Sensor ${id} (hex) :`, ds18b20.temperatureSync(id, {parser: 'hex'})); + }); +}); diff --git a/types/ds18b20/index.d.ts b/types/ds18b20/index.d.ts new file mode 100644 index 0000000000..04fc1a1f9d --- /dev/null +++ b/types/ds18b20/index.d.ts @@ -0,0 +1,58 @@ +// Type definitions for ds18b20 0.1 +// Project: https://github.com/chamerling/ds18b20 +// Definitions by: Peter Müller +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// TypeScript Version: 2.1 + +/// + +/** + * Options for a `temperature` call + */ +export interface TemperatureOptions { + parser: 'decimal' | 'default' | 'hex'; +} + +/** + * Callback for a `sensors` call + * @param err An error or null + * @param ids An array of sensor IDs + */ +export type SensorsCallback = (err: Error | null, ids: string[]) => void; + +/** + * Callback for a `temperature` call + * @param err An error or null + * @param value The temperature + */ +export type TemperatureCallback = (err: Error | null, value: number) => void; + +/** + * Get all connected sensor IDs as array + * @param callback callback(err, array) + */ +export function sensors(callback: SensorsCallback): void; + +/** + * Get the temperature of a given sensor + * @param sensorId The sensor ID + * @param callback callback(err, value) + */ +export function temperature(sensorId: string, callback: TemperatureCallback): void; + +/** + * Get the temperature of a given sensor + * @param sensorId The sensor ID + * @param options The options + * @param callback callback(err, value) + */ +export function temperature(sensorId: string, options: TemperatureOptions, callback: TemperatureCallback): void; + +/** + * Get the temperature of a given sensor sync + * @param sensorId The sensor ID + * @param options The options + * @return The temperature + */ +export function temperatureSync(sensorId: string, options?: TemperatureOptions): number; diff --git a/types/ds18b20/tsconfig.json b/types/ds18b20/tsconfig.json new file mode 100644 index 0000000000..14b3d6641f --- /dev/null +++ b/types/ds18b20/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ds18b20-tests.ts" + ] +} diff --git a/types/ds18b20/tslint.json b/types/ds18b20/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/ds18b20/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }