From 45d945ebb5ffbc353142424a7f3381ed5ce5f30c Mon Sep 17 00:00:00 2001 From: erikma Date: Wed, 19 Sep 2018 23:01:33 -0700 Subject: [PATCH] Add new types for pigpio-dht NPM package --- types/pigpio-dht/index.d.ts | 46 ++++++++++++++++++++++++++++ types/pigpio-dht/pigpio-dht-tests.ts | 9 ++++++ types/pigpio-dht/tsconfig.json | 23 ++++++++++++++ types/pigpio-dht/tslint.json | 4 +++ 4 files changed, 82 insertions(+) create mode 100644 types/pigpio-dht/index.d.ts create mode 100644 types/pigpio-dht/pigpio-dht-tests.ts create mode 100644 types/pigpio-dht/tsconfig.json create mode 100644 types/pigpio-dht/tslint.json diff --git a/types/pigpio-dht/index.d.ts b/types/pigpio-dht/index.d.ts new file mode 100644 index 0000000000..54227f0190 --- /dev/null +++ b/types/pigpio-dht/index.d.ts @@ -0,0 +1,46 @@ +// Type definitions for pigpio-dht 1.1 +// Project: https://github.com/depuits/pigpio-dht +// Definitions by: Erik Mavrinac +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +export = dht; + +/** + * Returns a new DHT object for accessing a DHT11 or DHT22/AM2302 via GPIO. + * @param gpio an unsigned integer specifying the GPIO number + * @param type A DhtType enumeration value for the sensor type + */ +declare function dht(gpio: number, type: dht.DhtType | number): dht.Dht; + +declare namespace dht { + /** The various sensor types supported by pigpio-dht. */ + enum DhtType { + DHT11 = 11, + DHT22 = 22, + AM2302 = 22, + } + + interface Dht extends NodeJS.EventEmitter { + /** + * Starts reading the sensor value. Emits the 'start' event before starting, + * the 'end' event on end whether or not an error occurred, the 'result' + * event with a DhtResult on success, and the 'badChecksum' event on a + * receive failure. + * @returns false if a read is already underway, true if a new read was started. + */ + read(): boolean; + } + + /** + * Result from a successful sensor read. + */ + interface DhtResult { + /** The temperature value in degress Celsius. */ + temperature: number; + + /** The humidity value in percentage points. */ + humidity: number; + } +} diff --git a/types/pigpio-dht/pigpio-dht-tests.ts b/types/pigpio-dht/pigpio-dht-tests.ts new file mode 100644 index 0000000000..fefa27c415 --- /dev/null +++ b/types/pigpio-dht/pigpio-dht-tests.ts @@ -0,0 +1,9 @@ +import * as dht from 'pigpio-dht'; + +const dhtReadPin = 2; +const sensor: dht.Dht = dht(dhtReadPin, dht.DhtType.DHT11); +sensor.on('start', () => console.log("start")); +sensor.on('end', () => console.log("end")); +sensor.on('result', (result: dht.DhtResult) => console.log(`result: ${result.temperature} degC, ${result.humidity}% relative humidity`)); +sensor.on('badChecksum', () => console.log("bad checksum!")); +sensor.read(); diff --git a/types/pigpio-dht/tsconfig.json b/types/pigpio-dht/tsconfig.json new file mode 100644 index 0000000000..cc51ae06e5 --- /dev/null +++ b/types/pigpio-dht/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pigpio-dht-tests.ts" + ] +} \ No newline at end of file diff --git a/types/pigpio-dht/tslint.json b/types/pigpio-dht/tslint.json new file mode 100644 index 0000000000..2fb367c7b7 --- /dev/null +++ b/types/pigpio-dht/tslint.json @@ -0,0 +1,4 @@ +{ + "extends": "dtslint/dt.json", + "rules": { } +}