Skip to content

Commit af8920c

Browse files
rzrmrstegeman
authored andcommitted
adc: Add Adc class (#46)
* adc: Add Adc class To abstract different ADC or sysfs API, Note: later this class could land later in separate project (ie: gpio, iotjs-node? sysfs-node?) but since it's trivial let's share it here. Relate-to: EnotionZ/gpio#53 Change-Id: I923159901d3af3e1990ccf3e1510d561c2e0783b Signed-off-by: Philippe Coval <p.coval@samsung.com> * adc: Add AdcInProperty sensor class To support analog inputs Aligned to Gpio class Change-Id: Ic2b25cd172e9591fca4ce1e2920af92d2a218e0d Signed-off-by: Philippe Coval <p.coval@samsung.com> * artik: Support ADC properties Change-Id: I3e858f3a66e92546a3bba920a75d3fa299ae7c00 Signed-off-by: Philippe Coval <p.coval@samsung.com>
1 parent 7ab89dd commit af8920c

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

example/platform/adc/adc-property.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// -*- mode: js; js-indent-level:2; -*-
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
/**
5+
*
6+
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
11+
*/
12+
13+
const console = require('console');
14+
15+
// Disable logs here by editing to '!console.log'
16+
const log = console.log || function() {};
17+
18+
const {
19+
Property,
20+
Value,
21+
} = require('webthing');
22+
23+
const adc = require('../adc');
24+
25+
class AdcInProperty extends Property {
26+
constructor(thing, name, value, metadata, config) {
27+
const valueObject = new Value(Number(value), () => {
28+
});
29+
super(thing, name, valueObject,
30+
{
31+
'@type': 'LevelProperty',
32+
label: (metadata && metadata.label) || `Level: ${name}`,
33+
type: 'number',
34+
readOnly: true,
35+
description:
36+
(metadata && metadata.description) ||
37+
(`ADC Sensor on pin=${config.pin}`),
38+
});
39+
const self = this;
40+
this.valueObject = valueObject;
41+
config.frequency = config.frequency || 1;
42+
config.range = config.range || 4096;
43+
this.period = 1000.0 / config.frequency;
44+
this.config = config;
45+
this.port = adc.open(config, function(err) {
46+
log(`log: ADC: ${self.getName()}: open: ${err} (null expected)`);
47+
if (err) {
48+
console.error(`errror: ADC: ${self.getName()}: Fail to open:\
49+
${config.pin}`);
50+
return null;
51+
}
52+
self.inverval = setInterval(() => {
53+
let value = self.port.readSync();
54+
log(`log: ADC:\
55+
${self.getName()}: update: 0x${Number(value).toString(0xF)}`);
56+
value = Number(Math.floor(100.0 * value / self.config.range));
57+
if (value !== self.lastValue) {
58+
log(`log: ADC: ${self.getName()}: change: ${value}%`);
59+
self.valueObject.notifyOfExternalUpdate(value);
60+
self.lastValue = value;
61+
}
62+
}, self.period);
63+
});
64+
}
65+
66+
close() {
67+
try {
68+
this.inverval && clearInterval(this.inverval);
69+
this.port && this.port.closeSync();
70+
} catch (err) {
71+
console.error(`error: ADC: ${this.getName()} close:${err}`);
72+
return err;
73+
}
74+
log(`log: ADC: ${self.getName()}: close:`);
75+
}
76+
}
77+
78+
function AdcProperty(thing, name, value, metadata, config) {
79+
if (config.direction === 'in') {
80+
return new AdcInProperty(thing, name, value, metadata, config);
81+
}
82+
throw 'error: Invalid param';
83+
}
84+
85+
module.exports = AdcProperty;

example/platform/adc/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// -*- mode: js; js-indent-level:2; -*-
2+
// SPDX-License-Identifier: ISC
3+
/**
4+
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
5+
*
6+
* This Source Code Form is subject to the terms of the ICS Licence:
7+
* https://spdx.org/licenses/ISC.html#licenseText
8+
*/
9+
10+
const fs = require('fs');
11+
12+
function Adc() {
13+
this.open = function(config, callback) {
14+
this.config = config;
15+
fs.access(config.device, fs.R_OK, callback);
16+
return this;
17+
};
18+
19+
this.readSync = function() {
20+
const contents = fs.readFileSync(this.config.device, 'ascii');
21+
return contents;
22+
};
23+
24+
this.closeSync = function() {
25+
};
26+
}
27+
28+
module.exports = new Adc();

example/platform/board/artik530.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
Thing,
1515
} = require('webthing');
1616

17+
const AdcProperty = require('../adc/adc-property');
1718
const GpioProperty = require('../gpio/gpio-property');
1819

1920
class ARTIK530Thing extends Thing {
@@ -22,7 +23,7 @@ class ARTIK530Thing extends Thing {
2223
type || [],
2324
description || 'A web connected ARTIK530 or ARTIK720');
2425
const _this = this;
25-
this.gpioProperties = [
26+
this.pinProperties = [
2627
new GpioProperty(this, 'RedLED', false,
2728
{description:
2829
'Red LED on interposer board (on GPIO28)'},
@@ -40,14 +41,24 @@ class ARTIK530Thing extends Thing {
4041
{description:
4142
'SW404 Button: Next to blue LED (on GPIO32)'},
4243
{direction: 'in', pin: 32}),
44+
new AdcProperty(this, 'ADC0', 0,
45+
{description: 'Analog port of ARTIK05x'},
46+
{direction: 'in',
47+
device: '/sys/bus/platform/devices\
48+
/c0053000.adc/iio:device0/in_voltage0_raw'}),
49+
new AdcProperty(this, 'ADC1', 0,
50+
{description: 'Analog port of ARTIK05x'},
51+
{direction: 'in',
52+
device: '/sys/bus/platform/devices/\
53+
c0053000.adc/iio:device0/in_voltage1_raw'}),
4354
];
44-
this.gpioProperties.forEach((property) => {
55+
this.pinProperties.forEach((property) => {
4556
_this.addProperty(property);
4657
});
4758
}
4859

4960
close() {
50-
this.gpioProperties.forEach((property) => {
61+
this.pinProperties.forEach((property) => {
5162
property.close && property.close();
5263
});
5364
}

0 commit comments

Comments
 (0)