@chirimen/aht10 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 CHIRIMEN Open Hardware
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/aht10.js ADDED
@@ -0,0 +1,46 @@
1
+ // @ts-check
2
+ // AHT10 driver for CHIRIMEN raspberry pi3
3
+ // Temperature and Humidity I2C Sensor
4
+ // based on https://github.com/adafruit/Adafruit_CircuitPython_AHTx0/blob/main/adafruit_ahtx0.py
5
+ // Programmed by Satoru Takagi
6
+
7
+ /** @param {number} ms Delay for a number of milliseconds. */
8
+ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
9
+
10
+ var AHT10 = function(i2cPort,slaveAddress){
11
+ if (!slaveAddress){
12
+ slaveAddress = 0x38;
13
+ }
14
+ this.i2cPort = i2cPort;
15
+ this.i2cSlave = null;
16
+ this.slaveAddress = slaveAddress;
17
+ }
18
+
19
+ AHT10.prototype = {
20
+ init: async function(){
21
+ this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
22
+ await this.i2cSlave.writeBytes([ 0xBA]); // softreset
23
+ await sleep(30); // wait for measurement?
24
+ await this.i2cSlave.writeBytes([ 0xE1, 0x08, 0x00]); // calibrate
25
+ while ( !(await this.i2cSlave.readByte() & 0x08) ){
26
+ }
27
+ },
28
+ readData: async function(){
29
+ await this.i2cSlave.writeBytes([ 0xAC, 0x33, 0x00]); // read
30
+ while ( await this.i2cSlave.readByte() & 0x80 ){
31
+ await sleep(10);
32
+ }
33
+ var mdata = await this.i2cSlave.readBytes(6); // prev data..
34
+ var cTemp = ((mdata[3] & 0xF) << 16) | (mdata[4] << 8) | mdata[5];
35
+ cTemp = ((cTemp * 200.0) / 0x100000) - 50;
36
+ var humidity = (mdata[1] << 12) | (mdata[2] << 4) | (mdata[3] >> 4);
37
+ humidity = (humidity * 100) / 0x100000;
38
+
39
+ return {
40
+ humidity: humidity,
41
+ temperature: cTemp
42
+ }
43
+ }
44
+ };
45
+
46
+ export default AHT10;
package/index.js ADDED
@@ -0,0 +1,54 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.AHT10 = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ // @ts-check
8
+ // AHT10 driver for CHIRIMEN raspberry pi3
9
+ // Temperature and Humidity I2C Sensor
10
+ // based on https://github.com/adafruit/Adafruit_CircuitPython_AHTx0/blob/main/adafruit_ahtx0.py
11
+ // Programmed by Satoru Takagi
12
+
13
+ /** @param {number} ms Delay for a number of milliseconds. */
14
+ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
15
+
16
+ var AHT10 = function(i2cPort,slaveAddress){
17
+ if (!slaveAddress){
18
+ slaveAddress = 0x38;
19
+ }
20
+ this.i2cPort = i2cPort;
21
+ this.i2cSlave = null;
22
+ this.slaveAddress = slaveAddress;
23
+ };
24
+
25
+ AHT10.prototype = {
26
+ init: async function(){
27
+ this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
28
+ await this.i2cSlave.writeBytes([ 0xBA]); // softreset
29
+ await sleep(30); // wait for measurement?
30
+ await this.i2cSlave.writeBytes([ 0xE1, 0x08, 0x00]); // calibrate
31
+ while ( !(await this.i2cSlave.readByte() & 0x08) ){
32
+ }
33
+ },
34
+ readData: async function(){
35
+ await this.i2cSlave.writeBytes([ 0xAC, 0x33, 0x00]); // read
36
+ while ( await this.i2cSlave.readByte() & 0x80 ){
37
+ await sleep(10);
38
+ }
39
+ var mdata = await this.i2cSlave.readBytes(6); // prev data..
40
+ var cTemp = ((mdata[3] & 0xF) << 16) | (mdata[4] << 8) | mdata[5];
41
+ cTemp = ((cTemp * 200.0) / 0x100000) - 50;
42
+ var humidity = (mdata[1] << 12) | (mdata[2] << 4) | (mdata[3] >> 4);
43
+ humidity = (humidity * 100) / 0x100000;
44
+
45
+ return {
46
+ humidity: humidity,
47
+ temperature: cTemp
48
+ }
49
+ }
50
+ };
51
+
52
+ return AHT10;
53
+
54
+ }));
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@chirimen/aht10",
3
+ "version": "1.0.0",
4
+ "description": "Driver for AHT10 with WebI2C",
5
+ "main": "index.js",
6
+ "module": "aht10.js",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/chirimen-oh/chirimen-drivers.git",
11
+ "directory": "packages/aht10"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "scripts": {
17
+ "build": "rollup -c",
18
+ "prepublishOnly": "npm run build"
19
+ },
20
+ "devDependencies": {
21
+ "rollup": "^3.0.0"
22
+ },
23
+ "gitHead": "c91361388edf5c6aeaef30fbbe686c45b6ea5545"
24
+ }
@@ -0,0 +1,8 @@
1
+ export default {
2
+ input: "aht10.js",
3
+ output: {
4
+ file: "index.js",
5
+ format: "umd",
6
+ name: "AHT10"
7
+ }
8
+ };