@chirimen/ads1015 1.0.5 → 2.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/index.js CHANGED
@@ -1,52 +1,60 @@
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.ADS1015 = factory());
5
- }(this, (function () { 'use strict';
6
-
7
- // @ts-check
8
-
9
- /** @param {number} ms Delay for a number of milliseconds. */
10
- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
11
-
12
- var ADS1015 = function (i2cPort, slaveAddress) {
1
+ // @ts-check
2
+
3
+ /** @param {number} ms Delay for a number of milliseconds. */
4
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
5
+
6
+ /**
7
+ * ADS1015 ADC driver
8
+ * @constructor
9
+ * @param {import('node-web-i2c').I2CPort} i2cPort I2C port instance
10
+ * @param {number} slaveAddress I2C slave address
11
+ */
12
+ class ADS1015 {
13
+ constructor(i2cPort, slaveAddress) {
13
14
  this.i2cPort = i2cPort;
14
15
  this.slaveAddress = slaveAddress;
16
+ /** @type {import('node-web-i2c').I2CSlaveDevice | null} */
15
17
  this.i2cSlave = null;
16
- };
17
-
18
- ADS1015.prototype = {
19
- init: async function () {
20
- this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
21
- },
22
- read: async function (channel) {
23
- if (this.i2cSlave == null) {
24
- throw new Error("i2cSlave is not open yet.");
25
- }
26
-
27
- if ((channel < 0) || (3 < channel)) {
28
- throw new Error("ADS1015.read: channel error" + channel);
29
- }
30
-
31
- var config = 0x4000 + (channel * 0x1000); // ADC channel
32
- config |= 0x8000; // Set 'start single-conversion' bit
33
- config |= 0x0003; // Disable the comparator (default val)
34
- config |= 0x0080; // 1600 samples per second (default)
35
- config |= 0x0100; // Power-down single-shot mode (default)
36
- config |= 0x0200; // +/-4.096V range = Gain 1
37
- var confL = config >> 8;
38
- var confH = config & 0x00ff;
39
- var data = confH | confL;
40
- await this.i2cSlave.write16(0x01, data);
41
- await sleep(10);
42
- var v = await this.i2cSlave.read16(0);
43
- var vH = (v & 0x00ff) << 8;
44
- var vL = (v >> 8) & 0x00ffff;
45
- var value = (vH | vL) >> 4;
46
- return value;
18
+ }
19
+
20
+ /**
21
+ * Initialize the sensor
22
+ * @returns {Promise<void>}
23
+ */
24
+ async init() {
25
+ this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
26
+ }
27
+ /**
28
+ * Read ADC value from specified channel
29
+ * @param {number} channel - ADC channel (0-3)
30
+ * @returns {Promise<number>} ADC value
31
+ */
32
+ async read(channel) {
33
+ if (this.i2cSlave == null) {
34
+ throw new Error("i2cSlave is not open yet.");
47
35
  }
48
- };
49
36
 
50
- return ADS1015;
37
+ if (channel < 0 || 3 < channel) {
38
+ throw new Error("ADS1015.read: channel error" + channel);
39
+ }
51
40
 
52
- })));
41
+ var config = 0x4000 + channel * 0x1000; // ADC channel
42
+ config |= 0x8000; // Set 'start single-conversion' bit
43
+ config |= 0x0003; // Disable the comparator (default val)
44
+ config |= 0x0080; // 1600 samples per second (default)
45
+ config |= 0x0100; // Power-down single-shot mode (default)
46
+ config |= 0x0200; // +/-4.096V range = Gain 1
47
+ var confL = config >> 8;
48
+ var confH = config & 0x00ff;
49
+ var data = confH | confL;
50
+ await this.i2cSlave.write16(0x01, data);
51
+ await sleep(10);
52
+ var v = await this.i2cSlave.read16(0);
53
+ var vH = (v & 0x00ff) << 8;
54
+ var vL = (v >> 8) & 0x00ffff;
55
+ var value = (vH | vL) >> 4;
56
+ return value;
57
+ }
58
+ }
59
+
60
+ export default ADS1015;
package/package.json CHANGED
@@ -1,9 +1,7 @@
1
1
  {
2
2
  "name": "@chirimen/ads1015",
3
- "version": "1.0.5",
4
3
  "description": "Driver for ADS1015 with WebI2C",
5
- "main": "index.js",
6
- "module": "ads1015.js",
4
+ "version": "2.0.0",
7
5
  "license": "MIT",
8
6
  "repository": {
9
7
  "type": "git",
@@ -13,12 +11,10 @@
13
11
  "publishConfig": {
14
12
  "access": "public"
15
13
  },
16
- "scripts": {
17
- "build": "rollup -c",
18
- "prepublishOnly": "npm run build"
14
+ "type": "module",
15
+ "exports": "./index.js",
16
+ "peerDependencies": {
17
+ "node-web-i2c": "^1.1.51"
19
18
  },
20
- "devDependencies": {
21
- "rollup": "^2.34.2"
22
- },
23
- "gitHead": "bcccf29cc06de94168dd100ef2936965ff1a616e"
19
+ "gitHead": "0e5bcde729a304d14cad59735c9cd2737f8a78ce"
24
20
  }
package/ads1015.js DELETED
@@ -1,44 +0,0 @@
1
- // @ts-check
2
-
3
- /** @param {number} ms Delay for a number of milliseconds. */
4
- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
5
-
6
- var ADS1015 = function (i2cPort, slaveAddress) {
7
- this.i2cPort = i2cPort;
8
- this.slaveAddress = slaveAddress;
9
- this.i2cSlave = null;
10
- };
11
-
12
- ADS1015.prototype = {
13
- init: async function () {
14
- this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
15
- },
16
- read: async function (channel) {
17
- if (this.i2cSlave == null) {
18
- throw new Error("i2cSlave is not open yet.");
19
- }
20
-
21
- if ((channel < 0) || (3 < channel)) {
22
- throw new Error("ADS1015.read: channel error" + channel);
23
- }
24
-
25
- var config = 0x4000 + (channel * 0x1000); // ADC channel
26
- config |= 0x8000; // Set 'start single-conversion' bit
27
- config |= 0x0003; // Disable the comparator (default val)
28
- config |= 0x0080; // 1600 samples per second (default)
29
- config |= 0x0100; // Power-down single-shot mode (default)
30
- config |= 0x0200; // +/-4.096V range = Gain 1
31
- var confL = config >> 8;
32
- var confH = config & 0x00ff;
33
- var data = confH | confL;
34
- await this.i2cSlave.write16(0x01, data);
35
- await sleep(10);
36
- var v = await this.i2cSlave.read16(0);
37
- var vH = (v & 0x00ff) << 8;
38
- var vL = (v >> 8) & 0x00ffff;
39
- var value = (vH | vL) >> 4;
40
- return value;
41
- }
42
- };
43
-
44
- export default ADS1015;
package/rollup.config.js DELETED
@@ -1,10 +0,0 @@
1
- import { module as input, main as outputFile } from "./package.json";
2
-
3
- export default {
4
- input,
5
- output: {
6
- file: outputFile,
7
- format: "umd",
8
- name: "ADS1015"
9
- }
10
- };