diff --git a/README.md b/README.md index 62edd4d..7be096a 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,12 @@ blink(); ### I2C Example ```js -const { adt7410 } = require("megabit"); +const { sht30 } = require("megabit"); async function measure() { - const temperature = await adt7410().read(); - console.log(`Temperature: ${temperature} ℃`); + const { humidity, temperature } = await sht30().read(); + console.log(`Humidity: ${humidity.toFixed(2)}%`); + console.log(`Temperature: ${temperature.toFixed(2)} ℃`); } measure(); @@ -51,6 +52,7 @@ measure(); | PAJ7620 | Gesture Recognition Sensor | [paj7620](https://kou029w.github.io/megabit/globals.html#paj7620) | | PCA9685 | 16-Channel 12-Bit PWM/Servo Driver | [pca9685](https://kou029w.github.io/megabit/globals.html#pca9685) | | S11059 | Color Sensor | [s11059](https://kou029w.github.io/megabit/globals.html#s11059) | +| SHT30 | Humidity and Temperature Sensor | [sht30](https://kou029w.github.io/megabit/globals.html#sht30) | | SSD1306 | 128x64 Dot Matrix OLED | [ssd1306](https://kou029w.github.io/megabit/globals.html#ssd1306) | | SSD1308 | 128x64 Dot Matrix OLED | [ssd1308](https://kou029w.github.io/megabit/globals.html#ssd1308) | | TSL2561 | Ambient Light Sensor | [tsl2561](https://kou029w.github.io/megabit/globals.html#tsl2561) | diff --git a/package.json b/package.json index 4acff34..363ac53 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@chirimen/grove-touch": "~1", "@chirimen/pca9685": "~1", "@chirimen/s11059": "~1", + "@chirimen/sht30": "~1", "@chirimen/veml6070": "~1", "@chirimen/vl53l0x": "~1", "node-web-gpio": "~1", diff --git a/src/Device.ts b/src/Device.ts index a201611..802b82c 100644 --- a/src/Device.ts +++ b/src/Device.ts @@ -1,6 +1,4 @@ -export interface ReadableDevice< - T extends boolean | number | string | boolean[] | number[] -> { +export interface ReadableDevice { read(): Promise; } diff --git a/src/index.ts b/src/index.ts index 62ecf8b..3a393da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export { default as mpr121 } from "./mpr121"; export { default as paj7620 } from "./paj7620"; export { default as pca9685 } from "./pca9685"; export { default as s11059 } from "./s11059"; +export { default as sht30 } from "./sht30"; export { default as ssd1308 } from "./ssd1308"; export { default as tsl2561 } from "./tsl2561"; export { default as veml6070 } from "./veml6070"; diff --git a/src/sht30.ts b/src/sht30.ts new file mode 100644 index 0000000..05bf77b --- /dev/null +++ b/src/sht30.ts @@ -0,0 +1,31 @@ +import Device from "@chirimen/sht30"; +import { ReadableDevice } from "./Device"; +import { I2C, i2c } from "./i2c"; + +/** @type Humidity (%) */ +type Humidity = number; + +/** @type Temperature (°C) */ +type Temperature = number; + +/** @type Values */ +type Values = { + humidity: Humidity; + temperature: Temperature; +}; + +export function sht30( + bus: I2C = i2c(), + address: number = 0x44 +): ReadableDevice { + const device = new Device(bus, address); + + return { + async read(): Promise { + if (device.i2cSlave == null) await device.init(); + return await device.readData(); + }, + }; +} + +export default sht30; diff --git a/yarn.lock b/yarn.lock index 11ab1b1..b170f0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -52,6 +52,11 @@ resolved "https://registry.yarnpkg.com/@chirimen/s11059/-/s11059-1.0.2.tgz#d27e1d205975296e28348fc5d8c17a573d23364d" integrity sha512-qj9Vp9pxEsL5XF1bM374xAWIpDJFnM3kx03UZnkZzHI76RG03+ghoh6iNu8eicy2fM55LIForUV+ZQDNcqOy1A== +"@chirimen/sht30@~1": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@chirimen/sht30/-/sht30-1.0.3.tgz#e401ded73d3d70f936ac90f5b67ebfde01975682" + integrity sha512-S6LUFYjSdIPSaK8RukEYD+UKS4yKpiFfMbUK9m9Phbsa704SQAvo+bUtbPqJko+XZX8lADXr12BdEW8QrLl+Qw== + "@chirimen/veml6070@~1": version "1.0.0" resolved "https://registry.yarnpkg.com/@chirimen/veml6070/-/veml6070-1.0.0.tgz#407fdef1f1a6017196879b90fad8858eb20ad33a"