-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
7ab89dd
commit af8920c
Showing
3 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters