@chirimen/ads1015 1.0.4 → 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.
@@ -1,28 +1,44 @@
1
1
  // @ts-check
2
2
 
3
3
  /** @param {number} ms Delay for a number of milliseconds. */
4
- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
4
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
5
5
 
6
- var ADS1015 = function (i2cPort, slaveAddress) {
7
- this.i2cPort = i2cPort;
8
- this.slaveAddress = slaveAddress;
9
- this.i2cSlave = null;
10
- };
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) {
14
+ this.i2cPort = i2cPort;
15
+ this.slaveAddress = slaveAddress;
16
+ /** @type {import('node-web-i2c').I2CSlaveDevice | null} */
17
+ this.i2cSlave = null;
18
+ }
11
19
 
12
- ADS1015.prototype = {
13
- init: async function () {
20
+ /**
21
+ * Initialize the sensor
22
+ * @returns {Promise<void>}
23
+ */
24
+ async init() {
14
25
  this.i2cSlave = await this.i2cPort.open(this.slaveAddress);
15
- },
16
- read: async function (channel) {
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) {
17
33
  if (this.i2cSlave == null) {
18
34
  throw new Error("i2cSlave is not open yet.");
19
35
  }
20
36
 
21
- if ((channel < 0) || (3 < channel)) {
37
+ if (channel < 0 || 3 < channel) {
22
38
  throw new Error("ADS1015.read: channel error" + channel);
23
39
  }
24
40
 
25
- var config = 0x4000 + (channel * 0x1000); // ADC channel
41
+ var config = 0x4000 + channel * 0x1000; // ADC channel
26
42
  config |= 0x8000; // Set 'start single-conversion' bit
27
43
  config |= 0x0003; // Disable the comparator (default val)
28
44
  config |= 0x0080; // 1600 samples per second (default)
@@ -39,6 +55,6 @@ ADS1015.prototype = {
39
55
  var value = (vH | vL) >> 4;
40
56
  return value;
41
57
  }
42
- };
58
+ }
43
59
 
44
60
  export default ADS1015;
package/package.json CHANGED
@@ -1,9 +1,7 @@
1
1
  {
2
2
  "name": "@chirimen/ads1015",
3
- "version": "1.0.4",
4
3
  "description": "Driver for ADS1015 with WebI2C",
5
- "main": "ads1015.js",
6
- "module": "ads1015.mjs",
4
+ "version": "2.0.0",
7
5
  "license": "MIT",
8
6
  "repository": {
9
7
  "type": "git",
@@ -13,11 +11,10 @@
13
11
  "publishConfig": {
14
12
  "access": "public"
15
13
  },
16
- "scripts": {
17
- "build": "rollup -c"
14
+ "type": "module",
15
+ "exports": "./index.js",
16
+ "peerDependencies": {
17
+ "node-web-i2c": "^1.1.51"
18
18
  },
19
- "devDependencies": {
20
- "rollup": "^2.34.2"
21
- },
22
- "gitHead": "f8958ce1dadd6028397af59597b8b83313af045f"
19
+ "gitHead": "0e5bcde729a304d14cad59735c9cd2737f8a78ce"
23
20
  }
package/ads1015.js DELETED
@@ -1,52 +0,0 @@
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) {
13
- this.i2cPort = i2cPort;
14
- this.slaveAddress = slaveAddress;
15
- 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;
47
- }
48
- };
49
-
50
- return ADS1015;
51
-
52
- })));
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
- };