Skip to content

Commit

Permalink
adc: Add Adc class (#46)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* adc: Add AdcInProperty sensor class

To support analog inputs

Aligned to Gpio class

Change-Id: Ic2b25cd172e9591fca4ce1e2920af92d2a218e0d
Signed-off-by: Philippe Coval <[email protected]>

* artik: Support ADC properties

Change-Id: I3e858f3a66e92546a3bba920a75d3fa299ae7c00
Signed-off-by: Philippe Coval <[email protected]>
  • Loading branch information
rzr authored and mrstegeman committed Oct 1, 2018
1 parent 7ab89dd commit af8920c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
85 changes: 85 additions & 0 deletions example/platform/adc/adc-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// -*- mode: js; js-indent-level:2; -*-
// SPDX-License-Identifier: MPL-2.0

/**
*
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/

const console = require('console');

// Disable logs here by editing to '!console.log'
const log = console.log || function() {};

const {
Property,
Value,
} = require('webthing');

const adc = require('../adc');

class AdcInProperty extends Property {
constructor(thing, name, value, metadata, config) {
const valueObject = new Value(Number(value), () => {
});
super(thing, name, valueObject,
{
'@type': 'LevelProperty',
label: (metadata && metadata.label) || `Level: ${name}`,
type: 'number',
readOnly: true,
description:
(metadata && metadata.description) ||
(`ADC Sensor on pin=${config.pin}`),
});
const self = this;
this.valueObject = valueObject;
config.frequency = config.frequency || 1;
config.range = config.range || 4096;
this.period = 1000.0 / config.frequency;
this.config = config;
this.port = adc.open(config, function(err) {
log(`log: ADC: ${self.getName()}: open: ${err} (null expected)`);
if (err) {
console.error(`errror: ADC: ${self.getName()}: Fail to open:\
${config.pin}`);
return null;
}
self.inverval = setInterval(() => {
let value = self.port.readSync();
log(`log: ADC:\
${self.getName()}: update: 0x${Number(value).toString(0xF)}`);
value = Number(Math.floor(100.0 * value / self.config.range));
if (value !== self.lastValue) {
log(`log: ADC: ${self.getName()}: change: ${value}%`);
self.valueObject.notifyOfExternalUpdate(value);
self.lastValue = value;
}
}, self.period);
});
}

close() {
try {
this.inverval && clearInterval(this.inverval);
this.port && this.port.closeSync();
} catch (err) {
console.error(`error: ADC: ${this.getName()} close:${err}`);
return err;
}
log(`log: ADC: ${self.getName()}: close:`);
}
}

function AdcProperty(thing, name, value, metadata, config) {
if (config.direction === 'in') {
return new AdcInProperty(thing, name, value, metadata, config);
}
throw 'error: Invalid param';
}

module.exports = AdcProperty;
28 changes: 28 additions & 0 deletions example/platform/adc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -*- mode: js; js-indent-level:2; -*-
// SPDX-License-Identifier: ISC
/**
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
*
* This Source Code Form is subject to the terms of the ICS Licence:
* https://spdx.org/licenses/ISC.html#licenseText
*/

const fs = require('fs');

function Adc() {
this.open = function(config, callback) {
this.config = config;
fs.access(config.device, fs.R_OK, callback);
return this;
};

this.readSync = function() {
const contents = fs.readFileSync(this.config.device, 'ascii');
return contents;
};

this.closeSync = function() {
};
}

module.exports = new Adc();
17 changes: 14 additions & 3 deletions example/platform/board/artik530.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
Thing,
} = require('webthing');

const AdcProperty = require('../adc/adc-property');
const GpioProperty = require('../gpio/gpio-property');

class ARTIK530Thing extends Thing {
Expand All @@ -22,7 +23,7 @@ class ARTIK530Thing extends Thing {
type || [],
description || 'A web connected ARTIK530 or ARTIK720');
const _this = this;
this.gpioProperties = [
this.pinProperties = [
new GpioProperty(this, 'RedLED', false,
{description:
'Red LED on interposer board (on GPIO28)'},
Expand All @@ -40,14 +41,24 @@ class ARTIK530Thing extends Thing {
{description:
'SW404 Button: Next to blue LED (on GPIO32)'},
{direction: 'in', pin: 32}),
new AdcProperty(this, 'ADC0', 0,
{description: 'Analog port of ARTIK05x'},
{direction: 'in',
device: '/sys/bus/platform/devices\
/c0053000.adc/iio:device0/in_voltage0_raw'}),
new AdcProperty(this, 'ADC1', 0,
{description: 'Analog port of ARTIK05x'},
{direction: 'in',
device: '/sys/bus/platform/devices/\
c0053000.adc/iio:device0/in_voltage1_raw'}),
];
this.gpioProperties.forEach((property) => {
this.pinProperties.forEach((property) => {
_this.addProperty(property);
});
}

close() {
this.gpioProperties.forEach((property) => {
this.pinProperties.forEach((property) => {
property.close && property.close();
});
}
Expand Down

0 comments on commit af8920c

Please sign in to comment.