Skip to content

Commit

Permalink
platform: Add PWM support
Browse files Browse the repository at this point in the history
It was tested on Intel edison on PWM0 but
GPIO12 must be manually configured first

```chip=0
id=0
gpio=12
echo $gpio | sudo tee /sys/class/gpio/export
echo out | sudo tee /sys/class/gpio/gpio$gpio/direction
echo 0 | sudo tee /sys/class/gpio/gpio$gpio/value
sudo cat /sys/kernel/debug/gpio_debug/gpio$gpio/current_pinmux # mode0
echo mode1 | sudo tee /sys/kernel/debug/gpio_debug/gpio${gpio}/current_pinmuxsh
```

More/Better gpio drivers maybe used later,
'pwm' one was used here as base for webthing-iotjs.

Relate-to: #3
Change-Id: I6f587a840e8c429d91ba2491e7cddfdfab66bf4b
Signed-off-by: Philippe Coval <p.coval@samsung.com>
  • Loading branch information
rzr committed Jun 26, 2019
1 parent 37f5e9b commit 88104d5
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
47 changes: 47 additions & 0 deletions example/platform/board/edison.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// -*- 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 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;
};
3 changes: 2 additions & 1 deletion example/platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/rzr/webthing-iotjs",
"dependencies": {
"gpio": "^0.2.10"
"gpio": "^0.2.10",
"pwm": "0.0.3"
}
}
111 changes: 111 additions & 0 deletions example/platform/pwm/pwm-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// -*- 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} (dutyCycle)`,
type: 'integer',
minimum: config.minimum || 0,
maximum: config.maximum || 100,
readOnly: false,
unit: 'percent',
description:
(metadata && metadata.description) ||
(`PWM DutyCycle`),
});
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,
(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,
() => {
self.port.setDutyCycle(
self.config.pwm.dutyCycle / 100 * 1000 * 1000,
() => {
self.port.setEnable(1, () => {
verbose(`log: ${self.getName()}: Enabled`);
});
});
});

self.value.valueForwarder = function(value) {
const usec = Math.floor((self.config.pwm.period * 1000 * 1000) *
(Number(value) / 100.0));

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);
}

0 comments on commit 88104d5

Please sign in to comment.