|
| 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; |
0 commit comments