diff --git a/example/platform/board/edison.js b/example/platform/board/edison.js new file mode 100644 index 0000000..02d3b44 --- /dev/null +++ b/example/platform/board/edison.js @@ -0,0 +1,49 @@ +// -*- 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 { + Thing, +} = require('webthing'); + +const AdcProperty = require('../adc/adc-property'); +const GpioProperty = require('../gpio/gpio-property'); +const PwmProperty = require('../pwm/pwm-property'); + +class EdisonThing extends Thing { + constructor(name, type, description) { + super(name || 'Edison', + type || [], + description || 'A web connected Edison'); + const self = this; + this.pinProperties = [ + new PwmProperty(this, 'PWM0', 50 , { + description: 'Analog port of Edison' + }) + ]; + this.pinProperties.forEach((property) => { + self.addProperty(property); + }); + } + + close() { + this.pinProperties.forEach((property) => { + property.close && property.close(); + }); + } +} + +module.exports = function() { + if (!module.exports.instance) { + module.exports.instance = new EdisonThing(); + } + return module.exports.instance; +}; diff --git a/example/platform/package.json b/example/platform/package.json index 41cf49c..5f60d11 100644 --- a/example/platform/package.json +++ b/example/platform/package.json @@ -28,6 +28,7 @@ }, "homepage": "https://github.com/rzr/webthing-iotjs", "dependencies": { - "gpio": "^0.2.10" + "gpio": "^0.2.10", + "pwm": "0.0.3" } } diff --git a/example/platform/pwm/pwm-property.js b/example/platform/pwm/pwm-property.js new file mode 100644 index 0000000..6a6265c --- /dev/null +++ b/example/platform/pwm/pwm-property.js @@ -0,0 +1,100 @@ +// -*- 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 verbose = !console.log || function() {}; + +const { + Property, + Value, +} = require('webthing'); + +const pwm = require('pwm'); + +class PwmOutProperty extends Property { + constructor(thing, name, value, metadata, config) { + if (typeof config === 'undefined') { + config = {}; + } + super(thing, name, new Value(Number(value)), + { + '@type': 'LevelProperty', + title: (metadata && metadata.title) || `PWM: ${name}`, + type: 'number', + minimum: config.minimum || 0, + maximum: config.maximum || 100, + readOnly: false, + description: + (metadata && metadata.description) || + (`PWM Actuator ${config.pin}`), + }); + const self = this; + this.config = config; + if (!this.config.pwm) { + this.config.pwm = { + chip: 0, + pin: 0, + dutyCycle: 0.5, // secs + period: 1, + }; + } + if (typeof this.config.pwm.dutyCycle == 'undefined') { + this.config.pwm.dutyCycle = 0.5; + } + verbose(`log: opening: ${this.description}`); + this.port = pwm.export(this.config.pwm.chip, this.config.pwm.pin, function(err) { + verbose(`log: PWM: ${self.getName()}: open: ${err}`); + if (err) { + console.error(`error: PWM: ${self.getName()}: open: ${err}`); + throw err; + } + self.port.freq = 1 / self.config.pwm.period; + // Linux sysfs uses usecs units + self.port.setPeriod(self.config.pwm.period * 1000 * 1000, function() { + self.port.setDutyCycle( self.config.pwm.dutyCycle / 100 * 1000*1000, function() { + self.port.setEnable(1, function() { + verbose(`log: ${self.getName()}: Enabled`); + }); + }); + }); + + self.value.valueForwarder = function(value) { + var usec = Math.floor((self.config.pwm.period * 1000 * 1000) * ( Number(value) / 100. )); + + self.port.setDutyCycle(usec, function() { + verbose(`log: setDutyCycle: usec=${usec}`); + }); + }; + }); + } + close() { + verbose(`log: PWM: ${this.getName()}: close:`); + try { + this.port && this.port.unexport(); + } catch (err) { + console.error(`error: PWM: ${this.getName()} close:${err}`); + return err; + } + log(`log: PWM: ${this.getName()}: close:`); + } +} + + +module.exports = PwmOutProperty; + + +if (module.parent === null) { + new PwmOutProperty; + setInterval(function() { console.log(new Date()); }, 10000); +}